开发者社区> 问答> 正文

通过Selenium运行JUnit测试用例时出错

这里是我的类主页的

    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);
}

但是我遇到一个错误:

在端口6024上启动ChromeDriver 80.0.3987.106(f68069574609230cf9b635cd784cfb1bf81bb53a-refs / branch-heads / 3987 @ {#882})仅允许本地连接。请保护ChromeDriver和相关测试框架使用的端口,以防止恶意代码访问。[1584285941.329] [WARNING]:连接到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)
    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)

问题来源:Stack Overflow

展开
收起
montos 2020-03-24 12:59:34 592 0
1 条回答
写回答
取消 提交回答
  • 您是否启动了PageFactory

    试试以下-

    PageFactory.initElements(driver, this);
    

    参考: Java / Selenium-java.lang.NullPointerException

    回答来源:Stack Overflow

    2020-03-24 13:00:19
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
移动互联网测试到质量的转变 立即下载
给ITer的技术实战进阶课-阿里CIO学院独家教材(四) 立即下载
F2etest — 多浏览器兼容性测试整体解决方案 立即下载