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 SelectElement}
13   */
14  public class WebDriverSelectElement extends WebDriverElement implements SelectElement
15  {
16       public WebDriverSelectElement(By locator)
17      {
18          super(locator);
19      }
20  
21      public WebDriverSelectElement(By locator, TimeoutType defaultTimeout)
22      {
23          super(locator, defaultTimeout);
24      }
25  
26      public WebDriverSelectElement(WebDriverLocatable locatable, TimeoutType timeoutType)
27      {
28          super(locatable, timeoutType);
29      }
30  
31      public WebDriverSelectElement(By locator, WebDriverLocatable parent)
32      {    
33          super(locator, parent);
34      }
35  
36      public WebDriverSelectElement(By locator, WebDriverLocatable parent, TimeoutType timeoutType)
37      {
38          super(locator, parent, timeoutType);
39      }
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      public Option getSelected()
63      {
64          return buildOption(new Select(waitForWebElement()).getFirstSelectedOption());
65      }
66  
67      public SelectElement select(Option option)
68      {
69          for(WebElement currentOption: new Select(waitForWebElement()).getOptions())
70          {
71              if(option.equals(buildOption(currentOption)))
72              {
73                  if (!currentOption.isSelected()) {
74                      currentOption.click();
75                  }
76                  break;
77              }
78          }
79  
80          return this;
81      }
82  }