Skip to content

Commit 0342d5b

Browse files
author
travis-ci
committed
Adding tolerant interactions with WebElements as well as Bys
1 parent 68cb71d commit 0342d5b

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

src/main/java/uk/co/evoco/webdriver/utils/RadioButtonUtils.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,34 @@ public static void tolerantSelectByLabel(
8282
}
8383
}
8484
}
85+
86+
/**
87+
*
88+
* @param elements WebElements
89+
* @param visibleLabelText text that is visible on the page in the label tags
90+
* @param waitTimeBeforeRetry time to wait before we retry
91+
* @throws InterruptedException because there's a Thread.sleep here
92+
*/
93+
public static void tolerantSelectByLabel(List<WebElement> elements, String visibleLabelText, int waitTimeBeforeRetry) throws InterruptedException {
94+
try {
95+
selectByLabel(elements, visibleLabelText);
96+
} catch (Exception e) {
97+
logger.error("Encountered an issue while trying to select visible text from select box, will check " +
98+
"to see if we tolerate this exception. Debug this issue to make your tests more stable. " +
99+
"Stacktrace follows.");
100+
e.printStackTrace();
101+
for (String exceptionToHandle :
102+
TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
103+
if (e.getClass().getName().contains(exceptionToHandle)) {
104+
logger.error(
105+
"Exception {} is tolerated, retrying after a {} second wait",
106+
waitTimeBeforeRetry,
107+
exceptionToHandle);
108+
Thread.sleep(waitTimeBeforeRetry);
109+
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
110+
selectByLabel(elements, visibleLabelText);
111+
}
112+
}
113+
}
114+
}
85115
}

src/main/java/uk/co/evoco/webdriver/utils/SelectBoxUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,36 @@ public static void tolerantSelectBoxByVisibleText(
9898
}
9999
}
100100
}
101+
102+
/**
103+
*
104+
* @param webDriver active WebDriver
105+
* @param element WebElement
106+
* @param visibleText visible text in the select box (NOT the HTML value attribute)
107+
* @param waitTimeBeforeRetry time to wait before we retry
108+
* @throws InterruptedException because there's a Thread.sleep here
109+
*/
110+
public static void tolerantSelectBoxByVisibleText(
111+
WebDriver webDriver, WebElement element, String visibleText, int waitTimeBeforeRetry) throws InterruptedException {
112+
try {
113+
itemByVisibleText(element, visibleText);
114+
} catch (Exception e) {
115+
logger.error("Encountered an issue while trying to select visible text from select box, will check " +
116+
"to see if we tolerate this exception. Debug this issue to make your tests more stable. " +
117+
"Stacktrace follows.");
118+
e.printStackTrace();
119+
for (String exceptionToHandle :
120+
TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
121+
if (e.getClass().getName().contains(exceptionToHandle)) {
122+
logger.error(
123+
"Exception {} is tolerated, retrying after a {} second wait",
124+
waitTimeBeforeRetry,
125+
exceptionToHandle);
126+
Thread.sleep(waitTimeBeforeRetry);
127+
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
128+
itemByVisibleText(element, visibleText);
129+
}
130+
}
131+
}
132+
}
101133
}

src/main/java/uk/co/evoco/webdriver/utils/SendKeysUtils.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.openqa.selenium.By;
44
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.WebElement;
56
import org.openqa.selenium.support.ui.ExpectedConditions;
67
import org.openqa.selenium.support.ui.WebDriverWait;
78
import org.slf4j.Logger;
@@ -25,7 +26,7 @@ public static void tolerantType(
2526
try {
2627
new WebDriverWait(
2728
webDriver, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout())
28-
.until(ExpectedConditions.visibilityOfElementLocated(locator)).click();
29+
.until(ExpectedConditions.visibilityOfElementLocated(locator)).sendKeys(textToType);
2930
} catch (Exception e) {
3031
logger.error("Encountered an issue while trying to select visible text from select box, will check " +
3132
"to see if we tolerate this exception. Debug this issue to make your tests more stable. " +
@@ -42,7 +43,42 @@ public static void tolerantType(
4243
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
4344
new WebDriverWait(
4445
webDriver, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout())
45-
.until(ExpectedConditions.visibilityOfElementLocated(locator)).click();
46+
.until(ExpectedConditions.visibilityOfElementLocated(locator)).sendKeys(textToType);
47+
}
48+
}
49+
}
50+
}
51+
52+
/**
53+
*
54+
* @param webDriver active WebDriver
55+
* @param element WebElement to type in
56+
* @param textToType the text to type in the targeted WebElement
57+
* @param waitTimeBeforeRetry time to wait before we retry
58+
* @throws InterruptedException because there's a Thread.sleep here
59+
*/
60+
public static void tolerantType(WebDriver webDriver, WebElement element, String textToType, int waitTimeBeforeRetry) throws InterruptedException {
61+
try {
62+
new WebDriverWait(
63+
webDriver, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout())
64+
.until(ExpectedConditions.elementToBeClickable(element)).sendKeys(textToType);
65+
} catch (Exception e) {
66+
logger.error("Encountered an issue while trying to select visible text from select box, will check " +
67+
"to see if we tolerate this exception. Debug this issue to make your tests more stable. " +
68+
"Stacktrace follows.");
69+
e.printStackTrace();
70+
for (String exceptionToHandle :
71+
TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
72+
if (e.getClass().getName().contains(exceptionToHandle)) {
73+
logger.error(
74+
"Exception {} is tolerated, retrying after a {} second wait",
75+
waitTimeBeforeRetry,
76+
exceptionToHandle);
77+
Thread.sleep(waitTimeBeforeRetry);
78+
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
79+
new WebDriverWait(
80+
webDriver, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout())
81+
.until(ExpectedConditions.elementToBeClickable(element)).sendKeys(textToType);
4682
}
4783
}
4884
}

0 commit comments

Comments
 (0)