在通过Selenium运行JUnit测试用例时出现错误。

huangapple go评论97阅读模式
英文:

Errors when running a JUnit test case via Selenium

问题

以下是你的代码翻译部分:

这是我的 Main Page 类:

public class MainPage {
    private WebDriver driver;

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

    @FindBy(xpath = "//form/h3")
    private WebElement heading;
}

这是我想通过 JUnit 进行测试的方法:

public String getErrorText(){
    return errorMessage.getText();
}

这是我的 JUnit 测试类:

public class MainPageTest {
    private WebDriver driver;
    private MainPage mainPage;

    @Before
    public void setUp(){
        System.setProperty("webdriver.chrome.driver", "D:\\Intellij IDEA\\testgroup\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://dtapi.if.ua/login");
        mainPage = new MainPage(driver);
    }

    @Test
    public void changeLanguage(){
        String heading = mainPage.getHeadingText();
        Assert.assertEquals("Authorization", heading);
    }
}

但是我遇到了一个错误:

启动 ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}),端口为 6024
仅允许本地连接。
请保护 ChromeDriver 和相关测试框架使用的端口,以防止恶意代码访问。
[1584285941.329][警告]: 连接 Chrome 超时,正在重试...

以及

java.lang.NullPointerException
    at MainPage.getHeadingText(MainPage.java:70)
    at MainPageTest.changeLanguage(MainPageTest.java:26)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    // 更多错误信息...
英文:

Here is my Class Main Page an

    private WebDriver driver;

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

@FindBy(xpath = "//form/h3")
 private WebElement heading;

And here is my method that i want to test by JUnit

public String getErrorText(){
return errorMessage.getText();
    }

Here my JUnit class

public class MainPageTest {
private WebDriver driver;
private MainPage mainPage;

@Before
public  void setUp(){
    System.setProperty("webdriver.chrome.driver", "D:\\Intellij IDEA\\testgroup\\drivers\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("https://dtapi.if.ua/login");
    mainPage = new MainPage(driver);
}
@Test
public void changeLanguage(){

    String heading = mainPage.getHeadingText();
    Assert.assertEquals("Authorization",heading);
}

But i'm getting an error:

Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 6024
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1584285941.329][WARNING]: Timed out connecting to Chrome, retrying..

And

 java.lang.NullPointerException
    	
at MainPage.getHeadingText(MainPage.java:70)
	at MainPageTest.changeLanguage(MainPageTest.java:26)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	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:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

答案1

得分: 0

你已经初始化了你的PageFactory吗?

尝试以下操作 -

PageFactory.initElements(driver, this);

参考:
https://stackoverflow.com/questions/51185170/java-selenium-java-lang-nullpointerexception

英文:

Have you initated your PageFactory?

Try the following -

PageFactory.initElements(driver, this);

Ref:
https://stackoverflow.com/questions/51185170/java-selenium-java-lang-nullpointerexception

huangapple
  • 本文由 发表于 2020年3月15日 23:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/60694256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定