我正在使用带有Java + Testng的Selenium Webdriver来测试我的应用程序gui。我的项目中有2个类文件(相同的程序包)。一个类文件具有所有@Test方法,第二个类文件具有1个方法,该方法用于生成一个Excel文件,该文件具有在第一个类文件中的test方法中生成的ID。基本上,每次运行测试方法时,我的AUT都会生成一个唯一的ID,这需要捕获。

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

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

}


在同一个程序包中的2nd class文件中,我正在执行以下操作-

@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方法从@Test方法发送到第二个类文件中的方法的方法,这些方法将这些uniqueID保存到excel工作表中。有什么建议是实现此目标的有效方法吗?

最佳答案

从测试类中调用第二类的方法,并传递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);
        }

}

08-18 09:26
查看更多