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      private Option buildOption(WebElement option)
42      {
43          return Options.full(
44              option.getAttribute("id"),
45              option.getAttribute("value"),
46              option.getText());
47      }
48  
49      public List<Option> getAllOptions()
50      {
51          List<Option> optionList = new ArrayList<Option>();
52  
53          for(WebElement option: new Select(waitForWebElement()).getOptions())
54          {
55              optionList.add(buildOption(option));
56          }
57  
58          return optionList;
59      }
60  
61      public Option getSelected()
62      {
63          return buildOption(new Select(waitForWebElement()).getFirstSelectedOption());
64      }
65  
66      public SelectElement select(Option option)
67      {
68          for(WebElement currentOption: new Select(waitForWebElement()).getOptions())
69          {
70              if(option.equals(buildOption(currentOption)))
71              {
72                  currentOption.setSelected();
73                  break;
74              }
75          }
76  
77          return this;
78      }
79  }