Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium POM模型:java.lang.NullPointerException错误。无法为Webelements输入值或选择元素_Java_Selenium_Automated Tests_Pageobjects - Fatal编程技术网

Selenium POM模型:java.lang.NullPointerException错误。无法为Webelements输入值或选择元素

Selenium POM模型:java.lang.NullPointerException错误。无法为Webelements输入值或选择元素,java,selenium,automated-tests,pageobjects,Java,Selenium,Automated Tests,Pageobjects,我第一次实现POM模型sleneium 我正在使用骨架函数初始化我的WebDriver。详情如下: File pathToBinary = new File("<path>\firefox.exe"); FirefoxBinary binary = new FirefoxBinary(pathToBinary); FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("network.p

我第一次实现POM模型sleneium

我正在使用骨架函数初始化我的WebDriver。详情如下:

File pathToBinary = new File("<path>\firefox.exe");
FirefoxBinary binary = new FirefoxBinary(pathToBinary);     
FirefoxProfile profile = new FirefoxProfile();  
profile.setPreference("network.proxy.http", "<proxyaddress>");
profile.setPreference("network.proxy.http_port", "<portnumber>");
driver = new FirefoxDriver(binary, new FirefoxProfile());
driver.manage().window().maximize();
使用objFirstClass,我可以登录到我的系统并验证我的登录。 使用objSecondClass,我可以打印表示登录成功的字符串。 但是使用objThirdClass,我无法为WebElements输入值或选择对象

它给出了Null异常错误

public class TestClass 
{
    WebDriver driver = Driver.AppDriver.getInstance();

    FirstClass obFirstClass  = new FirstClass();
    SecondClass objSecondClass  = new SecondClass ();   
    ThirdClass objThirdClass  = new ThirdClass (driver);

    @Test(priority=2)
    public void method()
        {   
            objThirdClass.action1();
            System.out.println("after action"); //-> This line is being printed

            objThirdClass.action2(param1, param2, param3);
        }           
}

public class ThirdClass {

    WebDriver driver = Driver.AppDriver.getInstance();

    public ThirdClass(WebDriver _driver){         
        //This initElements method will create all WebElements 
        driver = _driver;
        PageFactory.initElements(driver, this); 
    }

    @FindBy(xpath=<xpath>)
    WebElement elementCreate;

    @FindBy(id=<id1>)
    Select selectElement1;

    @FindBy(id=<id2>)
    Select selectElement2;

    @FindBy(id=<id3>)
    Select selectElement3;

    @FindBy(id="submit")
    WebElement elementSubmit;

    public void action1()
        {
            JavascriptExecutor executor2 = (JavascriptExecutor)driver;
            executor2.executeScript("arguments[0].click();", elementCreate);
            System.out.println("Create link found");
        }

    public void setElement1(String str1)
        {
            selectElement1.selectByVisibleText(str1);
        }

    public void setElement1(String str2)
        {
            selectElement2.selectByVisibleText(str2);
        }

    public void setElement1(String str3)
        {
            selectElement3.selectByVisibleText(str3);
        }


    public void submit()
        {
            submit.click();
        }


    public void action2(String string1, String string2, String string3,)
        {               
            this.setElement1(str1);
            this.setElement2(str2);
            this.setElement3( str3) 
            this.submit();  
        }   
}
公共类TestClass
{
WebDriver=driver.AppDriver.getInstance();
FirstClass obFirstClass=新的FirstClass();
SecondClass objSecondClass=新的SecondClass();
第三类对象第三类=新的第三类(驱动程序);
@测试(优先级=2)
公开作废法()
{   
objThirdClass.action1();
System.out.println(“操作后”);//->正在打印此行
objThirdClass.action2(参数1、参数2、参数3);
}           
}
公共类第三类{
WebDriver=driver.AppDriver.getInstance();
公共第三类(WebDriver _driver){
//此initElements方法将创建所有WebElements
驱动程序=_驱动程序;
PageFactory.initElements(驱动程序,this);
}
@FindBy(xpath=)
WebElement元素创建;
@FindBy(id=)
选择selectElement1;
@FindBy(id=)
选择selectElement2;
@FindBy(id=)
选择selectElement3;
@FindBy(id=“提交”)
WebElement元素提交;
公共无效行动1()
{
JavascriptExecutor Executor 2=(JavascriptExecutor)驱动程序;
Execute2.executeScript(“参数[0]。单击();”,元素创建);
System.out.println(“创建找到的链接”);
}
公共void setElement1(字符串str1)
{
SelectElement 1.selectByVisibleText(str1);
}
公共void setElement1(字符串str2)
{
SelectElement 2.selectByVisibleText(str2);
}
公共void setElement1(字符串str3)
{
SelectElement 3.selectByVisibleText(str3);
}
公开作废提交()
{
提交。单击();
}
公共无效操作2(字符串string1、字符串string2、字符串string3)
{               
这是setElement1(str1);
这是setElement2(str2);
此.setElement3(str3)
这个。提交();
}   
}

问题似乎出在
驱动程序实例上。您需要使用测试中的驱动程序覆盖PageObject中的驱动程序。确切地说,您应该创建一个基类来处理所有常用方法、驱动程序实例化、pageFactory和元素实例化,并从每个PageObjects继承这些方法,以减少混淆和重复。我举了一个例子,如果这有帮助的话

public class TestClass(){
    WebDriver driver = Driver.AppDriver.getInstance();
    driver = new ChromeDriver();

    ThirdClass objThirdClass  = new ThirdClass (driver);

    public void method()
    {   
        objThirdClass.action1();
        System.out.println("after action"); //-> This line is being printed

        objThirdClass.action2(param1, param2, param3);
    }
}


public class ThirdClass {

    WebDriver driver = Driver.AppDriver.getInstance();

    public ThirdClass(WebDriver _driver){ 
        driver  = _driver;
        //This initElements method will create all WebElements 
        PageFactory.initElements(driver, this); 
    }
}

我参考了以下链接:

所以,如果你这样做:

@FindBy(id="foo")
private WebElement wannabeSelect;
Select realSelect = new Select(wannabeSelect);
realSelect.selectByValue("myValue");
它应该会起作用

顺便说一句,我在“变通方法”中使用了与您相同的方法,因为我不想在需要选择对象时强制转换新的WebElement对象。但不管怎样

sDriver.findElement(By.id("foo"));
returns WebElement, so thats why its working. You can also do this:

 WebElement wannabeSelect = sDriver.findElement(By.id("foo"));
 Select foo = new Select(wannabeSelect);

它解决了这个问题。

可能会创建并显示如何实例化所有三个对象(如果可能),并缩小stacktrace的原因。哪一行生成了错误?我已经创建了一个基类,其中创建了所有类的对象。我使用了骨架函数,以便始终使用相同的webdriver对象。但一旦运行了测试,所有对象都将实例化,因此找不到WebElements和selectelements。对象在第一个注释中实例化,未找到元素。我如何解决这个问题,以便在需要时使用更新的驱动程序并填充元素。我让您参考github。我通常就是这样做的,我已经做了更改,但仍然无法解决问题。但是,我可以执行“objThirdClass.action1();”,但是setelement1仍然抛出nullexceptionerror。
@FindBy(id="foo")
private WebElement wannabeSelect;
Select realSelect = new Select(wannabeSelect);
realSelect.selectByValue("myValue");
sDriver.findElement(By.id("foo"));
returns WebElement, so thats why its working. You can also do this:

 WebElement wannabeSelect = sDriver.findElement(By.id("foo"));
 Select foo = new Select(wannabeSelect);