我是Selenium的新手,我使用IntelliJ,Selenium WebDriver,Junit。我的问题是在PageObject中设置TestBase类。这是我的TestBase类: 包TestBaseSetup;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class TestBase {
WebDriver driver;
public WebDriver getDriver() {
return driver;
}
@Before
public void testSetUp(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Dragana\\Desktop\\chromedriver.exe ");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized", "--disable-cache");
driver = new ChromeDriver(options);
driver.navigate().to("http://store.demoqa.com/");
}
@After
public void testTearDown(){
driver.close();
}
}
这是我的测试类:
package test;
import PageObjectPage.HomePage;
import PageObjectPage.LogInResultPage;
import PageObjectPage.MyAccount;
import TestBaseSetup.TestBase;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
public class AccountTest extends TestBase {
protected WebDriver driver;
public WebDriver getDriver() {
return driver;
}
@Test
public void shouldLogIn() {
HomePage onHomePage = new HomePage(driver);
System.out.println("Step 1 ");
MyAccount onMyAccount = onHomePage.clickOnMyAccount();
System.out.println("Step 2");
LogInResultPage onResultPage = onMyAccount.LogIn().submitForm();
System.out.println("Step 3");
wait(2000);
Assert.assertTrue(onResultPage.getMessage().contains("ERROR"));
}
public void wait(int seconds){
try {
Thread.sleep(2000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
这些是PageObject中的页面: 首页页面:
package PageObjectPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class HomePage {
private WebDriver driver;
public HomePage(WebDriver driver){
this.driver=driver;
}
public MyAccount clickOnMyAccount(){
//Click on My Account
driver.findElement(By.className("account_icon")).click();
return new MyAccount(driver);
}
//public HomePage navigateToWebApp(){
// return new HomePage();
//}
public void wait(int seconds){
try {
Thread.sleep(2000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
MyAccount页面:
package PageObjectPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class MyAccount {
protected WebDriver driver;
public MyAccount(WebDriver driver){
this.driver = driver;
}
public MyAccount LogIn(){
//Fill in the text box username
driver.findElement(By.id("log")).sendKeys("Dragana");
//Fill in the text box password
driver.findElement(By.id("pwd")).sendKeys("123456");
return new MyAccount(driver);
}
public LogInResultPage submitForm() {
//Click on button Log in
driver.findElement(By.id("login")).click();
return new LogInResultPage();
}
}
LogInResultPage页面:
package PageObjectPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LogInResultPage {
protected WebDriver driver;
public LogInResultPage(){
}
public String getMessage(){
//Printing message
return driver.findElement(By.tagName("p")).getText();
}
}
我的问题是当我运行测试输出时:
"C:\Program Files\Java\jdk1.8.0_101\bin\java" -ea -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.2.1\bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.2.1\lib\idea_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.2.1\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;C:\Users\Dragana\workspace\proba\target\test-classes;C:\Users\Dragana\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Dragana\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-java\3.0.0-beta3\selenium-java-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-chrome-driver\3.0.0-beta3\selenium-chrome-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-remote-driver\3.0.0-beta3\selenium-remote-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-api\3.0.0-beta3\selenium-api-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\cglib\cglib-nodep\3.2.4\cglib-nodep-3.2.4.jar;C:\Users\Dragana\.m2\repository\org\apache\commons\commons-exec\1.3\commons-exec-1.3.jar;C:\Users\Dragana\.m2\repository\com\google\code\gson\gson\2.3.1\gson-2.3.1.jar;C:\Users\Dragana\.m2\repository\com\google\guava\guava\19.0\guava-19.0.jar;C:\Users\Dragana\.m2\repository\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;C:\Users\Dragana\.m2\repository\org\apache\httpcomponents\httpcore\4.4.4\httpcore-4.4.4.jar;C:\Users\Dragana\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Dragana\.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;C:\Users\Dragana\.m2\repository\org\apache\httpcomponents\httpmime\4.5.2\httpmime-4.5.2.jar;C:\Users\Dragana\.m2\repository\net\java\dev\jna\jna\4.1.0\jna-4.1.0.jar;C:\Users\Dragana\.m2\repository\net\java\dev\jna\jna-platform\4.1.0\jna-platform-4.1.0.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-edge-driver\3.0.0-beta3\selenium-edge-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-firefox-driver\3.0.0-beta3\selenium-firefox-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-ie-driver\3.0.0-beta3\selenium-ie-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-opera-driver\3.0.0-beta3\selenium-opera-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-safari-driver\3.0.0-beta3\selenium-safari-driver-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\io\netty\netty\3.5.7.Final\netty-3.5.7.Final.jar;C:\Users\Dragana\.m2\repository\org\seleniumhq\selenium\selenium-support\3.0.0-beta3\selenium-support-3.0.0-beta3.jar;C:\Users\Dragana\.m2\repository\com\codeborne\phantomjsdriver\1.3.0\phantomjsdriver-1.3.0.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 test.AccountTest,shouldLogIn
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 9660
Only local connections are allowed.
Dec 05, 2016 3:41:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Dec 05, 2016 3:41:38 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Step 1
java.lang.NullPointerException
at PageObjectPage.HomePage.clickOnMyAccount(HomePage.java:18)
at test.AccountTest.shouldLogIn(AccountTest.java:26)
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:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code -1
运行此项目时,我不知道问题出在哪里。感谢。
答案 0 :(得分:0)
您需要使用PageFactory才能使用PageObjects,例如在PageObjectPage.HomePage.clickOnMyAccount(MyAccount.java:18)中 你应该返回这样的东西:
return PageFactory.initElements(getDriver(), SettingsPage.class);
当你开始测试时,第一页应该像这样初始化:
HomePage onHomePage = PageFactory.initElements(driver, HomePage .class);
并创建一个PageBase类(所有页面(Home,Account需要扩展它),如下所示:
protected WebDriver driver;
public PageBase(WebDriver driver){
this.driver = driver;
}
public WebDriver getDriver() {
return this.driver;
}
用这个你可以删除所有:
private WebDriver driver;
在页面中。 扩展PageObjects的页面也应该像这样初始化:
public HomePage(WebDriver driver) {
super(driver);
}
答案 1 :(得分:0)
这是我的TestBase设置类:
package TestBaseSetup;
import PageObjectPage.BasePage;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class TestBase {
WebDriver driver;
@Before
public void testSetUp(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Dragana\\Desktop\\chromedriver.exe ");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized", "--disable-cache");
driver = new ChromeDriver(options);
driver.navigate().to("http://store.demoqa.com/");
}
@After
public void testTearDown(){
driver.close();
}
}
这是BasePage类:
package PageObjectPage;
import org.openqa.selenium.WebDriver;
public class BasePage {
protected WebDriver driver;
public BasePage (WebDriver driver){
this.driver = driver;
}
public WebDriver getDriver() {
return this.driver;
}
}
这些是PageObject中的页面: 主页:
package PageObjectPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class HomePage extends BasePage {
@FindBy(how = How.CLASS_NAME, using = "account_icon")
@CacheLookup
WebElement button_my_accout;
public HomePage(WebDriver driver){
super(driver);
}
public MyAccount clickOnMyAccount(){
//Click on My Account
button_my_accout.click();
return PageFactory.initElements(getDriver(), MyAccount.class);
}
}
MyAccount页面:
package PageObjectPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class MyAccount extends BasePage {
@FindBy(id = "log")
@CacheLookup
WebElement username;
@FindBy(how = How.ID, using = "pwd")
@CacheLookup
WebElement password;
@FindBy(how = How.ID, using = "login")
@CacheLookup
WebElement login_button;
public MyAccount(WebDriver driver){
super(driver);
}
public MyAccount LogIn(){
//Fill in the text box username
username.sendKeys("Dragana");
//Fill in the text box password
password.sendKeys("123456");
return new MyAccount(driver);
}
public LogInResultPage submitForm() {
//Click on button Log in
login_button.click();
return new LogInResultPage(driver);
}
}
LogInResultPage:
package PageObjectPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LogInResultPage extends BasePage{
public LogInResultPage(WebDriver driver){
super(driver);
}
public String getMessage(){
//Printing message
return driver.findElement(By.tagName("p")).getText();
}
}
这是测试页面:
package test;
import PageObjectPage.HomePage;
import PageObjectPage.LogInResultPage;
import PageObjectPage.MyAccount;
import TestBaseSetup.TestBase;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
public class AccountTest extends TestBase {
WebDriver driver;
@Test
public void shouldLogIn() {
HomePage onHomePage = PageFactory.initElements(driver, HomePage.class);
System.out.println("Step 1 ");
MyAccount onMyAccount = onHomePage.clickOnMyAccount();
System.out.println("Step 2");
LogInResultPage onResultPage = onMyAccount.LogIn().submitForm();
System.out.println("Step 3");
wait(2000);
Assert.assertTrue(onResultPage.getMessage().contains("ERROR"));
}
public void wait(int seconds){
try {
Thread.sleep(2000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
这是一个问题:
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.$Proxy8.click(Unknown Source)
at PageObjectPage.HomePage.clickOnMyAccount(HomePage.java:25)
at test.AccountTest.shouldLogIn(AccountTest.java:23)
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:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code -1
答案 2 :(得分:0)
在您的测试脚本中,您尚未初始化WebDriver
,因此存在问题。对于例如在AccountTest
中,正在使用的驱动程序未初始化。您可以放置调试点并检查其值。或者在constructor
的{{1}}中,您可以初始化它,然后尝试执行。它应该工作。
如下所示:
TestBase
如果该方法有助于或需要任何帮助,请告诉我