简体   繁体   中英

Automatically take a screenshot of every new page with Selenium Webdriver (java)

Is there a way—perhaps with a listener—to take a screenshot of every new page that loads? Taking a screenshot itself is no problem, I can do that, I just need a way for it to happen automatically, so I don't have to manually enter the screenshto method before every time something is clicked on.

I was looking at the WebDriverEventListner listener, but it seems it can't really be used to detect any page loads without previously specifying the element that will be clicked/page that will load?

You can achieve this more easily by EventFiringWebDriver

EventFiringWebDriver is a wrapper around an arbitrary WebDriver instance which supports registering of a WebDriverEventListener, eg for logging purposes.

WebDriver driver = new FirefoxDriver();
//create an object of EventFiringWebDriver and pass the driver instance
EventFiringWebDriver wd = new EventFiringWebDriver(driver);
//create an object of  class WebDriverListener and pass the driver instance
WebDriverListener eventListener = new WebDriverListener(driver);
wd.register(eventListener);

Create a WebDriverListener class by implementing WebDriverEventListener interface

It has many methods like beforeClickOn , afterClickOn , beforeNavigateTo , afterNavigateTo , beforeFindBy , afterFindBy .These methods will automatically be called after respective actions ex : beforeFindBy and afterFindBy are called automatically before and after finding an element

   String title=""; //initially keep title empty
    //will automatically be called after click on an element
    public void afterClickOn(WebElement element, WebDriver driver) {

            //title is not equal to previous page title

            if(driver.getTitle()!=title){

            //take screenshot

            //assign the current title to string title
            title=driver.getTitle();

            }

        }

similarly you can override other methods specified above also for checking the title like afterNavigateTo and take screenshot when title of the page changes.

Hope this helps you...Kindly get back if you need any further help

Yes you can achieve your objective by creating task scheduler in java.

  1. You should create a class extending TimerTask(available in java.util package). TimerTask is a abstract class.
  2. Write your code in public void run() method that you want to execute periodically.

Note:- Make sure that WebDriver driver object is public and static and can be accessed from other classes.

import java.io.File;
import java.io.IOException;
import java.util.TimerTask;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
// Create a class extends with TimerTask
public class TakeScreenShot extends TimerTask {

    public static String screenTitle;

    public TakeScreenShot(String screenTitle){//constructor to initialize screenTitle
        this.screenTitle=screenTitle;
    }

    // Add your task here
    public void run() {
        if(screenTitle.equals(driver.getTitle().trim())){
            //do nothing; // as long as we are on same page dont take screenshot again.
        }else{
            screenTitle=driver.getTitle().trim();//if page changes then, take screenshot
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            try {
                FileUtils.copyFile(scrFile, new File("C:\\"+screenTitle+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
  1. Instantiate Timer Object Timer time = new Timer();
  2. Instantiate Scheduled Task class Object ScheduledTask st = new ScheduledTask();
  3. Assign scheduled task through Timer.shedule() method.

    import java.util.Timer;

    //Main class public class SchedulerMain { public static void main(String args[]) throws InterruptedException { Timer time = new Timer(); // Instantiate Timer Object TakeScreenShot shot = new TakeScreenShot(driver.getTitle()); // Instantiate TakeScreenShot class by initializing screenTitle time.schedule(shot, 0, 1000); // Create Repetitively task for every 1 secs and this will call run() of TakeScreenShot class

      for (int i = 0; ;i++) {//infinite loop till driver is not made to null Thread.sleep(2000); if(driver==null){//make sure to make driver to null when application exists to stop this scheduler. System.exit(0); //loop will break and Scheduler will stop } } } 

    }

Note:- If your code does not contain termination, it will not stop. So you should use a program terminator.

  1. You can call SchedulerMain class as below:-

    String ags[]=null; SchedulerMain.main(ags);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM