Я пишу сценарии автоматизации для туристического сайта.У меня есть один класс для HomePage.На домашней странице я сохраняю значение, которое я ввел в поле назначения, чтобы я мог сравнить это значение на странице результатов поиска, чтобы убедиться, что результат поиска соответствует значению, которое я ввел в пункте назначения.Итак, я получаю значение из класса Homepage и хочу использовать это значение на странице результатов поиска.Вот мой код ниже.Я пытаюсь интегрировать Selenium с платформой TestNG.В селене я сделал это, используя класс конструктора.
Класс HomePage:
'public class HomePage extends MainSite{
public static String Rdest ="";
public HomePage(WebDriver driver) {
// TODO Auto-generated constructor stub
}
@Test
public void Home() throws InterruptedException
{
// Entering values in destination field
SelectDestination();
// Clicking calender to 5 months future month
for (int i = 0; i <= 5; i++) {
driver.findElement(By.xpath("//div[contains(text(),'Select Departure
Date')]/following::a[contains(@class,'ui-datepicker-next ui-corner-all')]
[2]")).click();
}
//Calling SelectDate funtion to select date
SelectDate();
// clicking Search Flight button
driver.findElement(By.xpath("//*[@id='btn-search-
flight']")).click();
}
public String SelectDestination() throws InterruptedException{
WebElement dest = driver.findElement(By.id("destination_0"));
String[] des = { "Zurich", "Lahore", "Geneva", "Sydney", "Moscow",
"Stockholm", "Cali", "Rome" };
int index = (int) (Math.random() * 8);
Rdest = des[index];
dest.sendKeys(des[index]);
System.out.println(index);
//Char d =dest.charAt(index);
System.out.println("Randomly selected destination:"+Rdest);
Thread.sleep(5000);
//dest.sendKeys(Keys.ARROW_DOWN);
dest.sendKeys(Keys.RETURN);
Thread.sleep(2000);
System.out.println(Rdest);
System.out.println("Destination Selected");
//private static String Destval = Rdest;
return Rdest;
}
Я храню целевое значение в переменной Rdest
Поскольку я не хочу запускатьвсю вышеупомянутую функцию, я сохраняю только значение Rdest в отдельной функции DestinationVal в переменной с именем Destination, как показано ниже
public String DestinationVal()
{
String Destination = Rdest;
System.out.println("Destination function value="+Destination);
return Destination;
}
Пожалуйста, подскажите, как я могу использовать это значение Destination в классе результатов поиска
public void SelectDate()
{
//Selecting Departure Date
DateFormat dateFormat = new SimpleDateFormat("dd");
Date date = new Date();
String Dd=dateFormat.format(date);
System.out.println(dateFormat.format(date));
System.out.println(date);
driver.findElement(By.xpath("//div[contains(text(),'Select Departure
Date')]/following::a[contains(text(),'"+dateFormat.format(date)+"')]"))
.click();
//Selecting Return Date
int Dr= Integer.parseInt(Dd);
int abc = (int) (Math.random() * 5);
Dr= Dr+abc;
System.out.println("Number of days increased:"+Dr);
String Dr1 = Integer.toString(Dr);
driver.findElement(By.xpath("//div[contains(text(),'Select Return
Date')]/following::a[contains(text(),'"+Dr1+"')]")).click();
} '
Класс SearchResult:
public class SearchResult extends MainSite {
//@Test (Dataprovider="Destination")
public void Result() throws InterruptedException
{
// Waiting for the search result to get displayed
Thread.sleep(10000);
//Calling Homepage to use destination for comparition
HomePage hp1 = new HomePage(driver);
String returnVal = Destination;
//System.out.println(returnVal);
//Validating searched source
String Src =
driver.findElement(By.xpath("//span[contains(text(),'Flexible with
')]/following::div[@class='loc'][1]/span[1]")).getText();
//System.out.println(Src);
if(Src.equals("Colombo"))
{ System.out.println("Search result match source location" );}
else {System.out.println("Source locatoin is not match on second
page");}
String Dest =
driver.findElement(By.xpath("//span[contains(text(),'Flexible with
')]/following::span[contains(text(),'"+returnVal+"')][1]")).getText();
//System.out.println(Dest);
Thread.sleep(1000);
if (Dest.toLowerCase().contains(returnVal))
{System.out.println("Search Result match Destination location");}
else {System.out.println("Destination locatoin is not match on
second page");}
// Clicking on first book now button from the search result
driver.findElement(By.xpath("//span[contains(text(),'Flexible
with ')]/following::a[@class='btn-book'][1]")).click();
}