2012-05-17 19 views
1

我有TestNG一個非常不愉快的問題。發生NullPointerException時,我嘗試使用分配給它們的組來啓動一些測試。NullPointerException在啓動組testng的XML期間

的試驗的例子:

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(); 

    } 

} 

二測:

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()); 
    } 

} 

這裏是一個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(); 
    } 

} 

而在最後,我的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> 

可能是NullPointerException的原因是什麼?我該如何解決它?這是我項目上的link

+2

你能張貼堆棧跟蹤? – ControlAltDel

+1

如果您在您的標題中有** NullPointerException **,請將其置於您的問題中...... – keyser

+0

究竟是在拋出NullPointerException的位置?它在TestNG調用中還是在您的某個類中?堆棧跟蹤真的有幫助。 – artdanil

回答

0

似乎這個問題與您的Function_Library(FundamentalTest),你需要先訪問的方法之前運行。

4

只需將(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(); 
} 

}