Skip to content

Commit

Permalink
Merge 53d1fdb into 5cb8ed2
Browse files Browse the repository at this point in the history
  • Loading branch information
odelacruz4impact committed May 5, 2017
2 parents 5cb8ed2 + 53d1fdb commit 52c2b08
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
Expand Up @@ -36,15 +36,41 @@ import org.apache.log4j.Logger
*/
class WaitFor extends WebDriverBladeRunner {

private static final int maxSeconds = 30;
private static final int maxSeconds = 30; // default seconds
private static final String TEXT = "text"
private static final String SECONDS = "seconds"
private static final String RETRY = "retry"
private static final String REFRESH = "refresh"

private boolean refresh = false;
private int retryCount = 1;

public void execute(WebDriverStepRunner stepRunner, MadcowStep step) {
boolean found = false;
for (count in 1..maxSeconds) {
found = isDesiredConditionFound(count, step, stepRunner, found)
if (found) break;
Thread.sleep(1000);
try {
if (step.blade.parameters != null && (step.blade.parameters instanceof LinkedHashMap)) {
// For parameter: aLinkText.waitFor = ['text': 'A link' , 'seconds': '10']
String textValue = null;
int waitSeconds = maxSeconds;
step.blade.parameters.entrySet().each { Map.Entry<String, String> entry ->
String key = entry.getKey();
switch (key) {
case TEXT: textValue = entry.getValue();
step.blade.parameters = textValue;
break;
case SECONDS: waitSeconds = Integer.parseInt(entry.getValue()); break;
case RETRY: retryCount = Integer.parseInt(entry.getValue()); break;
case REFRESH: refresh = Boolean.parseBoolean(entry.getValue()); break;
default: break;
}
}
if (StringUtils.isNotEmpty(textValue))
found = checkWaitForCondition(waitSeconds, step, stepRunner, found)
} else
// For paramter : aLinkText.waitFor and aLinkText.waitFor = A link
found = checkWaitForCondition(maxSeconds, step, stepRunner, found)
} catch (Exception ignored) {
found = false;
}

if (found)
Expand All @@ -53,6 +79,27 @@ class WaitFor extends WebDriverBladeRunner {
step.result = MadcowStepResult.FAIL('Element didn\'t appear within the timeout');

}
private boolean checkWaitForCondition(int waitSeconds, MadcowStep step, WebDriverStepRunner stepRunner, boolean found) {
boolean flag = false
retry:
for(;retryCount>0;retryCount--) {
for (count in 1..waitSeconds) {
flag = isDesiredConditionFound(count, step, stepRunner, found)
if (flag) break;
Thread.sleep(1000);
}

if(refresh == true) {
step.testCase.logInfo("Refreshing Current Page: $stepRunner.driver.currentUrl")
stepRunner.driver.navigate().refresh();
try {
stepRunner.driver.switchTo().alert().accept();
} catch(ignore) {
}
}
}
return flag
}

private boolean isDesiredConditionFound(int count, MadcowStep step, WebDriverStepRunner stepRunner, boolean found) {
try {
Expand Down Expand Up @@ -104,4 +151,8 @@ class WaitFor extends WebDriverBladeRunner {
protected boolean allowEmptyParameterValue() {
return true;
}

protected List<Class> getSupportedParameterTypes() {
return [String.class, Map.class]
}
}
Expand Up @@ -70,6 +70,7 @@ class WaitForTest extends AbstractBladeTestCase {
verifyWaitFor(blade, true);
}


@Test
void testWaitForHTMLNoMatch() {
GrassBlade blade = new GrassBlade('waitFor = <BUGEDDYBUGGEDY />', testCase.grassParser);
Expand Down Expand Up @@ -109,4 +110,43 @@ class WaitForTest extends AbstractBladeTestCase {
GrassBlade blade = new GrassBlade('waitFor = Madcow WebDriver Runner Test HTML', testCase.grassParser);
verifyWaitFor(blade, true);
}

@Test
void testWaitForWithTextAndSeconds() {
MadcowMappings.addMapping(testCase, 'aLinkText', ['text': 'A link']);
GrassBlade blade = new GrassBlade('aLinkText.waitFor = [\'text\': \'A link\' , \'seconds\': \'10\']', testCase.grassParser);
verifyWaitFor(blade, true);
}

@Test
void testWaitForWithoutSeconds() {
MadcowMappings.addMapping(testCase, 'aLinkText', ['text': 'A link']);
GrassBlade blade = new GrassBlade('aLinkText.waitFor = [\'text\': \'A link\']', testCase.grassParser);
verifyWaitFor(blade, true);
}

@Test
void testWaitForWithSeconds() {
MadcowMappings.addMapping(testCase, 'aLinkText', ['text': 'A link']);
GrassBlade blade = new GrassBlade('aLinkText.waitFor = [\'seconds\': \'10\']', testCase.grassParser);
verifyWaitFor(blade, false);
}

@Test
void testWaitForNoResponse() {
GrassBlade blade = new GrassBlade('aLinkText.waitFor = [\'text\': \'link\' , \'seconds\': \'10\']', testCase.grassParser);
verifyWaitFor(blade, false);
}

@Test
void testWaitForRetryWithRefresh() {
GrassBlade blade = new GrassBlade('aLinkText.waitFor = [\'text\': \'link\' , \'seconds\': \'2\', \'retry\': \'3\', \'refresh\': \'true\']', testCase.grassParser);
verifyWaitFor(blade, false);
}

@Test
void testWaitForRetry() {
GrassBlade blade = new GrassBlade('aLinkText.waitFor = [\'text\': \'link\' , \'seconds\': \'2\', \'retry\': \'3\']', testCase.grassParser);
verifyWaitFor(blade, false);
}
}

0 comments on commit 52c2b08

Please sign in to comment.