gpt4 book ai didi

java - 无法在子类中使用 EventFiringWebDriver 对象

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

我正在尝试在我的项目中设置 EventFiringWebDriver,但在 EventFiringWebDriver 对象在同一个类中正常工作而在子类中不工作的某个点上,我卡住了。

父类:

package utility;

import java.util.List;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import eventListeners.WebDriverEventHandler;


public class common {
public static WebDriver driver = null;
protected static EventFiringWebDriver eDriver;
WebDriverEventHandler handler;
protected NavigatetoOverviews nav;

@BeforeClass
public void beforeClass() throws InterruptedException {

handler = new WebDriverEventHandler();
System.setProperty("webdriver.firefox.profile", "default");
driver = new FirefoxDriver();
eDriver = new EventFiringWebDriver(driver);
eDriver.register(handler);

nav = new NavigatetoOverviews();
eDriver.get("http://google.com");
}
}

我的 child 类(class):

package eventListeners;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

import bsh.commands.dir;
import utility.common;

public class TestExtendsCommon extends common{

@Test
public void sample() {

eDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
eDriver.findElement(By.id("gbqfq")).sendKeys("Selenium");
eDriver.findElement(By.id("gbqfba")).click();

}
}

我的测试结果:

[TestNG] Running:
C:\Users\Hema\AppData\Local\Temp\testng-eclipse--492139263\testng-customsuite.xml

Just before beforeNavigateTo http://google.com
Inside the afterNavigateTo to http://google.com
FAILED: sample
java.lang.NullPointerException
at eventListeners.WebDriverEventHandler.beforeFindBy(WebDriverEventHandler.java:35)
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.openqa.selenium.support.events.EventFiringWebDriver$1.invoke(EventFiringWebDriver.java:78)
at com.sun.proxy.$Proxy5.beforeFindBy(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:183)
at eventListeners.TestExtendsCommon.sample(TestExtendsCommon.java:17)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 7 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@77afea7d: 23 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6d86b085: 8 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@299a06ac: 34 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@4ac68d3e: 9 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@1f89ab83: 40 ms

提前致谢。

最佳答案

感谢所有花时间回答这个问题的人。我终于找到了答案。

如错误所述,错误在 WebDriverEventListener 类的 beforeFindBy 方法中。

at eventListeners.WebDriverEventHandler.beforeFindBy(WebDriverEventHandler.java:35)


public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {

System.out.println("Just before finding element " + arg1.toString());
}

在上面的程序中,当您通过WebDriver 对象查找元素时,arg1 参数将为null。所以我修改了如下代码:

public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {

if (arg1 == null) {
System.out.println("Just before finding element: " + arg0.toString() + " on browser: " + arg2.toString() );

} else {
System.out.println("Just before finding element: " + arg0.toString() + " inside " + arg1.toString() + " Web element on browser: " + arg2.toString() );
}

}

关于java - 无法在子类中使用 EventFiringWebDriver 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27156414/

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