gpt4 book ai didi

java - 使用 JavascriptExecutor 检查页面源时出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 10:40:39 25 4
gpt4 key购买 nike

我需要检查网站上是否有文字。根据driver.getPageSource() converts signs < to &lt;我试图避免 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”)并尝试将其与“if”混合“(同样)尝试添加 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);
}
}

直接到所有文件,它完美地工作。

我该如何处理?

最佳答案

是的,错误确实是因为驱动程序未在类 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 的测试用例。

注意:如果您按照问题中提到的方式检查登录,测试将会失败。这对我有用。尝试一下。

关于java - 使用 JavascriptExecutor 检查页面源时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34434508/

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