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

how to pass parameter from @Test method in 1 class file to a annotated method in another class file(testNG)

我正在使用带有 java + testng 的 selenium webdriver 来测试我的应用程序图形用户界面。我的项目中有 2 class 个文件(同一个包)。 1 class 文件具有所有 @Test 方法,第二个 class 文件具有 1 个方法,用于生成一个 excel 文件,其中在第一个 [=25] 的测试方法中生成了 ID =] 文件。基本上每次我 运行 我需要捕获的测试方法时,我的 AUT 都会生成一个唯一的 ID。

代码如下所示 - 第一个 class 文件

中的测试方法
@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();

}

在同一个包的第二个 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 从第一个 class 文件中的 @Test 方法发送到第二个 class 文件中的方法,从而保存这些 un​​iqueID 到 excel sheet。有什么建议可以有效地实现这一目标吗?

从您的测试 class 调用第二个 Class 的方法,将 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);
        }

}