我正在尝试对我的本地服务器运行 e2e 测试并测试生成的 url(在单击导航按钮后)是正确的结果。但是,生成的 url 总是错误的。
我的代码如下所示:
HTML:
//http://localhost/#/current_Page
<html>
<head><title></title></head>
<body>
//should change the current url to
//http://localhost/#/new_page
<button class="button" ng-click="change_page()">Change Page</button>
</html>
测试代码:
var protractor = require('protractor');
require('protractor/jasminewd');
describe('Tests', function() {
var ptor;
describe('Test 1', function() {
var ptor = protractor.getInstance();
ptor.get('#/current_page');
it('change page and current url', function() {
ptor.findElement(protractor.By.className('.button').click().then(function() {
expect(ptor.currentUrl()).toContain('#/new_page');
});
});
}, 30000);
});
问题是单击按钮后的当前 url 仍然是 #/current_url 并且不会更改为预期的结果 #/new_page。
有谁知道我哪里出错了?
最佳答案
在搜索了这个问题的答案后,我自己想通了
当前的 url 没有失败,我没有等待返回 angular 的 promise 。下面的修改后的代码显示了我出错的地方
var protractor = require('protractor');
require('protractor/jasminewd');
describe('Tests', function() {
var ptor;
describe('Test 1', function() {
var ptor = protractor.getInstance();
ptor.get('#/current_page');
it('change page and current url', function() {
ptor.findElement(protractor.By.className('.button').click().then(function() {
ptor.waitForAngular();
expect(ptor.currentUrl()).toContain('#/new_page');
});
});
}, 30000);
});
然后等待 angular 路由到新页面并更新任何绑定(bind),然后继续检查预期的结果,这就是我现在期望的结果。
请注意,这并不能解决与意外 getCurrentUrl() 结果相关的所有问题。如果使用 driver.findElement() 您可能需要引用 JulieMR 对 this question 的回答
我希望这有助于有人坚持这个问题。
关于angularjs - Protractor 期望 currenturl 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467167/