Skip to content

Commit 25e5dcc

Browse files
balu836balavengaiah.matam
andauthored
Tolerant methods (#52)
* 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 <[email protected]>
1 parent c0ef4b3 commit 25e5dcc

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package uk.co.evoco.webdriver.utils;
2+
3+
import com.codahale.metrics.Timer;
4+
import org.openqa.selenium.WebElement;
5+
import uk.co.evoco.metrics.MetricRegistryHelper;
6+
import uk.co.evoco.webdriver.configuration.TestConfigHelper;
7+
8+
import static com.codahale.metrics.MetricRegistry.name;
9+
10+
public class ClearUtils extends TolerantInteraction {
11+
private static final Timer tolerantClearAction = MetricRegistryHelper.get().timer(name("ClearUtils.tolerantClear"));
12+
13+
/**
14+
*
15+
* @param webElement active WebElement, already located
16+
* @param timeout time in seconds to keep trying
17+
* @throws Throwable any unhandled or un-tolerated exception
18+
*/
19+
public static void tolerantClear(WebElement webElement, int timeout) throws Throwable {
20+
try(final Timer.Context ignored = tolerantClearAction.time()) {
21+
new ClearUtils().tolerantInteractionToClear(webElement, timeout);
22+
}
23+
}
24+
25+
/**
26+
*
27+
* @param webElement active WebElement, already located
28+
* @throws Throwable any unhandled or un-tolerated exception
29+
*/
30+
public static void tolerantClear(WebElement webElement) throws Throwable {
31+
try(final Timer.Context ignored = tolerantClearAction.time()) {
32+
new ClearUtils().tolerantInteractionToClear(webElement,
33+
TestConfigHelper.get().getTolerantActionWaitTimeoutInSeconds());
34+
}
35+
}
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package uk.co.evoco.webdriver.utils;
2+
3+
import com.codahale.metrics.Timer;
4+
import org.openqa.selenium.WebElement;
5+
import uk.co.evoco.metrics.MetricRegistryHelper;
6+
import uk.co.evoco.webdriver.configuration.TestConfigHelper;
7+
8+
import java.util.Optional;
9+
10+
import static com.codahale.metrics.MetricRegistry.name;
11+
12+
public class GetAttributeUtils extends TolerantInteraction {
13+
private static final Timer tolerantGetAttributeAction = MetricRegistryHelper.get().timer(name("GetAttributeUtils.tolerantGetAttribute"));
14+
15+
/**
16+
*
17+
* @param webElement active WebElement, already located
18+
* @param attribute WebElement attribute
19+
* @param timeout time in seconds to keep trying
20+
* @return attribute property value
21+
* @throws Throwable any unhandled or un-tolerated exception
22+
*/
23+
public static String tolerantGetAttribute(WebElement webElement, String attribute, int timeout) throws Throwable {
24+
try (final Timer.Context ignored = tolerantGetAttributeAction.time()) {
25+
return new GetAttributeUtils().tolerantInteractionToGetAttribute(webElement,attribute,timeout);
26+
}
27+
}
28+
29+
/**
30+
*
31+
* @param webElement active WebElement, already located
32+
* @param attribute WebElement attribute
33+
* @return attribute property value
34+
* @throws Throwable any unhandled or un-tolerated exception
35+
*/
36+
public static String tolerantGetAttribute(WebElement webElement, String attribute) throws Throwable {
37+
try (final Timer.Context ignored = tolerantGetAttributeAction.time()) {
38+
return new GetAttributeUtils().tolerantInteractionToGetAttribute(webElement,attribute,
39+
TestConfigHelper.get().getTolerantActionWaitTimeoutInSeconds());
40+
}
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package uk.co.evoco.webdriver.utils;
2+
3+
import com.codahale.metrics.Timer;
4+
import org.openqa.selenium.WebElement;
5+
import uk.co.evoco.metrics.MetricRegistryHelper;
6+
import uk.co.evoco.webdriver.configuration.TestConfigHelper;
7+
8+
import static com.codahale.metrics.MetricRegistry.name;
9+
10+
public class GetTextUtils extends TolerantInteraction {
11+
private static final Timer tolerantGetTextAction = MetricRegistryHelper.get().timer(name("GetTextUtils.tolerantGetText"));
12+
13+
/**
14+
*
15+
* @param webElement active WebElement, already located
16+
* @param timeout time in seconds to keep trying
17+
* @return text property value
18+
* @throws Throwable any unhandled or un-tolerated exception
19+
*/
20+
public static String tolerantGetText(WebElement webElement, int timeout) throws Throwable {
21+
try (final Timer.Context ignored = tolerantGetTextAction.time()) {
22+
return new GetTextUtils().tolerantInteractionToGetText(webElement,timeout);
23+
}
24+
}
25+
26+
/**
27+
*
28+
* @param webElement active WebElement, already located
29+
* @return text property value
30+
* @throws Throwable any unhandled or un-tolerated exception
31+
*/
32+
33+
public static String tolerantGetText(WebElement webElement) throws Throwable {
34+
try (final Timer.Context ignored = tolerantGetTextAction.time()) {
35+
return new GetTextUtils().tolerantInteractionToGetText(webElement,
36+
TestConfigHelper.get().getTolerantActionWaitTimeoutInSeconds());
37+
}
38+
}
39+
}

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

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,131 @@ public WebElement tolerantInteraction(
165165
}
166166
}
167167

168+
/**
169+
*
170+
* @param webElement locator we will use to re-lookup the element on retry
171+
* @param attribute WebElement attribute
172+
* @param timeoutInSeconds time to continue trying for
173+
* @return attribute property value
174+
* @throws Throwable the last exception to be thrown
175+
*/
176+
public String tolerantInteractionToGetAttribute(
177+
WebElement webElement, String attribute, int timeoutInSeconds)
178+
throws Throwable {
179+
end = clock.instant().plusSeconds(timeoutInSeconds);
180+
while (true) {
181+
try {
182+
if (Boolean.TRUE.equals(webElement.isEnabled())) {
183+
return interactToGetAttribute(webElement, attribute);
184+
}
185+
} catch (Throwable e) {
186+
lastException = propagateIfNotIgnored(e);
187+
}
188+
if (end.isBefore(clock.instant())) {
189+
if (null == lastException) {
190+
logger.error(
191+
"Exception condition failed: Timeout (tried for {} seconds with 500ms interval",
192+
timeoutInSeconds);
193+
lastException = new TimeoutException();
194+
} else {
195+
logger.error("Exception condition failed: {} (tried for {} seconds with 500ms interval",
196+
lastException.getCause(), timeoutInSeconds);
197+
}
198+
throw lastException;
199+
}
200+
201+
try {
202+
sleeper.sleep(intervalDuration);
203+
} catch (InterruptedException e) {
204+
Thread.currentThread().interrupt();
205+
throw new WebDriverException(e);
206+
}
207+
}
208+
}
209+
210+
/**
211+
*
212+
* @param webElement locator we will use to re-lookup the element on retry
213+
* @param timeoutInSeconds time to continue trying for
214+
* @return text value of the WebElement
215+
* @throws Throwable the last exception to be thrown
216+
*/
217+
public String tolerantInteractionToGetText(
218+
WebElement webElement, int timeoutInSeconds)
219+
throws Throwable {
220+
end = clock.instant().plusSeconds(timeoutInSeconds);
221+
while (true) {
222+
try {
223+
if (Boolean.TRUE.equals(webElement.isEnabled())) {
224+
return interactToGetText(webElement);
225+
}
226+
} catch (Throwable e) {
227+
lastException = propagateIfNotIgnored(e);
228+
}
229+
if (end.isBefore(clock.instant())) {
230+
if (null == lastException) {
231+
logger.error(
232+
"Exception condition failed: Timeout (tried for {} seconds with 500ms interval",
233+
timeoutInSeconds);
234+
lastException = new TimeoutException();
235+
} else {
236+
logger.error("Exception condition failed: {} (tried for {} seconds with 500ms interval",
237+
lastException.getCause(), timeoutInSeconds);
238+
}
239+
throw lastException;
240+
}
241+
242+
try {
243+
sleeper.sleep(intervalDuration);
244+
} catch (InterruptedException e) {
245+
Thread.currentThread().interrupt();
246+
throw new WebDriverException(e);
247+
}
248+
}
249+
}
250+
251+
/**
252+
*
253+
* @param webElement locator we will use to re-lookup the element on retry
254+
* @param timeoutInSeconds time to continue trying for
255+
* @return WebElement to allow fluent method stringed calls
256+
* @throws Throwable the last exception to be thrown
257+
*/
258+
public WebElement tolerantInteractionToClear(
259+
WebElement webElement, int timeoutInSeconds)
260+
throws Throwable {
261+
end = clock.instant().plusSeconds(timeoutInSeconds);
262+
while (true) {
263+
try {
264+
if (Boolean.TRUE.equals(webElement.isEnabled())) {
265+
interactToClearField(webElement);
266+
return webElement;
267+
}
268+
} catch (Throwable e) {
269+
lastException = propagateIfNotIgnored(e);
270+
}
271+
if (end.isBefore(clock.instant())) {
272+
if (null == lastException) {
273+
logger.error(
274+
"Exception condition failed: Timeout (tried for {} seconds with 500ms interval",
275+
timeoutInSeconds);
276+
lastException = new TimeoutException();
277+
} else {
278+
logger.error("Exception condition failed: {} (tried for {} seconds with 500ms interval",
279+
lastException.getCause(), timeoutInSeconds);
280+
}
281+
throw lastException;
282+
}
283+
284+
try {
285+
sleeper.sleep(intervalDuration);
286+
} catch (InterruptedException e) {
287+
Thread.currentThread().interrupt();
288+
throw new WebDriverException(e);
289+
}
290+
}
291+
}
292+
168293
private void interact(WebElement webElement, String textToType) {
169294
webElement.sendKeys(textToType);
170295
}
@@ -173,6 +298,18 @@ private void interact(WebElement webElement) {
173298
webElement.click();
174299
}
175300

301+
private String interactToGetText(WebElement webElement) {
302+
return webElement.getText();
303+
}
304+
305+
private String interactToGetAttribute(WebElement webElement, String attribute) {
306+
return webElement.getAttribute(attribute);
307+
}
308+
309+
private void interactToClearField(WebElement webElement) {
310+
webElement.clear();
311+
}
312+
176313
private void interact(Select selectBox, String value, SelectBoxInteractionType selectBoxInteractionType) {
177314
switch(selectBoxInteractionType) {
178315
case BY_VALUE:

0 commit comments

Comments
 (0)