使用JavascriptExecutor在chekcing页面源中的Nullpointerexception

时间:2015-12-23 11:18:54

标签: java selenium nullpointerexception webdriver

我需要检查网站上是否有文字。根据{{​​3}},我试图避免getPageSource()。我需要在各种文件中使用我的函数,所以我想我可以在不同的文件中创建它并只编写一次代码。

这是一个文件,我只是为了检查页面来源而写的:

package pl.mainfolder.tests.selenium;

import static org.junit.Assert.fail;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

public class CheckSource {
private WebDriver driver;

public void CheckSite(String textToBeChecked, String error)
        throws Exception {
    Object test = (((JavascriptExecutor) driver)
            .executeScript("return document.body.innerHTML;"));
    if (!((String) test).contains(textToBeChecked)) {
        fail(error);
    }
}
}

我尝试用以下代码启动该代码:

package pl.mainfolder.tests.selenium;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

import pl.mainfolder.tests.selenium.utils.WebDriverUtil;

public class LoginTest {
private WebDriver driver;
private String baseUrl;
private final StringBuffer verificationErrors = new StringBuffer();
public CheckSource Source = new CheckSource();

@Before
public void setUp() throws Exception {
    driver = WebDriverUtil.getFirefoxWebDriver();
    driver.manage().deleteAllCookies();
    baseUrl = "https://google.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void Login() throws Exception {
    driver.get(baseUrl);
    Source.CheckSite("Log in", "Error1");
(...)

仍然出现错误NullPointerExcepiton

java.lang.NullPointerException
at pl.mainfolder.tests.selenium.CheckSource.CheckSite(CheckSource.java:14)
at pl.mainfolder.tests.selenium.LoginPLTest.Login(LoginPLTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
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)

我尝试使用try-catch,但是检查代码没有工作(一切都很好,没有问题,绝对,即使在网站上没有文字" Logggginnnnn"并尝试将其与"如果" (再次相同) 尝试添加else{}会再次导致NPE错误。

粘贴代码时

public void CheckSite(String textToBeChecked, String error)
    throws Exception {
Object test = (((JavascriptExecutor) driver)
        .executeScript("return document.body.innerHTML;"));
if (!((String) test).contains(textToBeChecked)) {
    fail(error);
}
}

直接对所有文件,它完美无缺。

我该如何处理?

1 个答案:

答案 0 :(得分:1)

是的,错误确实是因为驱动程序未在类CheckSource中初始化。您可能想要进行以下更改。

LoginTest中的更改

@Test
public void Login() throws Exception {
    driver.get(baseUrl);
    Source.CheckSite("Log in", "Error1",driver);// pass the instantiated driver object.

}

在课堂CheckSource中

public void CheckSite(String textToBeChecked, String error,WebDriver driver)
        throws Exception {
    Object test = (((JavascriptExecutor) driver)
            .executeScript("return document.body.innerHTML;"));
    if (!((String) test).contains(textToBeChecked)) {
        fail(error);
    }
}

这不会给你NPE,另外它通过了Text = Sign in的测试用例,提供了。

注意:如果您正在检查问题中提到的登录,它将无法通过测试。这对我有用。试一试。