Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 343
Andrio Avatar asked Jul 20 '15 03:07

Andrio


People also ask

How do I make screenshots automatically?

To capture your entire screen and automatically save the screenshot, tap the Windows key + Print Screen key. Your screen will briefly go dim to indicate that you've just taken a screenshot, and the screenshot will be saved to the Pictures > Screenshots folder.

Which is the method of Selenium WebDriver to capture the screenshots?

Selenium Webdriver allows you to take a screenshot using the getScreenShotAs method of the TakeScreenshot Interface. You can take a screenshot of a viewable area, full-page, or particular element. For full-page Screenshot, we can use the third party library AShot that provides screenshot taking ability.


1 Answers

You can achieve this more easily by EventFiringWebDriver

EventFiringWebDriver is a wrapper around an arbitrary WebDriver instance which supports registering of a WebDriverEventListener, e.g. 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

like image 171
Vicky Avatar answered Nov 03 '22 00:11

Vicky