使用DataProvider和Factory设置Selenium Testng基类

时间:2018-06-26 20:19:30

标签: testng factory dataprovider

一般来说,我是测试和Java的新手。我在这上停留了几天。您的帮助将不胜感激。

我正在尝试正确设置用于硒测试的基础类。我需要根据浏览器和用户类型使用不同的参数运行相同的测试。我尝试过使用DataProvider,然后使用BeforeClass进行使用,但似乎只能与@Test注释方法一起使用。然后,我查看了使用@Factory的方法,但不确定如何进行设置以使其起作用。以下是我的两节课。基类和扩展基类的测试类。目前,我收到一条错误消息:“只能将其中之一注入@BeforeClass注释的setUpClass。有人知道问题出在哪里吗?

-基类- 公共类SetLoginStart {

protected WebDriver driver;
protected boolean admin;
protected String browser;
protected String username;
protected String password;

@BeforeClass
protected void setUpClass(String browser, boolean admin) throws IOException, InterruptedException {
    this.admin = admin;
    driver = initLoginDriver(browser);
    if (admin) {
        this.username = prop.getProperty(get admin username);
        this.password = prop.getProperty(get admin username);
    } else {
        this.username = prop.getProperty(get non-admin username);
        this.password = prop.getProperty(get non-admin username);
    }
}

@AfterClass
protected void tearDownClass() {
    driver.quit();
}

private WebDriver initLoginDriver(String browser) throws IOException {
    prop = new Properties();
    FileInputStream fis = new FileInputStream("pathto/data.properties");
    prop.load(fis);

    WebDriver driver = null;

        switch (browser) {
            case "chrome":
                System.setProperty("webdriver.chrome.driver", "pathto/chromedriver");
                driver = new ChromeDriver();
                break;
            case "firefox":
                System.setProperty("webdriver.gecko.driver", "pathto/geckodriver");
                driver = new FirefoxDriver();
                break;
            case "IE":
                System.setProperty("webdriver.ie.driver", "pathto/MicrosoftWebDriver.exe");
                driver = new InternetExplorerDriver();
                break;
            default:
                System.setProperty("webdriver.chrome.driver", "pathto/chromedriver");
                driver = new ChromeDriver();
                break;
        }
    return driver;
}


@DataProvider(name = "dataProvider")
public static Object[][] dataProvider() {
    return new Object[][]{
        {"chrome", false},
        {"firefox", true}
    };
}

}

---测试类---

公共类LoginPageTest扩展了SetLoginStart {

@AfterMethod
public void tearDownMethod() throws IOException, InterruptedException {
    driver.get(prop.getProperty("get-to-a-starting-page-url"));
}

@Factory(dataProvider = "dataProvider")
public Object[] createInstance(String browser, boolean headless, boolean internal) {
    return new Object[]{new LoginPageTest()};
}

@Test
public void testXYZ() throws IOException, InterruptedException {
    TestPageAObj pageA = new TestPageAObj(driver);
pageA.doOnething();
    TestPageBObj pageB = new TestPageBObj(driver);
pageA.doSecondThing();

}

0 个答案:

没有答案