Click here to Skip to main content
12,689,695 members (21,649 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

15.1K views
4 bookmarked
Posted

Taking Full Page Screenshot of Web Page Using Selenium Webdriver

, 15 Oct 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Taking Full page screenshot Selenium Webdriver

While executing a testcase, if the testcase fails, then we need to take a screenshot of the page for error reporting. This can also be done by using Selenium Webdriver.

We can use the following syntax for capturing and saving full page screenshot.

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

Then it can be stored in our local drive using the following syntax:

FileUtils.copyFile(screenshot, new File(“D:\\screenshot.png”));

Now, here is the small example of webdriver script by which we can capture the full page screenshot.

package Screenshot;
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FullPage_Screenshot {

    public static void main(String[] args) throws Exception {

        WebDriver driver = new FirefoxDriver();

        try{
            driver.get("http://google.co.in");
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            
            //driver.findElement(By.xpath("//input[@id='gbqfq']")).sendKeys("test");
            driver.findElement(By.xpath("//input[@id='gbqfq1']")).sendKeys("test");
            System.out.println("Entered data in textfield");    
        }   
        catch (Exception e)
        {  
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
            FileUtils.copyFile(scrFile, new File("D:\\screenShot1.png"));
            System.out.println("Screenshot is captured for failed testcase");
        }  
    }  
}

 

// driver.findElement(By.xpath(“//input[@id='gbqfq']“)).sendKeys(“test”);

 

If we execute this statement, then we can enter data into the text field and the testcase will pass. So, it will not take the screenshot.

So, I have commented out the correct statement so that the testcase will fail and screenshot will be captured.

Hope this will help you. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

pratimard
Tester / Quality Assurance
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionScreenshot Pin
Member 1185071620-Jul-15 18:45
memberMember 1185071620-Jul-15 18:45 
QuestionScreenShot - Blank page Pin
Member 1185071620-Jul-15 18:37
memberMember 1185071620-Jul-15 18:37 
QuestionScreenshot code in wrong place Pin
Stephen Gregory16-Oct-14 13:18
memberStephen Gregory16-Oct-14 13:18 
AnswerRe: Screenshot code in wrong place Pin
pratimamf16-Oct-14 22:06
memberpratimamf16-Oct-14 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170113.4 | Last Updated 16 Oct 2014
Article Copyright 2014 by pratimard
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid