Skip to content

Commit 9b7594d

Browse files
author
travis-ci
committed
Adding additional tolerant interaction helpers
1 parent 703b884 commit 9b7594d

File tree

4 files changed

+170
-8
lines changed

4 files changed

+170
-8
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ public static void tolerantClick(WebDriver webDriver, WebElement webElement, int
3939
e.printStackTrace();
4040
for(String exceptionToHandle : TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
4141
if(e.getClass().getName().contains(exceptionToHandle)) {
42-
logger.error("Exception {} is tolerated, retrying after a 3 second wait", exceptionToHandle);
42+
logger.error("Exception {} is tolerated, retrying after a {} second wait", waitTimeBeforeRetry, exceptionToHandle);
4343
Thread.sleep(waitTimeBeforeRetry);
44-
logger.error("Waited 3 seconds after {}, now retrying", exceptionToHandle);
44+
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
4545
click(webDriver, webElement, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout());
4646
}
4747
}
48-
// Can't handle the exception with a retry so re-throwing the exception
49-
throw e;
5048
}
5149
}
5250

@@ -73,14 +71,12 @@ public static void tolerantClick(WebDriver webDriver, By locator, int waitTimeBe
7371
e.printStackTrace();
7472
for(String exceptionToHandle : TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
7573
if(e.getClass().getName().contains(exceptionToHandle)) {
76-
logger.error("Exception {} is tolerated, retrying after a 3 second wait", exceptionToHandle);
74+
logger.error("Exception {} is tolerated, retrying after a {} second wait", waitTimeBeforeRetry, exceptionToHandle);
7775
Thread.sleep(waitTimeBeforeRetry);
78-
logger.error("Waited 3 seconds after {}, now retrying", exceptionToHandle);
76+
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
7977
click(webDriver, locator, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout());
8078
}
8179
}
82-
// Can't handle the exception with a retry so re-throwing the exception
83-
throw e;
8480
}
8581
}
8682

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package uk.co.evoco.webdriver.utils;
22

3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
35
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.support.ui.ExpectedConditions;
7+
import org.openqa.selenium.support.ui.WebDriverWait;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import uk.co.evoco.webdriver.configuration.TestConfigManager;
411

512
import java.util.List;
613

@@ -9,6 +16,8 @@
916
*/
1017
public final class RadioButtonUtils {
1118

19+
private static final Logger logger = LoggerFactory.getLogger(RadioButtonUtils.class);
20+
1221
/**
1322
* Given a list of WebElements that locate the labels of the radio buttons,
1423
* finds the radio button with the given visible label text and selects it.
@@ -23,4 +32,54 @@ public static void selectByLabel(List<WebElement> webElements, String visibleLab
2332
}
2433
}
2534
}
35+
36+
/**
37+
*
38+
* @param webDriver active WebDriver
39+
* @param locator for us to manage the lookup of the WebElement
40+
* @param visibleLabelText text that is visible on the page in the label tags
41+
*/
42+
public static void selectByLabel(WebDriver webDriver, By locator, String visibleLabelText) {
43+
List<WebElement> webElements = new WebDriverWait(
44+
webDriver, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout())
45+
.until(ExpectedConditions.presenceOfElementLocated(locator)).findElements(locator);
46+
for (WebElement webElement : webElements) {
47+
if (webElement.getText().equals(visibleLabelText)) {
48+
webElement.click();
49+
break;
50+
}
51+
}
52+
}
53+
54+
/**
55+
*
56+
* @param webDriver active WebDriver
57+
* @param locator locator for us to manage the lookup of the WebElements
58+
* @param visibleLabelText text that is visible on the page in the label tags
59+
* @param waitTimeBeforeRetry time to wait before we retry
60+
* @throws InterruptedException because there's a Thread.sleep here
61+
*/
62+
public static void tolerantSelectByLabel(
63+
WebDriver webDriver, By locator, String visibleLabelText, int waitTimeBeforeRetry) throws InterruptedException {
64+
try {
65+
selectByLabel(webDriver, locator, visibleLabelText);
66+
} catch (Exception e) {
67+
logger.error("Encountered an issue while trying to select visible text from select box, will check " +
68+
"to see if we tolerate this exception. Debug this issue to make your tests more stable. " +
69+
"Stacktrace follows.");
70+
e.printStackTrace();
71+
for (String exceptionToHandle :
72+
TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
73+
if (e.getClass().getName().contains(exceptionToHandle)) {
74+
logger.error(
75+
"Exception {} is tolerated, retrying after a {} second wait",
76+
waitTimeBeforeRetry,
77+
exceptionToHandle);
78+
Thread.sleep(waitTimeBeforeRetry);
79+
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
80+
selectByLabel(webDriver, locator, visibleLabelText);
81+
}
82+
}
83+
}
84+
}
2685
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package uk.co.evoco.webdriver.utils;
22

3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
35
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.support.ui.ExpectedConditions;
47
import org.openqa.selenium.support.ui.Select;
8+
import org.openqa.selenium.support.ui.WebDriverWait;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import uk.co.evoco.webdriver.configuration.TestConfigManager;
512

613
/**
714
* Utilities class providing support methods for Select Boxes
@@ -10,6 +17,8 @@
1017
*/
1118
public final class SelectBoxUtils {
1219

20+
private static final Logger logger = LoggerFactory.getLogger(SelectBoxUtils.class);
21+
1322
/**
1423
* Selects an option that has a matching value attribute in the Options tag markup
1524
* @param selectBox active WebElement, already located
@@ -41,4 +50,52 @@ public static void itemByVisibleText(WebElement selectBox, String visibleText) {
4150
Select select = new Select(selectBox);
4251
select.selectByVisibleText(visibleText);
4352
}
53+
54+
/**
55+
* Selects an option by the text that is visible in the select box
56+
* @param webDriver active webdriver to use
57+
* @param locator locator for us to manage the lookup of the WebElements
58+
* @param visibleText visible text in the select box (NOT the HTML value attribute)
59+
*/
60+
public static void itemByVisibleText(WebDriver webDriver, By locator, String visibleText) {
61+
WebElement element = webDriver.findElement(locator);
62+
new WebDriverWait(
63+
webDriver,
64+
TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout())
65+
.until(ExpectedConditions.elementToBeClickable(element));
66+
Select select = new Select(element);
67+
select.selectByVisibleText(visibleText);
68+
}
69+
70+
/**
71+
*
72+
* @param webDriver active WebDriver
73+
* @param locator locator for us to manage the lookup of the WebElements
74+
* @param visibleText visible text in the select box (NOT the HTML value attribute)
75+
* @param waitTimeBeforeRetry time to wait before we retry
76+
* @throws InterruptedException because there's a Thread.sleep here
77+
*/
78+
public static void tolerantSelectBoxByVisibleText(
79+
WebDriver webDriver, By locator, String visibleText, int waitTimeBeforeRetry) throws InterruptedException {
80+
try {
81+
itemByVisibleText(webDriver, locator, visibleText);
82+
} catch (Exception e) {
83+
logger.error("Encountered an issue while trying to select visible text from select box, will check " +
84+
"to see if we tolerate this exception. Debug this issue to make your tests more stable. " +
85+
"Stacktrace follows.");
86+
e.printStackTrace();
87+
for (String exceptionToHandle :
88+
TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
89+
if (e.getClass().getName().contains(exceptionToHandle)) {
90+
logger.error(
91+
"Exception {} is tolerated, retrying after a {} second wait",
92+
waitTimeBeforeRetry,
93+
exceptionToHandle);
94+
Thread.sleep(waitTimeBeforeRetry);
95+
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
96+
itemByVisibleText(webDriver, locator, visibleText);
97+
}
98+
}
99+
}
100+
}
44101
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package uk.co.evoco.webdriver.utils;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.support.ui.ExpectedConditions;
6+
import org.openqa.selenium.support.ui.WebDriverWait;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import uk.co.evoco.webdriver.configuration.TestConfigManager;
10+
11+
public class SendKeysUtils {
12+
13+
private static final Logger logger = LoggerFactory.getLogger(RadioButtonUtils.class);
14+
15+
/**
16+
*
17+
* @param webDriver active WebDriver
18+
* @param locator locator for us to manage the lookup of the WebElements
19+
* @param textToType the text to type in the targeted WebElement
20+
* @param waitTimeBeforeRetry time to wait before we retry
21+
* @throws InterruptedException because there's a Thread.sleep here
22+
*/
23+
public static void tolerantType(
24+
WebDriver webDriver, By locator, String textToType, int waitTimeBeforeRetry) throws InterruptedException {
25+
try {
26+
new WebDriverWait(
27+
webDriver, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout())
28+
.until(ExpectedConditions.visibilityOfElementLocated(locator)).click();
29+
} catch (Exception e) {
30+
logger.error("Encountered an issue while trying to select visible text from select box, will check " +
31+
"to see if we tolerate this exception. Debug this issue to make your tests more stable. " +
32+
"Stacktrace follows.");
33+
e.printStackTrace();
34+
for (String exceptionToHandle :
35+
TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
36+
if (e.getClass().getName().contains(exceptionToHandle)) {
37+
logger.error(
38+
"Exception {} is tolerated, retrying after a {} second wait",
39+
waitTimeBeforeRetry,
40+
exceptionToHandle);
41+
Thread.sleep(waitTimeBeforeRetry);
42+
logger.error("Waited {} seconds after {}, now retrying", waitTimeBeforeRetry, exceptionToHandle);
43+
new WebDriverWait(
44+
webDriver, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout())
45+
.until(ExpectedConditions.visibilityOfElementLocated(locator)).click();
46+
}
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)