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

File TreeNodeElement.java

 

Coverage histogram

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

Code metrics

8
32
15
1
207
102
20
0.62
2.13
15
1.33

Classes

Class Line # Actions
TreeNodeElement 38 32 0% 20 17
0.690909169.1%
 

Contributing tests

This file is covered by 2 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.List;
24   
25    import org.openqa.selenium.By;
26    import org.openqa.selenium.StaleElementReferenceException;
27    import org.openqa.selenium.WebDriver;
28    import org.openqa.selenium.WebElement;
29    import org.openqa.selenium.support.ui.ExpectedCondition;
30    import org.xwiki.test.ui.po.BaseElement;
31   
32    /**
33    * Page object used to interact with a tree node.
34    *
35    * @version $Id: ade31cce3ef8ef5ef69d3a718be916fb072754f1 $
36    * @since 6.3RC1
37    */
 
38    public class TreeNodeElement extends BaseElement
39    {
40    /**
41    * The class HTML attribute.
42    */
43    private static final String CLASS = "class";
44   
45    /**
46    * The tree that contains this node.
47    */
48    private WebElement treeElement;
49   
50    /**
51    * The element that represents the tree node.
52    */
53    private By locator;
54   
55    /**
56    * Creates a new instance that can be used to interact with the tree node represented by the specified element.
57    *
58    * @param treeElement the tree that contains this node
59    * @param locator the element that represents the tree node
60    */
 
61  8 toggle public TreeNodeElement(WebElement treeElement, By locator)
62    {
63  8 this.treeElement = treeElement;
64  8 this.locator = locator;
65    }
66   
67    /**
68    * The element that represents this tree node is often replaced so we cannot cache it.
69    *
70    * @return the element that represents this tree node
71    */
 
72  34 toggle private WebElement getElement()
73    {
74  34 return this.treeElement.findElement(this.locator);
75    }
76   
77    /**
78    * @return the node label (as plain text)
79    */
 
80  5 toggle public String getLabel()
81    {
82  5 return getLabelElement().getText();
83    }
84   
 
85  6 toggle private WebElement getLabelElement()
86    {
87  6 String labelId = getElement().getAttribute("aria-labelledby");
88  6 return getElement().findElement(By.id(labelId));
89    }
90   
91    /**
92    * @return the list of child nodes
93    */
 
94  2 toggle public List<TreeNodeElement> getChildren()
95    {
96  2 List<TreeNodeElement> children = new ArrayList<>();
97  2 for (WebElement childElement : getElement().findElements(By.xpath("./ul/li"))) {
98  2 children.add(new TreeNodeElement(this.treeElement, By.id(childElement.getAttribute("id"))));
99    }
100  2 return children;
101    }
102   
103    /**
104    * @return {@code true} if this node is a leaf (no children), {@code false} otherwise
105    */
 
106  0 toggle public boolean isLeaf()
107    {
108  0 return getElement().getAttribute(CLASS).contains("jstree-leaf");
109    }
110   
111    /**
112    * @return {@code true} if this node is opened (children are visible), {@code false} otherwise
113    */
 
114  6 toggle public boolean isOpen()
115    {
116  6 return Boolean.valueOf(getElement().getAttribute("aria-expanded"));
117    }
118   
119    /**
120    * @return {@code true} if this node is selected, {@code false} otherwise
121    */
 
122  1 toggle public boolean isSelected()
123    {
124  1 return Boolean.valueOf(getElement().getAttribute("aria-selected"));
125    }
126   
127    /**
128    * Opens this tree node.
129    *
130    * @return this tree node
131    */
 
132  6 toggle public TreeNodeElement open()
133    {
134  6 if (!isOpen()) {
135  6 getToggleElement().click();
136    }
137  6 return this;
138    }
139   
140    /**
141    * Closes this tree node.
142    *
143    * @return this tree node
144    */
 
145  0 toggle public TreeNodeElement close()
146    {
147  0 if (isOpen()) {
148  0 getToggleElement().click();
149    }
150  0 return this;
151    }
152   
 
153  6 toggle private WebElement getToggleElement()
154    {
155  6 return getElement().findElement(By.xpath("./i[contains(@class, 'jstree-ocl')]"));
156    }
157   
158    /**
159    * Selects this tree node by clicking on its label.
160    *
161    * @return this tree node
162    */
 
163  1 toggle public TreeNodeElement select()
164    {
165  1 if (!isSelected()) {
166  1 getLabelElement().click();
167    }
168  1 return this;
169    }
170   
171    /**
172    * Deselects this tree node by clicking on its label.
173    *
174    * @return this tree node
175    */
 
176  0 toggle public TreeNodeElement deselect()
177    {
178  0 if (isSelected()) {
179  0 getLabelElement().click();
180    }
181  0 return this;
182    }
183   
184    /**
185    * Wait as long as this node is in loading state.
186    *
187    * @return this tree node
188    */
 
189  6 toggle public TreeNodeElement waitForIt()
190    {
191  6 getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
192    {
 
193  7 toggle @Override
194    public Boolean apply(WebDriver driver)
195    {
196  7 WebElement element = getElement();
197  7 try {
198  7 return !Boolean.valueOf(element.getAttribute("aria-busy"));
199    } catch (StaleElementReferenceException e) {
200    // The element has just been replaced. Try again.
201  0 return false;
202    }
203    }
204    });
205  6 return this;
206    }
207    }