Selenium WebDriver – Creating First Script

Recording and Exporting Script from IDE

We will record the test case using Selenium IDE and then export the test case using Java / JUnit 4 / WebDriver option. Follow the steps given below:

Step 1: Open Selenium IDE and verify that recording mode is ON

Step 2: Assuming that application Google search is already open in Firefox browser, Perform the following steps in IDE recording mode:

  • Search for an automationtutorial.com
  • Click on search button
  • Click on automationtutorial.com
  • Click on Selenium link in automationtutorial.com header

Step 3: Stop recording by clicking on Stop Recording button in record toolbar.

Step 4: Verify the steps below that recorded Selenium ID

Selenium_eclipse-webdriver-First_Script_IDE

Step 5: Select to File > Export Test Case As > Java / JUnit 4 / WebDriver

Selenium_eclipse-webdriver-First_Script_IDE-2

Step 6: Save it as MyFirstWebDriverTest in D:\Selenium folder. You will notice that the script is saved as MyFirstWebDriverTest.java file.

Step 7: Try to open the script you have saved in an editor like Eclipse.

Selenium_eclipse-webdriver-Script_Review

Let us review the exported Java code. The exported test is Junit test. JUint is a unit testing framework for the Java programming language. We will see a class name “MyFirstWebDriverTest” shown in snapshot.

Configure Eclipse to Work with Selenium WebDriver

Step 1: Launch Eclipse from your location of eclipse.exe file.

Step 2: Open the default workspace, D:\SeleniumTest

Step 3: Click  on workbench icon (if you see default view)

Step 4: Go to File > Project > Java Project and enter as SeleniumTestAutomation

Selenium_eclipse-webdriver-Create_Project

Step 5: Click on Finish

Step 6: Expand the project and select src folder

Selenium_eclipse-webdriver-src_folder

Step 7: Now we will create a new package which will contain all tests. Right click on the src folder. Select New > Package

Selenium_eclipse-webdriver-New_Package

Step 8: Name the Package as tests and click finish.

Selenium_eclipse-webdriver_Package_Name

Selenium_eclipse-webdriver_Package_show

Step 9: Right click on this tests package and select New > Class, in the New Java Class dialog enter Name: MyFirstWebDriverTest.

Selenium_eclipse-webdriver_New_Class

Selenium_eclipse-webdriver_New_Class_Name

Step 10:  Click finish.

Selenium_eclipse-webdriver_Class

Step 11: Open link http://www.seleniumhq.org/download/ and download Selenium Standalone Server jar file. Save the file in location D:\Selenium

Selenium_standalone_server

Step 12: Select SeleniumTestAutomation in Eclipse, right click and select New > Folder

Selenium_Webdriver_New_Folder

Step 12: Name the Package as tests and click finish.

Selenium_Webdriver_New_Folder_Create

Selenium_Webdriver_New_Folder_Show

Step 13: Go to the location where your workspace folder is stored. In our case it is D:\SeleniumTest\SeleniumTestAutomation. You will see Lib folder available. Double click to get in Lib folder.

Step 14: Copy selenium-server-standalone-3.0.1.jar file into the Lib folder which we had copied earlier in location D:\Selenium.

Step 15: Come back to Eclipse and click on your project and press F5. You should be able to see the Lib folder Jar file added to your project structure.

Selenium_Webdriver_New_Folder_Lib

Step 16: Now Click on the Project > Properties and in opened window click on ‘Java Build Path’. Then Click the Libraries tab.

Selenium_Webdriver_Project_Menu

Selenium_Webdriver_Java_Build_Path

Step 17: Click on Add External Jars button and add selenium-server-standalone-3.0.1.jar file from D:\SeleniumTest\SeleniumTestAutomation\Lib

Selenium_Webdriver_Add_Selenium_Server

Step 18:  Click OK. Copy the code from the exported Selenium IDE script, MyFirstWebDriverTest.jave (open in notepad and copy) and Overwrite existing code of Eclipse in MyFirstWebDriverTest.java

Selenium_Webdriver_class_details

You will notice the package name is throwing an error. This because currently our script is in different package named tests.

  • Make sure the package name is corrected to the right name of the package
  • Also make sure the class name is the same as the name of our newly created class in eclipse

Step 19: See below updated code after fixes

Selenium_Webdriver_updated_class_details

  • If you get any errors regarding classes not found then you have not imported the selenium jar correctly.
  • Make sure your JRE is not throwing an error. You can go to Project > Properties > Java Build Path and check the JRE is not throwing an error. If yes you can double click on JRE to select correct Java execution environment.

Running the Test

Now that we have configured our first WebDriver test let us run it and verify if it correctly executes. You have various options to run this Java code in eclipse. You can run it as:

  • A Java program using main method of a class.
  • Using JUnit Test Framework
  • using TestNG Framework

As part of this exercise we will run this script as JUnit script.

Project Clean-up

Before you run the script it is always good to clean up any existing .class file and re-build the project. To clean project files perform below steps:

Step 1: Select Project > Clean

Selenium_Webdriver_Project_Clean

Step 2: Select “Clean all projects” radio button on the Clean dialog box and click OK

Selenium_Webdriver_Project_Clean_all

Script Execution

There are two ways to execute code in Eclipse IDE.

  1. On Eclipse’s menu bar, click Run > Run or press ctrl+F11 to run the entire code.
    Selenium_Webdriver_Run_Project
  2. To execute your test, you right click script name in the project explorer and Run As > JUnit Test.

Selenium_Webdriver_Run_Project_JUnit

This will launch Firefox with the mentioned URL, perform your test steps.

Leave a Reply

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