Selenium InternetExplorerDriver在getScreenshotAs方法上抛出SessionNotFoundException

时间:2014-03-10 12:58:38

标签: java selenium selenium-webdriver screenshot

我正在使用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截取屏幕截图或可能导致问题的任何线索将非常受欢迎。

2 个答案:

答案 0 :(得分:1)

我发现了问题,正如可以预料的那样,它不在IEDriver中。 我不知道为什么它与FirefoxDriver无缝协作,这让我立即怀疑驱动程序,但无论如何。

更多的调试显示,在调用屏幕截图测试规则之前,驱动程序实例正在“关闭”事件,这自然会导致会话丢失。

这似乎是因为我在“@Before”和“@After”注释中启动/停止了我的驱动程序,但是为了正确使用屏幕截图测试规则(此问题的代码在问题中) - 定义为

@Rule
public ScreenshotTestRule screenshotTestRule = new ScreenshotTestRule();

我应该将驱动程序的启动和停止放在@BeforeClass和@AfterClass注释中。

谢谢大家,我希望将来有人能找到它。

答案 1 :(得分:0)

您是否尝试使用AugmentedDriver

这是代码

Date now = new Date();
String fn = screenshotsBase + method + now.getTime() + ".png";
WebDriver augmentedDriver = new Augmenter().augment(driver);
File source = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.File);
FileUtils.copyFile(source, new File(fn)); 

参考 - http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#taking-a-screenshot