Skip to content

Commit 8981d76

Browse files
manoj9788jleyba
authored andcommitted
Adding new functions to until class (#2139)
1 parent a1b0fbd commit 8981d76

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

javascript/node/selenium-webdriver/lib/until.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,57 @@ exports.titleMatches = function titleMatches(regex) {
176176
};
177177

178178

179+
/**
180+
* Creates a condition that will wait for the current page's url to match the
181+
* given value.
182+
*
183+
* @param {string} url The expected page url.
184+
* @return {!Condition<boolean>} The new condition.
185+
*/
186+
exports.urlIs = function urlIs(url){
187+
return new Condition(
188+
'for url to be ' + JSON.stringify(url),
189+
function(driver) {
190+
return driver.getCurrentUrl().then(function(u) {
191+
return u === url;
192+
});
193+
});
194+
};
195+
196+
/**
197+
* Creates a condition that will wait for the current page's url to contain
198+
* the given substring.
199+
*
200+
* @param {string} substrUrl The substring that should be present in the page
201+
* title.
202+
* @return {!Condition<boolean>} The new condition.
203+
*/
204+
exports.urlContains = function urlContains(substrUrl) {
205+
return new Condition(
206+
'for url to contain ' + JSON.stringify(substrUrl),
207+
function(driver) {
208+
return driver.getCurrentUrl().then(function(url) {
209+
return url.indexOf(substrUrl) !== -1;
210+
});
211+
});
212+
};
213+
214+
/**
215+
* Creates a condition that will wait for the current page's url to match the
216+
* given regular expression.
217+
*
218+
* @param {!RegExp} regex The regular expression to test against.
219+
* @return {!Condition<boolean>} The new condition.
220+
*/
221+
exports.urlMatches = function urlMatches(regex) {
222+
return new Condition('for url to match ' + regex, function(driver) {
223+
return driver.getCurrentUrl().then(function(url) {
224+
return regex.test(url);
225+
});
226+
});
227+
};
228+
229+
179230
/**
180231
* Creates a condition that will loop until an element is
181232
* {@link ./webdriver.WebDriver#findElement found} with the given locator.

javascript/node/selenium-webdriver/test/lib/until_test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ describe('until', function() {
174174
assert.deepStrictEqual(titles, ['google']);
175175
});
176176
});
177+
178+
it('testUntilUrlIs', function() {
179+
var urls = ['http://www.foo.com', 'https://boo.com', 'http://docs.yes.com'];
180+
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift());
181+
182+
return driver.wait(until.urlIs('https://boo.com'), 3000).then(function() {
183+
assert.deepStrictEqual(urls, ['http://docs.yes.com']);
184+
});
185+
});
186+
187+
it('testUntilUrlContains', function() {
188+
var urls = ['http://foo.com', 'https://groups.froogle.com', 'http://google.com'];
189+
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift());
190+
191+
return driver.wait(until.urlContains('oogle.com'), 3000).then(function() {
192+
assert.deepStrictEqual(urls, ['http://google.com']);
193+
});
194+
});
195+
196+
it('testUntilUrlMatches', function() {
197+
var urls = ['foo', 'froogle', 'aaaabc', 'aabbbc', 'google'];
198+
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift());
199+
200+
return driver.wait(until.urlMatches(/^a{2,3}b+c$/), 3000)
201+
.then(function() {
202+
assert.deepStrictEqual(urls, ['google']);
203+
});
204+
});
177205

178206
it('testUntilElementLocated', function() {
179207
var responses = [

0 commit comments

Comments
 (0)