我想在初始化完成后、 promise 解析之前测试 Controller 的状态。
我有一个按钮,在加载完成之前(按类)被禁用:
<button ng-class="{disabled: isLoading}">Show</button>
当 Controller 初始化时,会进行 ajax 调用,当它解析时,isLoading 将设置为 false:
$scope.isLoading = true;
$http.get('/list')
.then(function(response) {
$scope.list = response.data;
$scope.isLoading = false;
});
当我使用 Protractor 测试按钮时,没有禁用类。
it('should enable the button after load completes', function() {
element(by.buttonText('Show')).getAttribute('class')
.then(function(buttonClasses) {
expect(buttonClasses).toContain('disabled');
});
});
我修改了按钮以拥有另一个类,只是为了看看我不会发疯:
<button ng-class="{disabled: isLoading, enabled: !isLoading}">Show</button>
和测试:
it('should show the select tables list after clicking the Change button', function() {
element(by.buttonText('Show')).getAttribute('class')
.then(function(buttonClasses) {
expect(buttonClasses).toContain('enabled');
});
});
现在测试通过了。
我猜测 waitForAngular
是 Controller 初始化过程的一部分。如何在 promise 解决之前测试按钮的状态?
最佳答案
您可以在查找元素/类之前设置browser.ignoreSynchronization = true
,以不等待应用中的 Promise 解析
it('should show the select tables list after clicking the Change button', function() {
browser.ignoreSynchronization = true;
expect(element(by.buttonText('Show')).getAttribute('class')).toContain('disabled');
browser.ignoreSynchronization = false;
});
请注意,您很少需要对 expect
使用 .then
回调,因为 expect
处理 promise 的解包。
关于javascript - 在最初的 promise 解决之前进行 Protractor 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060171/