如何在TestNG框架中将值从一个类传递到另一个类

时间:2018-05-23 10:51:43

标签: java selenium-chromedriver

我正在为旅游网站编写自动化脚本。 我有一个课程主页。在主页中,我存储了我在目的地字段中输入的值,以便我可以在搜索结果页面上比较该值,以确保搜索结果与在目的地中输入的值I匹配。所以我从Homepage类中获取值,并希望在搜索结果页面中使用该值。 这是我的代码如下。我正在尝试将Selenium与TestNG框架集成。在selenium中,我使用构造函数类完成了这个。

主页分类:

 '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 Variable

由于我不想运行整个上述功能,我只存储Rdest 值中的一个单独的函数DestinationVal in Variable in the name of 目的地如下

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();

    } '

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();
}

3 个答案:

答案 0 :(得分:0)

您可以应用此逻辑:

Class TestA
{
    public void testMethod()
    {
    String testvalue = "XXXXXX"; 
    }    
}

此处testvalue是您要传递的变量, 目标类,您要在其中使用Base类的值:

您可以通过以下方式检索它:

TestA OBJ = new TestA();
System.out.println(OBJ.testvalue); 

答案 1 :(得分:0)

在您的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,您将获得更清晰的代码并且易于维护。

答案 2 :(得分:0)

您需要在主页类中执行以下3个步骤。

  1. 为目标
  2. 创建一个私有变量
  3. 创建一个公共方法以返回目标变量值
  4. 更新测试方法中的目标变量值(提取值后)
  5. <强>信息搜索结果

    1. 在HomePage类的Object上创建为HomePage主页= new HomePage();
    2. 使用HomePage类Object as访问getDestinationVariable方法 homepage.getDestinationValue();
    3. 代码需要在HomePage Class中添加:

      //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();