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

File XWikiExplorer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

0
55
36
1
403
191
36
0.65
1.53
36
1

Classes

Class Line # Actions
XWikiExplorer 38 55 0% 36 17
0.813186881.3%
 

Contributing tests

This file is covered by 24 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.wysiwyg.framework;
21   
22    import java.util.List;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.openqa.selenium.By;
26    import org.openqa.selenium.Keys;
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.XWikiWebDriver;
32   
33    /**
34    * Utility class used to write integration tests involving the XWiki Explorer tree.
35    *
36    * @version $Id: 08d6ba557079b82af53b36e7f16c3c0482b8d86d $
37    */
 
38    public class XWikiExplorer
39    {
40    private static final String FINDER_LOCATOR =
41    "//input[@class = 'xtree-finder' and following-sibling::div[contains(@class, 'xExplorer')]]";
42   
43    private static final By SELECTED_NODE = By
44    .xpath("//div[contains(@class, 'xExplorer')]//li[@role = 'treeitem' and @aria-selected = 'true']");
45   
46    private final XWikiWebDriver driver;
47   
48    /**
49    * Creates a new XWikiExplorer that uses the passed web driver.
50    *
51    * @param driver the web driver
52    */
 
53  71 toggle public XWikiExplorer(XWikiWebDriver driver)
54    {
55  71 this.driver = driver;
56    }
57   
58    /**
59    * Open the node that corresponds to the specified space.
60    *
61    * @param spaceName the space name
62    * @return this
63    */
 
64  0 toggle public XWikiExplorer openSpace(String spaceName)
65    {
66  0 return open(getSpaceNodeId(spaceName));
67    }
68   
69    /**
70    * Open the node that corresponds to the specified page.
71    *
72    * @param spaceName the space name
73    * @param pageName the page name
74    * @return this
75    */
 
76  1 toggle public XWikiExplorer openPage(String spaceName, String pageName)
77    {
78  1 return open(getDocumentNodeId(spaceName, pageName));
79    }
80   
81    /**
82    * Open the node that corresponds to the 'Attachments' meta node of the specified page.
83    *
84    * @param spaceName the space name
85    * @param pageName the page name
86    * @return this
87    */
 
88  1 toggle public XWikiExplorer openAttachments(String spaceName, String pageName)
89    {
90  1 return open(getAttachmentsNodeId(spaceName, pageName));
91    }
92   
 
93  2 toggle private XWikiExplorer open(String nodeId)
94    {
95    // Select the node first in order to scroll it into view. Clicking on the toggle to expand the node has no
96    // effect otherwise (Selenium doesn't manage to scroll the node into view when the toggle is clicked).
97  2 select(nodeId);
98   
99  2 String toggleLocatorFormat =
100    "//div[contains(@class, 'xExplorer')]//li[@id = '%s'"
101    + " and @aria-expanded = '%s']/i[contains(@class, 'jstree-ocl')]";
102  2 this.driver.findElementByXPath(String.format(toggleLocatorFormat, nodeId, false)).click();
103  2 this.driver.waitUntilElementIsVisible(By.xpath(String.format(toggleLocatorFormat, nodeId, true)));
104   
105  2 return this;
106    }
107   
108    /**
109    * Select the node that corresponds to the specified space.
110    *
111    * @param spaceName the space name
112    * @return this
113    */
 
114  0 toggle public XWikiExplorer selectSpace(String spaceName)
115    {
116  0 return select(getSpaceNodeId(spaceName));
117    }
118   
119    /**
120    * Select the node that corresponds to the 'New page...' meta node of the specified space.
121    *
122    * @param spaceName the space name
123    * @return this
124    */
 
125  2 toggle public XWikiExplorer selectNewPage(String spaceName)
126    {
127  2 return select(getNewDocumentNodeId(spaceName));
128    }
129   
130    /**
131    * Select the node that corresponds to the specified page.
132    *
133    * @param spaceName the space name
134    * @param pageName the page name
135    * @return this
136    */
 
137  0 toggle public XWikiExplorer selectPage(String spaceName, String pageName)
138    {
139  0 return select(getDocumentNodeId(spaceName, pageName));
140    }
141   
142    /**
143    * Toggles the selection of the specified page by clicking the label of the corresponding node while holding the
144    * Control key down.
145    *
146    * @param spaceName the space name
147    * @param pageName the page name
148    * @return
149    */
 
150  2 toggle public XWikiExplorer togglePageSelection(String spaceName, String pageName)
151    {
152  2 return toggleSelection(getDocumentNodeId(spaceName, pageName));
153    }
154   
155    /**
156    * Select the node that corresponds to the 'Upload file...' meta node of the specified page.
157    *
158    * @param spaceName the space name
159    * @param pageName the page name
160    * @return this
161    */
 
162  1 toggle public XWikiExplorer selectNewAttachment(String spaceName, String pageName)
163    {
164  1 return select(String.format("addAttachment:xwiki:%s.%s", spaceName, pageName));
165    }
166   
167    /**
168    * Select the node that corresponds to the specified attachment.
169    *
170    * @param spaceName the space name
171    * @param pageName the page name
172    * @param fileName the file name
173    * @return this
174    */
 
175  1 toggle public XWikiExplorer selectAttachment(String spaceName, String pageName, String fileName)
176    {
177  1 return select(getAttachmentNodeId(spaceName, pageName, fileName));
178    }
179   
 
180  6 toggle private XWikiExplorer select(String nodeId)
181    {
182  6 this.driver.findElementByXPath(getNodeLocator(nodeId) + "/a[@class = 'jstree-anchor']").click();
183  6 return this;
184    }
185   
 
186  2 toggle private XWikiExplorer toggleSelection(String nodeId)
187    {
188  2 WebElement node =
189    this.driver.findElementByXPath(getNodeLocator(nodeId) + "/a[contains(@class, 'jstree-anchor')]");
190  2 new Actions(this.driver).keyDown(Keys.CONTROL).click(node).keyUp(Keys.CONTROL).perform();
191  2 return this;
192    }
193   
194    /**
195    * @param spaceName the space name
196    * @return {@code true} if the node corresponding to the specified space is present in the tree
197    */
 
198  0 toggle public boolean hasSpace(String spaceName)
199    {
200  0 return hasNode(getSpaceNodeId(spaceName));
201    }
202   
203    /**
204    * @param spaceName the space name
205    * @param pageName the page name
206    * @return {@code true} if the node corresponding to the specified page is present in the tree
207    */
 
208  4 toggle public boolean hasPage(String spaceName, String pageName)
209    {
210  4 return hasNode(getDocumentNodeId(spaceName, pageName));
211    }
212   
213    /**
214    * @param spaceName the space name
215    * @param pageName the page name
216    * @param fileName the file name
217    * @return {@code true} if the node corresponding to the specified attachment is present in the tree
218    */
 
219  0 toggle public boolean hasAttachment(String spaceName, String pageName, String fileName)
220    {
221  0 return hasNode(getAttachmentNodeId(spaceName, pageName, fileName));
222    }
223   
 
224  4 toggle private boolean hasNode(String nodeId)
225    {
226  4 return this.driver.hasElementWithoutWaiting(By.xpath(getNodeLocator(nodeId)));
227    }
228   
229    /**
230    * Types the given space name into the finder and selects the first suggestions that matches the space name.
231    *
232    * @param spaceName the space name
233    * @return this
234    */
 
235  0 toggle public XWikiExplorer findAndSelectSpace(String spaceName)
236    {
237  0 find(spaceName).selectSpace(spaceName);
238  0 return waitForNodeSelected();
239    }
240   
241    /**
242    * Types the given page title into the finder and selects the first suggestions that matches the page title.
243    *
244    * @param pageTitle the page title
245    * @return this
246    */
 
247  16 toggle public XWikiExplorer findAndSelectPage(String pageTitle)
248    {
249  16 find(pageTitle).selectPage(pageTitle);
250  16 return waitForNodeSelected();
251    }
252   
253    /**
254    * Types the given file name into the finder and selects the first suggestions that matches the file name.
255    *
256    * @param fileName the file name
257    * @return this
258    */
 
259  5 toggle public XWikiExplorer findAndSelectAttachment(String fileName)
260    {
261  5 find(fileName).selectAttachment(fileName);
262  5 return waitForNodeSelected();
263    }
264   
265    /**
266    * Type the given text into the finder and wait for suggestions.
267    *
268    * @param text the text to find
269    * @return the suggestions page
270    */
 
271  26 toggle public TreeSuggestionsPane find(String text)
272    {
273  26 WebElement finder = this.driver.findElementByXPath(FINDER_LOCATOR);
274  26 finder.clear();
275  26 finder.sendKeys(text);
276    // Wait for the list of suggestions.
277  26 this.driver.waitUntilElementIsVisible(By
278    .xpath("//div[contains(@class, 'xtree-finder-suggestions')]//ul[contains(@class, 'suggestList')]"));
279  26 return new TreeSuggestionsPane(this.driver);
280    }
281   
282    /**
283    * Wait for the tree to load.
284    *
285    * @return this
286    */
 
287  12 toggle public XWikiExplorer waitForIt()
288    {
289    // The editor attempts to open the tree to a specified node. If the node is found then it is selected. Otherwise
290    // the value of the finder is set to the node id. Thus we wait for a node to be selected or for the finder to
291    // have a non-empty value.
292  12 this.driver.waitUntilCondition(new ExpectedCondition<Boolean>()
293    {
 
294  18 toggle @Override
295    public Boolean apply(WebDriver input)
296    {
297  18 List<WebElement> finders =
298    XWikiExplorer.this.driver.findElementsWithoutWaiting(By.xpath(FINDER_LOCATOR));
299  18 return (!finders.isEmpty() && !StringUtils.isEmpty(finders.get(0).getAttribute("value")))
300    || XWikiExplorer.this.driver.hasElementWithoutWaiting(SELECTED_NODE);
301    }
302    });
303  12 return this;
304    }
305   
306    /**
307    * Waits for the specified page to be selected.
308    *
309    * @param spaceName the name of the space containing the page
310    * @param pageName the name of the page to wait for
311    * @return this
312    */
 
313  16 toggle public XWikiExplorer waitForPageSelected(String spaceName, String pageName)
314    {
315  16 return waitForNodeSelected(getDocumentNodeId(spaceName, pageName));
316    }
317   
318    /**
319    * Waits for the "New page.." node under the specified space to be selected.
320    *
321    * @param spaceName the name of the space where the new page would be created
322    * @return this
323    */
 
324  0 toggle public XWikiExplorer waitForNewPageSelected(String spaceName)
325    {
326  0 return waitForNodeSelected(getNewDocumentNodeId(spaceName));
327    }
328   
329    /**
330    * Waits for the specified attachment to be selected.
331    *
332    * @param spaceName the name of the space containing the page
333    * @param pageName the name of the page who has the attachment
334    * @param fileName the attachment file name
335    * @return this
336    */
 
337  5 toggle public XWikiExplorer waitForAttachmentSelected(String spaceName, String pageName, String fileName)
338    {
339  5 return waitForNodeSelected(getAttachmentNodeId(spaceName, pageName, fileName));
340    }
341   
342    /**
343    * Waits for the attachments node of the specified page to be selected. The attachments node is the parent node for
344    * all the attachments of a page.
345    *
346    * @param spaceName the space containing the page
347    * @param pageName the name of the page whose attachments node is selected
348    * @return this
349    */
 
350  2 toggle public XWikiExplorer waitForAttachmentsSelected(String spaceName, String pageName)
351    {
352  2 return waitForNodeSelected(getAttachmentsNodeId(spaceName, pageName));
353    }
354   
 
355  21 toggle private XWikiExplorer waitForNodeSelected()
356    {
357  21 this.driver.waitUntilElementIsVisible(SELECTED_NODE);
358  21 return this;
359    }
360   
 
361  23 toggle private XWikiExplorer waitForNodeSelected(String nodeId)
362    {
363  23 this.driver.waitUntilElementIsVisible(By.xpath(String.format(
364    "//div[contains(@class, 'xExplorer')]//li[@id = '%s' and @aria-selected = 'true']", nodeId)));
365  23 return this;
366    }
367   
 
368  1 toggle public XWikiExplorer waitForFinderValue(final String value)
369    {
370  1 this.driver.waitUntilElementHasAttributeValue(By.xpath(FINDER_LOCATOR), "value", value);
371  1 return this;
372    }
373   
 
374  23 toggle private String getDocumentNodeId(String spaceName, String pageName)
375    {
376  23 return String.format("document:xwiki:%s.%s", spaceName, pageName);
377    }
378   
 
379  2 toggle private String getNewDocumentNodeId(String spaceName)
380    {
381  2 return String.format("addDocument:xwiki:%s.WebHome", spaceName);
382    }
383   
 
384  0 toggle private String getSpaceNodeId(String spaceName)
385    {
386  0 return "space:xwiki:" + spaceName;
387    }
388   
 
389  6 toggle private String getAttachmentNodeId(String spaceName, String pageName, String fileName)
390    {
391  6 return String.format("attachment:xwiki:%s.%s@%s", spaceName, pageName, fileName);
392    }
393   
 
394  3 toggle private String getAttachmentsNodeId(String spaceName, String pageName)
395    {
396  3 return String.format("attachments:xwiki:%s.%s", spaceName, pageName);
397    }
398   
 
399  12 toggle private String getNodeLocator(String nodeId)
400    {
401  12 return String.format("//div[contains(@class, 'xExplorer')]//li[@id = '%s']", nodeId);
402    }
403    }