【问题标题】:How to use @Before test variables by various @Test in TestNG如何在TestNG中通过各种@Test使用@Before测试变量
【发布时间】:2021-11-26 07:58:03
【问题描述】:

我对 Selenium 和 TestNG 非常陌生。我将通过将有效和无效数据传递给用户名字段和密码字段来验证登录页面。测试数据通过 Excel 工作表。我想在不同的@Test 中提及每个用户名字段和密码字段。我正在使用以下代码。当我使用不同的@Test 时,它不会获得 excel 工作表数据,因为它在 @BeforeTest 中提到。虽然我使用了 dependsOnMethods 它没有得到价值观。谁能帮帮我。

公共类登录{

WebDriver driver;
WebDriverWait wait;
XSSFWorkbook workbook;
XSSFSheet sheet;
XSSFCell cell;

@BeforeTest
public void browserSetup() throws IOException {
    System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
    FileInputStream file = new FileInputStream(new File("D:\\codtest.xlsx"));
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet sheet = workbook.getSheetAt(0);

    for (int i = 1; i <= sheet.getLastRowNum(); i++) {

        driver = new ChromeDriver();
        driver.get("https://opensource-demo.orangehrmlive.com/");
        driver.manage().window().maximize();

        String userName = sheet.getRow(i).getCell(0).getStringCellValue();
        String passWord = sheet.getRow(i).getCell(1).getStringCellValue();
      

    }

}

@Test(dependsOnMethods = {"browserSetup"})
public void userName(){

     driver.findElement(By.id("txtUsername")).sendKeys(userName);

    WebElement erro_message=driver.findElement(By.xpath("//*[@id=\"spanMessage\"]")).getText()
    //error message when username filed is empty
    Boolean Display = erro_message.isDisplayed();
    if (Display == true) {

        System.out.println("User Name field is required");
    }



}

@Test
public void passWord(){

     driver.findElement(By.id("txtPassword")).sendKeys(userName);
    WebElement erro_message=driver.findElement(By.xpath("//*[@id=\"spanMessage\"]")).getText()
    //error message when password filed is empty
    Boolean Display = erro_message.isDisplayed();
    if (Display == true) {

        System.out.println("Password field is required");
    }

}

}

【问题讨论】:

    标签: java selenium testng


    【解决方案1】:

    请在您的测试方法中使用 DataProvider 选项,如下例所示,

    参数化可以通过以下方式完成,

    1. 使用数据提供者
    2. 在测试方法中使用excel输入(所有输入/一个测试)
    3. 在测试方法之前将您的 Excel 输入转换为 TestNG 数据提供程序

    示例:

    @DataProvider(name = "loginCredentials")
    public Object[][] loginDetails(){
        return new Object[][] {{"jayanth1@gmail.com", "Password@123"}, {"jayanth2@gmail.com", "Password@123"}, {"jayanth3@gmail.com", "Password@123"}};
    }
    
    @Test(groups = {"dp"}, dataProvider="loginCredentials")
    public void justLogin(String username, String password){
        System.out.println("Username : "+username +" / Password : "+password);
    }
    

    【讨论】:

      【解决方案2】:

      你应该注解@BeforeMethod

      对不起伙计,你会尝试用@BeforeMethod 替换@BeforeTest 的注释吗?并从 searchProduct 方法中删除dependsOnMethods?

      像,

      @BeforeMethod
      public void browserSetup() throws IOException {
          System.out.println("browserSetup");
      }
      
      @Test
      public void searchProduct(){
          System.out.println("searchProduct");
      
      }
      
      @Test
      public void location(){
          System.out.println("location");
      
      }
      

      那么你的测试将如下执行。

      [RemoteTestNG] detected TestNG version 6.14.3
      browserSetup
      location
      browserSetup
      searchProduct
      PASSED: location
      PASSED: searchProduct
      

      如果您想按特定顺序执行测试,可以在方法中添加优先级。

      @BeforeMethod
      public void browserSetup() throws IOException {
          System.out.println("browserSetup");
      }
      
      @Test(dependsOnMethods = {"browserSetup"}, priority = 2)
      public void searchProduct(){
          System.out.println("searchProduct");
      
      }
      
      @Test(priority=1)
      public void location(){
          System.out.println("location");
      
      }
      

      【讨论】:

      • 请考虑在您的答案周围添加更多细节,以使其更清晰。
      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 2015-03-25
      相关资源
      最近更新 更多