Wednesday, May 20, 2020

Handling of DropDown using Selenium Webdriver

Handling of Dropdown:
Selenium provides us with a “Select”  class to perform operations on dropdown. This class can be found under the Selenium’s Support.UI.Select package.


ElementCommandDescription
Drop-Down BoxselectByVisibleText()/ deselectByVisibleText()selects/deselects an option by its displayed text
selectByValue()/ deselectByValue()selects/deselects an option by the value of its "value" attribute
selectByIndex()/ deselectByIndex()selects/deselects an option by its index
isMultiple()returns TRUE if the drop-down element allows multiple selection at a time; FALSE if otherwise
deselectAll()deselects all previously selected options

deselectByValue(“value”); – Deselects an option by value from a dropdown.
deselectByVisibleText(“text”); – Deselects an option by visible text from a dropdown.
deselectByIndex(index); – Deselects an option by index from a dropdown.
deselectAll(); – Deselects All options in a dropdown.
isMultiple(); – Boolean operation which verifies whether a dropdown allows multiple selection or not.
getOptions(); – Returns a list of all options from a dropdown.
getFirstSelectedOption(); – Returns first option selected in a dropdown as a WebElement .
getAllSelectedOptions(); – Returns a list of all selected options in a dropdown.


WebElement dropElement = Driver.findElement(By.id(“gh-cat”));



dropdown.selectByVisibleText(“Watches”);



The following is helpful to understand dropdown.

Project Scenario:
Feature: Launch FireFox browser and learn dropdown
Scenario: Launch ebay browser
Given User navigates to URL “http://www.ebay.in”
When User Enter some value in “Search Product” field
Then User enters a“Apple Watches”
When User select the category on your search from the dropdown
Then User select category as “Jewelry & Watches”
When User clicks on the search button
Then User should get the value of the total listings obtained as per your search

Project Format in eclipse:



There are two classes.
package com.automation.eng.AutomationProject.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import io.github.bonigarcia.wdm.WebDriverManager;

public class EbaySelectDropDownExpample {

public WebDriver driver;
public WebElement dropElement;

public void invokeBrowser(){

//setup the firefoxdriver using WebDriverManager
WebDriverManager.firefoxdriver().setup();
//Create driver object for Chrome
driver = new FirefoxDriver();
String url = "http://www.ebay.in";
//To maximise the browser
driver.manage().window().maximize();
//To delete all cookies
driver.manage().deleteAllCookies();
//To invoke URL
driver.get(url);
}

public void searchProduct(String product, String category){

//Locating Search Product field and entering value in it
driver.findElement(By.id("gh-ac")).sendKeys(product);

//Locating dropDown
dropElement = driver.findElement(By.id("gh-cat"));
//Object instantiation for selecting values from dropDown
Select dropdown = new Select(dropElement);

//Select value from the  DropDown
dropdown.selectByVisibleText("Jewelry & Watches");
//Clicking on Search button
driver.findElement(By.id("gh-btn")).click();

//Fetching count of total listings
String result = driver.findElement(By.xpath("//div[@class='srp-controls__control srp-controls__count']")).getText();
//Printing the count of  total listings
System.out.println("Result is : "+ result);
}
}

Here creation of another class where main method resides to execute the Script.
package com.automation.eng.AutomationProject.Select;
public class DemoEbay {
public static void main(String[] args) {
//Object instantiation for accessing 'EbayProject' class methods
EbaySelectDropDownExpample ebay = new EbaySelectDropDownExpample();
//Calling method invokeBrowser
ebay.invokeBrowser();
//Passing value for Search product field and value to be selected from dropDown
ebay.searchProduct("Apple watches", "Jewelry & Watches");
}
}

Output:
Result is : 334,495 results for Apple watches

No comments:

Post a Comment