我正在为旅游网站编写自动化脚本。 我在主页上有一个课程。在主页中,我存储了我在目标字段中输入的值,以便我可以在搜索结果页面上比较该值,以确保搜索结果与我在目的地输入的值匹配。所以我从主页类中获取值,并希望在搜索结果页面中使用该值。 这是我下面的代码。我正在尝试将Selenium与TestNG框架集成。在硒中,我使用构造函数类完成了此操作。
主页类:
'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 值在单独的函数中 目的地瓦尔 在变量中以 目的地如下
public String DestinationVal()
{
String Destination = Rdest;
System.out.println("Destination function value="+Destination);
return 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();
} '
搜索结果类:
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();
}
您需要在主页类中执行以下 3 个步骤。
- 为目标创建一个私有变量
- 创建一个公共方法来返回目标变量值
- 更新测试方法中的目标变量值(提取值后(
搜索结果:
- 在 Object 上为主页类创建 主页主页=新主页((;
- 使用 HomePage 类 Object 访问 getDestinationVariable 方法,作为 homepage.getDestinationValue((;
需要在主页类中添加代码:
//add it as instance variable
private String destinationValue;
public getDestinationValue(){
return destinationValue;
}
public String SelectDestination() throws InterruptedException{
-- etc ---
destinationValue=Rdest;
return Rdest;
}
搜索结果: 以下代码需要添加到搜索结果类中
HomePage homepage=new HomePage();
homepage.getDestinationValue();
您可以应用以下逻辑:
Class TestA
{
public void testMethod()
{
String testvalue = "XXXXXX";
}
}
这里testvalue
是您要传递到的变量, 要在其中使用基类值的目标类:
您可以通过以下方式检索它:
TestA OBJ = new TestA();
System.out.println(OBJ.testvalue);
在您的主站点中创建 您在两个类中扩展 为目的地创建 getter/setter:
private static String destination;
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
所以你可以执行以下操作:
HomePage homePage = new HomePage();
homePage.setDestination(homePage.SelectDestination()); //this will return Your random destination and store it in MainSite with get/set
而不是简单地打电话
SearchResult searchResult = new SearchResult();
searchResult.getDestination();
我希望我找到了问题的根源。但我的建议是,将所有页面放入PageObject设计模式中,https://martinfowler.com/bliki/PageObject.html 检查此页面,您将拥有更干净的代码且易于维护。