我是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$1.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$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.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();
}
}
答案 0 :(得分:1)
WebDriver未正确初始化,因为@FindBy
正在给出空指针异常。我认为主要问题是你以错误的方式使用了类Abstract。我认为在所有其他类中继承Abstract类没有用处。我知道您需要将WebDriver实例传递给所有类。但是使用继承不是正确的方法。 Inhertance应始终遵循is-a
条件。修改您的类,如下所示:
摘要页面
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类
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类。你必须使用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();
}
}
答案 1 :(得分:1)
我认为你需要在构造函数
中使用initElements初始化驱动程序下面是修改后的代码,可以解决您的问题,抱歉,我不熟悉这个格式不好的博客。
我的测试
公共类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);
}
}
然后创建一个用于运行测试的类,如下所示:
@Test
public void testulUnuCompletareFlexi()
{
CompletareFlexi输入=新的CompletareFlexi(驱动程序) input.completareFlexiCampuri();
}