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

File TableElement.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

26
43
10
1
178
105
26
0.6
4.3
10
2.6

Classes

Class Line # Actions
TableElement 34 43 0% 26 40
0.4936708849.4%
 

Contributing tests

This file is covered by 5 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.List;
23   
24    import org.openqa.selenium.By;
25    import org.openqa.selenium.WebElement;
26    import org.openqa.selenium.WebDriverException;
27   
28    /**
29    * Represents the actions possible on a static table.
30    *
31    * @version $Id: 453ddb489bee60e552dc3556a44300c97bd940c3 $
32    * @since 3.2M3
33    */
 
34    public class TableElement extends BaseElement
35    {
36    private final WebElement table;
37   
38    // Empty, numberOfColumns and numberOfRows are stored to cut down on processing.
39    // Empty -1 = unknown, 0 = no, 1 = yes.
40    private int empty = -1;
41   
42    private int numberOfColumns = -1;
43   
44    private int numberOfRows = -1;
45   
 
46  10 toggle public TableElement(WebElement table)
47    {
48  10 super();
49  10 if (!table.getTagName().toLowerCase().equals("table")) {
50  0 throw new WebDriverException("You can only create a TableElement using a <table> web element,"
51    + "you passed a <" + table.getTagName() + "> element");
52    }
53  10 if (!table.findElements(By.tagName("tbody")).isEmpty()) {
54  10 this.table = table.findElements(By.tagName("tbody")).get(0);
55    } else {
56  0 this.table = table;
57    }
58    }
59   
60    /** @return true if there are no colums or no rows in the table. True if the table only contains headings. */
 
61  0 toggle public boolean isEmpty()
62    {
63  0 if (empty == -1) {
64  0 for (WebElement row : table.findElements(By.tagName("tr"))) {
65  0 if (!row.findElements(By.tagName("td")).isEmpty()) {
66  0 empty = 0;
67  0 return false;
68    }
69    }
70  0 empty = 1;
71    }
72  0 return (empty == 1);
73    }
74   
75    /** @return the number of columns in the table. */
 
76  9 toggle public int numberOfColumns()
77    {
78  9 if (numberOfRows() == 0) {
79  0 return 0;
80    }
81  9 if (numberOfColumns == -1) {
82  9 numberOfColumns = getRow(0).size();
83    }
84  9 return numberOfColumns;
85    }
86   
87    /** @return The number of rows in the table. */
 
88  48 toggle public int numberOfRows()
89    {
90  48 if (numberOfRows == -1) {
91  10 numberOfRows = table.findElements(By.tagName("tr")).size();
92    }
93  48 return numberOfRows;
94    }
95   
96    /**
97    * @param firstEntry text content of the element in the first row (usually a heading).
98    * @return each WebElement in that column.
99    */
 
100  7 toggle public List<WebElement> getColumn(String firstEntry)
101    {
102  7 return getColumn(getColumnNumber(firstEntry));
103    }
104   
105    /**
106    * @param firstEntry text content of the element in the first row (usually a heading).
107    * @return number of that column (0 indexed)
108    */
 
109  7 toggle public int getColumnNumber(String firstEntry)
110    {
111  7 if (numberOfRows() == 0) {
112  0 return -1;
113    }
114  7 List<WebElement> headers = getRow(0);
115  7 for (WebElement header : headers) {
116  24 if (header.getText().equals(firstEntry)) {
117  7 return headers.indexOf(header);
118    }
119    }
120  0 return -1;
121    }
122   
123    /**
124    * @param columnNumber zero indexed number of the column.
125    * @return each WebElement in that column.
126    */
 
127  7 toggle public List<WebElement> getColumn(int columnNumber)
128    {
129  7 if (numberOfRows() == 0 || numberOfColumns() < columnNumber || columnNumber < 0) {
130  0 return null;
131    }
132    // xpaths are 1 indexed.
133  7 return table.findElements(By.xpath("//tr/th[" + (columnNumber + 1) + "]"
134    + " | //tr/td[" + (columnNumber + 1) + "]"));
135    }
136   
137    /**
138    * @param firstEntry text content of the element in the first column
139    * (a heading if the table has left side headings).
140    * @return each WebElement in that row.
141    */
 
142  0 toggle public List<WebElement> getRow(String firstEntry)
143    {
144  0 return getRow(getRowNumber(firstEntry));
145    }
146   
147    /**
148    * @param firstEntry text content of the element in the first column
149    * (a heading if the table has left side headings).
150    * @return numeric index of that row or -1 if not found.
151    */
 
152  0 toggle public int getRowNumber(String firstEntry)
153    {
154  0 if (numberOfColumns() == 0) {
155  0 return -1;
156    }
157  0 List<WebElement> headers = getColumn(0);
158  0 for (WebElement header : headers) {
159  0 if (header.getText().equals(firstEntry)) {
160  0 return headers.indexOf(header);
161    }
162    }
163  0 return -1;
164    }
165   
166    /**
167    * @param rowNumber zero indexed number of the row.
168    * @return each WebElement in that row.
169    */
 
170  23 toggle public List<WebElement> getRow(int rowNumber)
171    {
172  23 if (numberOfRows() <= rowNumber || rowNumber < 0) {
173  0 return null;
174    }
175  23 return table.findElements(By.xpath("//tr[" + (rowNumber + 1) + "]/th"
176    + " | //tr[" + (rowNumber + 1) + "]/td"));
177    }
178    }