1

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");
  • "How can I specify the target for getScreenshotAs method" - the example shows you that, you pass one of the OutputType values. "why we use OutputType.File and not OutputFile?" - well, what is OutputFile? If you're trying to ask how to get that screenshot file to somewhere specific, per the docs, "It is up to users to make a copy of this file." None of this seems specific to testing or QA. – jonrsharpe Aug 5 '19 at 10:22
  • The example shows how to get a specific type, not the target – PDHide Aug 5 '19 at 10:43
  • What do you mean by "the target", though? – jonrsharpe Aug 5 '19 at 10:44
  • The syntax shows shows getscreenshotas(outputtype<x> target) ; i was understanding it as getscreenshotas(outputtype<File> Destination) , were destination is a target location; File destination = new File("D:/test.png"). – PDHide Aug 5 '19 at 10:47
  • About outputFIle , i meant outputtype<File> , it was an human error. Sorry for that, i have updated it – PDHide Aug 5 '19 at 10:50
2

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.

  • Ok so, as String(byte[] bytes, int offset, int length) ,means String method takes a byte array, a int offset and int length. getScreenshotAs(OutputType<File> Destination) means that getScreenshotas takes an argument 'target' which can be of any of the three outputtype. Thank you so much. – PDHide Aug 5 '19 at 13:17
  • 1
    @PDHide actually not just those three - those are the only ones with precanned implementations, but you could write another implementation for a different T (or, for that matter, another implementation for one of those three, e.g. an OutputType<File> that directly used a real file getScreenshotAs(new MyFileOutputType("path/to/something.ext"))). – jonrsharpe Aug 5 '19 at 13:33
  • @jonrsharpe Nice addition, added to the answers you can also implement your own OutputType :) – Niels van Reijmersdal Aug 5 '19 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.