【问题标题】:I am getting "null pointer Exception" with com.sun.jdi.InvocationException occurred invoking method. when used PageFactory in selenium我在 com.sun.jdi.InvocationException 发生调用方法时收到“空指针异常”。在 selenium 中使用 PageFactory 时
【发布时间】:2017-10-12 14:53:15
【问题描述】:

我在 com.sun.jdi.InvocationException 发生调用方法时收到“空指针异常”。在 selenium 中使用 PageFactory 时。

我正在尝试使用 Junit 创建一个测试用例。当我尝试调用父类中存在的方法时,我面临空指针异常

我的代码如下

class 1:

public class Browser {

public static RemoteWebDriver driver;

public void openInternetExplorer(){
    System.setProperty("webdriver.ie.driver","path to ie driver");
      driver = new InterExplorerDriver();
 driver.get("https://www.gmail.com")
    }
}

Class2:

public class Elements extends Browser{

public enterText(WebElement element, String text){

element.sendKeys(text);
}
}

Class3:

public BaseClass extends Elements{

BaseClass(){
PageFactory.initElements(driver,this);
}

}

Class4:

public Testing extends BaseClass{

@FindBy(id="unique_name") WebElement uniqName;


public void method1(){
openInternetExplorer();
enterText(uniqName,"SuperMan");

}

}


Error Which i am getting is

    java.lang.NullPointerException
    at        org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(Defaul  tElementLocator.java:69)
    at      org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(L     ocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy7.click(Unknown Source)
    at com.framework.Browser.clickAction(Element.java:5)
    at com.framework.Testing.Testing(Testing.java:7)

如果我在这里遗漏了什么,请告诉我。

【问题讨论】:

    标签: java eclipse selenium-webdriver junit nullpointerexception


    【解决方案1】:

    你必须执行PageFactory.initElements(driver,this); 在调用enterText(uniqName,"SuperMan"); 方法调用元素之前在Testing 类中。

    否则您的@FindBy 元素将不会被调用。

    【讨论】:

    • 感谢您的快速回复。我已经添加了 PageFactory.initElements(driver,this);在 BaseClass 构造函数和扩展中。我猜会先调用构造函数,然后如果 @FindBy() 会继续执行
    • 是的,但这只会调用 BaseClass 中定义的元素,这些元素不适用于测试类 WebElements
    • 谢谢库沙尔。当我添加 PageFactory.initElements(driver,this); 时它起作用了特别是在 enterText() 之前的一些类似 PageFactory.initElements(driver,this);输入文本();然后它起作用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2013-09-17
    相关资源
    最近更新 更多