1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.ui.po.editor

File UserPicker.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
51
26
2
321
164
27
0.53
1.96
13
1.04

Classes

Class Line # Actions
UserPicker 39 39 0% 18 0
1.0100%
UserPicker.UserElement 41 12 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.ui.po.editor;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.openqa.selenium.By;
27    import org.openqa.selenium.WebDriver;
28    import org.openqa.selenium.WebElement;
29    import org.openqa.selenium.interactions.Actions;
30    import org.openqa.selenium.support.ui.ExpectedCondition;
31    import org.xwiki.test.ui.po.BaseElement;
32   
33    /**
34    * Represents the actions possible on the user picker.
35    *
36    * @version $Id: 7e2c4c7b06ee2aff11673552913d5d2d1152b5a9 $
37    * @since 4.5
38    */
 
39    public class UserPicker extends BaseElement
40    {
 
41    public static class UserElement extends BaseElement
42    {
43    /**
44    * The element that wraps the user display.
45    */
46    private WebElement container;
47   
48    /**
49    * Creates a new user element that wraps the given {@link WebElement}.
50    *
51    * @param container the element that wraps the user display
52    */
 
53  33 toggle public UserElement(WebElement container)
54    {
55  33 this.container = container;
56    }
57   
58    /**
59    * @return the avatar image
60    */
 
61  19 toggle public WebElement getAvatar()
62    {
63  19 return getDriver().findElementWithoutWaiting(container, By.className("icon"));
64    }
65   
66    /**
67    * @return the full user name
68    */
 
69  20 toggle public String getName()
70    {
71  20 String name = getDriver().findElementWithoutWaiting(container, By.className("user-name")).getText();
72    // Remove the "x" coming from the delete icon.
73  20 List<WebElement> delete = getDriver().findElementsWithoutWaiting(container, By.className("delete-tool"));
74  20 if (delete.size() > 0) {
75  14 name = StringUtils.removeEnd(name, delete.get(0).getText());
76    }
77  20 return name;
78    }
79   
80    /**
81    * @return the user alias
82    */
 
83  19 toggle public String getAlias()
84    {
85  19 return getDriver().findElementWithoutWaiting(container, By.className("user-alias")).getText();
86    }
87   
88    /**
89    * @return the text displayed by this element; this is useful when this element is not actually an user but a
90    * message like "User not found"
91    */
 
92  1 toggle public String getText()
93    {
94  1 return container.getText();
95    }
96   
97    /**
98    * Remove this user from the current selection.
99    */
 
100  3 toggle public void delete()
101    {
102  3 new Actions(getDriver()).moveToElement(container).click(getDriver().findElementWithoutWaiting(container,
103    By.className("delete-tool"))).perform();
104    }
105   
106    /**
107    * Moves the mouse over this element.
108    */
 
109  1 toggle public void moveMouseOver()
110    {
111  1 new Actions(getDriver()).moveToElement(container).perform();
112    }
113   
114    /**
115    * Moves this user before the given user using drag and drop.
116    *
117    * @param user the reference user
118    */
 
119  1 toggle public void moveBefore(UserElement user)
120    {
121  1 new Actions(getDriver()).clickAndHold(container).moveToElement(user.container, 0, 0).release().perform();
122    }
123    }
124   
125    /**
126    * The text input that is enhanced with a user picker.
127    */
128    private final WebElement textInput;
129   
130    /**
131    * Exposes the user picker bound to the given text input;
132    *
133    * @param textInput the text input that is enhanced with a user picker
134    */
 
135  19 toggle public UserPicker(WebElement textInput)
136    {
137  19 this.textInput = textInput;
138    }
139   
140    /**
141    * Types into the text input.
142    *
143    * @param keysToSend the keys to type into the text input
144    * @return this
145    */
 
146  34 toggle public UserPicker sendKeys(CharSequence... keysToSend)
147    {
148  34 textInput.sendKeys(keysToSend);
149  34 return this;
150    }
151   
152    /**
153    * Clears the content of the text input.
154    *
155    * @return this
156    */
 
157  3 toggle public UserPicker clear()
158    {
159  3 textInput.clear();
160  3 return this;
161    }
162   
163    /**
164    * @return the value of the text input
165    */
 
166  10 toggle public String getValue()
167    {
168  10 return textInput.getAttribute("value");
169    }
170   
171    /**
172    * @return the link element used to clear the list of selected users
173    */
 
174  10 toggle public WebElement getClearSelectionLink()
175    {
176  10 String xpath = "preceding-sibling::a[@class = 'clear-tool' and position() = last()]";
177  10 return getDriver().findElementWithoutWaiting(textInput, By.xpath(xpath));
178    }
179   
180    /**
181    * Waits until the suggestions for the current value of the text input are retrieved.
182    *
183    * @return this
184    */
 
185  24 toggle public UserPicker waitForSuggestions()
186    {
187  24 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
188    {
 
189  67 toggle public Boolean apply(WebDriver driver)
190    {
191  67 return !textInput.getAttribute("class").contains("loading") && isSuggestListDisplayed(driver);
192    }
193   
 
194  67 toggle private boolean isSuggestListDisplayed(WebDriver driver)
195    {
196  67 WebElement suggestItems = driver.findElement(By.className("suggestItems"));
197    // div.suggestItems is added to the document before the suggestions are retrieved. We need to wait for
198    // the actual list of suggestions (ul.suggestList).
199  47 WebElement suggestList = suggestItems.findElement(By.className("suggestList"));
200    // div.suggestItems fades when closing.
201  24 return suggestList.isDisplayed() && Double.parseDouble(suggestItems.getCssValue("opacity")) == 1;
202    }
203    });
204  23 return this;
205    }
206   
207    /**
208    * Waits until the list of suggestions disappears.
209    *
210    * @param timeout how long to wait, in seconds
211    * @return this
212    */
 
213  14 toggle private UserPicker waitForSuggestionsToDisappear(int timeout)
214    {
215  14 int previousTimeout = getDriver().getTimeout();
216  14 getDriver().setTimeout(timeout);
217  14 try {
218  14 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
219    {
 
220  157 toggle public Boolean apply(WebDriver driver)
221    {
222  157 return driver.findElements(By.className("suggestItems")).size() == 0;
223    }
224    });
225    } finally {
226  14 getDriver().setTimeout(previousTimeout);
227    }
228  13 return this;
229    }
230   
231    /**
232    * Waits for the list of suggestions to fade out.
233    *
234    * @return this
235    */
 
236  10 toggle public UserPicker waitForSuggestionsToFadeOut()
237    {
238  10 return waitForSuggestionsToDisappear(2);
239    }
240   
241    /**
242    * Waits for the list of suggestions to disappear.
243    *
244    * @return this
245    */
 
246  4 toggle public UserPicker waitForSuggestionsToDisappear()
247    {
248  4 return waitForSuggestionsToDisappear(35);
249    }
250   
251    /**
252    * Clicks on the suggestion that contains the given text.
253    *
254    * @param userNameOrAlias user name or alias
255    * @return this
256    */
 
257  5 toggle public UserPicker select(String userNameOrAlias)
258    {
259  5 getDriver().findElementWithoutWaiting(
260    By.xpath("//ul[@class = 'xlist suggestList']/li[contains(., '" + userNameOrAlias + "')]")).click();
261  5 return this;
262    }
263   
264    /**
265    * @return the list of suggested users based on the value of the text input
266    */
 
267  7 toggle public List<UserElement> getSuggestions()
268    {
269  7 List<UserElement> suggestions = new ArrayList<UserElement>();
270  7 for (WebElement item : getDriver().findElementsWithoutWaiting(
271    By.xpath("//ul[@class = 'xlist suggestList']/li")))
272    {
273  13 suggestions.add(new UserElement(item));
274    }
275  7 return suggestions;
276    }
277   
278    /**
279    * @return the list of selected users
280    */
 
281  15 toggle public List<UserElement> getAcceptedSuggestions()
282    {
283  15 List<UserElement> acceptedSuggestions = new ArrayList<UserElement>();
284  15 for (WebElement item : getDriver().findElementsWithoutWaiting(textInput,
285    By.xpath("preceding-sibling::ul[contains(@class, 'accepted-suggestions') and position() = last()]/li")))
286    {
287  19 acceptedSuggestions.add(new UserElement(item));
288    }
289  15 return acceptedSuggestions;
290    }
291   
292    /**
293    * Moves the mouse over the text input.
294    *
295    * @return this
296    */
 
297  2 toggle public UserPicker moveMouseOver()
298    {
299  2 new Actions(getDriver()).moveToElement(textInput).perform();
300  2 return this;
301    }
302   
303    /**
304    * Waits for the user picker to load. You need to call this when the initial selection is not empty (because the
305    * user picker makes separate requests to retrieve information about the selected users).
306    *
307    * @return this
308    */
 
309  11 toggle public UserPicker waitToLoad()
310    {
311  11 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
312    {
 
313  11 toggle public Boolean apply(WebDriver driver)
314    {
315  11 String classNames = textInput.getAttribute("class");
316  11 return classNames.contains("initialized") && !classNames.contains("loading");
317    }
318    });
319  11 return this;
320    }
321    }