Reputation: 1
I need to take screenshot after every interaction with page (clicks, scrolls etc) How can i do that? I use Selenium and Java.
Upvotes: -2
Views: 593
Reputation: 2073
If you are using Selenium 3 ,You could use WebDriverEventListener Interface provided in Selenium, add implementation for the actions after which you need screenshot
It has multiple methods that you can implement and add a method to take screenshot after every action that you want it to
@Override
public void beforeAlertAccept(WebDriver webDriver) {
takeSnapShot(driver, filepaht);
}
@Override
public void afterAlertAccept(WebDriver webDriver) {
takeSnapShot(webDriver, filepath);
}
@Override
public void afterAlertDismiss(WebDriver webDriver) {
takeSnapShot(webDriver, filepath);
}
@Override
public void beforeAlertDismiss(WebDriver webDriver) {
}
@Override
public void beforeNavigateTo(String s, WebDriver webDriver) {
}
@Override
public void afterNavigateTo(String s, WebDriver webDriver) {
}
@Override
public void beforeNavigateBack(WebDriver webDriver) {
}
@Override
public void afterNavigateBack(WebDriver webDriver) {
}
@Override
public void beforeNavigateForward(WebDriver webDriver) {
}
@Override
public void afterNavigateForward(WebDriver webDriver) {
}
@Override
public void beforeNavigateRefresh(WebDriver webDriver) {
}
@Override
public void afterNavigateRefresh(WebDriver webDriver) {
}
@Override
public void beforeFindBy(By by, WebElement webElement, WebDriver webDriver) {
}
@Override
public void afterFindBy(By by, WebElement webElement, WebDriver webDriver) {
}
@Override
public void beforeClickOn(WebElement webElement, WebDriver webDriver) {
}
@Override
public void afterClickOn(WebElement webElement, WebDriver webDriver) {
takeSnapShot(webDriver, filepath);
}
@Override
public void beforeChangeValueOf(WebElement webElement, WebDriver webDriver, CharSequence[] charSequences) {
}
@Override
public void afterChangeValueOf(WebElement webElement, WebDriver webDriver, CharSequence[] charSequences) {
}
@Override
public void beforeScript(String s, WebDriver webDriver) {
}
@Override
public void afterScript(String s, WebDriver webDriver) {
}
@Override
public void beforeSwitchToWindow(String s, WebDriver webDriver) {
}
@Override
public void afterSwitchToWindow(String s, WebDriver webDriver) {
}
@Override
public void onException(Throwable throwable, WebDriver webDriver) {
}
@Override
public <X> void beforeGetScreenshotAs(OutputType<X> outputType) {
}
@Override
public <X> void afterGetScreenshotAs(OutputType<X> outputType, X x) {
}
@Override
public void beforeGetText(WebElement webElement, WebDriver webDriver) {
}
@Override
public void afterGetText(WebElement webElement, WebDriver webDriver, String s) {
}
Write a method to take screenshots like below
public void takeSnapShot(WebDriver webdriver,String fileWithPath) throws Exception{
//Convert web driver object to TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
//Call getScreenshotAs method to create image file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
//Move image file to new destination
File DestFile=new File(fileWithPath);
//Copy file at destination
FileUtils.copyFile(SrcFile, DestFile);
}
Your driver Initialisation should like this where we now use EventFiringWebDriver instead of WebDriver
EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
Create an instance of your eventHandler class and register it for events using the register method of EventFiringWebDriver object created above. Here is the code
EventHandler handler = new EventHandler();
eventDriver.register(handler);
For Selenium 4 you can use EventFiringDecorator EventFiringDecorator
Upvotes: 0
Reputation: 8589
Here's a simple example that shows how to take a screenshot using Selenium and Java.
You can use the takeSnapshot functino wherever you need it:
import java.io.File;
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;
import org.testng.annotations.Test;
public class Guru99TakeScreenshot {
@Test
public void testGuru99TakeScreenShot() throws Exception{
WebDriver driver ;
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
driver = new FirefoxDriver();
//goto url
driver.get("http://demo.guru99.com/V4/");
//Call take screenshot function
this.takeSnapShot(driver, "c://test.png") ;
}
/**
* This function will take screenshot
* @param webdriver
* @param fileWithPath
* @throws Exception
*/
public static void takeSnapShot(WebDriver webdriver,String fileWithPath) throws Exception{
//Convert web driver object to TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
//Call getScreenshotAs method to create image file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
//Move image file to new destination
File DestFile=new File(fileWithPath);
//Copy file at destination
FileUtils.copyFile(SrcFile, DestFile);
}
}
Upvotes: 1