2

I'm trying to verify that an account was created successfully, but after clicking the submit button, I need to wait until the next page has loaded and verify that the user ended up at the correct URL.

I'm using pollUntil to check the URL client side, but that results in Detected a page unload event; script execution does not work across page loads. in Safari at least. I can add a sleep, but I was wondering if there is a better way.

Questions:

  1. How can you poll on something like this.remote.getCurrentUrl()? Basically I want to do something like this.remote.waitForCurrentUrlToEqual(...), but I'm also curious how to poll on anything from Selenium commands vs using pollUntil which executes code in the remote browser.

  2. I'm checking to see if the user ended up at a protected URL after logging in here. Is there a better way to check this besides polling?

  3. Best practices: do I need to make an assertion with Chai or is it even possible when I'm polling and waiting for stuff as my test? For example, in this case, I'm just trying to poll to make sure we ended up at the right URL within 30 seconds and I don't have an explicit assertion. I'm just assuming the test will fail, but it won't say why. If the best practice is to make an assertion here, how would I do it here or any time I'm using wait?

Here's an example of my code:

'create new account': function() {
  return this.remote
    // Hidden: populate all account details
    .findByClassName('nextButton')
      .click()
      .end()
    .then(pollUntil('return location.pathname === "/protected-page" ? true : null', [], 30000));
}
jon_wu
  • 1,113
  • 11
  • 26

1 Answers1

2

The pollUntil helper works by running an asynchronous script in the browser to check a condition, so it's not going to work across page loads (because the script disappears when a page loads). One way to poll the current remote URL would be to write a poller that would run as part of your functional test, something like (untested):

function pollUrl(remote, targetUrl, timeout) {
    return function () {
        var dfd = new Deferred();
        var endTime = Number(new Date()) + timeout;

        (function poll() {
            remote.getCurrentUrl().then(function (url) {
                if (url === targetUrl) {
                    dfd.resolve();
                }
                else if (Number(new Date()) < endTime) {
                    setTimeout(poll, 500);
                }
                else {
                    var error = new Error('timed out; final url is ' + url);
                    dfd.reject(error);
                }
            });
        })();

        return dfd.promise;
    }
}

You could call it as:

.then(pollUrl(this.remote, '/protected-page', 30000))

When you're using something like pollUntil, there's no need (or place) to make an assertion. However, with your own polling function you could have it reject its promise with an informative error.

jason0x43
  • 3,363
  • 1
  • 16
  • 15