1
votes

I'm using IEDriverServer (Win-32 version) 2.40.0 (taken directly from the selenium downloads page), and everything seems to work except for taking screenshots - my code is intended for taking screenshots on test failure, as follows:

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);
            }
        }
    };
}

it works fine with firefox driver, but fails with IE Driver (throws exception and doesn't take the screenshot). The code for instantiating the IEDriver is:

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;
    }

I've removed the "INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS" from the code, following the advice in http://jimevansmusic.blogspot.co.il/2012/08/youre-doing-it-wrong-protected-mode-and.html, and set all the "Enable Protected Mode" to true for all zones.

The error thrown is:

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}]

I've also tried using the syntax below, following the advice on Selenium page on RemoteWebDriver screenshots at http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp-

                WebDriver augmentedDriver = new Augmenter().augment(driver);
                File source = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);

but that fails with exception

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"

and it seems the "Augmenter" is not meant to work with IEDriver.

Any clues on how to get selenium IEdriver to take screenshots or what might be causing the problem will be most welcome.

2

2 Answers

1
votes

I've found the problem, and as may be expected, it was not within the IEDriver. I don't know why it worked seamlessly with the FirefoxDriver, which made me immediately suspect the driver, but anyway.

Some more debugging revealed that the driver instance was getting "close" event before the screenshot test rule was called, which naturally makes it lose the session.

It seems like this is because I had my driver start/stop at the "@Before" And "@After" annotations, but to work correctly with the screenshot test rule (the code for this is in the question) - that is defined as

@Rule
public ScreenshotTestRule screenshotTestRule = new ScreenshotTestRule();

I should have instead place the starting and stopping of the driver at the @BeforeClass and @AfterClass annotations.

Thanks everyone, and I hope it helps whoever finds it in the future.

0
votes

Did you try using AugmentedDriver?

Here is the code

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)); 

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