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
  • Click on search button
  • Click on
  • Click on Selenium link in header

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

Step 4: Verify the steps below that recorded Selenium ID

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

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

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

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

Step 5: Click on Finish

Step 6: Expand the project and select 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

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

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

Step 10:  Click finish.

Step 11: Open link and download Selenium Standalone Server jar file. Save the file in location D:\Selenium

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

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

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.

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

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

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

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

  • 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

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

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.
  2. To execute your test, you right click script name in the project explorer and Run As > JUnit Test.

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

Leave a Reply Cancel reply