我在@FindBy WebDriver 得到 java.lang.NullPointerException

I get java.lang.NullPointerException at @FindBy WebDriver

我是 Selenium WebDriver 的新手,我正在尝试创建一些测试,但是每次我在测试中使用 @FindBy 时都不起作用(java.lang.NullPointerException),相反,如果我使用 findElement,工作正常。

错误:

java.lang.NullPointerException
at 123.TestFormular.testFormular(TestFormular.java:38)
at 123.CompletareFlexi.completareFlexiCampuri(CompletareFlexi.java:29)
at 123.TestUnu.testulUnuCompletareFlexi(TestUnu.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

这是我的代码: 我的摘要页面:

package 123;

import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import parametrii.Utilitara;

public class Abstract {

    public Abstract() {
        super();
    }

    protected WebDriver driver;

    public Abstract(WebDriver driver) {
       this.driver=driver;
    }

    @Before
    public void setApp(){
        Utilitara util = new Utilitara(driver);
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.navigate().to("http://xxxxx.logon.jsp");
    }

    @After
    public void closeApp(){
        //driver.quit();
    }
}

我的登录页面:

package 123;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class TestLogIn extends Abstract {
    public TestLogIn(WebDriver driver) {
        super(driver);
    }


    //@Test
    public void logareXXX() {
        driver.findElement(By.cssSelector("form input[type='text'][name='username']")).sendKeys("XX");
        driver.findElement(By.cssSelector("form input[name='password']")).sendKeys("XX");
        driver.findElement(By.cssSelector("form input[name='submit']")).click()
        //Assert.assertEquals(driver.findElement(By.tagName("h3")).getText(), "xxx");
        //driver.findElement(By.cssSelector("[href='logoff.do']")).click();
        //driver.findElement(By.cssSelector("#info")).getText();
        //System.out.println();
        //Assert.assertEquals("Informatii generale", driver); ("announcement")
    }
}

我的形成方式:

package 123;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class TestFormular extends Abstract{

    public TestFormular(WebDriver driver) {
        super(driver);
    }

    @Test
    public CompletareFlexi testFormular() throws Throwable { 
        TestLogIn TestLogIn = new TestLogIn(driver);
        TestLogIn.logareXX();

        //driver.findElement(By.linkText("xxxx")).click();
        //adaugarePresocring.click();

        Select Produs = new Select(driver.findElement(By.className("valid")));
        Produs.selectByIndex(2);                
        driver.findElement(By.cssSelector(".submitButton")).click();

        driver.findElement(By.cssSelector("form input[name='xxxx']")).sendKeys("xxxx");
        driver.findElement(By.cssSelector("form input[type='button'][onclick='xxxx()']")).click();
        //Alert alert =
                driver.switchTo().alert().accept();;
        //System.out.println(alert.getText());
        //alert.accept();

        driver.findElement(By.cssSelector("form input[type='button'][onclick='xxx();']")).click();

        PrintareProblem thr = new PrintareProblem( "PrintareProblem", 5000);
        driver.findElement(By.cssSelector(".button")).click();

        return PageFactory.initElements(driver, CompletareFlexi.class);
    }
}

这里测试失败,这里我使用@FindBy:

package 123;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import parametrii.Utilitara;

public class CompletareFlexi extends Abstract{

    //FindBy
    @FindBy(css="input[type='text'][name='price2']")  
    WebElement price2two;

    public CompletareFlexi(WebDriver driver) {
        super(driver);
    }

    public TestFormular  completareFlexiCampuri() throws Throwable{
        Utilitara util = new Utilitara(driver);
        TestFormular TestFormular = new TestFormular(driver);
        TestFormular.testFormular();
        price2two.sendKeys(Utilitara.randomNumar(5));

        //driver.findElement(By.cssSelector("input[type='text'][name='xxxxx']")).sendKeys(Utilitara.randomNumar(5));
        return PageFactory.initElements(driver, TestFormular.class);
    }
}

这是我的测试:

package 123;

import org.junit.Test;
import org.openqa.selenium.WebDriver;

public class TestUnu extends Abstract {

    @Test
    public void testulUnuCompletareFlexi() throws Throwable {
        CompletareFlexi CompletareFlexi = new   CompletareFlexi(driver);
        CompletareFlexi.completareFlexiCampuri();
    }
}

WebDriver 未正确初始化,因为 @FindBy 给出了空指针异常。我认为主要问题是您以错误的方式使用了 class Abstract。我认为在所有其他 class 中继承摘要 class 没有用。我知道您需要将 WebDriver 实例传递给所有 classes。但是为此使用继承并不是正确的方法。继承应始终遵循 is-a 条件。如下所示修改您的 classes:

摘要页:

package 123;

import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import parametrii.Utilitara;

public abstract class Abstract {

    protected WebDriver driver;

    // This constructor is not required
    //public Abstract(WebDriver driver) {
      // this.driver=driver;
    //}

    @Before
    public void setApp(){
        Utilitara util = new Utilitara(driver);
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.navigate().to("http://xxxxx.logon.jsp");
    }

    @After
    public void closeApp(){
        //driver.quit();
    }
}

登录页面:

package 123;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class TestLogIn {
    private WebDriver driver;

    public TestLogIn(WebDriver driver) {
        this.driver = driver;
    }


    //@Test
    public void logareXXX() {
        driver.findElement(By.cssSelector("form input[type='text'][name='username']")).sendKeys("XX");
        driver.findElement(By.cssSelector("form input[name='password']")).sendKeys("XX");
        driver.findElement(By.cssSelector("form input[name='submit']")).click()
        //Assert.assertEquals(driver.findElement(By.tagName("h3")).getText(), "xxx");
        //driver.findElement(By.cssSelector("[href='logoff.do']")).click();
        //driver.findElement(By.cssSelector("#info")).getText();
        //System.out.println();
        //Assert.assertEquals("Informatii generale", driver); ("announcement")
    }
}

形成方式:

package 123;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class TestFormular{
    private WebDriver driver;       

    public TestFormular(WebDriver driver) {
        this.driver = driver;
    }

    @Test
    public CompletareFlexi testFormular() throws Throwable { 
        TestLogIn TestLogIn = new TestLogIn(driver);
        TestLogIn.logareXX();

        //driver.findElement(By.linkText("xxxx")).click();
        //adaugarePresocring.click();

        Select Produs = new Select(driver.findElement(By.className("valid")));
        Produs.selectByIndex(2);                
        driver.findElement(By.cssSelector(".submitButton")).click();

        driver.findElement(By.cssSelector("form input[name='xxxx']")).sendKeys("xxxx");
        driver.findElement(By.cssSelector("form input[type='button'][onclick='xxxx()']")).click();
        //Alert alert =
                driver.switchTo().alert().accept();;
        //System.out.println(alert.getText());
        //alert.accept();

        driver.findElement(By.cssSelector("form input[type='button'][onclick='xxx();']")).click();

        PrintareProblem thr = new PrintareProblem( "PrintareProblem", 5000);
        driver.findElement(By.cssSelector(".button")).click();

        return PageFactory.initElements(driver, CompletareFlexi.class);
    }
}

CompletareFlexi class

package 123;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import parametrii.Utilitara;

public class CompletareFlexi {
    private WebDriver driver;

    //FindBy
    @FindBy(css="input[type='text'][name='price2']")  
    WebElement price2two;

    public CompletareFlexi(WebDriver driver) {
        this.driver = driver
    }

    public TestFormular  completareFlexiCampuri() throws Throwable{
        Utilitara util = new Utilitara(driver);
        TestFormular TestFormular = new TestFormular(driver);
        TestFormular.testFormular();
        price2two.sendKeys(Utilitara.randomNumar(5));

        //driver.findElement(By.cssSelector("input[type='text'][name='xxxxx']")).sendKeys(Utilitara.randomNumar(5));
        return PageFactory.initElements(driver, TestFormular.class);
    }
}

**编辑:**您在这里使用正常调用调用 CompletareFlexi class。您必须改为使用 PageFactory 初始化。

这是我的测试:

package 123;

import org.junit.Test;
import org.openqa.selenium.WebDriver;

public class TestUnu extends Abstract {

    @Test
    public void testulUnuCompletareFlexi() throws Throwable {
        /*
        * Change in code here
        */
        CompletareFlexi CompletareFlexi = PageFactory.initElements(driver, CompletareFlexi.class);
        CompletareFlexi.completareFlexiCampuri();
    }
}

我认为您需要在构造函数中使用 initElements 来初始化驱动程序

下面是可以解决您问题的修改后的代码,对不起,我是这个博客的新手,不擅长格式化。

我的测试

public class CompletareFlexi

{

WebDriver driver;

public CompletareFlexi(WebDriver driver){
    PageFactory.initElements(driver, this);
    this.driver = driver;

}
//FindBy
@FindBy(css="input[type='text'][name='price2']")  
WebElement price2two;



public CompletareFlexi completareFlexiCampuri() {
    price2two.sendKeys(Utilitara.randomNumar(5));

   return PageFactory.initElements(driver, CompletareFlexi.class);
}

}

然后为 运行 你的测试创建一个 class,如下所示:

@测试

public void testulUnuCompletareFlexi()

{

CompletareFlexi 输入 = new CompletareFlexi(驱动程序) input.completareFlexiCampuri();

}