diff --git a/madcow-webdriver/src/main/groovy/au/com/ps4impact/madcow/runner/webdriver/blade/WaitUntilExists.groovy b/madcow-webdriver/src/main/groovy/au/com/ps4impact/madcow/runner/webdriver/blade/WaitUntilExists.groovy new file mode 100644 index 0000000..5fad22c --- /dev/null +++ b/madcow-webdriver/src/main/groovy/au/com/ps4impact/madcow/runner/webdriver/blade/WaitUntilExists.groovy @@ -0,0 +1,121 @@ +package au.com.ps4impact.madcow.runner.webdriver.blade + +import au.com.ps4impact.madcow.grass.GrassBlade +import au.com.ps4impact.madcow.runner.webdriver.WebDriverBladeRunner +import au.com.ps4impact.madcow.runner.webdriver.WebDriverStepRunner +import au.com.ps4impact.madcow.step.MadcowStep +import au.com.ps4impact.madcow.step.MadcowStepResult +import au.com.ps4impact.madcow.step.MadcowStepRunner +import org.openqa.selenium.By +import org.openqa.selenium.WebDriver +import org.openqa.selenium.WebElement +import org.openqa.selenium.support.ui.ExpectedCondition +import org.openqa.selenium.support.ui.WebDriverWait +import org.apache.commons.lang3.StringUtils + +/** + * Usage: + * 1. waitUntilExists = [value:'textToFind', seconds:'5'] + * or + * 2. MappingId.waitUntilExists = 10 + */ +class WaitUntilExists extends WebDriverBladeRunner{ + + private int timeout = 0 + + /** + * Wait for the element to show within specified time. + * @param stepRunner + * @param step + */ + public void execute(WebDriverStepRunner stepRunner, MadcowStep step) { + MadcowStepResult result = null + try{ + result = findElementInPage(stepRunner, step) + }catch (Exception e){ + result = MadcowStepResult.FAIL("Element not found within specified timeout of ${timeout} seconds.") + } + step.result = result + } + + /** + * Allow verifying the element is empty. + */ + protected boolean allowEmptyParameterValue() { + return true; + } + + /** + * Allow null selectors. + */ + protected boolean allowNullSelectorType() { + return true; + } + + /** + * Supported types of blades. + */ + protected Collection getSupportedBladeTypes() { + return [GrassBlade.GrassBladeType.EQUATION]; + } + + protected List getSupportedParameterTypes() { + return [String.class, Map.class] + } + + /** + * Actual search and wait implementation. + * @param stepRunner + * @param id + * @param txt + * @param timeout + */ + private void waitTilExists(WebDriverStepRunner stepRunner, String id, String txt, int timeout){ + String xpath = "" + if(StringUtils.isEmpty(id)){ + xpath = "//*[text() = '$txt']" + }else{ + xpath = "//*[@id='$id']" + } + WebDriverWait wait = new WebDriverWait(stepRunner.driver, timeout, 2000); + wait.until( new ExpectedCondition() { + public Boolean apply(WebDriver d) { + return d.findElement(By.xpath(xpath)) != null + } + }) + } + + /** + * Determine if the script has id and value in it. + * @param stepRunner + * @param step + */ + private MadcowStepResult findElementInPage(WebDriverStepRunner stepRunner, MadcowStep step){ + String id = step.blade.mappingSelectorValue != null ? step.blade.mappingSelectorValue : "" + String txt = "" + int sec = 0 + if (Map.class.isInstance(step.blade.parameters)) { + Map map = step.blade.parameters as Map + sec = map.seconds != null ? Integer.parseInt(map.seconds) : 0 + timeout = sec + txt = map.value != null ? map.value : "" + if(StringUtils.isEmpty(map.seconds) || StringUtils.isEmpty(map.value)){ + return MadcowStepResult.FAIL("seconds and value are required in a Map parameter. waitUntilExists =[seconds:'', value:'' ]") + }else{ + waitTilExists(stepRunner,null,txt, sec) + return MadcowStepResult.PASS() + } + }else{ + sec = step.blade.parameters as int + timeout = sec + if(StringUtils.isEmpty(id) || StringUtils.isEmpty(step.blade.parameters)){ + return MadcowStepResult.FAIL("mapping is required. Page_element.waitUntilExists = 2") + }else{ + waitTilExists(stepRunner,id,null, sec) + return MadcowStepResult.PASS() + } + } + } + + +} diff --git a/madcow-webdriver/src/test/groovy/au/com/ps4impact/madcow/runner/webdriver/blade/WaitUntilExistsTest.groovy b/madcow-webdriver/src/test/groovy/au/com/ps4impact/madcow/runner/webdriver/blade/WaitUntilExistsTest.groovy new file mode 100644 index 0000000..0f6a282 --- /dev/null +++ b/madcow-webdriver/src/test/groovy/au/com/ps4impact/madcow/runner/webdriver/blade/WaitUntilExistsTest.groovy @@ -0,0 +1,80 @@ +/* + * Copyright 2012 4impact, Brisbane, Australia + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package au.com.ps4impact.madcow.runner.webdriver.blade + +import au.com.ps4impact.madcow.grass.GrassBlade +import au.com.ps4impact.madcow.mappings.MadcowMappings +import au.com.ps4impact.madcow.runner.webdriver.WebDriverStepRunner +import au.com.ps4impact.madcow.step.MadcowStep +import org.junit.Test + +import static org.junit.Assert.* + +/** + * Test for the ClickLink BladeRunner. + * + * @author Gavin Bunney + */ +class WaitUntilExistsTest extends AbstractBladeTestCase { + + ClickLink clickLink = new ClickLink(); + + protected verifyLinkExecution(GrassBlade blade, boolean shouldPass) { + (testCase.stepRunner as WebDriverStepRunner).driver.get("file://${testHtmlFilePath}"); + MadcowStep step = new MadcowStep(testCase, blade, null); + testCase.stepRunner.execute(step); + assertEquals(shouldPass, step.result.passed()); + } + + @Test + void testWithoutMapping() { + GrassBlade blade = new GrassBlade('waitUntilExists = 2', testCase.grassParser); + verifyLinkExecution(blade, false); + + } + + @Test + void testWithMapping() { + MadcowMappings.addMapping(testCase, 'aLinkId', ['id': 'aLinkId']); + GrassBlade blade = new GrassBlade('aLinkId.waitUntilExists = 2', testCase.grassParser); + verifyLinkExecution(blade, true); + + } + + @Test + void testMapParamWithoutSeconds() { + GrassBlade blade = new GrassBlade("waitUntilExists = [value:'test']", testCase.grassParser); + verifyLinkExecution(blade, false); + } + + @Test + void testMapParamWithoutValue() { + GrassBlade blade = new GrassBlade("waitUntilExists = [seconds:'2']", testCase.grassParser); + verifyLinkExecution(blade, false); + } + + @Test + void testMapParamWithSecondsAndValue() { + GrassBlade blade = new GrassBlade("waitUntilExists = [seconds:'2', value:'A link']", testCase.grassParser); + verifyLinkExecution(blade, true); + } +}