how can I get the current url using protractor?

后端 未结 3 1406
离开以前
离开以前 2020-12-05 04:25

I am testing a website using protractor, and jasmine. I would like to know the current url in order to verify a test.

I have tried

function waitForU         


        
相关标签:
3条回答
  • 2020-12-05 04:59

    Since Protractor 4.0.0 you can now use expected conditions to know when your url changed.

    const EC = protractor.ExpectedConditions;
    browser.wait(EC.urlContains('my-url'), 5000);
    

    The asnwer I found comes from this one, I have no real credits for it. But just in case it help.

    Protractor- Generic wait for URL to change

    0 讨论(0)
  • 2020-12-05 05:04

    If you want to just check the current URL, then use browser.getCurrentUrl():

    expect(browser.getCurrentUrl()).toEqual("expectedUrl");
    

    But, if you need to wait until URL matches a certain value, see the next part of the answer.


    Here is a working code based on the example provided by the author of the Expected Conditions:

    var urlChanged = function(url) {
      return function () {
        return browser.getCurrentUrl().then(function(actualUrl) {
          return url != actualUrl;
        });
      };
    };
    

    Usage:

    element(by.linkText('Acceder')).click();
    browser.wait(urlChanged("http://localhost:9000/#/solicitudes"), 5000); 
    
    0 讨论(0)
  • 2020-12-05 05:17

    Alecxe your answer was very helpful.

    But couldn't you just code:

     expect(browser.getCurrentUrl()).toEqual('whateverbrowseryouarexpectingtobein');
    

    And if this fails, then you know you're going to the wrong place.

    0 讨论(0)
提交回复
热议问题