How to Select a value from drop down which doesn’t have
Select Tags?
Problem: I see many people posting questions how to select a value
from the drop downs which doesn’t have Select Tags?
And everyone looks for only Select when there is drop down
and they struggle to write locators or make the selection dynamic, so now let’s
see how we can write methods or locators which will take dynamic inputs and
select any value
There are many drop downs (Single or Multi Select) which
doesn’t have Select Tag implemented so how to handle this one??
There are some tags mostly used with these kind of drop
downs.
Li tag: - List Items
ul tag: - un ordered list
ol tag: - order list
I had tried to find one website which has drop down and
without select Tags here is the link
1.
Try to find the element which we need to click
to open the drop downs
private By howCanWeHelpDropDown=By.xpath("//label[contains(text(),'How
can we help')]/following-sibling::div//input[@placeholder='Select an
Option']");
2.
Once we click on the drop down then write on
xpath or locator which will point to all the list items in the drop down
private By howCanWeHelpDropDownList=By.xpath("//input[@placeholder='Select an
Option']/../following-sibling::div//span[@class='slds-truncate']");
3.
Write loop and extract either text or value or
some unique one which will help to select
for (int i = 0; i < valuesList.size(); i++) {
String value = valuesList.get(i).getText().trim();
4.
Write one if condition with equal or contains
and make that as variable so that we can pass any value
if (value.equals("Billing"))
{
5.
Once We select the required value always try to
break the loop so that u can decrease the time for completing test cases
otherwise what if it has 100/1000 values and you’re just selecting some 1 or 10
element and it will keep running for 100/1000 elements
valuesList.get(i).click();
break;
}
WebDriver driver = null;
WebDriverWait wait = null;
private By howCanWeHelpDropDown=By.xpath("//label[contains(text(),'How
can we help')]/following-sibling::div//input[@placeholder='Select an
Option']");
private By howCanWeHelpDropDownList=By.xpath("//input[@placeholder='Select
an Option']/../following-sibling::div//span[@class='slds-truncate']");
@BeforeTest
public void openBrowser() {
WebDriverManager.chromedriver().version("83").setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://indeed.force.com/employerSupport1/s/contactsupport");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 30);
}
@Test
public void selectValueFromBootStrapDropDown() throws InterruptedException {
wait.until(ExpectedConditions.elementToBeClickable(howCanWeHelpDropDown));
driver.findElement(howCanWeHelpDropDown).click();
List<WebElement> valuesList = driver.findElements(howCanWeHelpDropDownList);
for (int i = 0; i < valuesList.size(); i++) {
String value = valuesList.get(i).getText().trim();
System.out.println(value);
if (value.equals("Billing"))
{
valuesList.get(i).click();
break;
}
}
}
So here we can even make this as method and just cal this
where is required by just passing the value to be selected and if its multi
select then we can take the inputs with some separator
(“Value1”;”Value2”,”Value3” or “Value 1”,”Value 2”,”Value 3”), this will avoid
hard coding of selection and even if we keep changing the inputs then it will
never break nothing to modify until unless there is locator changes
Problem: How to verify the dropdown values is showing in sorted order
(Alphabetical Ascending or Descending Order)
1.
We need to extract all the values from the drop
down then add that into one Array List (as we know AL maintain insertion so u
can ensure the displayed order of the elements are not changed after adding
into list)
List<String>actualList=new ArrayList<String>();
wait.until(ExpectedConditions.elementToBeClickable(howCanWeHelpDropDown));
driver.findElement(howCanWeHelpDropDown).click();
List<WebElement> valuesList = driver.findElements(howCanWeHelpDropDownList);
for (int i = 0; i < valuesList.size(); i++) {
actualList.add(valuesList.get(i).getText().trim());
}
2.
Take one more input list (pass from XL or
directly in Test case however your feeding Test data to scripts) and add that
into Array or List then Sort it using Collections inbuilt method
Collections.sort(expectedList): - This will always sort
in Alphabetical Ascending order
Collections.reverse(expectedList): - This
will just reverse the order of
Elements stored in the List, as we
already sorted above this will give the Descending
order
Note: -
Collections.reverse(expectedList) this will never do sorting in de
Descending order just reverse the order
String inputArray[]= {"Account
Verification","Advertise
on Indeed","Contact
Sales","Disabled
Account","Job
Postings","Resume
Search","Password
Reset","Billing"};
Or
String inputArray[]=inputFromXl.split(“;”);
List<String>expectedList=Arrays.asList(inputArray);
Collections.sort(expectedList);
3.
Now as most of them are using either TestNG or
Junit so let’s use the Assertion methods which will take List<String> as
inputs
Here I’m using Assert and we can even use
SoftAssert as well and its up to you guys
Assert.assertEquals(actualList, expectedList, "Values are not Sorted in
Aplhabetical Ascending order");
Here if
the will compare both the lists one by one value and if it finds any value at
any index is not matching then Assertion will fail so that we can easily come
to know some value is missing from drop down or diff one is added or it’s not
sorted correctly.
Below is the example if it fails: -
java.lang.AssertionError:
Values are not Sorted in Alphabetical Ascending order: Lists differ at element
[2]: Contact Sales != Billing expected [Contact Sales] but found [Billing]
4.
If we want to verify list is sorted in
Descending order or not just use the below lines, it will work
List<String>expectedList=Arrays.asList(inputArray);
Collections.sort(expectedList);
Collections.reverse(expectedList);
Assert.assertEquals(actualList, expectedList, "Values are not Sorted in
Alphabetical Descending order");
Notes: -
1. Here I
have not covered if we have some drop downs which are sorted with diff logic
for that we might need to write custom sort then compare it or expected input
should be always on the way drop down is listed
2. If the
dropdown values is dynamic this method will not work as our input might be
static so it will always fail but its best way to handle some static data
(Country, State or Currency … etc)
Code fo above
package selenium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ValidateDropDownValues {
WebDriver driver = null;
WebDriverWait wait = null;
private By howCanWeHelpDropDown =
By.xpath("//label[contains(text(),'How can we
help')]/following-sibling::div//input[@placeholder='Select an Option']");
private By howCanWeHelpDropDownList =
By.xpath("//input[@placeholder='Select an
Option']/../following-sibling::div//span[@class='slds-truncate']");
@BeforeTest
public void openBrwoser() {
WebDriverManager.chromedriver().version("83").setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://indeed.force.com/employerSupport1/s/contactsupport");
driver.manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 30);
}
@Test
public void selectValueFromBootStrapDropDown() throws
InterruptedException {
List<String> actualList = new
ArrayList<String>();
wait.until(ExpectedConditions.elementToBeClickable(howCanWeHelpDropDown));
driver.findElement(howCanWeHelpDropDown).click();
List<WebElement> valuesList =
driver.findElements(howCanWeHelpDropDownList);
for (int i = 0; i < valuesList.size(); i++) {
actualList.add(valuesList.get(i).getText().trim());
}
System.out.println("Actual List:-" + actualList);
String inputArray[] = { "Account Verification",
"Advertise on Indeed", "Contact Sales", "Disabled
Account","Job Postings", "Resume Search",
"Password Reset", "Billing" };
List<String> expectedList = Arrays.asList(inputArray);
System.out.println("Expected List:-" + expectedList);
Collections.sort(expectedList);
Assert.assertEquals(actualList, expectedList, "Values
are not Sorted in Aplhabetical Ascending order"); }
@AfterTest
public void closeBrwoser() {
driver.quit();
}}
No comments:
Post a Comment