diff --git a/actor/selenium/src/main/java/org/getopentest/selenium/AssertUrl.java b/actor/selenium/src/main/java/org/getopentest/selenium/AssertUrl.java new file mode 100644 index 00000000..2b546d3e --- /dev/null +++ b/actor/selenium/src/main/java/org/getopentest/selenium/AssertUrl.java @@ -0,0 +1,40 @@ +package org.getopentest.selenium; + +import java.util.regex.Pattern; +import org.getopentest.selenium.core.CustomConditions; +import org.getopentest.selenium.core.SeleniumTestAction; +import org.openqa.selenium.support.ui.WebDriverWait; + +public class AssertUrl extends SeleniumTestAction { + + @Override + public void run() { + + super.run(); + + String urlIs = this.readStringArgument("urlIs", null); + String urlContains = this.readStringArgument("urlContains", null); + Pattern urlMatches = this.readRegexArgument("urlMatches", null); + + this.waitForAsyncCallsToFinish(); + + Pattern regexPattern = null; + + if (urlIs != null) { + regexPattern = Pattern.compile("^" + Pattern.quote(urlIs) + "$"); + } else if (urlContains != null) { + regexPattern = Pattern.compile("^.*" + Pattern.quote(urlContains) + ".*$"); + } else if (urlMatches != null) { + regexPattern = urlMatches; + } + + try { + WebDriverWait wait = new WebDriverWait(this.driver, this.getExplicitWaitSec()); + wait.until(CustomConditions.urlToMatch(regexPattern)); + } catch (Exception ex) { + throw new RuntimeException(String.format( + "Failed to validate the current page URL. At the time of this error, the URL was %s", + driver.getCurrentUrl()), ex); + } + } +} diff --git a/actor/selenium/src/main/java/org/getopentest/selenium/core/CustomConditions.java b/actor/selenium/src/main/java/org/getopentest/selenium/core/CustomConditions.java index 65033fbd..0c3afa41 100644 --- a/actor/selenium/src/main/java/org/getopentest/selenium/core/CustomConditions.java +++ b/actor/selenium/src/main/java/org/getopentest/selenium/core/CustomConditions.java @@ -1,5 +1,6 @@ package org.getopentest.selenium.core; +import java.util.regex.Pattern; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.StaleElementReferenceException; @@ -11,8 +12,6 @@ public class CustomConditions { /** * Returns an ExpectedCondition instance that waits for an element to be * absent from the current page. - * @param locator - * @return */ public static ExpectedCondition absenceOfElementLocated(final By locator) { return new ExpectedCondition() { @@ -34,4 +33,24 @@ public String toString() { } }; } + + /** + * Returns an ExpectedCondition instance that waits for the current page URL + * to match the provided regular expression. + */ + public static ExpectedCondition urlToMatch(final Pattern regexPattern) { + return new ExpectedCondition() { + @Override + public Boolean apply(WebDriver driver) { + String url = driver.getCurrentUrl(); + Boolean matches = regexPattern.matcher(url).matches(); + return matches; + } + + @Override + public String toString() { + return "current page URL to match: " + regexPattern.toString(); + } + }; + } }