Writing your first Automation Script in Selenium

Writing your first Automation Script in Selenium

In this tutorial, we are going to demonstrate writing your first Automation Script in Selenium. This test script will show how to test a particular value in a drop down list box.

Objective of this tutorial is to demonstrate:

  • How to read values of a drop down list box?
  • How to check if a particular value is present

This is chapter 13 of the Free Selenium Tutorials being developed by Techcanvass. These tutorials are categorized into Java, Selenium, TestNG, Automation Framework and Grid.

The Test case description

We have created a simple page to demonstrate how to write a realistic test script using Selenium. We will check and verify if the dropdown list box on this dropdown page has a value “Samsung Note 3”? If the value is present, confirm it in the output panel.

View the dropdown HTML page

Let’s get to writing the code.

Instantiating Gecko Driver

Remember, we are working with Selenium version 3.0 so we need to work with Gecko driver, even in firefox. Let’s get started by writing the code to get Gecko driver on board (literally).

//Using the Gecko Driver
System.setProperty(“webdriver.gecko.driver”, “E:\\Selenium 3\\geckodriver.exe”);

//Defining driver
WebDriver driver;

//instantiating the driver

driver = new FirefoxDriver();

System.setProperty(“webdriver.gecko.driver”, “E:\\Selenium 3\\geckodriver.exe”) is the new way of employing the gecko driver for Firefox.

System.setProperty uses two parameters. The first parameter tells to set the gecko driver path to E:\\Selenium 3\\geckodriver.exe defined as second parameter.

The next lines of code define and instantiate the gecko driver.

Open the Dropdown page and access dropdown list

Once the gecko driver is instantiated, we are ready to go. So, we first of all we need to open the page dropdown.html. The code for doing so is as shown below:

//Launching the dropdown Page
driver.get(“http://learnselenium.techcanvass.com/dropdown.html”);

This is simple, you just provide the URL of the page to driver.get() method. This method will open the page.

The next step is to access the dropdown list box. To do so, we need to create an instance of the dropdown box as shown below:

//Creating instance of Drop down list box
Select drpdwn = new Select(driver.findElement(By.name(“model”)));

 

Running the test case to check the test data

Our task was to write the test script to check, if the drop down list contains the value “Samsung Note 3”.

To do so, we need to iterate through the values of the drop down. This requires you to use the List and Iterator in Java as shown below:

//List Element to capture all the elements from the dropdown list
List<WebElement> allOptions= drpdwn.getOptions();

Iterator<WebElement> it = allOptions.iterator();

The Iterator is a multi-purpose technique to iterate through any multi-value object. In the above piece of code, Iterator<WebElement> it is now capable to navigating through all the elements of the dropdown list as shown below by using while loop

while(it.hasNext())
{

WebElement el = (WebElement) it.next();
//Check for the required element by Text and click it

{

//Test Case to be written here

}

while(it.hasNext()) will keep running till there are elements in the dropdown list. This way, we pick up one item at a time and compare it with the expected value “Samsung Note 3”. Based on the match status, we print the result.

The complete code with while loop is as follows:

while(it.hasNext())
{

WebElement el = (WebElement) it.next();
//Check for the required element by Text and click it
if(el.getText().equals(“Samsung Note 3”))
{
//System.out.println(el.getAttribute(“value”));
el.click();
System.out.println(“Test Passed”);
}
else
{
System.out.println(“Test Failed”);
}
}

Congratulations, you have written the first automated test script.

Complete Code for the example

For your benefit, I am providing complete code for the program. Replace the class name and package name as the case may be.

package basicSeleniumScripts;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Ch5Assignment5 {

public static void main(String[] args) throws InterruptedException {

//Using the Gecko Driver
System.setProperty(“webdriver.gecko.driver”, “E:\\Selenium 3\\geckodriver.exe”);

//Defining driver
WebDriver driver;

driver = new FirefoxDriver();
//Launching the dropdown Page
driver.get(“http://learnselenium.techcanvass.com/dropdown.html”);

//Maximize the newly loaded window
driver.manage().window().maximize();

//Creating instance of Drop down list box
Select drpdwn = new Select(driver.findElement(By.name(“model”)));

//List Element to capture all the elements from the dropdown list
List<WebElement> allOptions= drpdwn.getOptions();

Iterator<WebElement> it = allOptions.iterator();

System.out.println(allOptions.size());

while(it.hasNext())
{

WebElement el = (WebElement) it.next();
//Check for the required element by Text and click it
if(el.getText().equals(“Samsung Note 3”))
{
//System.out.println(el.getAttribute(“value”));
el.click();
System.out.println(“Test Passed”);
}
else
{
System.out.println(“Test Failed”);
}
}

drpdwn.selectByIndex(3);
Thread.sleep(5000);

//close Firefox
driver.close();
driver.quit();
}

}

About Techcanvass

Techcanvass is a software training organization and offers certification courses in Business Analysis and automation testing. Our courses in selenium are:

Selenium Certification (CP-SAT) Training

Selenium Certification Course

 

 

 

 

 

Selenium Training with Java

Selenium Training Course

Leave a Reply

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