0
我在我的WebDriver測試中遇到問題。兩個測試一個接一個
在第一個包我有2個類(頁)HomePage,LoginPage。
在secound包中我有測試 - goToLiginPageTest和LoginTest。
在goToLiginPage我檢查,我在主頁上,進入登錄頁面。
在LoginTest中檢查我是否在登錄頁面並登錄。
但是來自goToLiginPageTest的兩個測試通過,但是來自LoginTest的測試失敗。
我不確定這是不是我的textng.xlm錯誤,或者我犯了錯誤。請幫幫我。
public class HomePage {
WebDriver driver;
public static final String PAGE_TITLE = "page title";
public static final String PAGE_URL = "www.blbl.pl";
@FindBy(xpath = "//*[@id='global-nav']/div/div/div[3]/ul/li[1]/a")
WebElement LogInLink;
public HomePage(WebDriver driver){
this.driver = driver;
}
public void isHomePage(){
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, PAGE_TITLE);
}
public void goToLoginPage(){
LogInLink.click();
}
}
LoginPage
public class LoginPage {
WebDriver driver;
public static final String PAGE_TITLE = "Login";
@FindBy(id="user_email")
WebElement inputUserEmail;
@FindBy(id="user_password")
WebElement inputUserPassword;
public LoginPage(WebDriver driver){
this.driver = driver;
}
public void isLoginPage(){
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, PAGE_TITLE);
}
public void fillUserEmail(){
inputUserEmail.sendKeys("[email protected]");
Assert.assertEquals(inputUserEmail.getAttribute("value"), "[email protected]");
}
public void fillUserPassword(){
inputUserPassword.sendKeys("123456");
Assert.assertEquals(inputUserPassword.getAttribute("value"), "123456");
}
}
GotoLoginPageTest
import pages.HomePage;
public class GotoLoginPageTest {
WebDriver driver;
HomePage hp;
@BeforeClass
public void setup(){
this.driver = new FirefoxDriver();
hp = PageFactory.initElements(driver, HomePage.class);
driver.get(HomePage.PAGE_URL);
}
@Test(priority = 1)
public void isHomePage(){
hp.isHomePage();
}
@Test(dependsOnMethods = "isHomePage")
public void gotoLoginPage(){
hp.goToLoginPage();
}
}
LoginTest
public class LoginTest {
WebDriver driver;
LoginPage lp = PageFactory.initElements(driver, LoginPage.class);
@Test
public void cheskIsLoginPage(){
lp.isLoginPage();
}
@Test
public void logInBase(){
lp.fillUserEmail();
lp.fillUserPassword();
}
}
我的testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="tests.GotoLoginPageTest"/>
<class name="tests.LoginTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
我有錯誤
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy5.sendKeys(Unknown Source)
at pages.LoginPage.fillUserEmail(LoginPage.java:30)
at tests.LoginTest.logInBase(LoginTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:771)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1032)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
但我不想創建新的FFdriver,我想在登錄頁面後登錄。在同一個測試中。我只想要一個接一個地運行GotoLoginPageTest和LoginTest。林不知道,但我認爲測試運行不良,首先是LoginTest和GotoLoginPageTest。我把這裏放在eclipse屏幕上http://zapodaj.net/c776122be852e.png.html – ciupakabrans
否則你必須傳遞一個null驅動程序到Loginpage。另一方面,您應該在全局創建驅動程序,而不是在每個測試對象中創建驅動程序。看到和示例[這裏](https://github.com/safrrhmn/SeleniumTestNG/blob/master/src/main/java/com/github/tests/GitHubHomePageTests.java) – Saifur
對不起,我的問題,但我不想在每個測試中創建新的firefoxDriver對象。我只是想做一個測試在哪裏運行兩個案例。如果我創建新的ffdriver,那麼將會打開新的瀏覽器,我想在一個瀏覽器中執行這兩種情況,一個接一個的 – ciupakabrans