Working with Select examples

As we have seen earlier WebDriver’s support classes called “Select”, which provides useful methods for interacting with select options. User can perform operations on a select dropdown and also de-select operation using select methods.

We will now show you how to work with select methods using examples tests. We have created a common resuables which can be used across the tests.

Below is the sample test with three test methods. Each with a different example.

1. test_ToSelectByVisibleText - This test will select a value in the dropdown which is visible and verify if that is displayed as selected in the dropdown.

2. test_ToSelectByIndexValue - This test will select a value in the dropdown based on the index value and verify is selected correctly. Here index is the order which the values are displayed in order. if you want to select the first value, we need to provide index value as '1' and so on as per our needs.

3. testCase_ToSelectByValue - This test will select the value in the dropdown based on the value. Here value is different then the visible value in the dropdown. Value is the one which we need to view the html source. Value is an attribute and every value attribute will have a value which we can pass and select.

4. test_ToCheckValueInDropdown - This test will check the values that are present in the dropdown. Sometimes you may want to check if the values that you are desired to work.

Example for the above scenario : In a simple leave application, the user who is applyig will just have two option 'Apply' and 'Cancel' in the dropdown. But when the approver or manager log in to the application, he should be able to view 'Approve' and 'Reject' options along with the other two. In cases like these we need to verify values based on the login.

The below is the example tests:

@Test(priority=1)
	public void test_ToSelectByVisibleText() {
		WebElement element = driver.findElement(By.name("Basic_Mobiles"));
		//Selecting Nokia from the dropdown using text
		select_Option_In_DropDown_ByVisibleText(element, "Nokia");
		//Verifying if selected value is displaying or not
		Assert.assertEquals("Nokia", getSelectedOption(element), "Selected Value not displaying");
	}
	
	@Test(priority=3)
	public void test_ToSelectByIndexValue() {
		driver.navigate().refresh();
		WebElement element = driver.findElement(By.name("Costly_Mobiles"));
		//Selecting iphone from the dropdown using index value
		select_Option_In_DropDown_ByIndexValue(element, 1);
		//Verifying if selected value is displaying or not
		Assert.assertEquals("iPhone", getSelectedOption(element), "Selected Value not displaying");
	}
	
	@Test(priority=3)
	public void test_ToSelectByValue() {
		driver.navigate().refresh();
		WebElement element = driver.findElement(By.name("China_Mobiles"));
		//Selecting iphone from the dropdown using index value
		select_Option_In_DropDown_ByValue(element, "xiomi");
		//Verifying if selected value is displaying or not
		Assert.assertEquals("Xiomi", getSelectedOption(element), "Selected Value not displaying");
	}
	
	@Test(priority=4)
	public void test_ToCheckValueInDropdown() {
		driver.navigate().refresh();
		WebElement element = driver.findElement(By.name("Mobiles"));
		//Get all the values in dropdown
		List<WebElement> valuesInDropdown = getElementsByTagName(element, "option");
		//Pass the values that you want to check in the dropdown
		String[] checkValues= {"iPhone", "Nokia", "Samsung"};
		Assert.assertTrue(verify_Values_In_Dropdown(valuesInDropdown, checkValues), "Values are not matching with the dropdown list");
	}

The below method is used to get all the list of values in the dropdown. The only thing we need to send is 'WebElement' and 'option' tag name. This again depends on the application that you are working on. We need to make sure and pass the exact element and option. In the below example, the below screen shot (html code) is used.

Here it becomes as WebElement as "WebElement element = driver.findElement(By.name("Mobiles"));" and option name as "option"

public static List<WebElement> getElementsByTagName(WebElement element, String optionName)
	    {
	       List<WebElement> listOfElements = element.findElements(By.tagName(optionName));
	       if(listOfElements.size()!=0)
	          return listOfElements;
	       else
	    	   return null;
	    }

The below method is used to get the value which is selected in the dropdown. If no other value is selected, it will return the default selected value 'Please select'.

	public String get_SelectedOption(WebElement element) {
		Select select = new Select(element);
		WebElement selectedElement = select.getFirstSelectedOption();
		String selectedOption = selectedElement.getText();
		return selectedOption;
	}

Below method is used to select the option in dropdown based on the text that is visible in the dropdown. For this we need to pass two arguments. One is the WebElement and the String value.

	 public static void select_Option_In_DropDown_ByVisibleText(
	            WebElement element, String sVisibleTextOptionToSelect) {
	        try {
	            Select select = new Select(element);
	            select.selectByVisibleText(sVisibleTextOptionToSelect);
	           
	        } catch (NoSuchElementException e) {
	            System.out.println("Option value not find in dropdown");
	        
	        }
	    }

Below method is used to select the option in dropdown based on the value (attribute) which will be present in html source. For this we need to pass two arguments. One is the WebElement and the sting value.

	    public static void select_Option_In_DropDown_ByIndexVal(WebElement element, String sValueTextToSelect) {
	        try {
	            Select select = new Select(element);
	            select.selectByValue("nokia");
	            return true;
	        } catch (NoSuchElementException e) {
	            System.out.println("Value not find in dropdown");
	          }
	    }

Below method is used to select the option in dropdown based on the index value. For this we need to pass two arguments. One is the WebElement and the integer index value.

	public static void select_Option_In_DropDown_ByValue(WebElement element, int indexVal) {
	        try {
	            Select select = new Select(element);
	            select.selectByIndex(indexVal);
	        } catch (NoSuchElementException e) {
	            System.out.println("Option value not find in dropdown");
	        }
	    }

Below method is used to check the values present in the dropdown. Here we need to pass the List of WebElements (List of values in the dropdown) and the String array (Multiple strings that you need to compare with values in the dropdown.

	public static boolean verify_Values_In_Dropdown(List<WebElement> listOfElements, String[] strValues) {
		boolean bValue=false;
		List<String> list = new ArrayList<String>();
		for (String strValue : strValues) {
			boolean bflag = false;
			for (WebElement element : listOfElements) {
				String elementValue = element.getText();
				if (strValue.equals(elementValue)) {
					bflag= true;
				}
			}
			if (!bflag)
				list.add(strValue);
		}
		if (list.size() > 0) {
			for(String strList : list) {
				System.out.println("Value not present in dropdown: "+strList);
			}
			//Assign false if any of the value not found in dropdown
			bValue = false;
		} else {
			//Assign true if all values found in dropdown
			System.out.println("All value(s) found in dropdown");
			bValue=true;
		}
		return bValue;
	}

In the above method, if we provide any values which are not present in the dropdown, it will return false and it will print all the values that are not present in the dropdown. If all the values are matched, it will return true and a message will be displayed as 'All value(s) found in dropdown'.

Selenium Tutorials: 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.