Skip to content

Commit

Permalink
Tolerant methods (#52)
Browse files Browse the repository at this point in the history
* 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

* futureDataAvoidingWeekendsAndBankHolidays issue which need to avoid weekends when adding bank holidays count

* added overload method for tolerantItemByIndex and tolerantItemByHtmlValueAttribute

* resolved conflicts

* changed from private package to public

* Changed onException method to take screenshot only on Local run not on running on grid

* Changed onException method to take screenshot only on Local run not on running on grid

* Added boolean parameter in config file to handle taking snapshots

* 1.Created tolerant methods for clear,getAttribute,getText and isDisplayed methods.
2. Modified interact methods to handle tolerant methods.

* 1.Created tolerant methods for clear,getAttribute and getText methods to handle required exceptions

Co-authored-by: balavengaiah.matam <balavengaiah.matam@dwp.gov.uk>
  • Loading branch information
balu836 and balavengaiah.matam authored Apr 29, 2020
1 parent c0ef4b3 commit 25e5dcc
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/uk/co/evoco/webdriver/utils/ClearUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package uk.co.evoco.webdriver.utils;

import com.codahale.metrics.Timer;
import org.openqa.selenium.WebElement;
import uk.co.evoco.metrics.MetricRegistryHelper;
import uk.co.evoco.webdriver.configuration.TestConfigHelper;

import static com.codahale.metrics.MetricRegistry.name;

public class ClearUtils extends TolerantInteraction {
private static final Timer tolerantClearAction = MetricRegistryHelper.get().timer(name("ClearUtils.tolerantClear"));

/**
*
* @param webElement active WebElement, already located
* @param timeout time in seconds to keep trying
* @throws Throwable any unhandled or un-tolerated exception
*/
public static void tolerantClear(WebElement webElement, int timeout) throws Throwable {
try(final Timer.Context ignored = tolerantClearAction.time()) {
new ClearUtils().tolerantInteractionToClear(webElement, timeout);
}
}

/**
*
* @param webElement active WebElement, already located
* @throws Throwable any unhandled or un-tolerated exception
*/
public static void tolerantClear(WebElement webElement) throws Throwable {
try(final Timer.Context ignored = tolerantClearAction.time()) {
new ClearUtils().tolerantInteractionToClear(webElement,
TestConfigHelper.get().getTolerantActionWaitTimeoutInSeconds());
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/uk/co/evoco/webdriver/utils/GetAttributeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package uk.co.evoco.webdriver.utils;

import com.codahale.metrics.Timer;
import org.openqa.selenium.WebElement;
import uk.co.evoco.metrics.MetricRegistryHelper;
import uk.co.evoco.webdriver.configuration.TestConfigHelper;

import java.util.Optional;

import static com.codahale.metrics.MetricRegistry.name;

public class GetAttributeUtils extends TolerantInteraction {
private static final Timer tolerantGetAttributeAction = MetricRegistryHelper.get().timer(name("GetAttributeUtils.tolerantGetAttribute"));

/**
*
* @param webElement active WebElement, already located
* @param attribute WebElement attribute
* @param timeout time in seconds to keep trying
* @return attribute property value
* @throws Throwable any unhandled or un-tolerated exception
*/
public static String tolerantGetAttribute(WebElement webElement, String attribute, int timeout) throws Throwable {
try (final Timer.Context ignored = tolerantGetAttributeAction.time()) {
return new GetAttributeUtils().tolerantInteractionToGetAttribute(webElement,attribute,timeout);
}
}

/**
*
* @param webElement active WebElement, already located
* @param attribute WebElement attribute
* @return attribute property value
* @throws Throwable any unhandled or un-tolerated exception
*/
public static String tolerantGetAttribute(WebElement webElement, String attribute) throws Throwable {
try (final Timer.Context ignored = tolerantGetAttributeAction.time()) {
return new GetAttributeUtils().tolerantInteractionToGetAttribute(webElement,attribute,
TestConfigHelper.get().getTolerantActionWaitTimeoutInSeconds());
}
}
}
39 changes: 39 additions & 0 deletions src/main/java/uk/co/evoco/webdriver/utils/GetTextUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.co.evoco.webdriver.utils;

import com.codahale.metrics.Timer;
import org.openqa.selenium.WebElement;
import uk.co.evoco.metrics.MetricRegistryHelper;
import uk.co.evoco.webdriver.configuration.TestConfigHelper;

import static com.codahale.metrics.MetricRegistry.name;

public class GetTextUtils extends TolerantInteraction {
private static final Timer tolerantGetTextAction = MetricRegistryHelper.get().timer(name("GetTextUtils.tolerantGetText"));

/**
*
* @param webElement active WebElement, already located
* @param timeout time in seconds to keep trying
* @return text property value
* @throws Throwable any unhandled or un-tolerated exception
*/
public static String tolerantGetText(WebElement webElement, int timeout) throws Throwable {
try (final Timer.Context ignored = tolerantGetTextAction.time()) {
return new GetTextUtils().tolerantInteractionToGetText(webElement,timeout);
}
}

/**
*
* @param webElement active WebElement, already located
* @return text property value
* @throws Throwable any unhandled or un-tolerated exception
*/

public static String tolerantGetText(WebElement webElement) throws Throwable {
try (final Timer.Context ignored = tolerantGetTextAction.time()) {
return new GetTextUtils().tolerantInteractionToGetText(webElement,
TestConfigHelper.get().getTolerantActionWaitTimeoutInSeconds());
}
}
}
137 changes: 137 additions & 0 deletions src/main/java/uk/co/evoco/webdriver/utils/TolerantInteraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,131 @@ public WebElement tolerantInteraction(
}
}

/**
*
* @param webElement locator we will use to re-lookup the element on retry
* @param attribute WebElement attribute
* @param timeoutInSeconds time to continue trying for
* @return attribute property value
* @throws Throwable the last exception to be thrown
*/
public String tolerantInteractionToGetAttribute(
WebElement webElement, String attribute, int timeoutInSeconds)
throws Throwable {
end = clock.instant().plusSeconds(timeoutInSeconds);
while (true) {
try {
if (Boolean.TRUE.equals(webElement.isEnabled())) {
return interactToGetAttribute(webElement, attribute);
}
} catch (Throwable e) {
lastException = propagateIfNotIgnored(e);
}
if (end.isBefore(clock.instant())) {
if (null == lastException) {
logger.error(
"Exception condition failed: Timeout (tried for {} seconds with 500ms interval",
timeoutInSeconds);
lastException = new TimeoutException();
} else {
logger.error("Exception condition failed: {} (tried for {} seconds with 500ms interval",
lastException.getCause(), timeoutInSeconds);
}
throw lastException;
}

try {
sleeper.sleep(intervalDuration);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new WebDriverException(e);
}
}
}

/**
*
* @param webElement locator we will use to re-lookup the element on retry
* @param timeoutInSeconds time to continue trying for
* @return text value of the WebElement
* @throws Throwable the last exception to be thrown
*/
public String tolerantInteractionToGetText(
WebElement webElement, int timeoutInSeconds)
throws Throwable {
end = clock.instant().plusSeconds(timeoutInSeconds);
while (true) {
try {
if (Boolean.TRUE.equals(webElement.isEnabled())) {
return interactToGetText(webElement);
}
} catch (Throwable e) {
lastException = propagateIfNotIgnored(e);
}
if (end.isBefore(clock.instant())) {
if (null == lastException) {
logger.error(
"Exception condition failed: Timeout (tried for {} seconds with 500ms interval",
timeoutInSeconds);
lastException = new TimeoutException();
} else {
logger.error("Exception condition failed: {} (tried for {} seconds with 500ms interval",
lastException.getCause(), timeoutInSeconds);
}
throw lastException;
}

try {
sleeper.sleep(intervalDuration);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new WebDriverException(e);
}
}
}

/**
*
* @param webElement locator we will use to re-lookup the element on retry
* @param timeoutInSeconds time to continue trying for
* @return WebElement to allow fluent method stringed calls
* @throws Throwable the last exception to be thrown
*/
public WebElement tolerantInteractionToClear(
WebElement webElement, int timeoutInSeconds)
throws Throwable {
end = clock.instant().plusSeconds(timeoutInSeconds);
while (true) {
try {
if (Boolean.TRUE.equals(webElement.isEnabled())) {
interactToClearField(webElement);
return webElement;
}
} catch (Throwable e) {
lastException = propagateIfNotIgnored(e);
}
if (end.isBefore(clock.instant())) {
if (null == lastException) {
logger.error(
"Exception condition failed: Timeout (tried for {} seconds with 500ms interval",
timeoutInSeconds);
lastException = new TimeoutException();
} else {
logger.error("Exception condition failed: {} (tried for {} seconds with 500ms interval",
lastException.getCause(), timeoutInSeconds);
}
throw lastException;
}

try {
sleeper.sleep(intervalDuration);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new WebDriverException(e);
}
}
}

private void interact(WebElement webElement, String textToType) {
webElement.sendKeys(textToType);
}
Expand All @@ -173,6 +298,18 @@ private void interact(WebElement webElement) {
webElement.click();
}

private String interactToGetText(WebElement webElement) {
return webElement.getText();
}

private String interactToGetAttribute(WebElement webElement, String attribute) {
return webElement.getAttribute(attribute);
}

private void interactToClearField(WebElement webElement) {
webElement.clear();
}

private void interact(Select selectBox, String value, SelectBoxInteractionType selectBoxInteractionType) {
switch(selectBoxInteractionType) {
case BY_VALUE:
Expand Down

0 comments on commit 25e5dcc

Please sign in to comment.