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

Is there a way - perhaps with a listener - to take a screenshot of every page that loads? Taking the screenshot itself is not a problem, I can do it, I just need a way to make it happen automatically, so I don't have to manually enter the screenhto method before every mouse click.

I've looked at a listener WebDriverEventListner

, but it doesn't seem like it can be used to detect any page loads without first specifying the element that will be clicked / the page that will be loaded?

+3


source to share


2 answers


You can achieve this easily by EventFiringWebDriver

EventFiringWebDriver is a wrapper around an arbitrary WebDriver instance that supports registering a WebDriverEventListener, for example. for logging.

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 the WebDriverEventListener interface



He has a lot of methods, such as beforeClickOn

, afterClickOn

, beforeNavigateTo

, afterNavigateTo

, beforeFindBy

, afterFindBy

. These methods will be automatically called after the corresponding ex: actions beforeFindBy

and afterFindBy

called automatically before and after the element is found

   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();

            }

        }

      

in a similar way, you can override the other methods above to also check the title, like afterNavigateTo, and take a screenshot when the page title changes.

Hope this helps you ... Please come back if you need more help.

+5


source


Yes, you can achieve your goal by creating a task scheduler in java.

  • You should create a class that extends TimerTask (available in the java.util package). TimerTask is an abstract class.
  • Write your code in a public void run () method that you want to run periodically.

Note. - Make sure the WebDriver 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. Instant start timer timer = new timer ();
  2. Instant start of the scheduled task class Object ScheduledTask st = new ScheduledTask ();
  3. Assign a scheduled task using the Timer.sment () method.

    import java.util.Timer;

    // Main class public class SchedulerMain {public static void main (String args []) throws InterruptedException {Timer time = new timer (); // Create timer object TakeScreenShot shot = new TakeScreenShot (driver.getTitle ()); // Create TakeScreenShot class by initializing screenTitle time.schedule (shot, 0, 1000); // Create a Resetitively task every 1 second, and this will call run () of the 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. Therefore, you must use a program terminator.

  1. You can call the SchedulerMain class as shown below: -

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

+1


source







All Articles