Feed 피드

Job Coding Feed

Taking Full page screenshot of Web page using Selenium Webdriver - CodeProject

imageUrl:

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();
        driver.get("http://google.co.in");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    
        try{
            driver.findElement(By.xpath("//input[@id='gbqfq']")).sendKeys("test");
            System.out.println("Entered data in textfield");
                    
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
            FileUtils.copyFile(scrFile, new File("D:\\screenShot1.png"));
            System.out.println("Screenshot is captured and stored in your D: Drive");
        }   
        catch (Exception e)
        {  
            System.out.println("Error in loading the Google page");
        }  
    }  
}

Hope this will help you Smile | :)

back to top