如何将参数从1类文件中的@Test方法传递到另一个类文件中的注释方法(testNG)

时间:2016-05-20 10:06:26

标签: java selenium-webdriver apache-poi testng

我正在使用selenium webdriver和java + testng来测试我的应用程序gui。我的项目中有2个类文件(相同的包)。 1个类文件包含所有@Test方法,第二个类文件有1个方法,用于生成在第一个类文件的test方法中生成Id的excel文件。基本上我的AUT每次运行测试方法时都会生成一个唯一的ID,我需要捕获它。

代码看起来像这样 - 第一类文件中的测试方法

std::vector<std::thread> producers;

fillThread(producers, 10, [&] () {
        cout << "attempting to take a job ..." << endl;
        int job = queue.take();
        cout << "consumed job " << job << endl;
    });

在同一个包中的第二个类文件中,我正在做这样的事情 -

@Test (invocationCount = 1)
  public void TestIncident() {


      ProfilesIni profile = new ProfilesIni();
      FirefoxProfile myprofile = profile.getProfile("selenium");

      WebDriver driver=new FirefoxDriver(myprofile);
      vDriver = driver;
      driver.manage().window().maximize();

      driver.get("URL");
      driver.findElement(By.id("username-id")).click();
      driver.findElement(By.id("username-id")).sendKeys("username");
      driver.findElement(By.id("pwd-id")).click();
      driver.findElement(By.id("pwd-id")).sendKeys("password");
      driver.findElement(By.name("login")).click();

      Thread.sleep(5000);


      driver.findElement(By.id("reg_img_304316340")).click();
      driver.findElement(By.xpath("//div[@class='PageBody pbChrome']"));
      //new Actions(driver).moveToElement(driver.findElement(By.xpath("//div[@class='PageBody pbChrome']"))).perform();


      driver.findElement(By.xpath("//span[contains(.,'Page1')]")).click();
      driver.findElement(By.xpath("//span[contains(.,'New Page')]")).click();
      driver.findElement(By.xpath("//div[@class='PageBody pbChrome']")).click();


      uniqueID  = driver.findElement(By.xpath("//div[@id='WIN_0_304248710']/descendant::dd[@class='HNavSelected arfid304247442 ardbnz3Btn_BCItem3']/descendant::a[@class='btn']")).getAttribute("innerHTML");

      driver.findElement(By.id("arid_WIN_3_303530000")).clear();
      driver.findElement(By.id("arid_WIN_3_303530000")).click();

}

现在我无法想出一种方法,可以将@BeforeClass public void createExcel() throws IOException { System.out.println("did this run"); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("IncidentNo"); //some code to save uniqueID in excel file } 从第一个类文件中的uniqueID方法发送到第二个类文件中的方法,该方法将这些唯一ID保存到Excel工作表中。任何建议是否有效实现这一目标?

1 个答案:

答案 0 :(得分:0)

从测试类中调用第2类的方法,并将uniqueID作为参数传递。

您的代码应如下所示

    ExcelUtils objExcelutils; //Object of your 2nd class

    public class Test{


    //all the code from your first class


      objExcelutils.writeUniqueID(uniqueID);

    }

    // this is your 2nd class
    int rowCount=1;   //variable for the row count
    public class Excelutils
    {

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet worksheet = workbook.createSheet("IncidentNo");
       public void writeUniqueID(String uniqueID)
       {
           HSSFRow row = worksheet.createRow(rowCount);
           HSSFCell cell = row.createCell(rowCount);
           cell.setCellValue(uniqueID);
           rowCount++;
       }

    }
    try (FileOutputStream outputStream = new FileOutputStream("JavaBooks.xlsx"))        {
            workbook.write(outputStream);
        }

}