Javascript 如何使用量角器获取当前网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29397800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I get the current url using protractor?
提问by lmiguelvargasf
I am testing a website using protractor, and jasmine. I would like to know the current url in order to verify a test.
我正在使用量角器和茉莉花测试网站。我想知道当前的 url 以验证测试。
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");
});
回答by alecxe
If you want to just check the current URL, then use browser.getCurrentUrl():
如果您只想检查当前 URL,请使用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.
但是,如果您需要等到 URL 匹配某个值,请参阅答案的下一部分。
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);
回答by LucCW
回答by etiennejcharles
Since Protractor 4.0.0you can now use expected conditions to know when your url changed.
从Protractor 4.0.0 开始,您现在可以使用预期条件来了解您的 url 何时更改。
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.
我发现的答案来自这个,我没有真正的功劳。但以防万一它有帮助。

