1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.tree.test.po

File TreeElement.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

8
27
12
1
199
98
16
0.59
2.25
12
1.33

Classes

Class Line # Actions
TreeElement 40 27 0% 16 18
0.6170212661.7%
 

Contributing tests

This file is covered by 7 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.tree.test.po;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25    import java.util.stream.Collectors;
26   
27    import org.openqa.selenium.By;
28    import org.openqa.selenium.JavascriptExecutor;
29    import org.openqa.selenium.WebDriver;
30    import org.openqa.selenium.WebElement;
31    import org.openqa.selenium.support.ui.ExpectedCondition;
32    import org.xwiki.test.ui.po.BaseElement;
33   
34    /**
35    * Page object used to interact with the generic tree widget available in XWiki.
36    *
37    * @version $Id: 2060a7916b5a4d3680d6bedc583a2eee0392b882 $
38    * @since 6.3RC1
39    */
 
40    public class TreeElement extends BaseElement
41    {
42    /**
43    * The element that represents the tree.
44    */
45    private WebElement element;
46   
47    /**
48    * Creates a new instance that can be used to interact with the tree represented by the given element.
49    *
50    * @param element the element that represents the tree
51    */
 
52  27 toggle public TreeElement(WebElement element)
53    {
54  27 this.element = element;
55    }
56   
57    /**
58    * @param nodeId the node identifier
59    * @return the node with the specified identifier
60    */
 
61  6 toggle public TreeNodeElement getNode(String nodeId)
62    {
63  6 return new TreeNodeElement(this.element, By.id(nodeId));
64    }
65   
66    /**
67    * @param nodeId the node identifier
68    * @return {@code true} if the specified node is present (loaded), {@code false} otherwise
69    */
 
70  7 toggle public boolean hasNode(String nodeId)
71    {
72    // We cannot use By.id(nodeId) because findElements returns 0 elements if the id contains special characters
73    // such as backslash (which is used to escape special characters in an entity reference which the node id can
74    // be). Such an element id is technically invalid but the browsers are handling it fine.
75    // See https://code.google.com/p/selenium/issues/detail?id=8173
76  7 return getDriver().findElementsWithoutWaiting(this.element,
77    By.xpath(".//*[@id = '" + nodeId + "']")).size() > 0;
78    }
79   
80    /**
81    * Open the tree to the specified node and select it.
82    *
83    * @param nodeId the node to open to
84    * @return this tree
85    */
 
86  5 toggle public TreeElement openTo(String nodeId)
87    {
88  5 if (getDriver() instanceof JavascriptExecutor) {
89  5 ((JavascriptExecutor) getDriver()).executeScript(
90    "jQuery.jstree.reference(jQuery(arguments[0])).openTo(arguments[1])", this.element, nodeId);
91    }
92  5 waitForNodeSelected(nodeId);
93   
94  5 return this;
95    }
96   
97    /**
98    * Clear the selection in the tree.
99    *
100    * @return this tree
101    * @since 7.2M3
102    */
 
103  4 toggle public TreeElement clearSelection()
104    {
105  4 if (getDriver() instanceof JavascriptExecutor) {
106  4 ((JavascriptExecutor) getDriver()).executeScript(
107    "jQuery.jstree.reference(jQuery(arguments[0])).deselect_all()", this.element);
108    }
109  4 return this;
110    }
111   
112    /**
113    * Wait as long as the tree in busy (loading).
114    *
115    * @return this tree
116    */
 
117  26 toggle public TreeElement waitForIt()
118    {
119    // Wait for the loading animation container. This element is generated from JavaScript when the tree is
120    // being initialized, so its presence guarantees that the tree initialization has started.
121  26 getDriver().waitUntilElementIsVisible(this.element, By.cssSelector(".jstree-container-ul"));
122    // Wait for the root node to be loaded.
123  26 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
124    {
 
125  29 toggle @Override
126    public Boolean apply(WebDriver driver)
127    {
128    // The tree element is marked as busy while the tree nodes are being loaded.
129  29 return !Boolean.valueOf(element.getAttribute("aria-busy"));
130    }
131    });
132  26 return this;
133    }
134   
135    /**
136    * Waits for the specified node to be selected.
137    *
138    * @param nodeId the id of the node to wait for
139    * @return this tree
140    * @since 7.2
141    */
 
142  6 toggle public TreeElement waitForNodeSelected(String nodeId)
143    {
144  6 String selectedNodeXPath = String.format(".//*[@id = '%s' and @aria-selected = 'true']", nodeId);
145  6 getDriver().waitUntilElementIsVisible(this.element, By.xpath(selectedNodeXPath));
146  6 return this;
147    }
148   
 
149  0 toggle protected WebElement getElement()
150    {
151  0 return this.element;
152    }
153   
154    /**
155    * @return the list of selected node IDs.
156    */
 
157  0 toggle public List<String> getSelectedNodeIDs()
158    {
159  0 if (getDriver() instanceof JavascriptExecutor) {
160  0 List<String> selectedNodeIDs =
161    (List<String>) ((JavascriptExecutor) getDriver()).executeScript(
162    "return jQuery.jstree.reference(jQuery(arguments[0])).get_selected()", this.element);
163   
164  0 return selectedNodeIDs;
165    }
166   
167  0 return new ArrayList<String>();
168    }
169   
170    /**
171    * @return a list of loaded node IDs.
172    */
 
173  0 toggle public List<String> getNodeIDs()
174    {
175  0 if (getDriver() instanceof JavascriptExecutor) {
176  0 String[] selectedNodeIDs =
177    (String[]) ((JavascriptExecutor) getDriver()).executeScript(
178    "return jQuery.jstree.reference(jQuery(arguments[0])).get_json('#', "
179    + "{flat:true, no_data:true, no_state:true})" + ".map(function(element) {return element.id});",
180    this.element);
181   
182  0 return Arrays.asList(selectedNodeIDs);
183    }
184   
185  0 return new ArrayList<String>();
186    }
187   
188    /**
189    * @return the list of top level nodes
190    */
 
191  15 toggle public List<TreeNodeElement> getTopLevelNodes()
192    {
193  15 return getDriver()
194    .findElementsWithoutWaiting(this.element,
195    By.cssSelector(".jstree-node[aria-level=\"1\"]:not(.jstree-hidden)"))
196    .stream().map(nodeElement -> By.id(nodeElement.getAttribute("id")))
197    .map(nodeLocator -> new TreeNodeElement(this.element, nodeLocator)).collect(Collectors.toList());
198    }
199    }