Selenium TestNG根据其他方法进行测试

时间:2018-01-08 01:13:47

标签: 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实际登录并使用数据提供商?

1 个答案:

答案 0 :(得分:1)

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

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

你不能将这两个标签组合在一起(@Test + @BeforeTest)。这样做没有任何意义。你告诉Testng在别人测试之前运行这个方法。 @BeforeTest通常用于配置,就像你使用driver.exe一样。保留@Test仅用于测试目的。

那么现在用@BeforeTest试图完成什么?也许我误解了。