Let’s take a simple scenario to automate using Selenium WebDriver and see explanations of each command in the program source code.
1. If you have downloaded the latest Selenium Java Client Driver and it is not compatible with the latest Firefox browser, you downgrade to Mozilla Firefox version 47.0.2 (32 bit) and download it. You can also download Firefox version 47.0.2 for 64 bit here.
Now download selenium-server-standalone-2.53.1 and add it in Java built path. Both are compatible with each other. Remove all Jar files of selenium java client driver. Keep only selenium- sever-standalone as shown in the below screenshot. Now the code will work.
2. In the previous version of Selenium (Selenium RC (Remote Control)), it is mandatory that you need to run a selenium-server-standalone.jar jar file, which acts as a server. Selenium RC uses this server to communicate between browser and code. This jar file contains all the library functions to be used in our code.
But in the later version of Selenium (Selenium WebDriver), there is no need to run this jar file because WebDriver API can directly communicate with the browser’s native language. Hence, the selenium-server standalone jar file has been replaced with selenium-java jar files in the later version.
First Script (Test Case) in Selenium WebDriver
Under this test, we will automate the following scenarios given below:
1. Launch the Firefox browser.
2. Open URL: https://www.google.co.in.
3. Wait for the page to load.
4. Print a message (URL is opened successfully) on the console.
5. Close the browser.
Important Steps to Automate Test Script:
1. To create your first Selenium WebDriver script, first, open your Eclipse IDE.
2. Create a Java Project. Go to File > New > Java Project and write “Selenium WebDriver Project”.
3. Create a Package. Go to src > Right-click > New > Package and declare a name “newProject”.
4. Now create a Java class. Go to package “newProject” > Right-click > New > Class and declare a class name “FirstTestScript”.
5. Now we will create an object of a class FirefoxDriver by taking WebDriver reference. You have known that WebDriver is an interface. In this way, we can implement all methods of WebDriver interface. We cannot create an object of interface directly.
The following syntax to create an object of FirefoxDriver class is shown in below figure.
6. Open URL of webpage in the browser by calling get() command. It takes a string URL as a parameter and returns nothing. The syntax is as follows:
7. We will wait for the whole page to be load in browser by using the following syntax.
We will learn more about this syntax in further tutorial.
8. To close browser, we will call close() command by using a reference variable driver. The syntax is as follows:
Let us understand in detail the following program source code to automate the above scenarios. We have explained comments for each block of code to explain the steps clearly.
Program source code: 1
Explaining the code:
➲ When you will write the statement “WebDriver driver=new FirefoxDriver();” in your script, you will notice that there are two errors underlined with the red color line at the statement in the code.These red color lines can be seen in the below figure.
➲ This underlined red colored word represents the error in the Eclipse IDE. Since the Eclipse IDE is a powerful tool and a lot of very wonderful features. Therefore, It will give you to use the best feature like “Solution suggestion“.
➲ To use this feature, you just bring the mouse cursor on the first red line error keyword “WebDriver” and then second red line keyword “FirefoxDriver”. Eclipse will automatically show you the best solutions to fix the error.
➲ This suggestion will work only when you have added the selenium WebDriver jars to your project otherwise, Eclipse will not show suggestions of the correct solution to fix the errors.
➲ As you bring the mouse cursor to the error, Eclipse will show you different solutions as shown in the above figure but you have to select the first solution (Import ‘WebDriver’) provided by the eclipse.
After selecting the first solution, Eclipse IDE will import two import statements “import org.openqa.selenium.WebDriver;” and “import org.openqa.selenium.firefox.FirefoxDriver;” at the top of the coding script. After importing, the error will be gone off from your code.
➲ org.openqa.selenium.WebDriver: It contains the WebDriver class to instantiate a new browser loaded with a specific driver.
➲ org.openqa.selenium.firefox.FirefoxDriver: It contains the FirefoxDriver class to instantiate a Firefox driver onto the browser instantiated by the web driver class.
Running the Java Test Case:
After resolving all errors, You have to execute the code of your first automated test case in the Eclipse IDE. There are two ways to execute the test case. They are as follows:
1. On Eclipse’s menu bar, click Run > Run As > Java Application or Right click on the eclipse code, click Run As > Java Application.
2. Press Ctrl+F11 to run the entire code.
After a few seconds, a Firefox browser will open and Selenium will launch the Google page with the help of your script. After launching completely, the browser will be closed after waiting a few seconds.
Once the execution is finished, a message “Website is opened successfully” will display on the console section of the Eclipse IDE.
Hope that this tutorial has covered almost all the important points related to how to create first test script in selenium web driver with example and related screenshot. I hope that you will have understood this topic and enjoyed coding.
In the next tutorial, we will discuss a new topic Selenium WebDriver commands.
Thanks for reading!
⏪ PrevNext ⏩