WebDriver Tutorial 1 : Basic Project Setup

This is Part 1 of the Selenium WebDriver Tutorial Series.

In this tutorial, we will be setting up a simple webdriver project in eclipse.

Before diving into eclipse, we will be needing two files – the selenium webdriver standalone server jar file and chromedriver binary. Download the latest files from these locations.

Selenium WebDriver Downloads

 

 

 

 

Summary of what we will be doing in this tutorial

  • Navigate to Google homepage
  • Type a search term(Selenium) on the text field and submit
  • Clicks on the first result (Selenium Downloads)
  • Verify the opened Selenium Downloads page
  • Close the browser window and exit application

Setup Eclipse project with WebDriver

1. Create new java project in Eclipse (File > New > Java Project).
2. Create lib and res folder in the project root for selenium jar and chromedriver.exe files. Project structure should now look likes this

Selenium Standalone Jar and ChromeDriver binary

3. Add the jar to the project classpath by right clicking on the jar file and selecting Build Path > Add to Build Path

Setting Selenium Standalone Server jar to classpath

This can also be done via RightClick on Project > Properties > Java Build Path section > Libraries tab > Add jars and select the jar from lib folder.
4. Create a package and add a class under in it. The complete code for the class is given below

Code Explained

Initializing WebDriver object

WebDriver object is initialized using different classes for different browsers.

SEE ALSO:   Create Selenium WebDriver TestNG Project using ANT

For Firefox :

For Chrome :

For chrome we have to provide the path to chromedriver executable before initializing the driver. Since we have placed the chromedrive.exe file in res folder, we set the system property ‘webdriver.chrome.driver‘ with value ‘res\\chromedriver.exe

Once webdriver object is initialized, the browser opens up but will be in restored state. The next line maximizes the opened browser.

Loading URL

Loading the URL is done using the webdriver’s get() method. Please note that we have to provide the protocol (http:// or https://) while giving the URL.

Performing Search

Next operation we have to perform is type on the text field. In webdriver, we first find the element to act on and then perform the operation on it.

In this case, we use the id attribute of the text field to find the element. If you are using chrome, with google.com loaded, right click anywhere on the page and click Inspect to bring up the developer tab. Click on the inspector icon on the left top corner and then click on the text field to see its html code.

Inspect Element

From the html code for the text field (input field), we see that it has a class attribute(‘gsfi’), a name attribute(‘q’) and and id attribute(‘lst-ib’). We should be selecting the locator which is unique and non changing. Since id is unique, we will use that.

More details on finding the locators for elements will be covered in another tutorial.

SEE ALSO:   How to Run TestNG xml Programmatically

Using id attribute of ‘lsb-ib’, we can get the WebElement using the below lines.

We will see more about locating elements in the next tutorial.

Using this element we can perform the type operation – the method is sendKeys();

We can simulate pressing of Enter key after typing by using the Keys class (Keys.ENTER will simulate the Enter key press). Adding it to the above line, we get

Waiting for Result Link

Now we need to wait for the result page to load. We can give a delay using Thread.sleep(), but the better approach is to wait till the element is present and clickable. This is done using the WebDriverWait class as shown below.

The first line initializes the WebDriverWait object providing the driver object and a timeout of 5000 ms. It will wait for the required element for 5000 millisecond and then returns to next statement. The second line sets the wait until condition using the ExpectedConditions class. Expected conditions class has several methods like elementToBeClickable, presenceOfElementLocated, textToBePresentInElement etc. We want to wait till the Downloads – Selenium link is present and clickable and therefor use ExpectedConditions.elementToBeClickable() method giving the webelement for the link.

Note that here, we used By.linkText() as we wanted to find the element using the linkText of the link.

After the element loads and is clickable the next line is executed which clicks on the element. The ‘driver.findElement(By.linkText(“Downloads – Selenium”))‘ part finds the web element and the ‘click()‘ method performs the click operation on it.

SEE ALSO:   WebDriver Tutorial 2 : Locating Elements and Mastering XPath

Verify Final Page Title

We can get the title of the page using driver.getTitle() method. The next lines compare it with the expected string, and prints out whether the test is pass or fail.

Closing Browser Window

The driver.quit() method can be used to close all open windows and exit the application. If multiple windows are present and you only want to close the current window drive.close() can be used. In our case, we can use driver.quit().

We will see more about finding locators/elements in the next tutorial.

Selenium WebDriver Tutorial 1 : Basic Project Setup : Source Code

Next Tutorial : Locating Elements and Mastering XPath

You may also like...

3 Responses

  1. Bohdan says:

    Isn’t it better to use Asserts to verify final page title?

    • axatrikx says:

      Yes. Thats the right way. I just wanted to leave the other aspects out of that particular tutorial, thatsy i used a simple comparison.

  1. April 16, 2016

    […] WebDriver Tutorial 1 : Basic Project Setup […]

Leave a Reply

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