我正在使用IEDriverServer(Win-32版本)2.40.0(直接从selenium下载页面上获取),除抓取屏幕截图外,其他一切似乎都可以正常工作-我的代码旨在对测试失败进行抓屏,如下所示:
public Statement apply(final Statement statement, final Description arg1) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
statement.evaluate();
} catch (Throwable t) {
captureScreenshot(arg1.toString());
throw t; // rethrow to allow the failure to be reported to JUnit
}
}
public void captureScreenshot(String method) {
try {
driver = WebDriverManager.getDriverInstance();
new File(screenshotsBase).mkdirs(); // Insure directory is there
Date now = new Date();
String fn = screenshotsBase + method + now.getTime() + ".png";
File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File(fn));
} catch (Exception e) {
// No need to crash the tests if the screenshot fails
System.out.println(e);
}
}
};
}
它在firefox驱动程序上正常工作,但在IE驱动程序上失败(抛出异常并且不截屏)。实例化IEDriver的代码是:
private static WebDriver startIEDriver() {
File file = new File("C:\\workspace\\IEDriver32\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
// capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
d = new InternetExplorerDriver(capabilities);
return d;
}
按照http://jimevansmusic.blogspot.co.il/2012/08/youre-doing-it-wrong-protected-mode-and.html中的建议,我已从代码中删除了“ INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS”,并将所有区域的所有“启用保护模式”设置为true。
引发的错误是:
org.openqa.selenium.remote.SessionNotFoundException: session 7018d7ae-e03a-4eb6-96a5-7bdf31eb4004 does not exist
Command duration or timeout: 3 milliseconds
Build info: version: '2.39.0', revision: '14fa800511cc5d66d426e08b0b2ab926c7ed7398', time: '2013-12-16 13:18:38'
System info: host: 'Ayelet-PC', ip: '192.168.1.23', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Session ID: 7018d7ae-e03a-4eb6-96a5-7bdf31eb4004
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=11, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:25063/, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
我还尝试使用以下语法,遵循http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp-上RemoteWebDriver屏幕截图上Selenium页面上的建议
WebDriver augmentedDriver = new Augmenter().augment(driver);
File source = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
但这失败了
net.sf.cglib.core.CodeGenerationException: java.lang.IllegalAccessException-->Class org.openqa.selenium.remote.Augmenter$CompoundHandler can not access a member of class org.openqa.selenium.ie.InternetExplorerDriver with modifiers "protected"
并且似乎“ Augmenter”不是要与IEDriver一起使用。
非常欢迎您提供有关如何使Selenium IEdriver截取屏幕截图或可能导致问题的任何线索。
最佳答案
我发现了问题,并且可以预期,它不在IEDriver中。
我不知道为什么它可以与FirefoxDriver无缝协作,这使我立即怀疑该驱动程序,但是无论如何。
进一步的调试显示,在调用屏幕快照测试规则之前,驱动程序实例正在获得“关闭”事件,这自然使其失去了会话。
看来这是因为我的驱动程序在“ @Before”和“ @After”注解中启动/停止,但为了与屏幕截图测试规则正确配合使用(此代码在问题中)-定义为
@Rule
public ScreenshotTestRule screenshotTestRule = new ScreenshotTestRule();
相反,我应该将驱动程序的启动和停止放在@BeforeClass和@AfterClass批注中。
谢谢大家,希望以后对找到它的人有所帮助。