View Javadoc

1   package com.atlassian.pageobjects.elements;
2   
3   import com.atlassian.pageobjects.elements.timeout.TimeoutType;
4   import org.openqa.selenium.By;
5   import org.openqa.selenium.WebElement;
6   import org.openqa.selenium.support.ui.Select;
7   
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  /**
12   * Implementation of {@link MultiSelectElement}.
13   *
14   */
15  public class WebDriverMultiSelectElement extends WebDriverElement implements MultiSelectElement
16  {
17      public WebDriverMultiSelectElement(By locator)
18      {
19          super(locator);
20      }
21  
22      public WebDriverMultiSelectElement(By locator, TimeoutType defaultTimeout)
23      {
24          super(locator, defaultTimeout);
25      }
26  
27      public WebDriverMultiSelectElement(By locator, WebDriverLocatable parent)
28      {
29          super(locator, parent);
30      }
31  
32      public WebDriverMultiSelectElement(WebDriverLocatable locatable, TimeoutType timeoutType)
33      {
34          super(locatable, timeoutType);
35      }
36  
37      public WebDriverMultiSelectElement(By locator, WebDriverLocatable parent, TimeoutType timeoutType)
38      {
39          super(locator, parent, timeoutType);
40      }
41  
42      private Option buildOption(WebElement option)
43      {
44          return Options.full(
45              option.getAttribute("id"),
46              option.getAttribute("value"),
47              option.getText());
48      }
49  
50      public List<Option> getAllOptions()
51      {
52          List<Option> optionList = new ArrayList<Option>();
53  
54          for(WebElement option: new Select(waitForWebElement()).getOptions())
55          {
56              optionList.add(buildOption(option));
57          }
58  
59          return optionList;
60      }
61  
62  
63      public List<Option> getSelected()
64      {
65          List<Option> selectedOptions = new ArrayList<Option>();
66  
67          for(WebElement option: new Select(waitForWebElement()).getAllSelectedOptions())
68          {
69              selectedOptions.add(buildOption(option));
70          }
71  
72          return selectedOptions;
73      }
74  
75      public MultiSelectElement select(Option option)
76      {
77          for(WebElement currentOption: new Select(waitForWebElement()).getOptions())
78          {
79              if(option.equals(buildOption(currentOption)))
80              {
81                  if(!currentOption.isSelected()) {
82                      currentOption.click();
83                  }
84                  break;
85              }
86          }
87  
88          return this;
89      }
90  
91      public MultiSelectElement unselect(Option option)
92      {
93          for(WebElement currentOption: new Select(waitForWebElement()).getOptions())
94          {
95              if(option.equals(buildOption(currentOption)))
96              {
97                  if(currentOption.isSelected())
98                  {
99                      currentOption.click();
100                 }
101                 break;
102             }
103         }
104 
105         return this;
106     }
107 
108     public MultiSelectElement selectAll()
109     {
110         for(WebElement option: new Select(waitForWebElement()).getOptions())
111         {
112             if(!option.isSelected())
113             {
114                 option.click();
115             }
116         }
117 
118         return null;
119     }
120 
121     public MultiSelectElement unselectAll()
122     {
123         new Select(waitForWebElement()).deselectAll();
124         return this;
125     }
126 }