Skip to content

Commit 3bbdedb

Browse files
ddavisonlukeis
authored andcommitted
adding ExpectedConditions for URLs (contains, toBe, and matches regex)
Adding tests for ExpectedConditions.urlToBe|urlContains|urlMatches adding tests to ExpectedConditionsTest Signed-off-by: Luke Inman-Semerau <[email protected]>
1 parent 889904a commit 3bbdedb

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.List;
3232
import java.util.logging.Level;
3333
import java.util.logging.Logger;
34+
import java.util.regex.Matcher;
35+
import java.util.regex.Pattern;
3436

3537
/**
3638
* Canned {@link ExpectedCondition}s which are generally useful within webdriver
@@ -91,6 +93,79 @@ public String toString() {
9193
};
9294
}
9395

96+
/**
97+
* An expectation for the URL of the current page to be a specific url.
98+
*
99+
* @param url the url that the page should be on
100+
* @return <code>true</code> when the URL is what it should be
101+
*/
102+
public static ExpectedCondition<Boolean> urlToBe(final String url) {
103+
return new ExpectedCondition<Boolean>() {
104+
private String currentUrl = "";
105+
106+
@Override
107+
public Boolean apply(WebDriver driver) {
108+
currentUrl = driver.getCurrentUrl();
109+
return currentUrl != null && currentUrl.equals(url);
110+
}
111+
112+
@Override
113+
public String toString() {
114+
return String.format("url to be \"%s\". Current url: \"%s\"", url, currentUrl);
115+
}
116+
};
117+
}
118+
119+
/**
120+
* An expectation for the URL of the current page to contain specific text.
121+
*
122+
* @param fraction the fraction of the url that the page should be on
123+
* @return <code>true</code> when the URL contains the text
124+
*/
125+
public static ExpectedCondition<Boolean> urlContains(final String fraction) {
126+
return new ExpectedCondition<Boolean>() {
127+
private String currentUrl = "";
128+
129+
@Override
130+
public Boolean apply(WebDriver driver) {
131+
currentUrl = driver.getCurrentUrl();
132+
return currentUrl != null && currentUrl.contains(fraction);
133+
}
134+
135+
@Override
136+
public String toString() {
137+
return String.format("url to contain \"%s\". Current url: \"%s\"", fraction, currentUrl);
138+
}
139+
};
140+
}
141+
142+
/**
143+
* Expectation for the URL to match a specific regular expression
144+
*
145+
* @param regex the regular expression that the URL should match
146+
* @return <code>true</code> if the URL matches the specified regular expression
147+
*/
148+
public static ExpectedCondition<Boolean> urlMatches(final String regex) {
149+
return new ExpectedCondition<Boolean>() {
150+
private String currentUrl;
151+
private Pattern pattern;
152+
private Matcher matcher;
153+
154+
@Override
155+
public Boolean apply(WebDriver driver) {
156+
currentUrl = driver.getCurrentUrl();
157+
pattern = Pattern.compile(regex);
158+
matcher = pattern.matcher(currentUrl);
159+
return matcher.find();
160+
}
161+
162+
@Override
163+
public String toString() {
164+
return String.format("url to match the regex \"%s\". Current url: \"%s\"", regex, currentUrl);
165+
}
166+
};
167+
}
168+
94169
/**
95170
* An expectation for checking that an element is present on the DOM of a
96171
* page. This does not necessarily mean that the element is visible.

java/client/test/org/openqa/selenium/support/ui/ExpectedConditionsTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import static org.openqa.selenium.support.ui.ExpectedConditions.elementSelectionStateToBe;
2929
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
3030
import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElementLocated;
31+
import static org.openqa.selenium.support.ui.ExpectedConditions.urlContains;
32+
import static org.openqa.selenium.support.ui.ExpectedConditions.urlMatches;
33+
import static org.openqa.selenium.support.ui.ExpectedConditions.urlToBe;
3134
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;
3235
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElements;
3336
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElementsLocatedBy;
@@ -74,6 +77,66 @@ public void setUpMocks() {
7477
.pollingEvery(250, TimeUnit.MILLISECONDS);
7578
}
7679

80+
@Test
81+
public void waitingForUrlToBeOpened_urlToBe() {
82+
final String url = "http://some_url";
83+
when(mockDriver.getCurrentUrl()).thenReturn(url);
84+
wait.until(urlToBe(url));
85+
}
86+
87+
@Test
88+
public void waitingForUrlToBeOpened_urlContains() {
89+
final String url = "http://some_url";
90+
when(mockDriver.getCurrentUrl()).thenReturn(url);
91+
wait.until(urlContains("some_url"));
92+
}
93+
94+
@Test
95+
public void waitingForUrlToBeOpened_urlMatches() {
96+
final String url = "http://some-dynamic:4000/url";
97+
when(mockDriver.getCurrentUrl()).thenReturn(url);
98+
wait.until(urlMatches(".*:\\d{4}\\/url"));
99+
}
100+
101+
@Test
102+
public void negative_waitingForUrlToBeOpened_urlToBe() {
103+
final String url = "http://some_url";
104+
when(mockDriver.getCurrentUrl()).thenReturn(url);
105+
106+
try {
107+
wait.until(urlToBe(url + "/malformed"));
108+
fail();
109+
} catch (TimeoutException ex) {
110+
// do nothing
111+
}
112+
}
113+
114+
@Test
115+
public void negative_waitingForUrlToBeOpened_urlContains() {
116+
final String url = "http://some_url";
117+
when(mockDriver.getCurrentUrl()).thenReturn(url);
118+
119+
try {
120+
wait.until(urlContains("/malformed"));
121+
fail();
122+
} catch (TimeoutException ex) {
123+
// do nothing
124+
}
125+
}
126+
127+
@Test
128+
public void negative_waitingForUrlToBeOpened_urlMatches() {
129+
final String url = "http://some-dynamic:4000/url";
130+
when(mockDriver.getCurrentUrl()).thenReturn(url);
131+
132+
try {
133+
wait.until(urlMatches(".*\\/malformed.*"));
134+
fail();
135+
} catch (TimeoutException ex) {
136+
// do nothing
137+
}
138+
}
139+
77140
@Test
78141
public void waitingForVisibilityOfElement_elementAlreadyVisible() {
79142
when(mockElement.isDisplayed()).thenReturn(true);

0 commit comments

Comments
 (0)