Starting with Selenium WebDriver…

As you might be aware that Selenium is used for Test Automation for Web Applications. Selenium is composed of multiple software tools like Selenium IDE, Selenium WebDriver, Selenium Grid etc. We will be concentrating more on Selenium WebDriver as this is very powerful and commonly used for test automation. Selenium WebDriver is also compatible with multiple browsers like Google Chrome, Firefox, IE, Safari etc. I will also share details of Selenium Grid in upcoming posts which is mainly used for running scripts parallely in multiple environments.

You can go to below link and get complete details of Selenium:
Selenium Documentation

Create our first Selenium Script:

Here I will be mainly focusing on creating our first Selenium script using JAVA language and by using Eclipse as our IDE. You can simply follow the step by step approach which I will be taking. They are very easy to follow…

  • First of all open Eclipse and select the default workspace as your working directory. You can change the location if required.
  • Go to File -> New -> Java Project to create a new Java Project. Enter the Project Name as “FirstSeleniumProject”. You can provide any name as your liking.
  • You can see by default the JRE environment is selected. In my case its Java SE 1.8
  • Click Next and Finish to complete the Java Project creation. This is very much same to our last “TestMadness” project creating.
  • Right Click on the “src” folder to create a new package. src -> New -> Package.
  • Enter the package name as “com.selenium.firstscript”. As mentioned earlier, package will create a folder heirarchy ‘com -> selenium -> firstscript’ and store the class file in it. Click Finish to complete the Package creating.
  • Right Click on the Package name and create a new class file. Package -> New -> Class.
  • Name the class as “FirstScript” and select the “public static void main(String[] args)” option. For simplicity we will be creating our first Selenium script inside the main method. When a program starts executing, it has to start execution from somewhere. That somewhere is called main method. Click Finish to complete the class creation.
screen-shot-2017-02-19-at-12-17-25-pm
This how FirstSeleniumProject looks like

Now that we have completed our Java Project creation, lets start creating creating our first Selenium Script inside the main method.

  • Enter the following code “WebDriver driver = new ChromeDriver()” in the main method as  shown below. As you enter the code, you will see an error coming “- WebDriver cannot be resolved to a type”. Don’t worry on the error. This is because we haven’t added the Selenium Jars into our project. We will have to import the Selenium Jars into our project in order to work with Selenium.
Screen Shot 2017-02-19 at 12.27.04 PM.png
Error in our first script

Next steps are to add the Selenium API into our Java Project.

  • Go to Download section of the Selenium.org website and download the Selenium Standalone Server as shown below.

URL: Selenium Download

screen-shot-2017-02-19-at-12-34-34-pm
Click 3.0.1 to download Selenium Standalone Server
  • Also download the Chrome Driver from ‘Third Party Drivers, Bindings, and Plugins’ in the same page. We will be executing our first Selenium script in the Chrome Browser. Chrome Driver is an executable file and it depends on the OS and configuration of the machine. Since I am working on a Mac 64 bit machine, I will be downloading the chromedriver_mac64.zip version.
Screen Shot 2017-02-19 at 12.37.31 PM.png
Click Google Chrome Driver from Third Party Drivers section
Screen Shot 2017-02-19 at 12.43.23 PM.png
Download the appropriate Driver version

Next step is to add the downloaded Jar file into our Java project.

  • Right Click the Java Project name “FirstSeleniumProjects” and select properties.
  • Select the Java Build Path in the Properties pop up.
  • Click on Add External Jars to import the downloaded Selenium Standalone Jar.
  • Go the correct location of the downloaded Selenium Standalone Jar and click OK to finish the import.
Screen Shot 2017-02-19 at 1.02.49 PM.png
Selenium Standalone Server Jar import
  • Once the jar is imported, you can see Referenced Libraries is added in our project. Referenced Libraries contains the Selenium Standalone Server Jar that we imported.
Screen Shot 2017-02-19 at 1.06.06 PM.png
Referenced Libraries
  • Now go to our “FirstScript.java” and click on the ‘WebDriver’ error as displayed. You can see a new option ‘import WebDriver’ is displayed. Click on this option to import all the required packages and classes for writing our first Selenium Script. Eclipse will do the import process. You can see a new code “import org.openqa.selenium.WebDriver” is added in our script.
    Screen Shot 2017-02-19 at 1.10.09 PM.png
    Click import WebDriver

    Similarly we can remove the ChromeDriver error by clicking on the error and importing the necessary packages for Chrome Driver. Both imports are from Selenium Standalone Server Jar that we imported into our project. Save the project and can see all the errors are removed now.

    Screen Shot 2017-02-19 at 1.21.11 PM.png
    Error removed

    Congrats! We started with our first Selenium script. Although this script is not validating anything but its great that we reached till this point. Please note to execute the Selenium script in Chrome browser, the browser need to be installed in your Windows or Mac machine.

    I will continue with the scripting in the next blog. So stay tuned…

    Additional Information:

    What is a WebDriver?

    As you have seen that in our first script we have used the command ‘WebDriver’. Many of you have wondered what WebDriver is actually. WebDriver is an Interface, and we are defining a reference variable (driver) whose type is an interface.Now any object we assign to it must be an instance of a class (For example ChromeDriver) that implements the interface.

    Now obviously the next question comes, What’s an Interface?

    Interface is OOPS concept. It is like a blueprint of Class. It contains variables and body less methods (Abstract methods), where we just declare methods but we implement them inside the class which inherit Interface. It always helps in understanding the Selenium script exercises if you have knowledge in OOPS concept.

     

    What are different types of Drivers?

    As mentioned, different Drivers contains the implementation of WebDriver Interface. Based on the browser requirement, we can use any of the following Drivers:

  • FirefoxDriver – For Firefox browser.
  • InternetExplorerDriver – For IE browser.
  • ChromeDriver – For Chrome browser.
  • SafariDriver – For Safari browser.
  • OperaDriver – For Opera browser.
  • AndroidDriver – For Android devices.
  • IPhoneDriver – For iPhone devices.
  • HtmlUnitDriver – For headless browser. This is a web-browser without a graphical user interface. It will behave just like a browser but will not show any GUI.