gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:50:25 25 4
gpt4 key购买 nike

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

最佳答案

WebDriver 未正确初始化,因为此 @FindBy 给出空指针异常。我认为主要问题是您以错误的方式使用了 class Abstract 。我认为在所有其他类中继承抽象类没有用。我知道您需要将 WebDriver 实例传递给所有类。但是为此使用继承并不是正确的方法。继承应始终遵循 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();
}
}

关于java - 我在@FindBy WebDriver 得到 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30997596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com