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

File CreatePagePage.java

 

Coverage histogram

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

Code metrics

18
66
24
1
313
183
35
0.53
2.75
24
1.46

Classes

Class Line # Actions
CreatePagePage 40 66 0% 35 22
0.796296379.6%
 

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;
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.FindBy;
30    import org.openqa.selenium.support.pagefactory.ByChained;
31    import org.openqa.selenium.support.ui.ExpectedCondition;
32    import org.xwiki.test.ui.po.editor.EditPage;
33   
34    /**
35    * Represents the actions possible on the Create Page template page.
36    *
37    * @version $Id: 7ca54039d0ef9c57717711ea12cc4c5eaf2e1446 $
38    * @since 3.2M3
39    */
 
40    public class CreatePagePage extends ViewPage
41    {
42    /**
43    * The element that contains the document picker used to select the target document.
44    */
45    @FindBy(className = "location-picker")
46    private WebElement documentPickerElement;
47   
48    private DocumentPicker documentPicker;
49   
50    @FindBy(id = "terminal")
51    private WebElement isTerminalCheckbox;
52   
 
53  1 toggle public static CreatePagePage gotoPage()
54    {
55  1 getUtil().gotoPage("Main", "WebHome", "create");
56  1 return new CreatePagePage();
57    }
58   
59    /**
60    * @return the document picker used to select the target document
61    */
 
62  61 toggle public DocumentPicker getDocumentPicker()
63    {
64  61 if (this.documentPicker == null) {
65  17 this.documentPicker = new DocumentPicker(this.documentPickerElement);
66    }
67   
68  61 return this.documentPicker;
69    }
70   
 
71  13 toggle private List<WebElement> getAvailableTemplateInputs()
72    {
73  13 return getDriver().findElementsWithoutWaiting(By.xpath("//input[@name = 'type' and @data-type = 'template']"));
74    }
75   
 
76  0 toggle private List<WebElement> getAvailableTypeInputs()
77    {
78  0 return getDriver().findElementsWithoutWaiting(By.xpath("//input[@name = 'type']"));
79    }
80   
81    /**
82    * @since 3.2M3
83    */
 
84  4 toggle public int getAvailableTemplateSize()
85    {
86  4 return getAvailableTemplateInputs().size();
87    }
88   
 
89  4 toggle public List<String> getAvailableTemplates()
90    {
91  4 List<String> availableTemplates = new ArrayList<String>();
92  4 List<WebElement> templateInputs = getAvailableTemplateInputs();
93  4 for (WebElement input : templateInputs) {
94  3 if (input.getAttribute("value").length() > 0) {
95  3 availableTemplates.add(input.getAttribute("value"));
96    }
97    }
98   
99  4 return availableTemplates;
100    }
101   
 
102  5 toggle public void setTemplate(String template)
103    {
104  5 List<WebElement> templates = getAvailableTemplateInputs();
105  5 for (WebElement templateInput : templates) {
106  5 if (templateInput.getAttribute("value").equals(template)) {
107    // Get the label corresponding to the input so we can click on it
108  5 WebElement label =
109    getDriver().findElementWithoutWaiting(
110    By.xpath("//label[@for = '" + templateInput.getAttribute("id") + "']"));
111  5 label.click();
112  5 return;
113    }
114    }
115  0 throw new RuntimeException("Failed to find template [" + template + "]");
116    }
117   
 
118  0 toggle public void setType(String type)
119    {
120  0 List<WebElement> types = getAvailableTypeInputs();
121  0 for (WebElement typeInput : types) {
122  0 if (typeInput.getAttribute("value").equals(type)) {
123    // Get the label corresponding to the input so we can click on it
124  0 WebElement label =
125    getDriver().findElementWithoutWaiting(
126    By.xpath("//label[@for = '" + typeInput.getAttribute("id") + "']"));
127  0 label.click();
128  0 return;
129    }
130    }
131  0 throw new RuntimeException("Failed to find type [" + type + "]");
132    }
133   
 
134  26 toggle public void clickCreate()
135    {
136    // Submit the create form. Don`t use the DocumentPicker element since it might not always be there.
137  26 getDriver().findElementWithoutWaiting(By.id("create")).submit();
138    }
139   
 
140  2 toggle public EditPage createPage(String spaceValue, String pageValue)
141    {
142  2 return createPage(spaceValue, pageValue, false);
143    }
144   
 
145  3 toggle public EditPage createPage(String spaceValue, String pageValue, boolean isTerminalPage)
146    {
147  3 return createPage(null, spaceValue, pageValue, isTerminalPage);
148    }
149   
150    /**
151    * @since 7.2M3
152    */
 
153  10 toggle public EditPage createPage(String title, String spaceValue, String pageValue, boolean isTerminalPage)
154    {
155  10 fillForm(title, spaceValue, pageValue, isTerminalPage);
156  10 clickCreate();
157  10 return new EditPage();
158    }
159   
 
160  1 toggle public EditPage createPageFromTemplate(String spaceValue, String pageValue, String templateValue)
161    {
162  1 return createPageFromTemplate(null, spaceValue, pageValue, templateValue);
163    }
164   
 
165  2 toggle public EditPage createPageFromTemplate(String title, String spaceValue, String pageValue, String templateValue)
166    {
167  2 return createPageFromTemplate(title, spaceValue, pageValue, templateValue, false);
168    }
169   
170    /**
171    * @since 7.2M1
172    */
 
173  0 toggle public EditPage createPageFromTemplate(String spaceValue, String pageValue, String templateValue,
174    boolean isTerminalPage)
175    {
176  0 return createPageFromTemplate(null, spaceValue, pageValue, templateValue, isTerminalPage);
177    }
178   
179    /**
180    * @since 7.2M3
181    */
 
182  2 toggle public EditPage createPageFromTemplate(String title, String spaceValue, String pageValue, String templateValue,
183    boolean isTerminalPage)
184    {
185  2 fillForm(title, spaceValue, pageValue, isTerminalPage);
186  2 setTemplate(templateValue);
187  2 clickCreate();
188  2 return new EditPage();
189    }
190   
191    /**
192    * @param title document title, ignored if {@code null}
193    * @param spaceReference document's space reference (parent nested document), ignored if {@code null}
194    * @param pageName document's name (space name or page name, depending if terminal or not), ignored if {@code null}
195    * @param isTerminalPage true if the new document is terminal, false for non-terminal
196    * @since public since 7.4M2
197    */
 
198  13 toggle public void fillForm(String title, String spaceReference, String pageName, boolean isTerminalPage)
199    {
200  13 if (title != null) {
201  9 getDocumentPicker().setTitle(title);
202    }
203   
204  13 if (spaceReference != null) {
205  12 getDocumentPicker().setParent(spaceReference);
206    }
207   
208  13 if (pageName != null) {
209  4 getDocumentPicker().setName(pageName);
210    }
211   
212    // Since the default is to not create terminal pages, only set this if the user is asking to create a terminal
213    // page. This allows this API to work when using isTerminalPage = false even for simpler users which don't get
214    // to see the Terminal option.
215  13 if (isTerminalPage) {
216  1 setTerminalPage(isTerminalPage);
217    }
218    }
219   
220    /**
221    * Waits for a global error message in the page.
222    *
223    * @since 3.2M3
224    */
 
225  3 toggle public void waitForErrorMessage()
226    {
227  3 getDriver().waitUntilElementIsVisible(By.className("errormessage"));
228    }
229   
230    /**
231    * Waits for a validation error in a field.
232    *
233    * @since 3.2M3
234    */
 
235  1 toggle public void waitForFieldErrorMessage()
236    {
237  1 getDriver().waitUntilElementIsVisible(new ByChained(By.className("LV_invalid")));
238    }
239   
240    /**
241    * @return true if the page to create should be a terminal page, false otherwise
242    * @since 7.2M1
243    */
 
244  0 toggle public boolean isTerminalPage()
245    {
246  0 return this.isTerminalCheckbox.isSelected();
247    }
248   
249    /**
250    * @param isTerminalPage true if the page to create is terminal, false otherwise
251    * @since 7.2M1
252    */
 
253  2 toggle public void setTerminalPage(boolean isTerminalPage)
254    {
255  2 if (isTerminalPage != this.isTerminalCheckbox.isSelected()) {
256  2 this.isTerminalCheckbox.click();
257    }
258    }
259   
260    /**
261    * @return true if the choice between terminal or non-terminal document is displayed, false otherwise.
262    * @since 7.2M3
263    */
 
264  1 toggle public boolean isTerminalOptionDisplayed()
265    {
266  1 List<WebElement> elements = getDriver().findElementsWithoutWaiting(By.id("terminal"));
267  1 return elements.size() > 0 && elements.get(0).isDisplayed();
268    }
269   
270    /**
271    * Wait for the location preview to display the passed path string and throw an exception if the timeout is reached.
272    * Note that we need to wait since the Breadcrumb is udated live and asserting its content without waiting would
273    * lead to false positives.
274    * <p>
275    * Note: This method can not be implemented inside {@link BreadcrumbElement} because a change of parent replaces
276    * completely the {@link BreadcrumbElement}'s container and thus it becomes stale. To avoid that, at each wait
277    * iteration, we lookup the current breadcrumb element and not a cached one.
278    * <p>
279    * TODO: Reuse {@link DocumentPicker} inside this PO instead of duplicating this method.
280    *
281    * @param expectedPathString the path string to wait for
282    * @since 7.2RC1
283    */
 
284  7 toggle public void waitForLocationPreviewContent(final String expectedPathString)
285    {
286    // TODO: Ugly hack. Would need to find a better solution
287  7 final StringBuilder currentValue = new StringBuilder();
288   
289  7 try {
290  7 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
291    {
 
292  10 toggle @Override
293    public Boolean apply(WebDriver driver)
294    {
295  10 try {
296  10 String value = getDocumentPicker().getLocation().getPathAsString();
297   
298  10 currentValue.setLength(0);
299  10 currentValue.append(value);
300   
301  10 return expectedPathString.equals(value);
302    } catch (Exception e) {
303  0 return false;
304    }
305    }
306    });
307    } catch (WebDriverException e) {
308    // Display a nicer error message than would be displayed otherwise
309  0 throw new WebDriverException(String.format("Found [%s], was expecting [%s]", currentValue.toString(),
310    expectedPathString), e);
311    }
312    }
313    }