-
Notifications
You must be signed in to change notification settings - Fork 30
/
CommonUtils.java
285 lines (238 loc) · 9.3 KB
/
CommonUtils.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
package utils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.TestException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by tmaher on 12/21/2015.
*/
public abstract class CommonUtils {
private static int timeout = 10;
public CommonUtils() {
_driver = DriverUtils.getDriver();
}
public static WebDriver _driver;
public WebDriverWait wait;
public Actions actions;
public Select select;
public void navigateToURL(String URL) {
try {
_driver.navigate().to(URL);
} catch (Exception e) {
System.out.println("FAILURE: URL did not load: " + URL);
throw new TestException("URL did not load");
}
}
public void navigateBack() {
try {
_driver.navigate().back();
} catch (Exception e) {
System.out.println("FAILURE: Could not navigate back to previous page.");
throw new TestException("Could not navigate back to previous page.");
}
}
public String getPageTitle() {
try {
return _driver.getTitle();
} catch (Exception e) {
throw new TestException(String.format("Current page title is: %s", _driver.getTitle()));
}
}
public String getCurrentURL() {
try {
return _driver.getCurrentUrl();
} catch (Exception e) {
throw new TestException(String.format("Current URL is: %s", _driver.getCurrentUrl()));
}
}
public WebElement getElement(By selector) {
try {
return _driver.findElement(selector);
} catch (Exception e) {
System.out.println(String.format("Element %s does not exist - proceeding", selector));
}
return null;
}
public String getElementText(By selector) {
waitUntilElementIsDisplayedOnScreen(selector);
try {
return StringUtils.trim(_driver.findElement(selector).getText());
} catch (Exception e) {
System.out.println(String.format("Element %s does not exist - proceeding", selector));
}
return null;
}
public List<WebElement> getElements(By Selector) {
waitForElementToBeVisible(Selector);
try {
return _driver.findElements(Selector);
} catch (Exception e) {
throw new NoSuchElementException(String.format("The following element did not display: [%s] ", Selector.toString()));
}
}
public List<String> getListOfElementTexts(By selector) {
List<String> elementList = new ArrayList<String>();
List<WebElement> elements = getElements(selector);
for (WebElement element : elements) {
if (element == null) {
throw new TestException("Some elements in the list do not exist");
}
if (element.isDisplayed()) {
elementList.add(element.getText().trim());
}
}
return elementList;
}
public void click(By selector) {
WebElement element = getElement(selector);
waitForElementToBeClickable(selector);
try {
element.click();
} catch (Exception e) {
throw new TestException(String.format("The following element is not clickable: [%s]", selector));
}
}
public void scrollToThenClick(By selector) {
WebElement element = _driver.findElement(selector);
actions = new Actions(_driver);
try {
((JavascriptExecutor) _driver).executeScript("arguments[0].scrollIntoView(true);", element);
actions.moveToElement(element).perform();
actions.click(element).perform();
} catch (Exception e) {
throw new TestException(String.format("The following element is not clickable: [%s]", element.toString()));
}
}
public void sendKeys(By selector, String value) {
WebElement element = getElement(selector);
clearField(element);
try {
element.sendKeys(value);
} catch (Exception e) {
throw new TestException(String.format("Error in sending [%s] to the following element: [%s]", value, selector.toString()));
}
}
public void clearField(WebElement element) {
try {
element.clear();
waitForElementTextToBeEmpty(element);
} catch (Exception e) {
System.out.print(String.format("The following element could not be cleared: [%s]", element.getText()));
}
}
public void waitForElementToDisplay(By Selector) {
WebElement element = getElement(Selector);
while (!element.isDisplayed()) {
System.out.println("Waiting for element to display: " + Selector);
sleep(200);
}
}
public void waitForElementTextToBeEmpty(WebElement element) {
String text;
try {
text = element.getText();
int maxRetries = 10;
int retry = 0;
while ((text.length() >= 1) || (retry < maxRetries)) {
retry++;
text = element.getText();
}
} catch (Exception e) {
System.out.print(String.format("The following element could not be cleared: [%s]", element.getText()));
}
}
public void waitForElementToBeVisible(By selector) {
try {
wait = new WebDriverWait(_driver, timeout);
wait.until(ExpectedConditions.presenceOfElementLocated(selector));
} catch (Exception e) {
throw new NoSuchElementException(String.format("The following element was not visible: %s", selector));
}
}
public void waitUntilElementIsDisplayedOnScreen(By selector) {
try {
wait = new WebDriverWait(_driver, timeout);
wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
} catch (Exception e) {
throw new NoSuchElementException(String.format("The following element was not visible: %s ", selector));
}
}
public void waitForElementToBeClickable(By selector) {
try {
wait = new WebDriverWait(_driver, timeout);
wait.until(ExpectedConditions.elementToBeClickable(selector));
} catch (Exception e) {
throw new TestException("The following element is not clickable: " + selector);
}
}
public void sleep(final long millis) {
System.out.println((String.format("sleeping %d ms", millis)));
try {
Thread.sleep(millis);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
public void selectIfOptionTextContains(By selector, String searchCriteria) {
waitForElementToBeClickable(selector);
Select dropdown = new Select(getElement(selector));
List<WebElement> options = dropdown.getOptions();
String optionText = "";
if (options == null) {
throw new TestException("Options for the dropdown list cannot be found.");
}
for (WebElement option : options) {
optionText = option.getText().trim();
boolean isOptionDisplayed = option.isDisplayed();
if (optionText.contains(searchCriteria) && isOptionDisplayed) {
try {
dropdown.selectByVisibleText(optionText);
break;
} catch (Exception e) {
throw new NoSuchElementException(String.format("The following element did not display: [%s] ", selector.toString()));
}
}
}
}
public void selectIfOptionTextEquals(By selector, String searchCriteria) {
waitForElementToBeClickable(selector);
Select dropdown = new Select(getElement(selector));
List<WebElement> options = dropdown.getOptions();
String optionText = "";
if (options == null) {
throw new TestException("Options for the dropdown list cannot be found.");
}
for (WebElement option : options) {
optionText = option.getText().trim();
boolean isOptionDisplayed = option.isDisplayed();
if (optionText.equals(searchCriteria) && isOptionDisplayed) {
try {
dropdown.selectByVisibleText(optionText);
break;
} catch (Exception e) {
throw new NoSuchElementException(String.format("The following element did not display: [%s] ", selector.toString()));
}
}
}
}
public List<String> getDropdownValues(By selector) {
waitForElementToDisplay(selector);
Select dropdown = new Select(getElement(selector));
List<String> elementList = new ArrayList<String>();
List<WebElement> allValues = dropdown.getOptions();
if (allValues == null) {
throw new TestException("Some elements in the list do not exist");
}
for (WebElement value : allValues) {
if (value.isDisplayed()) {
elementList.add(value.getText().trim());
}
}
return elementList;
}
}