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.

Click link and wait for URL to change?

See original GitHub issue

I’m trying to do something seemingly simple, yet I can’t get it to work properly. Others have asked similar questions, but the solutions haven’t fixed things for me.

I want to do the following:

  1. Click a form submit button
  2. Wait for the new page to load
  3. Expect the URL to be something else

Here is the relevant code:

 beforeEach(function () {
        ptor = protractor.getInstance();
        ptor.ignoreSynchronization = true;
        ptor.get('/campaigns');
    }, 60000);

it("selecting continue", function() {
       var continueBtn = element(by.id('continue'));
        continueBtn.click().then(function() {
            expect(ptor.getCurrentUrl()).toContain(ptor.baseUrl + '/templates');
        });
    });

Clicking the submit button can take some time (anywhere from 1 - 10+ secs on my local machine), before the new page is loaded.

The expect statement consistently leads to an error:

Message:
     Expected 'http://localhost:9000/campaigns' to contain 'http://localhost:9000/templates'.

Any thoughts on the correct way to resolve this?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
juliemrcommented, Apr 11, 2014

Wait doesn’t understand expectations, you need to return a ‘true’ or ‘false’ value:

    it("should link to templates", function() {
        var continueBtn = element(by.id('continue'));
        continueBtn.click();

        ptor.wait(function() {
            return ptor.getCurrentUrl().then(function(url) {
                return (url.indexOf(ptor.baseUrl + '/templates?campaignId=') !== -1);
            });
        });
    });
3reactions
stwiccommented, Mar 5, 2019

@AEIOU-1 I had a similar issue, and here is my solution.

function waitForUrl(newUrl) {
      browser.getCurrentUrl().then(function () {
          browser.wait(function () {
              return browser.getCurrentUrl().then(function (url) {
                  return newUrl.test(url);
              });
          });
      });
}

it('opens up the url', () => {
    browser.get('yoururlhere');
    // call the wait function and pass a part of your url, 
    // in my case it was /app#/app/inbox/empty
    waitForUrl(/app#\/app\/inbox\/empty/);
}); 
Read more comments on GitHub >

github_iconTop Results From Across the Web

wait for the url to change on click event - Stack Overflow
I have an image, on clicking the image the URL changes but the page is not reloaded(partial navigation). I have used window.location.href ...
Read more >
How to get Selenium to wait for a page to load - BrowserStack
Wondering how to wait for a web page to load in Selenium testing? Read tutorial to understand 3 methods to execute the same....
Read more >
wait - Cypress Documentation
Pass in an options object to change the default behavior of cy.wait() . Option, Default, Description. log, true, Displays the command in the...
Read more >
Set up Safe Links policies in Microsoft Defender for Office 365
Admins can learn how to view, create, modify, and delete Safe Links ... Wait for URL scanning to complete before delivering the message ......
Read more >
Waiting in Cypress and how to avoid it - Filip Hric
wait () in your test. Use "defaultCommandTimeout" to change default timeout. Every element you query for an element using .get() .contains() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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