简体   繁体   中英

Run Selenium Webdriver TestNG tests using webdriver-factory

I write tests for Selenium WebDriver via webdriver-factory by Java and have problem with tests. I have this base class for tests:

public class BaseTest {

private WebDriver driver;

@BeforeSuite
public void setUp() {
    WebDriverFactory.setMode(WebDriverFactoryMode.SINGLETON);
    this.driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    driver.manage().timeouts().setScriptTimeout(40, TimeUnit.SECONDS);
    driver.manage().window().maximize();
}

protected LoginPage navigate(String url) {
    driver.get(url);
    return new LoginPage(driver);
}

@AfterSuite(alwaysRun = true)
public void tearDown() {
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    if (driver != null) {
        WebDriverFactory.dismissAll();
    }
}}

And two classes, which extends this class: LoginTest and LogoutTest:

public class LoginTest extends BaseTest {

@Test
public void testLoginPage() {
    LoggedPage lp = navigate(Constants.URL).
            inputLogin(Constants.LOGIN).
            selectDomain(Constants.DOMAIN).
            inputPassword(Constants.PASSWORD).
            login();

    Assert.assertTrue(lp.
            isUserEmailPresented(),
            "User e-mail does not presented!");

    lp.logout();
}}
public class LogoutTest extends BaseTest {

private LoggedPage logP;

@BeforeMethod()
public void login() {
    logP = navigate(Constants.URL).
            inputLogin(Constants.LOGIN).
            selectDomain(Constants.DOMAIN).
            inputPassword(Constants.PASSWORD).
            login();
}

@Test
public void testLogout() {
    LoginPage lp = logP.logout();

    Assert.assertTrue(lp.
            isLoginFieldPresented(),
            "Login field does not presented!");

    Assert.assertTrue(lp.
            isPasswordFieldPresented(),
            "Password field does not presented!");

    Assert.assertTrue(lp.
            isLoginButtonPresented(),
            "Login button does not presented!");
}}

If I run my testng suite only with LoginTest - it works correct. But if I try run all my tests by suite, LoginPage - passed and LogoutPage has NullPointerException via navigate() method. How can I fix it? My suite:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Selenium Web Driver" parallel="methods" thread-count="3">
    <test name="Selenium">
        <classes>
            <class name="functional.LoginTest"/>
            <class name="functional.LogoutTest"/>
        </classes>
    </test>
</suite>

I think logout(); method is missing here..

对于此问题,需要使用static driver字段。

We struggled with a similar situation with our testing efforts. We also wanted to leverage the TestNG parallelization along with Selenium web driver. Unfortunately, managing the web driver life cycle within each test class or trying to use a static class holding the web driver wasn't effective when enabling any of the TestNG parallel modes. Matching classes and methods under test with the active TestNG thread and the instantiated web driver was unwieldy when it worked at all. We were spending more time figuring out the web driver lifecycles than getting to the real web application tests.

In your case since you've defined <suite parallel="methods"> when TestNG executes the classes are instantiated on one thread with all the appropriate @ annotations being processed. However, with the 'methods' parallel mode, TestNG instantiates each of the test classes methods on separate threads. These threads are unaware and unable to make use of any processed annotations that may have started a web driver, static or otherwise. Consequently, when your method executes you'll get NPE.

We have an open source project that abstracts away the web driver lifecycle management for TestNG/Selenium testing. It properly supports each of the TestNG parallel modes providing the correct and active web driver for each test class method under test. It's helped us greatly in our testing efforts. Perhaps it may be of use in your case as well. Or if nothing else give you an idea of how you might tackle it differently. The project is over on GitHub as Concurrent Selenium TestNG (COSENG) . Kind regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM