Skip to content

Commit 8dc874f

Browse files
Merge pull request #23 from digital-delivery-academy/tolerant-click-new-feature-#22
Adding new tolerantClick method
2 parents 103571b + a44a23d commit 8dc874f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

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

+39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.co.evoco.webdriver.utils;
22

3+
import org.openqa.selenium.By;
34
import org.openqa.selenium.WebDriver;
45
import org.openqa.selenium.WebElement;
56
import org.openqa.selenium.support.ui.ExpectedConditions;
@@ -50,7 +51,45 @@ public static void tolerantClick(WebDriver webDriver, WebElement webElement) thr
5051
}
5152
}
5253

54+
/**
55+
*
56+
* Use this method if you're experiencing exceptions that are UN-FIXABLE in your application
57+
* and you find that you're getting intermittent (and unhelpful) exceptions.
58+
*
59+
* This method takes its configuration from the configuration file (see documentation for how to add exceptions
60+
* to the list of exceptions that this method will tolerate).
61+
*
62+
* It retries only once and waits for 3 seconds. This may become configurable in the future.
63+
*
64+
* @param webDriver active WebDriver instance
65+
* @param locator locator we will use to re-lookup the element on retry
66+
* @throws InterruptedException
67+
*/
68+
public static void tolerantClick(WebDriver webDriver, By locator) throws InterruptedException {
69+
try {
70+
click(webDriver, locator, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout());
71+
} catch(Exception e) {
72+
logger.error("Encountered an issue while trying to click, will check to see if we tolerate this exception. Debug this issue to make your tests more stable. Stacktrace follows.");
73+
e.printStackTrace();
74+
for(String exceptionToHandle : TestConfigManager.getInstance().getWebDriverConfig().getExceptionsToHandleOnTolerantActions()) {
75+
if(e.getClass().getName().contains(exceptionToHandle)) {
76+
logger.error("Exception {} is tolerated, retrying after a 3 second wait", exceptionToHandle);
77+
Thread.sleep(3);
78+
logger.error("Waited 3 seconds after {}, now retrying", exceptionToHandle);
79+
click(webDriver, locator, TestConfigManager.getInstance().getWebDriverConfig().getWebDriverWaitTimeout());
80+
}
81+
}
82+
// Can't handle the exception with a retry so re-throwing the exception
83+
throw e;
84+
}
85+
}
86+
5387
private static void click(WebDriver webDriver, WebElement webElement, long timeout) {
5488
new WebDriverWait(webDriver, timeout).until(ExpectedConditions.elementToBeClickable(webElement)).click();
5589
}
90+
91+
private static void click(WebDriver webDriver, By locator, long timeout) {
92+
WebElement webElement = webDriver.findElement(locator);
93+
new WebDriverWait(webDriver, timeout).until(ExpectedConditions.elementToBeClickable(webElement)).click();
94+
}
5695
}

0 commit comments

Comments
 (0)