@@ -26,42 +26,69 @@ import au.com.ps4impact.madcow.step.MadcowStep
26
26
import org.apache.commons.lang3.StringUtils
27
27
import au.com.ps4impact.madcow.step.MadcowStepResult
28
28
import au.com.ps4impact.madcow.runner.webdriver.WebDriverBladeRunner
29
+ import org.apache.log4j.Logger
29
30
30
31
/**
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.
32
34
*
33
35
* @author : Gavin Bunney
34
36
*/
35
37
class WaitFor extends WebDriverBladeRunner {
36
38
37
- public void execute ( WebDriverStepRunner stepRunner , MadcowStep step ) {
39
+ private static final int maxSeconds = 30 ;
38
40
39
- final boolean found = (1 .. 30 ). any {
40
41
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 ;
57
47
Thread . sleep(1000 );
58
- return false ;
59
48
}
60
49
61
50
if (found)
62
51
step. result = MadcowStepResult . PASS ();
63
52
else
64
53
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;
65
92
}
66
93
67
94
/**
0 commit comments