Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 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);
                });
            });
        }
    );
}

and I am using this function in this way

it('should log', function() {
    //element(by.model('user.username')).sendKeys('asd');
    //element(by.model('user.password')).sendKeys('asd');
    element(by.linkText('Acceder')).click();
    waitForUrlToChangeTo("http://localhost:9000/#/solicitudes");
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
288 views
Welcome To Ask or Share your Answers For Others

1 Answer

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); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...