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
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 currentOption.setSelected();
82 break;
83 }
84 }
85
86 return this;
87 }
88
89 public MultiSelectElement unselect(Option option)
90 {
91 for(WebElement currentOption: new Select(waitForWebElement()).getOptions())
92 {
93 if(option.equals(buildOption(currentOption)))
94 {
95 if(currentOption.isSelected())
96 {
97 currentOption.toggle();
98 }
99 break;
100 }
101 }
102
103 return this;
104 }
105
106 public MultiSelectElement selectAll()
107 {
108 for(WebElement option: new Select(waitForWebElement()).getOptions())
109 {
110 if(!option.isSelected())
111 {
112 option.setSelected();
113 }
114 }
115
116 return null;
117 }
118
119 public MultiSelectElement unselectAll()
120 {
121 new Select(waitForWebElement()).deselectAll();
122 return this;
123 }
124 }