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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With