如何将全局变量从一个测试类传递到另一个测试类并在硒的下拉列表中选择该值?



我有这个类(DoctorRegistrationTest.java(并且有全局变量doctorCode,doctorFirstName,doctorFamilyName(

@BeforeMethod(groups = {"BVT", "Regression"})
public void loadRmsPage() {
loadRmsProfile();
softAssert = new SoftAssert();
doctorRegistrationPage = new DoctorRegistrationPage(webDriver);
}
@Epic("RMS - Create Doctor")
@Feature("RMS - Create Doctor functionality")
@Story("AUT-688")
@Severity(SeverityLevel.BLOCKER)
@Test(description = "Positive : Doctor Creation  ", groups = {"BVT", "Regression"})
public void createDoctorTestMethod() {
do {
doctorCode = String.valueOf(generateRandomNumber(1000, 9999));
} while (false && doctorRegistrationPage.verifyCreatedDoctor(doctorCode));
setDoctorRegistrationInformation();
createDoctor();
}
public void createDoctor() {
doctorRegistrationPage.loadDoctorRegistrationPage();
doctorRegistrationPage.fillDoctorNameInformation(doctorCode, doctorFirstName, doctorFamilyName, doctorFirstNameInArabic, doctorFamilyNameInArabic);
doctorRegistrationPage.fillDoctorGeneralInformation(dateOfBirth, joinDate, identityNo, expiryDate, gender, doctorTitle, employmentType, identityType);
doctorRegistrationPage.fillDoctorContactInformation(mobileNumber, companyEmail);
doctorRegistrationPage.fillDoctorOtherInformation(doctorInformationEnglish, doctorInformationArabic);
doctorRegistrationPage.fillDoctorTiming(followUpDays, slotDurationMinutes);
doctorRegistrationPage.fillDoctorHospitalsAndClinics(hospital, clinics);
doctorRegistrationPage.fillDoctorDefaultProcedure(defaultProcedur, consultationProcedure);
boolean result = doctorRegistrationPage.verifyCreatedDoctor(doctorCode);
LOG.info("Return value of verifyCreatedDoctor method " + result);
softAssert.assertTrue(result, "TEST FAILED - CREATED DOCTOR NOT LISTING IN THE GRID");
softAssert.assertAll();
doctorRegistrationPage.logOutUser();
}
public void setDoctorRegistrationInformation() {
readPropertiesFile();
setNames();
setNumberValues();
setDate();
}
public void setNames() {
doctorFirstName = "AUT DN " + getRandomStringName(4);
doctorFamilyName = "AUT DFN " + getRandomStringName(4);
companyEmail = getRandomStringName(5) + "@aut.com";
doctorInformationEnglish = getRandomStringName(6);
}

public String getRandomStringName(int randomStringLength) {
return RandomStringUtils.randomAlphabetic(randomStringLength).toLowerCase();
}
public int generateRandomNumber(int low, int high) {
return (new Random()).nextInt(high - low) + low;
}

}

我还有另一个类(DoctorScheduleTest.java(,需要将doctorCode,doctorFirstName,doctorFamilyName从(DoctorRegistrationTest.java传递到doctorSchedulePage.selectDoctorDropdown(doctor(;方法,而不是硬编码细节。必须像这样传递值(" 医生代码:医生名字医生姓氏(

private String doctor =" 8938: AUT DN gvgl AUT DFN wnrn ";

@BeforeMethod(groups = {"BVT", "Regression"})
public void loadRmsPage()
{
loadRmsProfile();
softAssert = new SoftAssert();
doctorSchedulePage = new DoctorSchedulePage(webDriver);
}
@Epic("RMS")
@Feature("RMS - Create Doctor Schedule functionality")
@Story("AUT-835")
@Severity(SeverityLevel.BLOCKER)
@Test(description = "Positive : Doctor Schedule Creation", groups = {"BVT", "Regression"})
public void doctorScheduleCreationTestMethod()
{
setTemplateName();
createInitialSchedule();
setSchedule();
saveTemplate();
setTemplate();
setDateRange();
generateSchedule();
}
public void createInitialSchedule()
{
doctorSchedulePage.loadDoctorScheduleDashboard();
doctorSchedulePage.selectHospitalDropDown(hospital);
doctorSchedulePage.selectClinicDropDown(clinic);
doctorSchedulePage.selectDoctorDropdown(doctor);
doctorSchedulePage.fillTemplate(scheduleTemplateName);
}
public void setSchedule()
{
doctorSchedulePage.sundaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.mondaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.tuesdaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.wednesdaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.thursdaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.fridaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.saturdaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
}
public void saveTemplate()
{
doctorSchedulePage.saveTemplate();
}
public void setTemplate()
{
doctorSchedulePage.selectTemplateDropdown(scheduleTemplateName);
}
public void setDateRange()
{
doctorSchedulePage.selectScheduleDate(scheduleStartDate,scheduleEndDate);
}
public void generateSchedule()
{
doctorSchedulePage.generateSchedule();
}
public int generateRandomNumber(int low, int high)
{
return (new Random()).nextInt(high - low) + low;
}
public void setTemplateName()
{
scheduleTemplateName = "Template " + getRandomStringName(3);
}
public String getRandomStringName(int randomStringLength)
{
return RandomStringUtils.randomAlphabetic(randomStringLength).toLowerCase();
}

}

医生计划页面.java ( 选择医生下拉方法 (

public void selectDoctorDropdown(String doctorcode)
{
selectValueFromDropDown(doctorDropdown, doctorcode);
}

BasePage.java (selectValueFromDropDown 方法(

protected void selectValueFromDropDown(WebElement element, String value( { if (value != null && !value.isEmpty((( {

waitForElementToPresent(element);
Select dropdown = new Select(element);
LOG.info("Selected "+value);
dropdown.selectByVisibleText(value);   
}

}

首先,您的问题表明,您在将测试数据传递到测试中时遇到问题。这个问题首先应该得到解决。

由于这是另一个主题(也是一个更大的主题(,我建议使用一个单独的类,使用医生的数据。

public static class Doctor{
public static String FirstName;
public static String FamilyName;

在设置中初始化数据,并在需要时使用。

正如我之前所说,这不是最好的解决方案,因为测试数据应该保留在测试之外,但现在可能会有所帮助。

最新更新