使用组启动testng xml时出现NullPointerException

Ale*_*ein 1 java testng selenium webdriver selenium-rc

我对TestNG有一个非常不愉快的问题.当我尝试使用我分配给它们的组启动我的一些测试时发生NullPointerException.

测试示例:

public class DemonstrationTest01 extends FundamentalTest {

    private static final String LOGIN = "Fruzenshtein";
    private static final String PASSWORD = "111111";

    @Test(priority=1, groups="foo")
    public void test01() throws InterruptedException {
        basePage.logIn(LOGIN, PASSWORD)
            .checkTextPresent("Welcome, "+LOGIN+"!")
            .clickSiteLogo()
            .checkTextPresent("Welcome, "+LOGIN+"!")
            .logOut();

    }

}
Run Code Online (Sandbox Code Playgroud)

第二次测试:

public class DemonstrationTest03 extends FundamentalTest {

    @Test(priority=1, groups="foo")
    public void test03() throws InterruptedException {
        ProductPage productPage = basePage.navToCategory("Homewear")
            .checkCategoryTitle("Mugs")
            .navToProductPage(2)
            .checkTextPresent("0 items")
            .addToCart()
            .checkTextPresent("1 items");

        Assert.assertEquals(basePage.getShoppingCartTotal(), productPage.getProductPrice());

        productPage.addToCart()
            .checkTextPresent("2 items");

        Assert.assertEquals(basePage.getShoppingCartTotal(), 2*productPage.getProductPrice());
    }

}
Run Code Online (Sandbox Code Playgroud)

这是一个FundamentalTest:

public class FundamentalTest {

    protected BasePage basePage;

    @BeforeClass
    @Parameters({ "testServer" })
    public void init(String testServer) {
        basePage = PageFactory.initElements(new FirefoxDriver(), BasePage.class);
        basePage.driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
        basePage.driver.get(testServer);
    }

    @AfterClass
    public void destruct() throws InterruptedException {
        Thread.sleep(5000);
        basePage.driver.close();
    }

}
Run Code Online (Sandbox Code Playgroud)

最后我的xml启动器:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="SingleTestSuite" >
  <parameter name="testServer"  value="https://shop.shakhtar.com/en/"/>
  <test name="StandAloneTest">
    <groups>
        <run>
            <include name="foo"></include>
        </run>
    </groups>
    <packages>
        <package name="com.shakhtar.qa.tests" />        
    </packages>         
  </test>      
</suite>
Run Code Online (Sandbox Code Playgroud)

NullPointerException的原因是什么?我该如何解决?这是我项目的链接.

小智 5

只需将(alwaysRun = true)添加到@BeforeClass注释即可.

所以你的基础课应该是这样的 - :

公共类FundamentalTest {

protected BasePage basePage;

@BeforeClass(alwaysRun = true)
@Parameters({ "testServer" })
public void init(String testServer) {
    basePage = PageFactory.initElements(new FirefoxDriver(), BasePage.class);
    basePage.driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
    basePage.driver.get(testServer);
}

@AfterClass
public void destruct() throws InterruptedException {
    Thread.sleep(5000);
    basePage.driver.close();
}
Run Code Online (Sandbox Code Playgroud)

}