我想将EventFiringWebDriver与RemoteWebDriver一起使用,因为我们正在使用硒网格,对于WebDriver,我的代码可以正常工作,但是当我尝试将EventFiringWebDriver转换为RemoteWebDriver时,运行时出现异常。
java.lang.ClassCastException: org.openqa.selenium.support.events.EventFiringWebDriver cannot be cast to org.openqa.selenium.remote.RemoteWebDriver
at com.sixt.test.eventfiringwebdriver.DriverFactory.getDriverInstance(DriverFactory.java:52)
at com.sixt.test.eventfiringwebdriver.DriverFactoryTest.test1(DriverFactoryTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
请帮助。
答案 0 :(得分:0)
EventFiringWebDriver
不是RemoteWebDriver
的子类,因此您不能直接对其进行强制转换。在极少数情况下,您可能需要直接使用RemoteWebDriver
,但是如果您需要事件触发包装程序正在使用的驱动程序实例,则可以执行以下操作:
// Note: Assume eventDriver is a valid EventFiringWebDriver instance
// Further note: Yes, it's possible to consolidate the below into fewer lines,
// but each step is shown here for clarity.
WrapsDriver wrapperAccess = (WrapsDriver)eventDriver;
WebDriver driver = wrapperAccess.getWrappedDriver();
// If you really, really need a RemoteWebDriver instance here, you can do this
RemoteWebDriver remoteDriver = (RemoteWebDriver)driver;
答案 1 :(得分:0)
Sometimes we need concrete implementation, for example:
((RemoteWebDriver) webdriver).getSessionId()
((RemoteWebDriver) webdriver).setFileDetector()
答案 2 :(得分:0)
尝试
((RemoteWebDriver) ((EventFiringWebDriver) this.browserContext.getDriver()).getWrappedDriver());
答案 3 :(得分:0)
对我有用(kotlin)
val driver = (getWebDriver() as EventFiringWebDriver).wrappedDriver as RemoteWebDriver
val sessionId = driver.sessionId.toString()
注意:我添加了类似的监听器
@BeforeEach
fun beforeEach() {
WebDriverRunner.addListener(Highlighter())
}