2016-12-19 90 views
0

當我試圖運行帶有2-3個測試用例的測試套件時,我只面臨NullPointerException的問題。但是,如果我單獨運行測試用例,它運行良好。以下是BaseSetup類。Selenium - Eclipse:運行測試套件時出現NullPointerException

public class BaseSetup { 
    private WebDriver driver; 

    public WebDriver getDriver() { 
     return driver; 
    }  

    private void setDriver(String browserType, String appURL) { 
     switch (browserType) { 
     case "chrome": 
      driver = initChromeDriver(appURL); 
      break; 
     case "firefox": 
      driver = initFirefoxDriver(appURL); 
      break;  
    } 

    private static WebDriver initChromeDriver(String appURL) { 
     ... 
    } 

    private static WebDriver initFirefoxDriver(String appURL) { 

     WebDriver driver = new FirefoxDriver(); 
     driver.navigate().to(appURL); 
     return driver; 
    } 

    @Parameters({ "browserType", "appURL" }) 
    @BeforeSuite 
    public void initializeTestBaseSetup(String browserType, String appURL) { 
     try { 
      setDriver(browserType, appURL); 

     } catch (Exception e) { 
      System.out.println("Error....." + e.getStackTrace()); 
     } 
    } 

    @AfterTest 
    public void tearDown() {} 

這是LoginPage類(頁面的元素)

public class LoginPage { 

//web elements of login page 
protected WebDriver driver; 
private By txtUsername = By.xpath(".//*[@id='email']"); 
private By txtPassword = By.xpath(".//*[@id='pass']"); 
private By btnlogin = By.xpath(".//*[@id='loginbutton']"); 
private String lblError = null; 

//constructor 
public LoginPage(WebDriver driver) { 
    this.driver = driver; 
} 

//verify using incorrect password 

//enter username into username textbox 
public void enterUsername (String username){ 
    WebElement txtname = driver.findElement(txtUsername); 
    if (txtname.isDisplayed()) 
    { 
     txtname.sendKeys(username); 
    } 
} 

//enter password into password textbox 
public void enterPassword (String password){ 
    WebElement txtpw = driver.findElement(txtPassword); 
    if (txtpw.isDisplayed()) 
    { 
     txtpw.sendKeys(password); 
    } 
} 

public void clickOnLoginBtn(){  
} 

這是LoginPageTest類。

public class LoginPageTest extends BaseSetup { 

private WebDriver driver; 
private LoginPage loginPage; 

@Test 
public void verifyLoginFunction() { 

    loginPage = new LoginPage(driver);  
    loginPage.enterUsername("abcdef"); //log trace the error here 
    loginPage.enterPassword("abcdef"); 
    loginPage.clickOnLoginBtn(); 

} 
@BeforeClass 
public void Setup() { 
    driver=getDriver(); 
} 
} 

這是VerifyErrorMessage類(第二情況)

private WebDriver driver; 
    private LoginPage loginPage; 

    @Test 
    public void verifyLoginMessage() {   
     loginPage = new LoginPage(driver);     
     loginPage.enterUsername("abc"); 
     loginPage.clickOnLoginBtn();   
     String errorMsg = loginPage.getErrorMessage(); 
     System.out.println(errorMsg); 
    } 


    @BeforeClass 
    public void Setup() { 
     driver=getDriver();  
    } 
} 

這是錯誤TestNG中。

java.lang.NullPointerException 
    at com.facebook.objects.LoginPage.enterUsername(LoginPage.java:56) 
    at com.facebook.tests.LoginPageTest.verifyLoginFunction(LoginPageTest.java:28) 
... Removed 25 stack frames 

於是我就VerifyErrorMessage,再LoginPageTest後。錯誤跟蹤在上面的塊代碼註釋中。

非常感謝。

回答

0

NullReferenceException該行是否爲代碼行?

WebElement txtname = driver.findElement(txtUsername); 

如果是這樣,那麼它的driver變量是null。所以爲了解決這個問題,你需要在你的代碼中跟蹤null

鑑於症狀,我猜測問題是您只在beforeSuite上初始化驅動程序,第一次測試可以正常工作,但第二次測試在獲取驅動程序時會得到空值。我沒有看到你的拆卸方法(@AfterTest)做了什麼,因此很難驗證,但我猜你將驅動程序設置爲空?

嘗試改變@BeforeSuite@BeforeTest,看看是否能解決問題:

@Parameters({ "browserType", "appURL" }) 
@BeforeSuite 
public void initializeTestBaseSetup(String browserType, String appURL) { 
    try { 
     setDriver(browserType, appURL); 

    } catch (Exception e) { 
     System.out.println("Error....." + e.getStackTrace()); 
    } 
} 

或者......如果你確實在拆卸方法與@AfterTest註釋設置驅動器爲null,你應該改變改爲@AfterSuite

相關問題