It’s Time To Setup Selenium Web Driver & Write Our First Test
Now we got to know about Selenium & the main tools under it. It’s time to start using the Web Driver, which is the main focus of this series. In this lesson, we will download Selenium & integrate it into Eclipse (you can use another IDE you like). After that, we will write our first Selenium test. It will simply launch a Firefox instance, and open a webpage with it, then close the browser. It may sound simple, but that’s how “Hello World” examples should be. We will expand on what we learn in this lessons in the upcoming lessons.
Quickly go to:-
Downloading Eclipse & Creating The Test Project
First, download the latest version of Eclipse from their official website here.. If you are interested, there are other eclipse packages you could choose from.
After you install Eclipse & launch it, you will encounter the following dialog:-
This dialog is asking you where you want your Workspace is. In simple terms, Workspace is where all your work will be stored. It can contains multiple projects & their configurations, as well as some settings for Eclipse itself. You can go with the default option if you like. I personally chose to have a different folder for my workspace. Click OK to continue launching Eclipse.
Once Eclipse has started, you will be greeted with this screen.
Let’s create a new project by selecting File->New->Java Project from the main menu.
The New Java Project dialog will appear. For the most part, and as long as you have Java installed properly on your computer, you won’t need to do anything here. Just type the project name in the “Project Name” field then click on the “Finish” button. I named the project “Selenium-Test”:-
Now we have created our project. Let’s create a class inside the project. That’s where we will write our code. right click on project, then select New->Class from the context menu:-
The New Java Class dialog appears:-
Give a name to the class just like we did with the project. Make sure the public static void main(String[] args) option is checked, so that Eclipse would create the main function for you. You will have to write the function on your own if you didn’t tick that option.
Click on Finish to close the dialog and create the class. We have an empty class with a main function to write our code in.
Downloading Selenium
Let’s download Selenium & add it to our Java project. All selenium downloads can be found here.
Click on the Download button for Java. Make sure you don’t click the Alpha Download link (assuming the page hasn’t changed since I wrote this lesson):-
Let’s open the .zip file & take a look at it. We simply need to add all the .jar files inside it to Eclipse class path. That includes the .jar inside the libs folder. Let’s do that now. Drag the files out of the zip file into some folder to extract them.
Adding Selenium To Our Eclipse Project
Select Project->Properties from the main menu. The projects properties dialog appears.
Go to the Libraries tab, and click the Add External JARs… button. Locate the .jar files where you extracted them just now, then press Open to import them into the project. This is how the dialog looks like after we imported the files:-
Click the Apply and Close button to close the dialog.
Downloading Selenium Firefox Web Driver (Gecko Driver)
Now we are done setting up Selenium. We need to download the Web Driver for the browser we intend to use, which is Firefox in our case. You can find all the Firefox Web Driver releases here.
Scroll down a little to locate the download links for the latest version. Click on the download link for your operating system of choice to download it:-
Extract the .zip file somewhere in your system. We will reference that file in our code shortly. I extracted mine in the following path:- C:\Fairy\Web Drivers\.
Now it’s time for us to write the first selenium code.
Writing Our Very First Web Driver Code
The code to open up Firefox, display a page, then close it requires only 4 lines of code. First, we need to tell Selenium where to find the Gecko driver for Firefox:-
System.setProperty("webdriver.gecko.driver", "C:\\Fairy\\Web Drivers\\geckodriver.exe");
The path to GeckoDriver.exe is where we extracted it in the last step. If you did this step wrong, you will get the following error when you run your test, so be careful:-
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
Next, we need to launch Firefox instance:-
WebDriver driver = new FirefoxDriver();
If you run your code now. You will end up with Firefox instance with an empty page. So let’s tell it to open a webpage:-
driver.get("http://example.com");
I picked http://example.com since suits our testing purpose, but you can literally put any URL here. Just make sure your page URL starts with http:// or https://. Or else you will get the following error in your console:-
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Malformed URL: URL constructor: example.com is not a valid URL.
If you like, you can write a function that checks the URL for you, and prefix the http:// part for you. We didn’t do that here for the sake of simplicity.
Here’s Firefox with example.com homepage opened:-
The last step is to tell Selenium to close the Firefox instance we started. The test can pretty much work without this step, but in that case, the program will close leaving you with a Firefox instance that you have to close manually:-
driver.close();
It may seem anticlimactic to see the page open then close immediately after that. Optionally, you can make the program wait for 5 seconds (5000 milliseconds) before it closes Firefox:-
try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Here’s the full code with all the needed imports. I added a line that made the program print that the test has started:-
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class OpenURL { public static void main(String[] args) { System.out.println("Web Driver Application Started"); System.setProperty("webdriver.gecko.driver", "C:\\Fairy\\Web Drivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://example.com"); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver.close(); } }
And Finally
The code we wrote today probably doesn’t do much, but just having Selenium Web Driver set up is a huge step. In the next few lessons, we will build on what we learned today & learn to do all sorts of things with Web Driver.
I hope you liked my lesson, and see you again in the next lesson.
See Also:-
-
- Introduction to selenium web driver series. What is it? And what browsers & programming languages it supports – Tech Fairy
- What are 60% Keyboards (65%)? And what are their advantages & disadvantages – Tech Fairy
- 40% Keyboards Form Factor, What are they? And why would you use one? – Tech Fairy
- Why ThinkPad laptops are popular, and what are their advantages?
- What are business-grade laptops? What are their advantages? And which one to buy?
- Java VS Python VS C# detailed comparison, which language to learn first?
- Do you need an Expensive motherboard for gaming?
- What is the meaning of the different USB port colors? (blue, teal blue, yellow, red, black)
- Why motherboards & laptops still come with USB 2.0 ports When USB 3.0 Is Backward Compatible?
- USB 2.0 VS USB 3.0 Comparison: What are the differences between the two ports?