|
1 | 1 | package uk.co.evoco.webdriver.utils;
|
2 | 2 |
|
| 3 | +import org.openqa.selenium.By; |
3 | 4 | import org.openqa.selenium.WebDriver;
|
4 | 5 | import org.openqa.selenium.WebElement;
|
5 | 6 | import org.openqa.selenium.support.ui.ExpectedConditions;
|
@@ -50,7 +51,45 @@ public static void tolerantClick(WebDriver webDriver, WebElement webElement) thr
|
50 | 51 | }
|
51 | 52 | }
|
52 | 53 |
|
| 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 | + |
53 | 87 | private static void click(WebDriver webDriver, WebElement webElement, long timeout) {
|
54 | 88 | new WebDriverWait(webDriver, timeout).until(ExpectedConditions.elementToBeClickable(webElement)).click();
|
55 | 89 | }
|
| 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 | + } |
56 | 95 | }
|
0 commit comments