如何使用量角器获取当前网址? [英] how can I get the current url using protractor?

查看:29
本文介绍了如何使用量角器获取当前网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用量角器和 jasmine 测试一个网站.我想知道当前的 url 以验证测试.

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

我试过了

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

推荐答案

如果你只想查看当前 URL,那么使用 browser.getCurrentUrl():

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

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

但是,如果您需要等到 URL 匹配某个值,请参阅答案的下一部分.

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

这是基于 预期条件:

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

用法:

element(by.linkText('Acceder')).click();
browser.wait(urlChanged("http://localhost:9000/#/solicitudes"), 5000); 

这篇关于如何使用量角器获取当前网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆