You are on page 1of 7

SUDHEER REDDY ANKIREDDYGARI

5/2/17
SELENIUM WEBDRIVER

WebDriver Code using Selenium Select Class

Step 1: Create a new java class named as “HandlingDropDown” under the “

Step 2: Copy and paste the below code in the “HandlingDropDown.java” class.

Below is the test script

WebDriver driver=new FirefoxDriver();

//Launch the application


driver.navigate().to("http://newtours.demoaut.com/");
//Type user name by identifying user name text box
driver.findElement(By.name("userName")).sendKeys("mercury");

//Identify the password text filed and type password


driver.findElement(By.name("password")).sendKeys("mercury");
//Identify the sign in button and click on that button
driver.findElement(By.xpath("//input[@value='Login']")).click();
//Identify drop down
Select ByValue=new Select(driver.findElement(By.name("fromPort")));

//select dropdown option by using visible text


ByValue.selectByVisibleText("Sydney");

//select dropdown option by using value


ByValue.selectByValue("London");

//select dropdown option by using index


ByValue.selectByIndex(2);

//Get drop down options


List<WebElement> options=ByValue.getOptions();
for(int i=0;i<options.size();i++)
{
String optionvalue=options.get(i).getText();
System.out.print("Drop Down Values:"+optionvalue+"\n");

Page 1 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER

Import Statements

------------

 import org.openqa.selenium.support.ui.Select – Import this package prior to the script


creation. The package references to the Select class which is required to handle the
dropdown.

Object Instantiation for Select class

Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));

We create a reference variable for Select class and instantiate it using Select class and the
identifier for the drop down.

The identifier or the locator value for the drop down can be found using the techniques discussed
in the initial tutorials (by using Selenium IDE and firebug).

Take a notice that the identifier for a dropdown can be found as below:

Step 1: Most or almost all the dropdowns elements are defined in the <Select> tag having
multiple values (values that can be set into the dropdown) that are defined under the <option>
tags.

Setting the value in the dropdown using selectByValue() method

Page 2 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER

selectByValue.selectByValue(“greenvalue”);

In the above java command, we select the value “green” in the drop down using the
selectByValue() method and parameterizing it with the text present in the value attribute.

Setting the value in the dropdown using selectByVisibleText() method

selectByValue.selectByVisibleText(“Lime”);

In the above java command, we select the value “Lime” in the drop down using the
selectByVisibleText() method and parameterizing it with the text present on the user interface or
the text present between the opening and closing <option> tags.

Setting the value in the dropdown using selectByIndex() method

selectByValue.selectByIndex(“2”);

Page 3 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER

In the above java command, we select the third value in the drop down using the selectByIndex()
method and parameterizing it with the index value of the element which is desired to be selected
in the dropdown.

Take a note that the index value starts with “0”.

Moving ahead in the Selenium series, we would be discussing about the various types of looping
and conditional commands in WebDriver like isSelected(), isEnabled() and isDispalyed(). These
methods are used to determine the visibility scope for the web elements.

So let us start with a brief introduction – WebDriver has a W3C specification that details out the
information about the different visibility preferences based out on the types of the web elements
upon which the actions are to be performed.

Handling Browser based alerts and pop ups Mouse Hover


Actions in Selenium Web driver

Handling pop up is one of the most challenging piece of work to automate while testing
web applications. Owing to the diversity in types of pop ups complexes the situation
even more.

What is Alert box/ Pop up box/ confirmation Box/ Prompt/ Authentication


Box?

It is nothing but a small box that appears on the display screen to give you some kind of
information or to warn you about a potentially damaging operation or it may even ask
you for the permissions for the operation.

Example: Let us consider a real life example for a better understanding; Let us assume
that we uploaded a photograph on any of these popular social networking sites. Later
on, i wish to delete the uploaded photograph. So in order to delete, i clicked on the
delete button. As soon as I click on the delete button, the system warns me against my
action, prompting – Do you really want to delete the file? So now we have an option to
either accept this alert or reject it.

So ahead in the session, let’s see how do we reject or accept the alerts
depending on their types. Starting with the web based pop ups.

Web Based Popups

Page 4 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER

Let us see how do we handle them using WebDriver.

Handling web based pop-up box

WebDriver offers the users with a very efficient way to handle these pop ups using Alert
interface.

There are the four methods that we would be using along with the Alert interface.
1) void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the
pop up window appears.
2) void accept() – The accept() method clicks on the “Ok” button as soon as the pop up
window appears.
3) String getText() – The getText() method returns the text displayed on the alert box.
4) void sendKeys(String stringToSend) – The sendKeys() method enters the specified
string pattern into the alert box.

Copy the below code in note pad and save it as .Html file, click on the Try It button alert
will generate

<!DOCTYPE html>

<html>

<body>

<p>Click the button to display a confirm box.</p>

<button onclick="myFunction()">Try it</button>

<script>

function myFunction()

Page 5 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER

confirm("Press a button!");

</script>

</body>

</html>

Below is the sample code to handling alerts

WebDriver driver=new FirefoxDriver();

//Launch the application

driver.navigate().to("file:///C:/Users/s.r.ankireddygari/Desktop/webdriver(selenium)/
Alerts.HTML");

//Click on button alert will generate


driver.findElement(By.xpath("//button")).click();

//switch to alert
Alert alert=driver.switchTo().alert();

//click on ok
alert.accept();

driver.findElement(By.xpath("//button")).click();

alert=driver.switchTo().alert();

//click on cancel
alert.dismiss();

driver.findElement(By.xpath("//button")).click();
alert=driver.switchTo().alert();

Page 6 of 7
SUDHEER REDDY ANKIREDDYGARI
5/2/17
SELENIUM WEBDRIVER

//Get text present on alert


String text= alert.getText();
System.out.println("text:"+text);

Page 7 of 7

You might also like