What is Selenium Webdriver | A Simple Selenium Test

The first question that must come to your mind is what is Selenium Webdriver? It is nothing but a web automation framework that allows you to execute test cases across different browsers. If you remember Selenium IDE you already know how it only allowed us to work on Firefox browser alone. With Selenium Webdriver you can run your tests all across different browsers like Chrome, Internet Explorer, Safari, Opera etc.

If that weren’t enough Selenium Webdriver also allows you to use a programming language of your choice. So, our coding will not be limited to Selenese that we had been using with Selenium IDE. The horizon has just gotten bigger!

Using a programming language opens possibilities for using conditional operations like if-else. You can also make use of loops now which wasn’t possible hitherto.

turkey chicken meme for what is selenium webdriver

Let’s skip to learning what is Selenium Webdriver first.

What is Selenium Webdriver

Selenium Webdriver is nothing but a set of libraries or APIs which interacts with a web application. It helps in automating browsers and testing them to check if they are working as expected. Even background processes (API testing) can be automated with the help of Selenium Webdriver.

As we had already seen in the chapter about evolution and components of Selenium, Selenium Webdriver had emanated from Selenium RC which was also known as Selenium – 1.

Selenium RC had a lot of limitations and a complex structure that was overcome with the advent of Selenium Webdriver. We are going to see what is Selenium Webdriver in here and prepare a simple script in Eclipse IDE to see how we can achieve what we were so easily able to using Selenium IDE.

Answering the most daunting question of what is Selenium Webdriver and why we use it:

  1. Fast
  2. Lets you use a programming language of your choice
  3. Directly talks to the browser
  4. API is concise
  5. Can use HTMLUnit

In order to get started, you need to have Eclipse IDE installed on your system, Java installed and Selenium libraries integrated, all of which you can go and learn from this chapter.

Different Driver Servers

To learn what is selenium webdriver you must know about driver servers. To run a test case on a particular browser you have to have its respective programs on your system. These programs are called Driver Servers.

A language helps in making your system understand that you wish to communicate with a browser. Webdriver is a set of APIs responsible for establishing that communication with the browser.

interaction with selenium webdrivers

So to sum it, you are supposed to have a Driver Server for a browser you wish to test/automate in, and provide the path for it for Eclipse to understand where it is located to start interpreting things.

HTMLUnit and Firefox browsers are some of those browsers that Webdriver can automate directly. You don’t need to provide a separate component for these two. For the rest of the browsers, you need to have their respective driver servers.

Selenium dudes have packed up their Driver Server names smartly. Named them after their Browser names.

  • For Firefox 35 and above you need to have MozillaGeckoDriver (Selenium 3)
  • For Chrome, you should have ChromeDriver
  • InternetExplorerDriver Server for IE.
  • For Opera, OperaDriver
  • For Safari, SafariDriver

and so on.

If you are working with Selenium 2, just remember these points:

  • Firefox version should be 46 or below. Others are not stable. You don’t need to have any driver server.

If you are working with Selenium 3, remember these points:

  • Firefox version can be 46 and above. Here you are supposed to use GeckoDriver.
  • Java 8 should be installed on your system. Previous versions don’t work with Selenium 3.

Steps for Downloading Driver Servers

As explained earlier, we need to have a Driver Server in order to interact with a particular browser. Let us say we wish to perform a test on Google Chrome. What’s the Driver Server we would need here?

ChromeDriver! Correct!

Let us download that first and save it at a fixed location on your hard drive.

Step 1: In order to download it, you just have to type Chrome Driver Download on Google.

Chrome Driver Download

Step 2: Open the first link from where you can download the chrome driver. Here’s what you will get.

chromedriver download link

You can download the latest release.

Step 3: Click on Blue link as shown in the figure above.

It will take you to a storage area from where based on the operating system you are using you can download its respective Driver. I am using Windows so I will click on chromedriver_win32.zip.

driver based on operating system

Let it download:

downloading zip file

Step 4: When it gets downloaded click it open and then create a folder where you wish to keep all your Drivers. Just drag and drop the exe file in that folder.

drag and drop driver exe

Repeat steps 1 to 4 for Internet Explorer, for Firefox, for Safari, for Opera or whichever browser you wish to work with. Just search their respective drivers instead of Chrome. That’s it!

Informing Eclipse about the Webdriver

How do you tell your Eclipse that it has to go looking at a particular location for the downloaded Webdriver to communicate with the browser?

There are two ways to do that:

  1. Using System.setproperty
  2. By Setting up the Webdriver Path

You can use the following code in your Eclipse to tell it to look for the Webdriver:

System.setProperty(driverProperty, "Path of the driver");

where you just have to replace the driverProperty with the Driver Property name of the Browser driver you are using. Each driver has a different property name which you are supposed to remember:

  • Firefox – webdriver.gecko.driver
  • Chrome – webdriver.chrome.driver
  • IE – webdriver.ie.driver
  • Opera – webdriver.opera.driver

Path will specify the exact path where the driver is located with the exact name of the driver server exe file.

For e.g. for Internet Explorer testing you are supposed to use:

System.setProperty("webdriver.ie.driver", "E:\\Software\\Selenium\\IEDriverServer.exe")

Here my IEDriverServer file is located at the above location. Notice how we have used “\\” instead of “\” or our Eclipse IDE would get confused.

The problem with the above code is that you have to include this every time you are writing your code which is just outright outrageous.

Alternate Way of Setting Up Webdriver Path in Environment Variables

We can get rid of the above conundrum by simply downloading all our necessary Driver files at a single location and then providing the path for the same in Environment Variables.

Here’s how to do that.

Step 1: Right click on “This PC” or “My Computer” (whatever Operating System you are using that name will differ) and then click on Properties.

this pc properties option

Step 2: It will open a box titled System showing all the system info like this. You just have to click on Advanced System Settings option.

advanced system settings option in system

Step 3: This will open a System Properties box. Click on Environment Variables…

environment variables option in system properties

When you click on the Environment Variables… button you will see a box open.

environment variables box

Step 4: In the System Variables section click on Path and then on Edit option. The following box will open:

Edit system variables path

You have to create a new variable here.

Step 5: Then navigate to the location where you have saved all your Webdrivers or Driver Servers.

path location in drive

Step 6: Click on the top bar to turn it into the following way:

E:\Software\Selenium\Drivers

Step 7:  Copy that path location and then navigate back to the waiting Edit Environment Variable box.

Step 8: Click on New on the right-hand side of the screen. Paste the copied location there.

adding driver location in path

Step 9: Click on Ok three times as the boxes will close one after the other.

That’s it!

You will no longer have to use System.setProperty command to point out to your Eclipse where the individual Browser drivers are.

Steps for a Simple Selenium Test

Let us understand what is Selenium Webdriver and how to run it using a simple selenium test.

Hope you have Java installed on your system already and Eclipse is up and running. I am also assuming you have created a new project by now and added the latest Selenium standalone Server jars.

Step 1: Create a new class in a package inside your project. With Eclipse things are fairly easier:

new class in eclipse

I have named the class as WebDriverDemo.

Step 2: Time to instantiate Chrome so that it automatically opens Google Chrome. We need to create a constructor using the ChromeDriver class.

Add the following code in your main method:

ChromeDriver driver = new ChromeDriver();

This will create an instance of the class ChromeDriver and place it in the variable ‘driver’. Now we can use all the methods of ChromeDriver by using the driver instance, and . (dot) operator.

When you will enter the above code, you will get a compile-time error in Eclipse. That’s because Eclipse doesn’t understand what you are talking about. It doesn’t recognize ChromeDriver as of now.

When you click on the error bulb you will get suggestions to import the ChromeDriver class from org.openqa.selenium.chrome.

import chromedriver class

Step 3: Just click on that suggestion or alternatively add the following code at the top, under your package.

import org.openqa.selenium.chrome.ChromeDriver;

Doing so will help Eclipse to understand what ChromeDriver class is.

If you will run the above program even now, you will see that it will open the Google Chrome Browser for you:

new chrome instance opened

The reason being the ChromeDriver() command in your code is nothing but a constructor which asks your Webdriver to open an instance of Chrome.

Using methods of Selenium Webdriver

As you already know by now the answer to the question – what is selenium webdriver, you shall be introduced to the methods it retains. There are tons of methods that you can use now that you have created its instance.

If you type “driver.” in your code now, Eclipse IDE will flare countless options which are nothing but methods from the ChromeDriver class that you can use.

all the methods of ChromeDriver class

The same holds true for each specific Webdriver you are using. They too will have these methods by default.

We will use the most simple navigational method here in this example:

Step 4: Type driver.get(“url”) in your code and replace url with the URL you wish to navigate to.

I am going to see if I could open Google’s site. So I am going to add the following code:

driver.get(https://www.google.co.in);

This is the full code we have so far:

import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverDemo {
public static void main(String[] args) { 

              ChromeDriver driver = new ChromeDriver();
              driver.get("https://wwww.google.co.in"); 
       }
}

Run the program now and you will notice that it automatically opens Chrome first and then types Google’s url in the Address bar and the website opens successfully:

google site opens

Closing a Browser

It is a good idea to close the browser once you are done testing. You can use the following methods to achieve that:

  • driver.close()
  • driver.quit()

While driver.close() only closes the current window (what Ctrl + W does), driver.quit() will close the entire browser irrespective of how many windows are open.

Let us add that to our code:

import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverDemo {
public static void main(String[] args) { 

              ChromeDriver driver = new ChromeDriver();
              driver.get("https://wwww.google.co.in");
              driver.quit();

       }
}

There! Perfect! Now this will open the website and then close it. Go ahead and run the program.

GetTitle() Method

If your internet connectivity is really fast, you wouldn’t even notice whether the website opened successfully or not. How do we confirm that? Let’s make use of another method called driver.getTitle() to grab the title of the website.

I will make use of my website here, since it has a really long title name.

So adding the following code now:

System.out.println(driver.getTitle());

and changing the url in our code to “http://dumbitdude.com”. Our entire code will look something like this:

import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverDemo {

            public static void main(String[] args) { 
                ChromeDriver driver = new ChromeDriver(); 
                driver.get("http://dumbitdude.com"); 
                System.out.println(driver.getTitle()); 
                driver.quit(); 
        }
}

As you will run the above you will notice the website open and close all by itself. Hurray! Automation!

In the console, the title of the website will be grabbed like this:

how to grab the title of a website using selenium webdriver

Capish?

A Final Word for My Fellow Tester Friends

Know what is selenium webdriver now? Understood how to download respective browser drivers? How to add their path location in environment variables? And more importantly how to automate?

If you wish to run the above code on Internet explorer, simply use InternetExplorer driver instead of ChromeDriver.
If you wish to run it on Firefox, simply use FirefoxDriver instead of ChromeDriver in the above code.
Just make sure you are importing their respective classes. If the program doesn’t run just try rolling back the version and you will be fine.

With that, you have learnt what is Selenium Webdriver. You have even created your first simple selenium test.

Go ahead and experiment with rest of methods in the classes!

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

1 Response

  1. October 9, 2017

    […] Let’s take the example that we were checking out in the Webdriver chapter. […]

Leave a Reply