Capturing snapshot is a very important thing in functional automation. Ways of capturing the snapshots are different in each tool. I will describe the ways of capturing the snapshots which you can use, while scripting with Selenium API, in case of any failure occur during execution of your script.
"TakesScreenshot" interface we can use to capture the snapshot. Below is the code for the same. In the below example i have captured the snapshot of Google search page. Capturing the snapshot is the 3 step process:
1. Covert the webdriver object to TakesScreenshot.
TakesScreenshot t=((TakesScreenshot)driver);
2. Create the image file using "getScreenshotAs" method and store the image in the file object.
Sourcefile=t.getScreenshotAs(OutputType.FILE);
3. Now, store the file in any folder where you want to store it.
FileUtils.copyFile(Sourcefile, Destfile);
OR
FileUtils.copyFile(Sourcefile, new File("d:/abc.jpg"));
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.chrome.ChromeDriver;
public class ScreenShot
{
public static WebDriver driver;
public static TakesScreenshot t;
public static File Destfile;
public static File Sourcefile;
public static void main(String args[]) throws IOException
{
Destfile=new File("D:/googlepge.jpg");
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.google.co.in");
TakesScreenshot t=((TakesScreenshot)driver);
Sourcefile=t.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(Sourcefile, Destfile);
System.out.println("Screen shot captured");
driver.quit();
}
}
Below is the image of captured snapshot.
**************************************************************************
Now, I have seen that how we can capture the snapshot of any webpage using TakesScreenshot Interface. With this interface the only problem faced that it capture only the visible area of screen but what happens if you want to capture full screenshot or a requirement of capturing an individual object or web element like text box, combo box,list box, web button etc. then we can use AShot. This is a new technology which has been introduced by Yandex and supported by Selenium Web Driver 3.
Below code will help us to understand that how we can capture the full screenshot( from top to bottom). I will take an example of Flipkart.com here.
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class AsotSnapshot
{
public static WebDriver driver;
public static File Destfile;
public static File Sourcefile;
public static AShot ashot;
public static Screenshot screen;
public static void main(String args[]) throws IOException
{
Destfile=new File("D:/googlepge.jpg");
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com");
screen=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(screen.getImage(), "jpg", Destfile);
System.out.println("Screen shot captured");
driver.quit();
}
}
Output of snapshot using AShot.
Any questions please provide comment in the comment box.
Best of luck!
Happy Learning.
No comments:
Post a Comment