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

File DocumentPicker.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

2
40
19
1
214
125
24
0.6
2.11
19
1.26

Classes

Class Line # Actions
DocumentPicker 37 40 0% 24 14
0.770491877%
 

Contributing tests

This file is covered by 20 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;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.openqa.selenium.By;
26    import org.openqa.selenium.WebDriver;
27    import org.openqa.selenium.WebDriverException;
28    import org.openqa.selenium.WebElement;
29    import org.openqa.selenium.support.ui.ExpectedCondition;
30   
31    /**
32    * Represents the Document Picker.
33    *
34    * @version $Id: 4d4983ed094501b8e16b82133f635d45cc8b8092 $
35    * @since 7.2M3
36    */
 
37    public class DocumentPicker extends BaseElement
38    {
39    protected WebElement container;
40   
41    /**
42    * Use this constructor only if there is only one document picker on the page.
43    */
 
44  15 toggle public DocumentPicker()
45    {
46  15 this.container = getDriver().findElementByClassName("location-picker");
47    }
48   
 
49  25 toggle public DocumentPicker(WebElement container)
50    {
51  25 this.container = container;
52    }
53   
 
54  17 toggle public String getTitle()
55    {
56  17 return getTitleInput().getAttribute("value");
57    }
58   
 
59  29 toggle public DocumentPicker setTitle(String title)
60    {
61  29 getDriver().setTextInputValue(getTitleInput(), title);
62  29 return this;
63    }
64   
 
65  51 toggle public WebElement getTitleInput()
66    {
67  51 return this.container.findElement(By.className("location-title-field"));
68    }
69   
 
70  19 toggle public BreadcrumbElement getLocation()
71    {
72  19 return new BreadcrumbElement(this.container.findElement(By.className("breadcrumb")));
73    }
74   
 
75  14 toggle public DocumentPicker toggleLocationAdvancedEdit()
76    {
77  14 this.container.findElement(By.className("location-action-edit")).click();
78  14 return this;
79    }
80   
 
81  1 toggle public String getParent()
82    {
83  1 return getParentInput().getAttribute("value");
84    }
85   
 
86  28 toggle public DocumentPicker setParent(String parent)
87    {
88  28 return setAdvancedField(getParentInput(), parent);
89    }
90   
 
91  30 toggle public WebElement getParentInput()
92    {
93  30 return this.container.findElement(By.className("location-parent-field"));
94    }
95   
 
96  3 toggle public String getName()
97    {
98  3 return getNameInput().getAttribute("value");
99    }
100   
 
101  17 toggle public DocumentPicker setName(String name)
102    {
103  17 return setAdvancedField(getNameInput(), name);
104    }
105   
 
106  20 toggle public WebElement getNameInput()
107    {
108  20 return this.container.findElement(By.className("location-name-field"));
109    }
110   
 
111  45 toggle private DocumentPicker setAdvancedField(WebElement field, String value)
112    {
113  45 if (!field.isDisplayed()) {
114  1 toggleLocationAdvancedEdit();
115    }
116  45 getDriver().setTextInputValue(field, value);
117  45 return this;
118    }
119   
120    /**
121    * Clicks the "pick document" button that triggers a modal pop-up to be displayed.
122    * <p>
123    * The caller is responsible for handling the modal (or instantiating the right page object element), such we limit
124    * the extra coupling that would be required from the test framework if it were to instantiate and return the page
125    * object for the modal pop-up.
126    */
 
127  2 toggle public void browseDocuments()
128    {
129  2 this.container.findElement(By.className("location-action-pick")).click();
130    }
131   
132    /**
133    * Wait for the Breadcrumb to display the passed path string and throw an exception if the timeout is reached. Note
134    * that we need to wait since the Breadcrumb is udated live and asserting its content without waiting would lead to
135    * false positives.
136    * <p>
137    * Note: This method can not be implemented inside {@link BreadcrumbElement} because a change of parent replaces
138    * completely the {@link BreadcrumbElement}'s container and thus it becomes stale. To avoid that, at each wait
139    * iteration, we lookup the current breadcrumb element and not a cached one.
140    *
141    * @param expectedPathString the path string to wait for
142    * @since 7.2RC1
143    */
 
144  0 toggle public void waitForLocation(final String expectedPathString)
145    {
146    // TODO: Ugly hack. Would need to find a better solution
147  0 final StringBuilder currentValue = new StringBuilder();
148   
149  0 try {
150  0 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
151    {
 
152  0 toggle @Override
153    public Boolean apply(WebDriver driver)
154    {
155  0 try {
156  0 String value = getLocation().getPathAsString();
157   
158  0 currentValue.setLength(0);
159  0 currentValue.append(value);
160   
161  0 return expectedPathString.equals(value);
162    } catch (Exception e) {
163  0 return false;
164    }
165    }
166    });
167    } catch (WebDriverException e) {
168    // Display a nicer error message than would be displayed otherwise
169  0 throw new WebDriverException(String.format("Found [%s], was expecting [%s]", currentValue.toString(),
170    expectedPathString), e);
171    }
172    }
173   
174    /**
175    * Wait for the Breadcrumb to display the passed path and throw an exception if the timeout is reached. Note that we
176    * need to wait since the Breadcrumb is udated live and asserting its content without waiting would lead to false
177    * positives.
178    * <p>
179    * Note: This method can not be implemented inside {@link BreadcrumbElement} because a change of parent replaces
180    * completely the {@link BreadcrumbElement}'s container and thus it becomes stale. To avoid that, at each wait
181    * iteration, we lookup the current breadcrumb element and not a cached one.
182    *
183    * @param expectedPath the path to wait for
184    * @since 7.2RC1
185    */
 
186  4 toggle public void waitForLocation(final List<String> expectedPath)
187    {
188    // TODO: Ugly hack. Would need to find a better solution
189  4 final List<String> currentPath = new ArrayList<String>();
190   
191  4 try {
192  4 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
193    {
 
194  7 toggle @Override
195    public Boolean apply(WebDriver driver)
196    {
197  7 try {
198  7 List<String> path = getLocation().getPath();
199   
200  7 currentPath.clear();
201  7 currentPath.addAll(path);
202   
203  7 return expectedPath.equals(path);
204    } catch (Exception e) {
205  0 return false;
206    }
207    }
208    });
209    } catch (WebDriverException e) {
210    // Display a nicer error message than would be displayed otherwise
211  0 throw new WebDriverException(String.format("Found %s, was expecting %s", currentPath, expectedPath), e);
212    }
213    }
214    }