How to take full page screenshots in browsers other than Firefox using Selenium?

Here is a problem which very few people would face. The situation is you need to have a full page screenshot of whatever page you visit while testing a web application and you are not allowed to use any third party tool for the same for any reason (e.g security purposes).

There is no predefined method to take full page screenshots using selenium for browsers other than Firefox. The solution for this is a work around code that will give page downs and take screenshots after each page down. Again we cannot determine the number of page downs required for a particular page as scroll length cannot be retrieved using java. So the number of page downs is solely dependent on the user. Then again we can take one element which appears at the bottom of the page across all the pages in the application. The last screen capture can be used by focusing on this particular element so that the bottom of the page is always captured. Below is the work around code to achieve this particular objective.

public void screenCaptureOB() throws Exception{
Robot robot = new Robot();
String screenName = new String();
screenName = "D:\\Regression\\Data Driven Regression\\test-output\\screenshots\\screenshot"+i+".png";
selenium.windowFocus();
selenium.captureScreenshot(screenName);
i++;
robot.keyPress(34);
screenName = "D:\\Regression\\Data Driven Regression\\test-output\\screenshots\\screenshot"+i+".png";
selenium.captureScreenshot(screenName);
i++;
robot.keyPress(34);
screenName = "D:\\Regression\\Data Driven Regression\\test-output\\screenshots\\screenshot"+i+".png";
selenium.captureScreenshot(screenName);
i++;
robot.keyPress(34);
screenName = "D:\\Regression\\Data Driven Regression\\test-output\\screenshots\\screenshot"+i+".png";
selenium.captureScreenshot(screenName);
i++;
robot.keyPress(34);
screenName = "D:\\Regression\\Data Driven Regression\\test-output\\screenshots\\screenshot"+i+".png";
selenium.captureScreenshot(screenName);
i++;
screenName = "D:\\Regression\\Data Driven Regression\\test-output\\screenshots\\screenshot"+i+".png";
selenium.focus("Last element of the page");
selenium.captureScreenshot(screenName);
i++;
}

 To use this method, we need to import the java.awt.Robot class. Please leave a comment if you need any clarification.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s