Selenium WebDriver Practical Guide Sample Chapter
Selenium WebDriver Practical Guide Sample Chapter
Selenium WebDriver Practical Guide Sample Chapter
Satya Avasarala
Chapter 7, Exploring RemoteWebDriver and WebDriverBackedSelenium, will deal with two very important topics of WebDriver: RemoteWebDriver and WebDriverBackedSelenium. If you want to execute a WebDriver installed on a different machine from your machine, you can use the RemoteWebDriver class to handle all your commands for that remote machine. One of its popular use cases is browser compatibility testing. The other class we talk about in this chapter is WebDriverBackedSelenium. This is useful for people who want to use WebDriver, but still have many of their existing tests using Selenium 1. Finally, we will migrate some code using Selenium1 APIs to use WebDriver APIs. Chapter 8, Understanding Selenium Grid, will talk about one important and interesting feature of Selenium named Selenium Grid. Using this, you can submit your developed automation scenarios to a server and specify there the target platform, that is, the OS, browser type, and version, upon which you want these scenarios to be executed. If a node with such a configuration is registered and available, the server will dispatch your job to that node, and it will take care of executing your automation scenarios in its environment and publish the results back to the server. Chapter 9, Understanding PageObject Pattern, will talk about a well-known design pattern named the PageObject pattern. This is a proven pattern that will give you a better handle on your automation framework and scenarios. Chapter 10, Testing iOS and Android Apps, we will take you through how WebDriver can be used to automate your test scripts for iOS and Android applications. We will also discuss a recently developed software tool called Appium. By the end of this book, we are sure you will be one of the world's advanced WebDriver users.
The following ow diagram explains what has to be done to capture all of the events raised by EventFiringWebDriver during the execution of test cases:
Create an EventListener Class
Create an instance of EventListener class created above Register the Event Listener class with the EventFiringWebDriver.
Verify if your Listener class got informed about the events occurence
[ 114 ]
Chapter 5
Implementing WebDriverEventListener
The WebDriverEventListener interface has all of the event methods declared. The EventFiringWebDriver class, as soon as it realizes an event has occurred, will invoke the registered method of WebDriverEventListener. Here, we have created an IAmTheEventListener named class and have implemented WebDriverEventListener. As a result, you have to provide implementations for all of the methods declared in it. Right now, in WebDriverEventListener, there are 15 methods. We will discuss each one of them shortly. For now, make sure you allow your Eclipse IDE to provide you dummy implementations for all of these methods. The class that we have created with all 15 overridden methods is as follows (we have provided implementations for couple of methods):
public class IAmTheEventListener implements WebDriverEventListener{ ////////// NAVIGATION RELATED METHODS //////////////// @Override public void beforeNavigateTo(String url, WebDriver driver) { System.out.println("Before Navigate To "+url); } @Override public void afterNavigateTo(String url, WebDriver driver) { // TODO Auto-generated method stub } @Override public void beforeNavigateBack(WebDriver driver) { System.out.println("Before Navigate Back. Right now I'm at "+driver.getCurrentUrl()); } @Override public void afterNavigateBack(WebDriver driver) { // TODO Auto-generated method stub } @Override public void beforeNavigateForward(WebDriver driver) { // TODO Auto-generated method stub } @Override [ 115 ]
Understanding WebDriver Events public void afterNavigateForward(WebDriver driver) { // TODO Auto-generated method stub } /////////////////// FINDBY RELATED METHODS /////////////// @Override public void beforeFindBy(By by, WebElement element, WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterFindBy(By by, WebElement element, WebDriver driver) { // TODO Auto-generated method stub } //////////////////// CLICKON RELATED METHODS /////////////// @Override public void beforeClickOn(WebElement element, WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterClickOn(WebElement element, WebDriver driver) { // TODO Auto-generated method stub } ///////////////// CHANGE OF VALUE RELATED METHODS ////////////// @Override public void beforeChangeValueOf(WebElement element, WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterChangeValueOf(WebElement element, WebDriver driver) { // TODO Auto-generated method stub } /////////////// SCRIPT EXECUTION RELATED METHODS /////////////// @Override [ 116 ]
Chapter 5 public void beforeScript(String script, WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterScript(String script, WebDriver driver) { // TODO Auto-generated method stub } /////////////// EXCEPTION RELATED METHODS /////////////////////// @Override public void onException(Throwable throwable, WebDriver driver) { // TODO Auto-generated method stub } }
Extending AbstractWebDriverEventListener
The second way to create a listener class is by extending the AbstractWebDriverEventListener class. AbstractWebDriverEventListener is an abstract class that implements WebDriverEventListener. Though it doesn't really provide any implementation for the methods in the WebDriverEventListener interface, it creates a dummy implementation such that the listener class that you are creating doesn't have to contain all of the methods; only the ones that you, as a test script developer, are interested. The following is a class we have created that extends AbstractWebDriverEventListener and provides implementations for a couple of methods in it. This way we can override only the methods that we are interested rather than all of the methods in our class:
public class IAmTheEventListener2 extends AbstractWebDriverEventListener{ @Override public void beforeNavigateTo(String url, WebDriver driver) { System.out.println("Before Navigate To "+url); } @Override public void beforeNavigateBack(WebDriver driver) { System.out.println("Before Navigate Back. Right now I'm at "+driver.getCurrentUrl()); } } [ 117 ]
The FirefoxDriver instance will be the underlying driver instance that drives all of your driver events. This is nothing new compared to what we have done until now in all of the chapters in this book. The step explained in the next section is where we make this driver an instance of EventFiringWebDriver.
created previously. This will be the class to which all of the events are dispatched:
EventFiringWebDriver eventFiringDriver = new EventFiringWebDriver(driver); IAmTheEventListener eventListener = new IAmTheEventListener();
[ 118 ]
Chapter 5
In the preceding code, we modify our listener class to record navigateTo and navigateBack before and after events inherited from the AbstractWebDriverEventListener class. The modied methods are as follows:
@Override public void beforeNavigateTo(String url, WebDriver driver) { System.out.println("Before Navigate To: "+url +" and Current url is: "+driver.getCurrentUrl()); } @Override public void afterNavigateTo(String url, WebDriver driver) { System.out.println("After Navigate To: "+url +" and Current url is: "+driver.getCurrentUrl()); } @Override public void beforeNavigateBack(WebDriver driver) { System.out.println("Before Navigate Back. Right now I'm at " +driver.getCurrentUrl()); }
[ 119 ]
Understanding WebDriver Events @Override public void afterNavigateBack(WebDriver driver) { System.out.println("After Navigate Back. Right now I'm at " +driver.getCurrentUrl()); }
Now, if you execute your test script, the output will be as follows:
If you observe the second line in the previously shown output, the expected after-navigation URL is http://www.google.com, but the current URL is http://www.google.com.au. This is due to Google redirection to your local server.
[ 120 ]
Chapter 5
Now, modify the listeners slightly to differentiate the log statements. Now, if you execute the preceding code, you will see the output as follows:
The preceding method is invoked before the WebDriver attempts to change the value of the WebElement. As a parameter, the WebElement itself is passed to the method so that you can log the value of the element before the change.
public void afterChangeValueOf(WebElement element, WebDriver driver)
The preceding method is the second method associated with the value-change event that is invoked after the driver changes the value of the WebElement. Again, the WebElement and the WebDriver are sent as parameters to the method. If an exception occurs while changing the value, this method is not invoked.
[ 121 ]
The preceding method is invoked when the WebDriver is about to click on a particular WebElement. The WebElement that is going to be clicked on and the WebDriver that is clicking on it are sent as parameters to this method so that the test script developer can interpret which driver performed the click action, and on which element the action was performed.
public void afterClickOn(WebElement element, WebDriver driver)
The EventFiringWebDriver class noties the preceding method after the click action is taken on a WebElement. Similar to the beforeClickOn() method, this method is also sent the WebElement and WebDriver instances. If an exception occurs during a click event, this method is not called.
The preceding method is invoked just before WebDriver begins searching for a particular WebElement on the page. For parameters, it sends the locating mechanism, that is, the WebElement that is searched for and the WebDriver instance that is performing the search, by instance.
public void afterFindBy(By by, WebElement element, WebDriver driver)
Similarly, the EventFiringWebDriver class calls the preceding method after the search for an element is over and the element is found. If there are any exceptions during the search, this method is not called, and an exception is raised.
Chapter 5
The preceding method is invoked before the browser takes you back in its history. The WebDriver that invoked this event is passed as a parameter to this method.
public void afterNavigateBack(WebDriver driver)
Just as in all after <<event>> methods, the preceding method is invoked when the navigate-back action is triggered. The preceding two methods will be invoked irrespective of the navigation of the browser; that is, if the browser doesn't have any history and you invoke this method, the browser doesn't take you to any of its history. But even in that case, as the event is triggered, those two methods are invoked.
Just as in browser back navigation, these methods are invoked irrespective of whether or not the browser takes you one level forward.
As we've seen earlier, this event occurs whenever the driver executes driver.
public void beforeNavigateTo(java.lang.String url, WebDriver driver) public void afterNavigateTo(java.lang.String url, WebDriver driver)
The URL that is used for the driver navigation is passed as a parameter to the preceding methods along with the driver that triggered the event.
The preceding methods get the JavaScript that was executed as a string, and the WebDriver that executed it as a parameter. If there an exception occurs during script execution, the afterScript() method will not be invoked.
In all the after<<event>> methods, we have seen that they will not be invoked if the driver comes across any exception. In that case, instead of those after<<events>> methods, the onException() method is invoked and the throwable object and the WebDriver object are sent to it as parameters.
The parameter of the method should be the event listener that wants to opt out of getting events notications.
Summary
In this chapter, you have learned about EventFiringWebDriver and EventListeners, and how they work together to make the developer's life easy in order to debug what is going on at each step while the test cases get executed. In the next chapter, we will learn how WebDriver handles I/O operations on a lesystem.
[ 124 ]
Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet book retailers.
www.PacktPub.com