How to specify target in the TakesScreenshot interface's getScreenshotAs method



  • On reading the Java documentation of TakesScreenshot interface, it was observed that the method syntax and description for getScreenshotAs are:

    getScreenshotAs(OutputType<X> target)
    

    Capture the screenshot and store it in the specified location.

    But the example usage shown is:

    File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    

    How can I specify the target for getScreenshotAs and why do we use OutputType.File and not OutputType< File> ``` ?

    How to interpret this Java documentation? I understood the syntax as:

    getScreenshotAs(OutputType<File> Destination)
    

    where Destination is:

    File Destination = new File("D:/new.png");
    


  • Documentation wording:

    Capture the screenshot and store it in the specified location.

    https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/TakesScreenshot.html

    An object in memory is also a valid location. The word location does not only mean a file location on disk. Thus one could argue that the word "location" could reference a object that is returned by the method.

    I do think the documentation is confusing. It could read something like: "Returns a screenshot object of the chosen output type."

    If you want to challenge the documentation create an issue on GitHub.

    Syntax:

    The target has to be one of the three OutputTypes.

    The OutputType documentation shows there are three default types for X:

    static OutputType<java.lang.String> BASE64 
    static OutputType<byte[]>   BYTES
    static OutputType<java.io.File> FILE
    

    You could also create your own implementation of an OutputType, as Jonrsharpe suggests.

    The syntax is:

    getScreenshotAs(OutputType.ONEOFTHETHREETYPES)
    

    The target is an output-type target, not a target destination. You could argue target might be a confusing name. The method returns either a String, Bytes or a File object containing the screenshot. Afterwards you decide how to store it in a destination.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2