MivaScott
MivaScott

Reputation: 1806

Unable to take a screenshot with Marionette/Firefox driver

Using Marionette geckodriver version 0.19.1.

Here is the method for set up the capabilities for Marionette/Firefox:

private static DesiredCapabilities setMarionetteCapabilities() {
    setMarionetteDriver(); // sets the correct path to the executable

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.startup.homepage", "about:blank");

    FirefoxOptions options = new FirefoxOptions();

    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);
    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
    return capabilities;
}

Here is where I try to take a screenshot. This code works fine in Chrome and originally worked in Firefox but now fails due to not being capable.

public static String takeScreenShot(String caption)  {
    System.out.println( "Test: " + ((HasCapabilities)driver).getCapabilities());

    if(!((HasCapabilities)driver).getCapabilities().is(CapabilityType.TAKES_SCREENSHOT)) {
        System.out.println("Cannot take a screenshot");
        return "";
    }

    TakesScreenshot camera = (TakesScreenshot)driver;
    File scrFile = camera.getScreenshotAs(OutputType.FILE);
    String filename = getFilename(caption);

    try {
        FileUtils.moveFile(scrFile, new File(captureDir + sep + filename));
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Saved to: " + captureDir + sep + filename);
    return filename;
}

But whenever I call the method, it just replies that it cannot take screen shots. Here is the results for getCapabilities():

[  
   {  
      moz:profile=C:\Users\____\AppData\Local\Temp\rust_mozprofile.9tfK2yUnv4jV,
      rotatable=false,
      timeouts=      {  
         implicit=0,
         pageLoad=300000,
         script=30000
      },
      pageLoadStrategy=normal,
      moz:headless=false,
      platform=XP,
      moz:accessibilityChecks=false,
      acceptInsecureCerts=true,
      browserVersion=57.0.4,
      platformVersion=10.0,
      moz:processID=11332,
      browserName=firefox,
      javascriptEnabled=true,
      platformName=XP,
      moz:webdriverClick=false
   }
]

Obviously, there is no takesScreenshot capability listed.

I've tried

    capabilities.setCapability("takesScreenshot", true);

but that does not seem to do anything.

So how do I add back in the ability to capture screenshots?

Note: I'm currently running this on a Windows 10 machine, but it will be run on Mac and Linux systems also, so answers needs to be operating system agnostic.

Upvotes: 0

Views: 689

Answers (1)

MivaScott
MivaScott

Reputation: 1806

Okay, stupid answer

Marionette can take screenshots; it just doesn't report that it can. Once I removed the test at the beginning of the method, everything ran fine. Here is the final method for those interested:

public static String takeScreenShot(String caption)  {
    String filename = getFilename(caption);

    try {
        TakesScreenshot camera = (TakesScreenshot)driver;
        File scrFile = camera.getScreenshotAs(OutputType.FILE);

        FileUtils.moveFile(scrFile, new File(captureDir + sep + filename));
        System.out.println("Saved to: " + captureDir + sep + filename);
    } catch (Exception e) {
        System.out.println("Cannot take a screenshot");
    }

    return filename;
}

Upvotes: 1

Related Questions