This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
WebElementUtil.java
341 lines (310 loc) · 13.6 KB
/
WebElementUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.util;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.zanata.page.WebDriverFactory;
import static java.util.concurrent.TimeUnit.SECONDS;
@Slf4j
public class WebElementUtil {
private WebElementUtil() {
}
/**
* This method potentially will suffer from StaleElementException if the
* WebElements given are dynamic elements on the page. If so consider using
* #elementsToText(org.openqa.selenium.WebDriver, org.openqa.selenium.By)
* instead.
*
* @param webElements
* collection of WebElement
* @return text representation of the elements
*/
public static List<String>
elementsToText(Collection<WebElement> webElements) {
return ImmutableList.copyOf(Collections2.transform(webElements,
WebElementToTextFunction.FUNCTION));
}
public static List<String> elementsToText(WebDriver driver, final By by) {
return waitForAMoment(driver).until(
new Function<WebDriver, List<String>>() {
@Override
public List<String> apply(@Nullable WebDriver input) {
if (input == null) {
throw new RuntimeException("Driver is null");
}
List<WebElement> elements = input.findElements(by);
return ImmutableList.copyOf(Lists.transform(elements,
WebElementToTextFunction.FUNCTION));
}
}
);
}
public static String getInnerHTML(WebDriver driver, WebElement element) {
return (String) ((JavascriptExecutor) driver).executeScript(
"return arguments[0].innerHTML;", element);
}
public static List<String> elementsToInnerHTML(WebDriver driver,
Collection<WebElement> webElements) {
return ImmutableList.copyOf(Collections2.transform(webElements,
new WebElementToInnerHTMLFunction(driver)));
}
public static List<TableRow> getTableRows(WebDriver driver,
final By byQueryForTable) {
return waitForAMoment(driver).until(
new Function<WebDriver, List<TableRow>>() {
@Override
public List<TableRow> apply(@Nullable WebDriver webDriver) {
if (webDriver == null) {
throw new RuntimeException("Driver is null");
}
final WebElement table =
webDriver.findElement(byQueryForTable);
List<WebElement> rows =
table.findElements(By.xpath(".//tbody[1]/tr"));
return ImmutableList.copyOf(Lists.transform(rows,
WebElementTableRowFunction.FUNCTION));
}
}
);
}
public static List<TableRow> getTableRows(WebDriver driver,
final WebElement table) {
return waitForAMoment(driver).until(
new Function<WebDriver, List<TableRow>>() {
@Override
public List<TableRow> apply(@Nullable WebDriver webDriver) {
if (webDriver == null) {
throw new RuntimeException("Driver is null");
}
List<WebElement> rows =
table.findElements(By.xpath(".//tbody[1]/tr"));
return ImmutableList.copyOf(Lists.transform(rows,
WebElementTableRowFunction.FUNCTION));
}
}
);
}
public static ImmutableList<List<String>> transformToTwoDimensionList(
List<TableRow> tableRows) {
return ImmutableList.copyOf(Lists.transform(tableRows,
new Function<TableRow, List<String>>() {
@Override
public List<String> apply(@Nullable TableRow from) {
if (from == null) {
throw new RuntimeException("Source table is null");
}
return from.getCellContents();
}
}));
}
public static FluentWait<WebDriver> waitForSeconds(WebDriver webDriver,
int durationInSec) {
return new WebDriverLogWait(WebDriverFactory.INSTANCE, durationInSec)
.ignoring(
// TODO is ignoring this safe?
StaleElementReferenceException.class);
}
public static FluentWait<WebDriver> waitForAMoment(WebDriver webDriver) {
return waitForSeconds(webDriver, WebDriverFactory.INSTANCE.getWebDriverWait());
}
public static List<String> getColumnContents(WebDriver driver, final By by,
final int columnIndex) {
return waitForAMoment(driver).until(
new Function<WebDriver, List<String>>() {
@Override
public List<String> apply(@Nullable WebDriver input) {
if (input == null) {
throw new RuntimeException("Driver is null");
}
WebElement table;
try {
table = input.findElement(by);
} catch (NoSuchElementException noElement) {
// Some pages don't show a table, if there's no
// items to show
return Collections.emptyList();
}
List<WebElement> rows =
table.findElements(By.xpath(".//tbody[1]/tr"));
List<TableRow> tableRows =
Lists.transform(rows,
WebElementTableRowFunction.FUNCTION);
return ImmutableList.copyOf(Lists.transform(tableRows,
new Function<TableRow, String>() {
@Override
public String apply(TableRow from) {
List<String> cellContents =
from.getCellContents();
Preconditions.checkElementIndex(
columnIndex,
cellContents.size(),
"column index");
return cellContents.get(columnIndex);
}
}
));
}
}
);
}
public static List<List<String>> getTwoDimensionList(WebDriver driver,
final By by) {
return waitForAMoment(driver).until(
new Function<WebDriver, List<List<String>>>() {
@Override
public List<List<String>> apply(@Nullable WebDriver input) {
if (input == null) {
throw new RuntimeException("Driver is null");
}
final WebElement table = input.findElement(by);
List<WebElement> rows =
table.findElements(By.xpath(".//tbody[1]/tr"));
List<TableRow> tableRows =
Lists.transform(rows,
WebElementTableRowFunction.FUNCTION);
return transformToTwoDimensionList(tableRows);
}
}
);
}
public static List<WebElement> getListItems(WebDriver driver, final By by) {
return waitForAMoment(driver).until(
new Function<WebDriver, List<WebElement>>() {
@Override
public List<WebElement> apply(@Nullable WebDriver input) {
if (input == null) {
throw new RuntimeException("Driver is null");
}
final WebElement list = input.findElement(by);
return list.findElements(By.xpath(".//li"));
}
}
);
}
/**
* This method is used to set JSF rich text editor (KCEditor) content.
*
* @param driver
* web driver
* @param richEditorWrapperField
* the wrapper div of the editor
* @param content
* content wants to set
*/
public static void setRichTextEditorContent(WebDriver driver,
WebElement richEditorWrapperField, String content) {
// This is how we can change JSF rich text editor content.
WebElement richTextEditorFrame =
richEditorWrapperField.findElement(By.tagName("iframe"));
driver.switchTo().frame(richTextEditorFrame);
((JavascriptExecutor) driver).executeScript("document.body.innerHTML='"
+ content + "'");
driver.switchTo().defaultContent();
}
private static class WebElementToInnerHTMLFunction implements
Function<WebElement, String> {
private final WebDriver driver;
public WebElementToInnerHTMLFunction(WebDriver driver) {
this.driver = driver;
}
@Override
public String apply(WebElement from) {
return getInnerHTML(driver, from);
}
}
private static enum WebElementTableRowFunction implements
Function<WebElement, TableRow> {
FUNCTION;
@Override
public TableRow apply(WebElement element) {
return new TableRow(element);
}
}
public static enum WebElementToTextFunction implements
Function<WebElement, String> {
FUNCTION;
@Override
public String apply(@Nullable WebElement from) {
if (from == null) {
throw new RuntimeException("Source element is null");
}
return from.getText().trim();
}
}
public static void searchAutocomplete(WebDriver driver, String id,
String query) {
final String locator = id + "-autocomplete__input";
waitForAMoment(driver).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
return input.findElement(By.id(locator)).isDisplayed();
}
});
driver.findElement(By.id(locator)).sendKeys(query);
}
public static List<WebElement> getSearchAutocompleteResults(
WebDriver driver, final String formId, final String id) {
return waitForAMoment(driver).until(
new Function<WebDriver, List<WebElement>>() {
@Override
public List<WebElement> apply(WebDriver input) {
String locator =
formId + ":" + id + ":" + id + "-result";
return input.findElement(By.id(locator)).findElements(
By.className("js-autocomplete__result"));
}
}
);
}
public static List<String> getSearchAutocompleteItems(WebDriver driver,
final String formId, final String id) {
List<WebElement> results =
getSearchAutocompleteResults(driver, formId, id);
List<String> resultsText =
Lists.transform(results, new Function<WebElement, String>() {
@Override
public String apply(WebElement li) {
return li.getText();
}
});
return resultsText;
}
public static void triggerScreenshot(final String tag) {
WebDriverFactory.INSTANCE.injectScreenshot(tag);
}
}