WebDriverEventListener is provided in WebDriver to track events those take place in WebDriver during script execution. But you would be thinking “Why I am talking about this and who cares to take track on Events in WebDriver script execution”. Then I would say one, “Every automation engineer “and they do care about it and closely want to see how all actions are triggered either this is click event on a button, URL or navigating back and forward in browser or some exception occur and Some time they thought like oh God what is happening and some time they talk themselves to find the answer, to take screenshot once some exceptions occur during execution. So WebDriverEventListener is just the answer of all the queries that runs in to each of the test developer or automation engineer around the globe.
So let’s see how to implement it..So remember these thumb rules
1- Create class to override events just by Implementing WebDriverEventListener interface or by Extending AbstractWebDriverEventListener abstract class where WebDriverEventListeneris a interface which have near about 15 methods that we can override to see after and before tracking of each events that we trigger through our scripts.
2- Initializing WebDriver instance in our actually test class or test script like this
3- Creating one object of EventFiringWebDriver just by calling one of its constructor which takes instance of WebDriver as parameter like this
4- Registering EventListener with EventFiringWebDriver:
So lets call the class where we have just implemented WebDriverEventListener or have extendedAbstractWebDriverEventListener. Suppose the name of that class is OverrideClass()So creating one object of this class like this
So lets call the class where we have just implemented WebDriverEventListener or have extendedAbstractWebDriverEventListener. Suppose the name of that class is OverrideClass()So creating one object of this class like this
Now let’s register the object of this event listener class with EventFiringWebDriver
Above we have seen all the thumb rules now its time to see its working in real time.
So let’s create the class in which we will override the events function and here i am going to manipulate the methods which are related to driver.navigate().back();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class OverrideClass implements WebDriverEventListener{
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
}
@Override
public void afterNavigateBack(WebDriver driver) {
System.out.println("After clicking back "+ driver.getCurrentUrl());
}
@Override
public void afterNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void afterScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateBack(WebDriver driver) {
System.out.println("Before clicking Back"+driver.getCurrentUrl());
}
@Override
public void beforeNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void beforeScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
@Override
public void onException(Throwable arg0, WebDriver arg1) {
// TODO Auto-generated method stub
}
}
|
Now let’s see the test script where we are going to implement everything that we have seen above
Once we will execute this program, we will see these results
So you can see that two url are printed but from where…then think once again and remember you have written these code in OverridClass and once navigate().back(); is called it first called beforeNavigateBack(WebDriver driver) method and later afterNavigateBack(WebDriver driver) method implicitly..and so these results are in Console.