我试图在自己的项目中设置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");
}
}
我的孩子班:
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
提前致谢。
最佳答案
感谢所有在此问题上花费了一些时间的人。终于我找到了答案。
如错误所述,错误在beforeFindBy
类的WebDriverEventListener
方法中。
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() );
}
}