java - Selenium TestNG 测试依赖于其他方法

标签 java selenium testng

先决条件

大多数网站的流程如下所示:simplified state transition diagram for most sites

其中“仪表板”是所有有趣的特定于站点的业务逻辑发生的地方。

问题是什么?

我正在尝试使用 Selenium WebDriver 和 TestNG 来测试这样的网站。到目前为止,我的代码库类似于:

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!-- Root tag for TestNG.xml will always be suite. The name can be whatever you want -->
<suite name="MyCustomSuite">
    <test name="MyFirstTest">
        <classes>
            <class name="com.mikewarren.testsuites.MercuryToursTest"></class>
            <class name="com.mikewarren.testsuites.MercuryLogin"></class>
            <!-- You can have the class tag for multiple classes of unique name -->

        </classes>
    </test>
</suite>

TestNGGroups.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!-- Root tag for TestNG.xml will always be suite. The name can be whatever you want -->
<suite name="MySmokeTestSuite">
    <test name="MyFirstTest">
        <groups>
            <run>
                <exclude name="regression"></exclude>
                <include name="smoke"></include>
            </run>
        </groups>
        <classes>
            <class name="com.mikewarren.testsuites.MercuryLogin">
                <methods>
                    <include name="methodName"></include>
                    <!-- you can also include or exclude methods --> 
                </methods>
            </class>
            <!-- You can have the class tag for multiple classes of unique name -->

        </classes>
    </test>
</suite>

MercuryToursTest.java

package com.mikewarren.testsuites;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class MercuryToursTest {
    protected static WebDriver driver;
    protected String url = "http://newtours.demoaut.com";

    @BeforeTest
    public void beforeTest()
    {
        System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe" );
        driver = new ChromeDriver();
        driver.get(url);
        // wait a second
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    }

    @AfterTest
    public void afterTest()
    {
        if (driver != null)
            driver.quit();
    }
}

MercuryLogin.java

package com.mikewarren.testsuites;

import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.mikewarren.pages.MercuryLoginFactory;


public class MercuryLogin extends MercuryToursTest {
  @Test(priority=0, groups={"smoke"})
  public void validateLandingPage() {
    Assert.assertEquals(driver.getTitle(), "Welcome: Mercury Tours");  
  }

  @Test(dependsOnMethods="validateLandingPage", 
          priority=2,
          groups={"regression", "somethingElse"}, 
          dataProvider="provideAccountDetailsDynamic")
//  @BeforeTest(groups = {"loginFirst"})
  public void loginToMercury(String username, String password)
  {
      MercuryLoginFactory mlf = new MercuryLoginFactory(driver);
      mlf.driverLogIntoMercury(username, password);

      driver.findElement(By.xpath("//a[contains(text(), 'Home')]")).click();
  }

  @DataProvider
  public Object[][] provideAccountDetailsDynamic() throws Exception { 
      File file = new File("src/test/resources/mercuryData.xlsx");
      FileInputStream fis = new FileInputStream(file);

      Workbook workbook = new XSSFWorkbook(fis);
      Sheet sheet = workbook.getSheet("sheet1");

      int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
      Object[][] data = new Object[rowCount][2];

      /*
       * Data driven framework example.
       * • This is a design patern for test automation where you develop the tests in a manner where they will run
       *    based on provided data. In this case, a tester could have 3 rows data, warranting the test to run 3 
       *    separate times with the given values. 
       *    This allows for configurable automation tests at the hands of a non-developer.
       */
      for (int i = 1; i <= rowCount; i++)
      {
          Row row = sheet.getRow(i);
          data[i-1] = new Object[] { 
              row.getCell(0).getStringCellValue(),
              row.getCell(1).getStringCellValue()
          };
      }

      return data;
  }


}

到目前为止我尝试过的

每当我在 MercuryLogin.java 上点击“运行”时,一切都很好,但是一旦我尝试取消注释 @BeforeTest(groups = {"loginFirst"}) MercuryLogin.loginToMercury() 上的 注释我的测试严重失败。也就是说,它告诉我该方法需要两个参数,但在 @Configuration 注释中只得到 0。

失败了,我做了以下事情:

我将 MercuryLogin.loginToMercury() 添加到 loginFirst 组。然后,与我的代码库其余部分的风格保持一致,创建了 MercuryToursTestLoginFirst.java :

package com.mikewarren.testsuites;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MercuryToursTestLoginFirst extends MercuryToursTest {
    @BeforeClass(dependsOnGroups = "loginFirst")
    public void init()
    {

    }

    @Test
    public void test()
    {
        System.out.println("mock test");
    }
}

test() 可以工作,但它是唯一实际运行的测试。即使类调用它,也不会发生登录! 如何确保 MercuryToursTestLoginFirst 实际登录并使用数据提供程序?

最佳答案

我猜您正在使用教程网站上的代码? 我认为您误解了@beforeTest 的概念。

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

您不要将两个标签组合在 1 中(@Test + @BeforeTest)。这样做没有任何意义。您告诉 Testng 在其他测试之前运行此方法。 @BeforeTest 通常用于配置,就像您对 driver.exe 所做的那样。留下您的@Test 仅用于测试目的。

那么现在我们试图用@BeforeTest 来完成什么?也许是我的理解有误。

关于java - Selenium TestNG 测试依赖于其他方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48143125/

相关文章:

java - BiConsumer/BiFunction/BiPredicate 中的剂量 'Bi' 意味着什么?

java - Delayer 与 Splitter 一起,消息不串行处理

selenium - 如何查找表中的第N个元素?

testing - 如何在监听器中使 TestNG 测试失败

java - 从方法访问属性 - Java

java - 针对这个问题的动态程序是如何设计的呢?

java - Selenium - 尝试使用 "contains"通过其内容来定位表格单元格

selenium - 验证选定的单选按钮 - Selenium IDE

unit-testing - 我们可以使用 maven-surefire-plugin 在同一个项目中执行 JUnit 5 和 TestNG 测试吗?

java - Arquillian + TestNG : How to access container managed objects in @Before/@After methods?