This is the next tutorial in selenium-java sereis. In the last tutorial, we had studied about how to work with different browsers. In this tutorial we are going to take a look at webdriver interface!
What you will Learn (Ctrl+Click to follow link):
Understand WebDriver Interface
Interface reference pointing to a class
Understand WebDriver Interface:
Go to https://selenium.dev/selenium/docs/api/java/index.html. On the left hand side menu, whatever you see in italic is an interface. Notice thatWebDriver too is in italic & hence is an interface.Click WebDriver link, the below page should open
Figure 1
If you scroll down, you would notice ‘Method Summary’ section. This section shows lot of methods present in webdriver interface, like findElement(), get(), getWindowHandle() etc alongwith their description
Figure 2
Click findElement() method link (or any method link), the method documentation gets displayed
Figure 3
As you can notice in figure 1, all the driver classes are implementing the same WebDriver Interface. So all these classes will also have these functions ‘findElement’, getTitle etc. The function names in all the implementing classes will be the same. So if you see over below, the ‘get’ function is present in all the classes. So the commands to interact with the browser will be the same, whether we have chrome, ie, ff, etc..
Practice few functions:
Figure 4
Similarly, findelement() function is present in all the browser classes
Figure 5
These driver classes corresponds to a browser. ChromeDriver will help you in executing the selenium scripts in chrome browser and similarly others. We simply create the objects of these driver classes and can start working with them. So technically WebDriver is an interface. Let us create a temp class.
Figure 6
Also read: Configure Selenium-Java Webdriver in Eclipse
So far we have been creating objects like this: ChromeDrivercd = new ChromeDriver()
Figure 7
VERY IMPORTANT: You can have the interface reference pointing to the class that implements that interface.So we can re-write the same statement as:
WebDrivercd = new ChromeDriver()
The reason being, WebDriver is an interface & ChromeDriver is an implementing class. So the interface reference ‘cd’ will have access to all the functions of ChromeDriver class. Make sure to import org.openqa.selenium.WebDriver
Figure 8
Similarly we can write lines#10 and 11
Figure 9
When we say WebDriver driver = null, it means that the driver reference is not pointing anywhere
Figure 10
Line#13 represents that in the below figure. Now, based on our choice of the browser, we can point the same driver reference to any class (lines#15, 16, 17)
Figure 11
Comment lines#9-11
Figure 12
Add lines#13-15
Figure 13
So our script should look like as seen below
Figure 14
Also read: Working with Various Browsers
Run, we should see all the 3 browsers launch
Figure 15
So using interface concept, we can use the same driver reference dynamically for any browser.We can use driver.close() to close all the browsers automatically (lines#20, 22, 24). Run the below script and check
Figure 16
We can now navigate to any website in all 3 browsers, see below.
Figure 17
Run the script, you will see the 3 browsers launch & navigate to google.com
Figure 18
Comment FF & IE code. Let us fetch the title & current url of the website, lines 21-22
Figure 19
Run, see below o/p
Figure 20
Comment lines 21-22. We will now get the page source
Figure 21
Run, see below o/p
Figure 22
Comment pagesource function. From google.com, let us try to navigate to another website yahoo.com. Run the script and check the behaviour
Figure 23
Let us now navigate back from yahoo to google. Run the script and check the behaviour
Figure 24
Let us now close the current browser (that is opened by selenium)
Figure 25
So this was all about the importance of WebDriver interface. Thank you for reading!