Skip to content

Commit 2dac8e4

Browse files
author
Danny Stevens
committed
Fixed waitFor to ensure it retries correctly and that html match strings will ignore white space discrepancies with the page source.
1 parent 341aab1 commit 2dac8e4

File tree

2 files changed

+56
-22
lines changed
  • madcow-webdriver/src

2 files changed

+56
-22
lines changed

madcow-webdriver/src/main/groovy/au/com/ps4impact/madcow/runner/webdriver/blade/WaitFor.groovy

+47-20
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,69 @@ import au.com.ps4impact.madcow.step.MadcowStep
2626
import org.apache.commons.lang3.StringUtils
2727
import au.com.ps4impact.madcow.step.MadcowStepResult
2828
import au.com.ps4impact.madcow.runner.webdriver.WebDriverBladeRunner
29+
import org.apache.log4j.Logger
2930

3031
/**
31-
* Blade runner to wait for a specific element/text to appear
32+
* Blade runner to wait for a specific element/text to appear.
33+
* Time out after 30 seconds.
3234
*
3335
* @author: Gavin Bunney
3436
*/
3537
class WaitFor extends WebDriverBladeRunner {
3638

37-
public void execute(WebDriverStepRunner stepRunner, MadcowStep step) {
39+
private static final int maxSeconds = 30;
3840

39-
final boolean found = (1..30).any {
4041

41-
try {
42-
if (StringUtils.isEmpty(step.blade.mappingSelectorValue)) {
43-
if (step.blade.parameters ?: '' == '' || stepRunner.driver.pageSource.contains(step.blade.parameters as String)) {
44-
step.testCase.logInfo("Waiting for: $step.blade.parameters");
45-
return true;
46-
} else {
47-
throw new NoSuchElementException();
48-
}
49-
} else {
50-
def element = findElement(stepRunner, step);
51-
if (StringUtils.isNotEmpty((step.blade.parameters ?: '')))
52-
return element.text == step.blade.parameters as String || element.getAttribute('value')==step.blade.parameters as String;
53-
else
54-
return true;
55-
}
56-
} catch (Exception ignored) { }
42+
public void execute(WebDriverStepRunner stepRunner, MadcowStep step) {
43+
boolean found = false;
44+
for (count in 1..maxSeconds) {
45+
found = isDesiredConditionFound(count, step, stepRunner, found)
46+
if (found) break;
5747
Thread.sleep(1000);
58-
return false;
5948
}
6049

6150
if (found)
6251
step.result = MadcowStepResult.PASS();
6352
else
6453
step.result = MadcowStepResult.FAIL('Element didn\'t appear within the timeout');
54+
55+
}
56+
57+
private boolean isDesiredConditionFound(int count, MadcowStep step, WebDriverStepRunner stepRunner, boolean found) {
58+
try {
59+
step.testCase.logInfo("Waiting ("+count+") for: $step.blade.parameters");
60+
if (StringUtils.isEmpty(step.blade.mappingSelectorValue)) {
61+
if (StringUtils.isBlank(step.blade.parameters)) {
62+
found = true; // request has no pattern to match
63+
} else {
64+
def pageSource = stepRunner.getDriver().getPageSource();
65+
if (StringUtils.isNotBlank(pageSource)) {
66+
pageSource = trimWhiteSpace(pageSource);
67+
def targetString = trimWhiteSpace(step.blade.parameters as String);
68+
if (pageSource.contains(targetString)) {
69+
found = true;
70+
}
71+
}
72+
}
73+
} else {
74+
def element = findElement(stepRunner, step);
75+
if (StringUtils.isNotEmpty((step.blade.parameters ?: '')))
76+
found = element.text == step.blade.parameters as String || element.getAttribute('value') == step.blade.parameters as String;
77+
else
78+
found = true;
79+
}
80+
} catch (Exception ignored) {
81+
}
82+
found
83+
}
84+
85+
private String trimWhiteSpace(String aStr) {
86+
aStr = aStr.trim();
87+
aStr = StringUtils.remove(aStr," ");
88+
aStr = StringUtils.remove(aStr,"\n");
89+
aStr = StringUtils.remove(aStr,"\r");
90+
aStr = StringUtils.remove(aStr,"\t");
91+
return aStr;
6592
}
6693

6794
/**

madcow-webdriver/src/test/groovy/au/com/ps4impact/madcow/runner/webdriver/blade/WaitForTest.groovy

+9-2
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,18 @@ class WaitForTest extends AbstractBladeTestCase {
7070
verifyWaitFor(blade, true);
7171
}
7272

73+
@Test
74+
void testWaitForHTMLNoMatch() {
75+
GrassBlade blade = new GrassBlade('waitFor = <BUGEDDYBUGGEDY />', testCase.grassParser);
76+
verifyWaitFor(blade, false);
77+
}
78+
7379
@Test
7480
void testWaitForHTML() {
75-
GrassBlade blade = new GrassBlade('waitFor = <button id="enabledButton" name="enabledButton">enabledButton</button>', testCase.grassParser);
81+
GrassBlade blade = new GrassBlade('waitFor =<button id="enabledButton" name="enabledButton">\n' +
82+
' enabledButton\n' +
83+
' </button>', testCase.grassParser);
7684
verifyWaitFor(blade, true);
77-
assertEquals(blade.parameters, "<button id=\"enabledButton\" name=\"enabledButton\">enabledButton</button>")
7885
}
7986

8087
@Test

0 commit comments

Comments
 (0)