Every tests should undergo WebDriverEventListener for analyzing tests and for better reporting. This post is very Generic and anyone can easily understand the concepts.
Note|
1| There are more to do and customize with the WebDriver EventListeners. Explained below is the most important functions under WebDriver EventListeners.
2| In the given example, make sure all the sections are commented out except one which you are working on. eg., if you working on 'onException' then comment out the other sections
EventFiringWebDriver supports registering of a WebDriverEventListener. Create a test class as shown below.
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class className {
private WebDriver driver;
@BeforeTest
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@Test
public void Test01() throws Exception {
EventFiringWebDriver event = new EventFiringWebDriver(driver);
WebDriverListerners eventListener = new WebDriverListerners();
event.register(eventListener);
// beforeNavigateTo & afterNavigateTo
event.get("http://www.google.com");
// beforeNavigateBack & afterNavigateBack
event.get("http://www.google.com");
event.get("http://www.bing.com");
event.navigate().back();
// beforeNavigateForward & afterNavigateForward
event.get("http://www.google.com");
event.get("http://www.bing.com");
event.navigate().back();
event.navigate().forward();
// onException
event.get("http://.........");
event.findElement(By.id("Wrong Value"));
// beforeFindBy & afterFindBy
event.get("http://.........");
event.findElement(By.id("Value"));
// beforeClickOn & afterClickOn
event.get("http://.........");
event.findElement(By.id("Value")).click();
// beforeChangeValueOf & afterChangeValueOf
event.get("http://.........");
event.findElement(By.id("Value")).clear();
event.findElement(By.id("Value")).sendKeys("Seleniumworks");
}
}
AbstractWebDriverEventListener
Use AbstractWebDriverEventListener as a Base class for implementing WebDriverEventListener.
package packagename;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
public class WebDriverListerners extends AbstractWebDriverEventListener {
private By lastFindBy;
private WebElement lastElement;
private String originalValue;
/*
* URL NAVIGATION | NAVIGATE() & GET()
*/
// Prints the URL before Navigating to specific URL "get("http://www.google.com");"
@Override
public void beforeNavigateTo(String url, WebDriver driver) {
System.out.println("Before Navigating To : " + url + ", my url was: "
+ driver.getCurrentUrl());
}
// Prints the current URL after Navigating to specific URL "get("http://www.google.com");"
@Override
public void afterNavigateTo(String url, WebDriver driver) {
System.out.println("After Navigating To: " + url + ", my url is: "
+ driver.getCurrentUrl());
}
// Prints the URL before Navigating back "navigate().back()"
@Override
public void beforeNavigateBack(WebDriver driver) {
System.out.println("Before Navigating Back. I was at "
+ driver.getCurrentUrl());
}
// Prints the current URL after Navigating back "navigate().back()"
@Override
public void afterNavigateBack(WebDriver driver) {
System.out.println("After Navigating Back. I'm at "
+ driver.getCurrentUrl());
}
// Prints the URL before Navigating forward "navigate().forward()"
@Override
public void beforeNavigateForward(WebDriver driver) {
System.out.println("Before Navigating Forward. I was at "
+ driver.getCurrentUrl());
}
// Prints the current URL after Navigating forward "navigate().forward()"
@Override
public void afterNavigateForward(WebDriver driver) {
System.out.println("After Navigating Forward. I'm at "
+ driver.getCurrentUrl());
}
/*
* ON EXCEPTION | SCREENSHOT, THROWING ERROR
*/
// Takes screenshot on any Exception thrown during test execution
@Override
public void onException(Throwable throwable, WebDriver webdriver) {
System.out.println("Caught Exception");
File scrFile = ((TakesScreenshot) webdriver)
.getScreenshotAs(OutputType.FILE);
try {
org.apache.commons.io.FileUtils.copyFile(scrFile, new File(
"C:\\Testfailure.jpeg"));
} catch (Exception e) {
System.out.println("Unable to Save");
}
}
/*
* FINDING ELEMENTS | FINDELEMENT() & FINDELEMENTS()
*/
// Called before finding Element(s)
@Override
public void beforeFindBy(By by, WebElement element, WebDriver driver) {
lastFindBy = by;
System.out.println("Trying to find: '" + lastFindBy + "'.");
System.out.println("Trying to find: " + by.toString()); // This is optional and an alternate way
}
// Called after finding Element(s)
@Override
public void afterFindBy(By by, WebElement element, WebDriver driver) {
lastFindBy = by;
System.out.println("Found: '" + lastFindBy + "'.");
System.out.println("Found: " + by.toString() + "'."); // This is optional and an alternate way
}
/*
* CLICK | CLICK()
*/
// Called before clicking an Element
@Override
public void beforeClickOn(WebElement element, WebDriver driver) {
System.out.println("Trying to click: '" + element + "'");
// Highlight Elements before clicking
for (int i = 0; i < 1; i++) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(
"arguments[0].setAttribute('style', arguments[1]);",
element, "color: black; border: 3px solid black;");
}
}
// Called after clicking an Element
@Override
public void afterClickOn(WebElement element, WebDriver driver) {
System.out.println("Clicked Element with: '" + element + "'");
}
@Override
public void afterClickOn(WebElement element, WebDriver driver) {
System.out.println("Clicked Element with: '" + element + "'");
}
/*
* CHANGING VALUES | CLEAR() & SENDKEYS()
*/
// Before Changing values
@Override
public void beforeChangeValueOf(WebElement element, WebDriver driver) {
lastElement = element;
originalValue = element.getText();
// What if the element is not visible anymore?
if (originalValue.isEmpty()) {
originalValue = element.getAttribute("value");
}
}
// After Changing values
@Override
public void afterChangeValueOf(WebElement element, WebDriver driver) {
lastElement = element;
String changedValue = "";
try {
changedValue = element.getText();
} catch (StaleElementReferenceException e) {
System.out
.println("Could not log change of element, because of a stale"
+ " element reference exception.");
return;
}
// What if the element is not visible anymore?
if (changedValue.isEmpty()) {
changedValue = element.getAttribute("value");
}
System.out.println("Changing value in element found " + lastElement
+ " from '" + originalValue + "' to '" + changedValue + "'");
}
/*
* SCRIPT - this section will be modified ASAP
*/
// Called before RemoteWebDriver.executeScript(java.lang.String, java.lang.Object[])
@Override
public void beforeScript(String script, WebDriver driver) {
// TODO Auto-generated method stub
}
// Called before RemoteWebDriver.executeScript(java.lang.String, java.lang.Object[])
@Override
public void afterScript(String script, WebDriver driver) {
// TODO Auto-generated method stub
}
OUTPUT |URL NAVIGATION | NAVIGATE() & GET()|
beforeNavigateTo & afterNavigateTo
Before Navigating To : http://www.google.com, my url was: about:blank
After Navigating To: http://www.google.com, my url is: https://www.google.co.in/?gfe_rd=cr&ei=qsb8Ur6bMqHM8gett4DoAQ
beforeNavigateBack & afterNavigateBack
Before Navigating Back. I was at http://www.bing.com/
After Navigating Back. I'm at https://www.google.co.in/?gfe_rd=cr&ei=Nsj8UqKwEoLV8gfnzYDwBQ
beforeNavigateForward & afterNavigateForward
Before Navigating Forward. I was at https://www.google.co.in/?gfe_rd=ctrl&ei=bMn8UonqD8PM8ge66YHICA&gws_rd=cr
After Navigating Forward. I'm at http://www.bing.com/
Check specific folder for screenshot on test failure due to Exception
OUTPUT |FINDING ELEMENTS | FINDELEMENT() & FINDELEMENTS()|
Trying to find: [Element]
Found: '[Element]'.
OUTPUT |CLICK | CLICK()|
Trying to click: '[Element]'
Clicked Element with: '[Element]'
OUTPUT |CHANGING VALUES | CLEAR() & SENDKEYS()|
Changing value in element found [Element] from 'old text' to ''
Changing value in element found [Element] from '' to 'Seleniumworks'
Thank you very much. This the best possible explanation
ReplyDeleteHello Team,
ReplyDeleteI'm using webDriverEventListner, in afterNavigateTo() method i'm checking an element using isDisplayed method, but if the element is not displayed in error log is generating in console, how to clear the console log, could you please help me. Thanks in advance
Thanks & Regards,
Hemanth
kyrie 9
ReplyDeleteair jordan shoes
bape clothing