Tutorial 3- Web Driver Interface

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

Practice few functions

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

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

Figure 2

Click findElement() method link (or any method link), the method documentation gets displayed

Figure 3

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

Figure 4

Similarly, findelement() function is present in all the browser classes

Figure 5

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

Figure 6

Also read: Configure Selenium-Java Webdriver in Eclipse

So far we have been creating objects like this: ChromeDrivercd = new ChromeDriver()

Figure 7

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

Figure 8

Similarly we can write lines#10 and 11

Figure 9

Figure 9

When we say WebDriver driver = null, it means that the driver reference is not pointing anywhere

Figure 10

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

Figure 11

Comment lines#9-11

Figure 12

Figure 12

Add lines#13-15

Figure 13

Figure 13

So our script should look like as seen below

Figure 14

Figure 14

Also read: Working with Various Browsers

Run, we should see all the 3 browsers launch

Figure 15

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

Figure 16

We can now navigate to any website in all 3 browsers, see below.

Figure 17

Figure 17

Run the script, you will see the 3 browsers launch & navigate to google.com

Figure 18

Figure 18

Comment FF & IE code. Let us fetch the title & current url of the website, lines 21-22

Figure 19

Figure 19

Run, see below o/p

Figure 20

Figure 20

Comment lines 21-22. We will now get the page source

Figure 21

Figure 21

Run, see below o/p

Figure 22

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

Figure 23

Let us now navigate back from yahoo to google. Run the script and check the behaviour

Figure 24

Figure 24

Let us now close the current browser (that is opened by selenium)

Figure 25

Figure 25

So this was all about the importance of WebDriver interface. Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024