问题描述
我正在尝试使用页面对象模式执行基本的硒操作。但是,当我运行测试时,我看到浏览器随同给定的URL一起打开,但是由于抱怨无法初始化声明的Web元素而失败了。可以请一个人帮助我了解我在这里缺少什么。下面是我的基类,页面对象类和测试类。
BasePage
public class Basepage {
public static WebDriver driver;
public Basepage(){}
public Basepage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
}
public void Openbrowser(){
System.setProperty("webdriver.chrome.driver","C:\\SeleniumWork\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("https://www.google.com");
}
页面对象类
public class SecurityMainPage extends Basepage{
public SecurityMainPage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
}
@FindBy( xpath = "//a[text()='What We Do']")
WebElement dropdown;
public void selectOption(String option){
Actions actions = new Actions(driver);
actions.movetoElement(dropdown).click().build().perform(); //dropdown element is returning null
}
TestClass
public class SecurityMainPageTests extends Basepage {
protected static SecurityMainPage sec = new SecurityMainPage(driver);
public SecurityMainPageTests(){}
@BeforeTest
public void setup(){
Openbrowser();
}
@Test
public void selectOptiontest(){
sec.selectOption("Networking");
}
@AfterTest
public void tearDown(){
closebrowser();
}
org.openqa.selenium.json.JsonException: java.lang.reflect.InvocationTargetException
Build info: version: '3.13.0',revision: '2f0d292',time: '2018-06-25T15:24:21.231Z'
System info: host: 'LAPTOP-5J2KPMM3',ip: '192.168.0.36',os.name: 'Windows 10',os.arch: 'amd64',os.version: '10.0',java.version: '11.0.7'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.json.JsonOutput.convertUsingMethod(JsonOutput.java:302)
at org.openqa.selenium.json.JsonOutput.lambda$new$13(JsonOutput.java:139)
at org.openqa.selenium.json.JsonOutput$SafeBiConsumer.accept(JsonOutput.java:394)
at org.openqa.selenium.json.JsonOutput.write(JsonOutput.java:248)
at org.openqa.selenium.json.JsonOutput.lambda$null$18(JsonOutput.java:152)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)
at org.openqa.selenium.json.JsonOutput.lambda$new$19(JsonOutput.java:152)
at org.openqa.selenium.json.JsonOutput$SafeBiConsumer.accept(JsonOutput.java:394)
at org.openqa.selenium.json.JsonOutput.write(JsonOutput.java:248)
at org.openqa.selenium.json.JsonOutput.lambda$null$20(JsonOutput.java:161)
at com.google.common.collect.SingletonImmutableBiMap.forEach(SingletonImmutableBiMap.java:65)
at org.openqa.selenium.json.JsonOutput.lambda$new$21(JsonOutput.java:160)
at org.openqa.selenium.json.JsonOutput$SafeBiConsumer.accept(JsonOutput.java:394)
at org.openqa.selenium.json.JsonOutput.write(JsonOutput.java:248)
at org.openqa.selenium.json.JsonOutput.write(JsonOutput.java:239)
at org.openqa.selenium.json.Json.toJson(Json.java:42)
at org.openqa.selenium.remote.http.AbstractHttpCommandCodec.encode(AbstractHttpCommandCodec.java:227)
at org.openqa.selenium.remote.http.AbstractHttpCommandCodec.encode(AbstractHttpCommandCodec.java:117)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:152)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:614)
at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:638)
at test.java.SecurityMainPage.selectOption(SecurityMainPage.java:37)
at test.java.SecurityMainPageTests.selectOptionTest(SecurityMainPageTests.java:28)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
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:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1191)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.run(TestNG.java:1024)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
Suppressed: org.openqa.selenium.json.JsonException: Attempting to close incomplete json stream
Build info: version: '3.13.0',time: '2018-06-25T15:24:21.231Z'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.json.JsonOutput.close(JsonOutput.java:263)
at org.openqa.selenium.json.Json.toJson(Json.java:44)
... 32 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.openqa.selenium.json.JsonOutput.convertUsingMethod(JsonOutput.java:298)
... 47 more
Caused by: 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.$Proxy9.getWrappedElement(UnkNown Source)
at org.openqa.selenium.interactions.PointerInput$Origin.asArg(PointerInput.java:203)
at org.openqa.selenium.interactions.PointerInput$Move.encode(PointerInput.java:154)
at org.openqa.selenium.interactions.Sequence.encode(Sequence.java:75)
at org.openqa.selenium.interactions.Sequence.toJson(Sequence.java:84)
... 52 more
解决方法
为便于理解,我将驱动程序的启动和导航分为两种,分别使用Basepage
中的url。而且您还没有声明closeBrowser()
方法。
基类
public class Basepage {
public static WebDriver driver;
public Basepage(){}
public Basepage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver,this);
}
public WebDriver initiateDriver(){
System.setProperty("webdriver.chrome.driver","C:\\SeleniumWork\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
return driver;
}
public void openBrowser(String url){
driver.navigate().to(url);
}
}
测试类
public class SecurityMainPageTests extends Basepage {
WebDriver driver = null;
protected static SecurityMainPage sec = null;
String url = "https://www.google.com";
public SecurityMainPageTests(){}
@BeforeTest
public void setup(){
driver = initiateDriver();
openBrowser(String url);
sec = new SecurityMainPage(driver);
}
@Test
public void selectOptionTest(){
sec.selectOption("Networking");
}
@AfterTest
public void tearDown(){
closeBrowser();
}
}