Skip to main content

Taking a screenshot for evidence

Taking a screenshot is very easy in selenium. It is supported in almost all the web browser drivers.

The driver instance is casted into a TakesScreenshot type and a file can be obtained. The returned file is saved in the temporary folder with a alphanumeric name. The file can be copied to the results folder using FileUtils class. and renamed to what we want. In the below example it is renamed to Error with time appended to it.

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

FileUtils.copyFile(screenshot, new File("C:\\Users\\Krishna\\Desktop\\Error" + (new Date().getTime()) +".png"));


RemoteWebDriver is used to run tests in remote browsers. They can also be used in the local system. The advantage of remotewebdriver class is the exceptions often have an attached screenshot encoded as BASE64 png. Most of the exceptions are implementing the Screenshot exception.
The code to use this feature is. 

public String extractScreenShot(WebDriverException e) {
Throwable cause = e.getCause();
if (cause instanceof ScreenshotException) {
return ((ScreenshotException) cause).getBase64EncodedScreenshot();
}
return null;
}

This obtained string can be decoded using Java classes. 

byte b1[]=Base64.decodeBase64(extractScreenshot(e));
FileWriter f=new FileWriter("test.png");
f.write(b1.toString());

This is how you save the screenshots taken in the remote desktop in your local system.

Comments

Popular posts from this blog

Selenium - Data Driven Testing

In Automation, we need to runs scripts to test the same functionality with different types of data. This data is usually stored in an excel file and accessed via the script to enter in the Application Under Test. In QTP, this is done using the DataTable .  In Selenium we can use the Apache POI API to do read an excel workbook and write the output to the excel file. Using the API is quite simple if you understand the basics. POI  - Poor Obfuscation Implementation. This name was humorously given  because Microsoft code was deliberately made difficult to reverse engineer but still it was reverse engineered. POIFS - Poor Obfuscation Implementation File System. Used to access the input file. HSSF - Horrible Spreadsheet Format This is used to read and write .xls files XSSF - XML Spreadsheet Format This is used to read and write .xlsx files. FileInputStream fs=new FileInputStream("C:\\Users\\Krishna\\selenium workspace\\Data\\firstone.xls"); POIFSFileSyst...

Logging using Apache Log4j

Introduction - Log4j is a brilliant logging API available both on Java and .net framework. - Log4j allows you to have a very good logging infrastructure without putting in any efforts. - Log4j gives you the ability to categorize logs at different levels (Trace, Debug, Info, Warn, Error and Fatal). - Log4j gives you the ability to direct logs to different outputs. For e.g. to a file, Console or a Database. - Log4j gives you the ability to define the format of output logs. - Log4j gives you the ability to write Asynchronous logs which helps to increase the performance of the application. - Loggers in Log4j follow a class hierarchy which may come handy to your applications Log4j consists of five main components - LogManager - Loggers - Appenders - Layouts - Configuration file   Log Manager This is the static class that helps us get loggers with different names and hierarchy. You can consider LogManager as a factory producing logger objects. package Log4jSam...

How to find a WebElement

Find Element & Find Elements Method The difference between “ Find Elemen t” and “ Find Elements ” method is the first returns a WebElement object otherwise it throws an exception and the latter returns a list of WebElements, it can return an empty list if no DOM elements match the query. The “Find” methods take a locator or query object called “ By ”. “ By ” methods are listed below. By ID With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a  NoSuchElementException   will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique. But in some cases UI developers make it having non-unique ids on a page or auto-generating the id, in both cases it should be avoided. Example : If an element is given like this: <input id=”username”></input> WebElement we=driver.findElement(By.id(“username”));  ...