我通過Java的webdriver-factory編寫了Selenium WebDriver的測試,並且遇到了測試問題。 我有測試此基類:使用webdriver-factory運行Selenium Webdriver TestNG測試
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();
}
}}
和兩個類,擴展該類:LoginTest和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!");
}}
如果我跑我的TestNG的套房只有LoginTest - 它的工作原理是正確的。但是,如果我嘗試按套件運行所有測試,LoginPage - 傳遞並且LogoutPage通過navigate()方法有NullPointerException。我該如何解決它? 我的套房:
<?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>
哪個元素給了你這個NullPointerException? –
@YuZhang'driver.get(url);' – Giymose