Skip to content

Commit 2658547

Browse files
authored
added exception wait time out in config file to handle tolerant methods (#43)
* added exception wait time out to handle tolerant methods * applied the changes as mentioned in the review comments for tolerant methods * Changed the method to package private to not expose this method * Added test to validate the tolerant method without passing wait time for method * changed the variable names for the TolerantAction object as mentioned
1 parent da5b792 commit 2658547

File tree

12 files changed

+205
-37
lines changed

12 files changed

+205
-37
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package uk.co.evoco.webdriver.configuration;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.List;
6+
7+
public class TolerantActionExceptions {
8+
9+
private String waitTimeoutInSeconds;
10+
private List<String> exceptionsToHandle;
11+
12+
/**
13+
*
14+
* @return the tolerant action wait time in seconds
15+
*/
16+
String getWaitTimeoutInSeconds() {
17+
return waitTimeoutInSeconds;
18+
}
19+
20+
/**
21+
*
22+
* @param waitTimeoutInSeconds set the tolerant action wait time in seconds
23+
*/
24+
@JsonProperty("waitTimeoutInSeconds")
25+
public void setWaitTimeoutInSeconds(String waitTimeoutInSeconds) {
26+
this.waitTimeoutInSeconds = waitTimeoutInSeconds;
27+
}
28+
29+
/**
30+
*
31+
* @return Exceptions list that we will use to tolerate in tolerable action wrappers
32+
*/
33+
public List<String> getExceptionsToHandle() {
34+
return exceptionsToHandle;
35+
}
36+
37+
/**
38+
*
39+
* @param exceptionsToHandle sets the list of exceptions for WebDriver that we will retry
40+
* on when using our tolerant wrapper
41+
*/
42+
@JsonProperty("exceptionsToHandle")
43+
public void setExceptionsToHandle(List<String> exceptionsToHandle) {
44+
this.exceptionsToHandle = exceptionsToHandle;
45+
}
46+
}

src/main/java/uk/co/evoco/webdriver/configuration/WebDriverConfig.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import java.net.MalformedURLException;
99
import java.net.URL;
10-
import java.util.List;
1110
import java.util.Map;
1211
import java.util.Optional;
1312

@@ -23,8 +22,8 @@ public class WebDriverConfig {
2322
private JsonNode testConfig;
2423
private GridConfig gridConfig;
2524
private RunType runType;
26-
private List<String> exceptionsToHandleOnTolerantActions;
2725
private Map<String, ObjectNode> browserPreferences;
26+
private TolerantActionExceptions tolerantActionExceptions;
2827

2928
/**
3029
*
@@ -185,19 +184,30 @@ public void setRunType(String runType) {
185184

186185
/**
187186
*
188-
* @return Exceptions list that we will use to tolerate in tolerable action wrappers
187+
* @return the tolerant action exceptions config
189188
*/
190-
public List<String> getExceptionsToHandleOnTolerantActions() {
191-
return exceptionsToHandleOnTolerantActions;
189+
190+
public TolerantActionExceptions getTolerantActionExceptions() {
191+
return tolerantActionExceptions;
192+
}
193+
194+
/**
195+
*
196+
* @param tolerantActionExceptions set the tolerant action exceptions and tolerant action wait time in Seconds
197+
*/
198+
@JsonProperty("tolerantActionExceptions")
199+
public void setTolerantActionExceptions(TolerantActionExceptions tolerantActionExceptions) {
200+
this.tolerantActionExceptions = tolerantActionExceptions;
192201
}
193202

194203
/**
195204
*
196-
* @param exceptionsToHandleOnTolerantActions sets the list of exceptions for WebDriver that we will retry
197-
* on when using our tolerant wrapper
205+
* @return tolerant action wait time in seconds if user specify the time in config file
206+
* other wise return default webdriver time out
198207
*/
199-
@JsonProperty("exceptionsToHandleOnTolerantActions")
200-
public void setExceptionsToHandleOnTolerantActions(List<String> exceptionsToHandleOnTolerantActions) {
201-
this.exceptionsToHandleOnTolerantActions = exceptionsToHandleOnTolerantActions;
208+
public int getTolerantActionWaitTimeoutInSeconds() {
209+
return Optional.ofNullable(tolerantActionExceptions.getWaitTimeoutInSeconds())
210+
.map(Integer::parseInt)
211+
.orElse((int) webDriverWaitTimeout);
202212
}
203213
}

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

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

33
import org.openqa.selenium.WebElement;
4+
import uk.co.evoco.webdriver.configuration.TestConfigManager;
45

56
import java.util.Optional;
67

@@ -19,4 +20,9 @@ public final class ClickUtils extends TolerantInteraction {
1920
public static void tolerantClick(WebElement webElement, int timeout) throws Throwable {
2021
new SendKeysUtils().tolerantInteraction(webElement, Optional.empty(), timeout);
2122
}
23+
24+
public static void tolerantClick(WebElement webElement) throws Throwable {
25+
new SendKeysUtils().tolerantInteraction(webElement, Optional.empty(),
26+
TestConfigManager.get().getTolerantActionWaitTimeoutInSeconds());
27+
}
2228
}

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

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

33
import org.openqa.selenium.WebElement;
4+
import uk.co.evoco.webdriver.configuration.TestConfigManager;
45

56
import java.util.List;
67

@@ -12,7 +13,8 @@ public final class RadioButtonUtils extends TolerantInteraction {
1213
/**
1314
* Given a list of WebElements that locate the labels of the radio buttons,
1415
* finds the radio button with the given visible label text and selects it.
15-
* @param webElements active WebElements, already located
16+
*
17+
* @param webElements active WebElements, already located
1618
* @param visibleLabelText text that is visible on the page in the label tags
1719
*/
1820
public static void selectByLabel(List<WebElement> webElements, String visibleLabelText) {
@@ -25,14 +27,19 @@ public static void selectByLabel(List<WebElement> webElements, String visibleLab
2527
}
2628

2729
/**
28-
*
29-
* @param webElements active WebElements, already located
30+
* @param webElements active WebElements, already located
3031
* @param visibleLabelText text that is visible on the page in the label tags
31-
* @param timeout time in seconds to keep trying
32+
* @param timeout time in seconds to keep trying
3233
* @throws Throwable any unhandled or un-tolerated exception
3334
*/
3435
public static void tolerantSelectByLabel(List<WebElement> webElements, String visibleLabelText, int timeout)
3536
throws Throwable {
3637
new RadioButtonUtils().tolerantInteraction(webElements, visibleLabelText, timeout);
3738
}
39+
40+
public static void tolerantSelectByLabel(List<WebElement> webElements, String visibleLabelText)
41+
throws Throwable {
42+
new RadioButtonUtils().tolerantInteraction(webElements, visibleLabelText,
43+
TestConfigManager.get().getTolerantActionWaitTimeoutInSeconds());
44+
}
3845
}

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

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

33
import org.openqa.selenium.WebElement;
44
import org.openqa.selenium.support.ui.Select;
5+
import uk.co.evoco.webdriver.configuration.TestConfigManager;
56

67
import java.util.Optional;
78

@@ -14,6 +15,7 @@ public final class SelectBoxUtils extends TolerantInteraction {
1415

1516
/**
1617
* Selects an option that has a matching value attribute in the Options tag markup
18+
*
1719
* @param selectBox active WebElement, already located
1820
* @param htmlValue HTML value attribute
1921
*/
@@ -25,8 +27,9 @@ public static void itemByHtmlValueAttribute(WebElement selectBox, String htmlVal
2527
/**
2628
* Selects an option by the index of the option in the list.
2729
* The input is NOT zero based, we're normalising the input internally.
30+
*
2831
* @param selectBox active WebElement, already located
29-
* @param index index in order of display
32+
* @param index index in order of display
3033
*/
3134
public static void itemByIndex(WebElement selectBox, int index) {
3235
int normalisedIndex = index - 1;
@@ -36,7 +39,8 @@ public static void itemByIndex(WebElement selectBox, int index) {
3639

3740
/**
3841
* Selects an option by the text that is visible in the select box
39-
* @param selectBox active WebElement, already located
42+
*
43+
* @param selectBox active WebElement, already located
4044
* @param visibleText visible text in the select box (NOT the HTML value attribute)
4145
*/
4246
public static void itemByVisibleText(WebElement selectBox, String visibleText) {
@@ -45,42 +49,47 @@ public static void itemByVisibleText(WebElement selectBox, String visibleText) {
4549
}
4650

4751
/**
48-
*
4952
* @param webElement active WebElement, already located
50-
* @param htmlValue HTML value attribute
51-
* @param timeout time in seconds to keep trying
53+
* @param htmlValue HTML value attribute
54+
* @param timeout time in seconds to keep trying
5255
* @throws Throwable any unhandled or un-tolerated exception
5356
*/
54-
public static void tolerantItemByHtmlValueAttribute(WebElement webElement, String htmlValue, int timeout)
57+
public static void tolerantItemByHtmlValueAttribute(WebElement webElement, String htmlValue, int timeout)
5558
throws Throwable {
5659
new SendKeysUtils().tolerantInteraction(
5760
webElement, SelectBoxInteractionType.BY_VALUE, Optional.of(htmlValue), Optional.empty(), timeout);
5861
}
5962

6063
/**
61-
*
62-
* @param webElement active WebElement, already located
64+
* @param webElement active WebElement, already located
6365
* @param visibleText visible text in the select box (NOT the HTML value attribute)
64-
* @param timeout time in seconds to keep trying
66+
* @param timeout time in seconds to keep trying
6567
* @throws Throwable any unhandled or un-tolerated exception
6668
*/
67-
public static void tolerantItemByVisibleText(WebElement webElement, String visibleText, int timeout)
69+
public static void tolerantItemByVisibleText(WebElement webElement, String visibleText, int timeout)
6870
throws Throwable {
6971
new SendKeysUtils().tolerantInteraction(
7072
webElement, SelectBoxInteractionType.BY_VISIBLE_TEXT,
7173
Optional.of(visibleText), Optional.empty(), timeout);
7274
}
7375

7476
/**
75-
*
7677
* @param webElement active WebElement, already located
77-
* @param index index in order of display
78-
* @param timeout time in seconds to keep trying
78+
* @param index index in order of display
79+
* @param timeout time in seconds to keep trying
7980
* @throws Throwable any unhandled or un-tolerated exception
8081
*/
81-
public static void tolerantItemByIndex(WebElement webElement, int index, int timeout) throws Throwable {
82+
public static void tolerantItemByIndex(WebElement webElement, int index, int timeout) throws Throwable {
8283
new SendKeysUtils().tolerantInteraction(
8384
webElement, SelectBoxInteractionType.BY_INDEX, Optional.empty(), Optional.of(index), timeout);
8485
}
8586

87+
public static void tolerantItemByVisibleText(WebElement webElement, String visibleText)
88+
throws Throwable {
89+
new SendKeysUtils().tolerantInteraction(
90+
webElement, SelectBoxInteractionType.BY_VISIBLE_TEXT,
91+
Optional.of(visibleText), Optional.empty(),
92+
TestConfigManager.get().getTolerantActionWaitTimeoutInSeconds());
93+
}
94+
8695
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package uk.co.evoco.webdriver.utils;
22

33
import org.openqa.selenium.WebElement;
4+
import uk.co.evoco.webdriver.configuration.TestConfigManager;
45

56
import java.util.Optional;
67

78
public final class SendKeysUtils extends TolerantInteraction {
89

910
/**
10-
*
1111
* @param webElement active WebElement, already located
1212
* @param textToType text that should be typed
13-
* @param timeout time in seconds to keep trying
13+
* @param timeout time in seconds to keep trying
1414
* @throws Throwable any unhandled or un-tolerated exception
1515
*/
1616
public static void tolerantType(WebElement webElement, String textToType, int timeout) throws Throwable {
1717
new SendKeysUtils().tolerantInteraction(webElement, Optional.of(textToType), timeout);
1818
}
19+
20+
public static void tolerantType(WebElement webElement, String textToType) throws Throwable {
21+
new SendKeysUtils().tolerantInteraction(webElement, Optional.of(textToType),
22+
TestConfigManager.get().getTolerantActionWaitTimeoutInSeconds());
23+
}
1924
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ private void interact(Select selectBox, int index) {
190190
}
191191

192192
private Throwable propagateIfNotIgnored(Throwable e) throws Throwable {
193-
for (String ignoredException : TestConfigManager.get().getExceptionsToHandleOnTolerantActions()) {
193+
for (String ignoredException : TestConfigManager.get()
194+
.getTolerantActionExceptions().getExceptionsToHandle()) {
194195
if (Class.forName(ignoredException).isInstance(e)) {
195196
logger.info("Exception {} will be ignored", ignoredException);
196197
} else {

src/test/java/uk/co/evoco/webdriver/configuration/WebDriverConfigTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,22 @@ public void testSettingBaseUrlWithBadUrlGivesGoodException() {
9191
webDriverConfig.setBaseUrl("bad-url");
9292
});
9393
}
94+
95+
@Test
96+
public void testExceptionsWaitTimeOutReturnValue() throws IOException {
97+
WebDriverConfig webDriverConfig = JsonUtils.fromFile(
98+
ClassLoader.getSystemResourceAsStream("fixtures/sample-config-with-tolerant-action-wait-time.json")
99+
, WebDriverConfig.class);
100+
assertThat(webDriverConfig.getTolerantActionWaitTimeoutInSeconds(), is(5));
101+
102+
}
103+
104+
@Test
105+
public void testExceptionsWaitTimeOutNotReturnValue() throws IOException {
106+
WebDriverConfig webDriverConfig = JsonUtils.fromFile(
107+
ClassLoader.getSystemResourceAsStream("fixtures/sample-config-with-out-tolerant-action-wait-time.json")
108+
, WebDriverConfig.class);
109+
assertThat(webDriverConfig.getTolerantActionWaitTimeoutInSeconds(), is(30));
110+
111+
}
94112
}

src/test/java/uk/co/evoco/webdriver/utils/WebDriverUtilsIT.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,12 @@ public void testCanClickOnHrefWithTolerantPollingWaitBeforeClickingHelperWhenEle
101101
ClickUtils.tolerantClick(element, 5);
102102
});
103103
}
104+
105+
@Test
106+
public void testCanClickOnHrefWithTolerantPollingWaitTimeGetFromConfigBeforeClickingHelperWhenElementIsDisabledAllTheTime() {
107+
assertThrows(TimeoutException.class, () -> {
108+
WebElement element = webDriver.findElement(By.id("clickutils-href-always-disabled"));
109+
ClickUtils.tolerantClick(element);
110+
});
111+
}
104112
}

src/test/resources/config.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010
"gridConfig": {
1111
"url": "http://localhost:4444/wd/hub"
1212
},
13-
"browserPreferences" : {
13+
"browserPreferences": {
1414
"firefox": {
1515
"browser.download.defaultFolder": "downloads/reports",
1616
"pdfjs.disabled": true,
1717
"browser.download.folderList": 2
1818
}
1919
},
20-
"exceptionsToHandleOnTolerantActions": [
21-
"StaleElementReferenceException",
22-
"ElementClickInterceptedException",
23-
"ElementNotInteractableException"
24-
]
20+
"tolerantActionExceptions": {
21+
"waitTimeoutInSeconds": 5,
22+
"exceptionsToHandle": [
23+
"StaleElementReferenceException",
24+
"ElementClickInterceptedException",
25+
"ElementNotInteractableException"
26+
]
27+
}
2528
}

0 commit comments

Comments
 (0)