并行使用TestNG运行Selenium测试会导致错误

时间:2017-02-07 20:08:02

标签: java selenium testng selenium-grid

我正在使用两个VM作为节点构建一个selenium网格框架(一个是chrome,另一个是Firefox)。当我连续运行我的样本测试时,它们没有任何问题。然而,当我并行运行它们时,Chrome中的测试只能启动我正在测试的网站,但没有做任何其他事情,Firefox浏览器完美地运行测试(在测试结束时打印消息)告诉我它通过了,至少应该已经通过了。当套件运行完毕后,测试告诉我chrome已经通过(它没有)并且Firefox抛出了一个未找到异常的元素。我知道我的问题与保持我的线程不会越过,但这是我第一次设置这样的东西,我不知道如何调试它。

的testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite" thread-count="2" parallel="tests">
    <test verbose="2" name="Chrome">
        <parameter name="browserName" value="chrome"/>
        <parameter name="nodeURL" value="http://10.11.25.170:5551/wd/hub"/>
        <packages>
            <package name="com.company.*"></package>
        </packages>
    </test>
    <test verbose="2" name="Firefox">
        <parameter name="browserName" value="firefox"/>
        <parameter name="nodeURL" value="http://10.11.25.170:5552/wd/hub"/>
        <packages>
            <package name="com.company.*"></package>
        </packages>
    </test>
</suite>

Base.java

public class Base {

    public WebDriver driver = null;

    public WebDriver getDriver(String browserName, String nodeURL) throws MalformedURLException {

        DesiredCapabilities caps = null;

        if (browserName.equals("firefox")) {
            caps = DesiredCapabilities.firefox();
        } else if (browserName.equals("chrome")) {
            caps = DesiredCapabilities.chrome();
        }

        caps.setBrowserName(browserName);
        caps.setPlatform(Platform.VISTA);
        driver = new RemoteWebDriver(new URL(nodeURL), caps);

        return driver;
    }

TestBase.java

public class TestBase extends Base {


    @BeforeMethod
    @Parameters({"browserName", "nodeURL"})
    public void beforeTest(String browserName, String nodeURL) throws MalformedURLException {
        Base b = new Base();
        driver = b.getDriver(browserName, nodeURL);
        //driver = b.getLocalDriver();      
    }

    @AfterMethod
    public void afterTest() {       
        driver.quit();
    }   
}

LoginTest.java

public class LoginTest extends TestBase {

    @Test
    public void one() throws IOException {
        driver.get("http://company.com");

        EmailModal modal = new EmailModal();
        modal.cancelModal();

        Header header = new Header();
        header.clickSignInLink();

        System.out.println("Page Title is: " + driver.getTitle());
    }

}

EmailModalPage.java

public class EmailModalPage extends Base {
    public WebElement emailModalCancelButton() {
        return driver.findElement(By.cssSelector("a[class='js-closeLayer closeLayer fr'")) ;  
        // ^^^ Line 11, where the error was thrown
    }
}

EmailModal.java

public class EmailModal extends EmailModalPage {
    public void cancelModal() {
        emailModalCancelButton().click();
    }

这是删除static关键字时抛出的错误:

java.lang.NullPointerException
    at com.company.automation.ecom.pages.EmailModalPage.subscribeToEmailModal(EmailModal_.java:11)

0 个答案:

没有答案