Friday 23 February 2018

How to handle drop down using selenium wrapper automation

Drop down is very common element in automation and there is many to handle drop down.

Selenium wrapper automation is very good approach to handle drop down in automation.

We can perform below actions on drop down.

  • Select option using text
  • Select option using index
  • Select option using value
  • Get all options

I have created one class 'DropDownFunction' and which contains all the above methods. You can use all methods with object of that class.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class DropDownFunction 
{ 
 public void mSelectByText(WebDriver driver, WebElement wDropdown, String vText)
 {  
  try
  {
   Select Dropdown = new Select(wDropdown);
   Dropdown.selectByVisibleText(vText);
  }
  catch (Exception E) 
  {
   System.out.println("Error in selecting dropdown option by visible text.");
  }  
 }
 
 public void mSelectByIndex(WebDriver driver, WebElement wDropdown, int vIndex)
 {  
  try
  {
   Select Dropdown = new Select(wDropdown);
   Dropdown.selectByIndex(vIndex);
  }
  catch (Exception E) 
  {
   System.out.println("Error in selecting dropdown option by index.");
  }  
 }
 
 public void mSelectByValue(WebDriver driver, WebElement wDropdown, String vValue)
 {  
  try
  {
   Select Dropdown = new Select(wDropdown);
   Dropdown.selectByValue(vValue);
  }
  catch (Exception E) 
  {
   System.out.println("Error in selecting dropdown option by value.");
  }  
 }
 
 public List<String> mGetAllOptions(WebDriver driver, WebElement wDropdown)
 {   
  List<String> Option = new ArrayList<String>(); 
  
  try
  {
   Select Dropdown = new Select(wDropdown);
   
   List<WebElement> temp = Dropdown.getOptions();
   
   for(int i=0;i<temp.size(); i++)
   {
    Option.add(temp.get(i).getText());
   }
   
  }
  catch(Exception E)
  {
   System.out.println("Error in getting options from dropdown");   
  }  
  return Option;
 } 
}

No comments:

Post a Comment

Popular