[英]How to pass values from one class to another class in TestNG framework
我正在為旅游網站編寫自動化腳本。 我有一個主頁類。 在主頁中,我將存儲在目標字段中輸入的值,以便可以在搜索結果頁面上比較該值,以確保搜索結果與我在目標中輸入的值匹配。 因此,我從Homepage類獲取值,並想在搜索結果頁面中使用該值。 這是下面的代碼。 我正在嘗試將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值存儲在名為Destination的Variable中的單獨函數DestinationVal中,如下所示
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();
}
您可以應用以下邏輯:
Class TestA
{
public void testMethod()
{
String testvalue = "XXXXXX";
}
}
這里的testvalue
是要傳遞給的變量,即目標類,您要在其中使用基類的值:
您可以通過以下方式檢索它:
TestA OBJ = new TestA();
System.out.println(OBJ.testvalue);
在您的MainSite中創建要在兩個類中都進行擴展的對象,以創建目的地的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 ,這樣您的代碼將更簡潔並且易於維護。
您需要在“首頁類”中執行以下3個步驟。
搜索結果:
需要在HomePage類中添加代碼:
//add it as instance variable
private String destinationValue;
public getDestinationValue(){
return destinationValue;
}
public String SelectDestination() throws InterruptedException{
-- etc ---
destinationValue=Rdest;
return Rdest;
}
SearchResult :需要在搜索結果類中添加以下代碼
HomePage homepage=new HomePage();
homepage.getDestinationValue();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.