Friday, April 3, 2015

Simple Code For Screen Shot of Web Page.

We have to use TakesScreenshot interface for taking screen shot of the page as shown bellow.
Here the path of the image we can give like this "C:\\Users\\Public\\Pictures\\gmr-automation.png"

Here gmr-automation.png image will be created in that place


 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(scrFile, new File("Path of the image"));
  

Sample Program:

import java.io.File;
import java.io.IOException;
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;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ScreenShotOfFacebookLoginPage {

 WebDriver driver;

 @BeforeTest
 public void start(){
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
 }

 @Test
 public void Test() {

  driver.get("https://www.facebook.com");
  File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(scrFile, new File("C:\\Users\\Public\\Pictures\\facebookloginpage.png"));

   }
}

No comments:

Post a Comment