如何在量角器/茉莉花测试中等待承诺?
How to wait for promise in protractor/ jasmine test?
我正在学习 Protractor(以及 Protractor 使用的扩展 Jasmine),出于某种原因,我需要等待 Promise 才能继续测试。但是,我不确定该怎么做。
让我们从 Protractor 教程中获取代码并添加一些代码,让我们添加一个简单的 Promise(ES6 风格),它什么都不做
describe('angularjs homepage todo list', function() {
it('should add a todo', function() {
browser.get('https://angularjs.org');
element(by.model('todoList.todoText')).sendKeys('write first protractor test');
element(by.css('[value="add"]')).click();
var todoList = element.all(by.repeater('todo in todoList.todos'));
Promise.resolve().then( () => {
expect(todoList.count()).toEqual(3);
expect(todoList.get(2).getText()).toEqual('write first protractor test');
// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
var completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);
});
});
});
这行不通,因为这就是答案
$ protractor conf.js
Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
F
Failures:
1) angularjs homepage todo list should add a todo
Message:
Error while waiting for Protractor to sync with the page: "angular could not be found on the window"
Stacktrace:
undefined
Finished in 0.212 seconds
1 test, 1 assertion, 1 failure
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1
我应该怎么做才能让它发挥作用?
我是通过以下方式解决的:
- 首先,使用 jasmine2 作为框架而不是 writin
framework: 'jasmine2'
到conf.js
像这样编辑文件
var flow = browser.flow()
describe('angularjs homepage todo list', function() {
it('should add a todo', function() {
browser.get('https://angularjs.org');
element(by.model('todoList.todoText')).sendKeys('write first protractor test');
element(by.css('[value="add"]')).click();
var todoList = element.all(by.repeater('todo in todoList.todos'));
flow.execute(function(){Promise.resolve()});
// or, more ES6-y version
// flow.execute(() => Promise.resolve());
expect(todoList.count()).toEqual(3);
expect(todoList.get(2).getText()).toEqual('write first protractor test');
// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
var completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);
});
});
Jasmine其实就是"chaining"expect together本身的promises,方便的解决了我的问题
我正在学习 Protractor(以及 Protractor 使用的扩展 Jasmine),出于某种原因,我需要等待 Promise 才能继续测试。但是,我不确定该怎么做。
让我们从 Protractor 教程中获取代码并添加一些代码,让我们添加一个简单的 Promise(ES6 风格),它什么都不做
describe('angularjs homepage todo list', function() {
it('should add a todo', function() {
browser.get('https://angularjs.org');
element(by.model('todoList.todoText')).sendKeys('write first protractor test');
element(by.css('[value="add"]')).click();
var todoList = element.all(by.repeater('todo in todoList.todos'));
Promise.resolve().then( () => {
expect(todoList.count()).toEqual(3);
expect(todoList.get(2).getText()).toEqual('write first protractor test');
// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
var completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);
});
});
});
这行不通,因为这就是答案
$ protractor conf.js
Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
F
Failures:
1) angularjs homepage todo list should add a todo
Message:
Error while waiting for Protractor to sync with the page: "angular could not be found on the window"
Stacktrace:
undefined
Finished in 0.212 seconds
1 test, 1 assertion, 1 failure
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1
我应该怎么做才能让它发挥作用?
我是通过以下方式解决的:
- 首先,使用 jasmine2 作为框架而不是 writin
framework: 'jasmine2'
到conf.js
像这样编辑文件
var flow = browser.flow() describe('angularjs homepage todo list', function() { it('should add a todo', function() { browser.get('https://angularjs.org'); element(by.model('todoList.todoText')).sendKeys('write first protractor test'); element(by.css('[value="add"]')).click(); var todoList = element.all(by.repeater('todo in todoList.todos')); flow.execute(function(){Promise.resolve()}); // or, more ES6-y version // flow.execute(() => Promise.resolve()); expect(todoList.count()).toEqual(3); expect(todoList.get(2).getText()).toEqual('write first protractor test'); // You wrote your first test, cross it off the list todoList.get(2).element(by.css('input')).click(); var completedAmount = element.all(by.css('.done-true')); expect(completedAmount.count()).toEqual(2); }); });
Jasmine其实就是"chaining"expect together本身的promises,方便的解决了我的问题