Skip to content

Commit

Permalink
feat(selenium): add AssertUrl action
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianth committed May 30, 2018
1 parent 6b67603 commit f76f141
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Boolean> absenceOfElementLocated(final By locator) {
return new ExpectedCondition<Boolean>() {
Expand All @@ -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<Boolean> urlToMatch(final Pattern regexPattern) {
return new ExpectedCondition<Boolean>() {
@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();
}
};
}
}

0 comments on commit f76f141

Please sign in to comment.