简体   繁体   English

无法在 selenium webdriver java 中使用 @FindBy 进行初始化

[英]Unable to initialise using @FindBy in selenium webdriver java

I am trying to perform basic selenium operations using page object pattern.我正在尝试使用页面对象模式执行基本的硒操作。 However, when I run the test, I see browser being opened along with given url, but fails the test by complaining it couldn't initialise the declared web element.但是,当我运行测试时,我看到浏览器与给定的 url 一起打开,但由于抱怨无法初始化声明的 web 元素而导致测试失败。 Can some one please help me to understand what I'm missing here.有人可以帮助我了解我在这里缺少什么。 Below are my base class, page object class and test class.下面是我的基类、页面对象类和测试类。

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");
}

Page object class页面对象类

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();
}

Log日志

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', revision: '2f0d292', 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

For easy understanding, I split the driver initiation and navigation to url to two methods in Basepage .为了便于理解,我将驱动程序启动和导航到 url 拆分为Basepage两个方法。 And you haven't declared the closeBrowser() method.而且您还没有声明closeBrowser()方法。

Base Class基类

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);
    }
}

Test Class测试班

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM