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

File TreeElement.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

8
26
11
1
186
89
15
0.58
2.36
11
1.36

Classes

Class Line # Actions
TreeElement 39 26 0% 15 18
0.660%
 

Contributing tests

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