In the previous blog, we had discussed in short, ”How to automate a browser!!“. If you are a beginner like me, you will be very keen to take a screen shot of a particular page.
Here is a simple script for taking a screen shot of any URL. I have taken “http://www.flipkart.com/” as mu URL.
package login;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Screen_shot {
public static void main(String[] args) throws IOException {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/");
driver.manage().window().maximize();
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\selenium\\screenshot1.png"), true);
driver.quit();
}
}
Below are few points with images:
Hope this helps beginners like me… :)
CodeProject