question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use browser.wait() to wait for URL to change?

See original GitHub issue

Hi

I want to write a Protractor test that waits until the current URL to change, so I have been looking at browser.wait(), combined with browser.getCurrentUrl().

However, I am not sure of how to use the two together: the browser.wait() method accepts as its first parameter a function that will return a boolean value – however, browser.getCurrentUrl() returns a promise.

My first attempt, which failed (as I expected), looks like this:

var currentUrl;
browser.getCurrentUrl().then(function (url) {
    currentUrl = url;
}).then(function () {
    return browser.wait(function () {
        return browser.getCurrentUrl().then(function (url) {
            return url !== currentUrl;
        });
    });
}).then(function () {
    // continue testing
});

Can anyone help?

many thanks

Matt

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:26 (15 by maintainers)

github_iconTop GitHub Comments

35reactions
pittgoosecommented, May 2, 2017

Ok, I know this is way way late but I believe you can do something like this:

var EC = browser.ExpectedConditions;
browser.wait(EC.urlContains('Expected URL'), timeout); // Checks that the current URL contains the expected text
browser.wait(EC.urlIs('Expected URL'), timeout); // Checks that the current URL matches the expected text
26reactions
mcalthropcommented, Mar 18, 2014

In case it may help others in future, here is the method I have written to wait for the URL to change to a particular value (as specified by a regex) :

/**
 * @name waitForUrlToChangeTo
 * @description Wait until the URL changes to match a provided regex
 * @param {RegExp} urlRegex wait until the URL changes to match this regex
 * @returns {!webdriver.promise.Promise} Promise
 */
function waitForUrlToChangeTo(urlRegex) {
    var currentUrl;

    return browser.getCurrentUrl().then(function storeCurrentUrl(url) {
            currentUrl = url;
        }
    ).then(function waitForUrlToChangeTo() {
            return browser.wait(function waitForUrlToChangeTo() {
                return browser.getCurrentUrl().then(function compareCurrentUrl(url) {
                    return urlRegex.test(url);
                });
            });
        }
    );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Protractor- Generic wait for URL to change - Stack Overflow
var expectedCondition = protractor.ExpectedConditions; // Waits for the URL to contain 'login page'. browser.wait(expectedCondition.urlContains( ...
Read more >
thewaiter: waiting for a URL in the browser to equal or contain ...
The classic approach for such a task would be something like: open the page or interact with the UI in a way that...
Read more >
How to get Selenium to wait for a page to load - BrowserStack
Using Implicit Wait. The Implicit Wait tells WebDriver to wait a specific amount of time (say, 30 seconds) before proceeding with the next...
Read more >
5. Waits — Selenium Python Bindings 2 documentation
An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The...
Read more >
waitUntil - WebdriverIO
This wait command is your universal weapon if you want to wait on something. ... Usage​. browser.waitUntil(condition, { timeout, timeoutMsg, interval }) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found