Javascript 如何在量角器/茉莉花测试中等待承诺?
我正在学习量角器(也就是说,量角器使用的Jasmine),出于某种原因,我需要等待继续测试的承诺。然而,我不知道如何做到这一点 让我们从量角器教程中获取代码并添加一些代码,然后添加一个简单的承诺(ES6样式),它什么都不做Javascript 如何在量角器/茉莉花测试中等待承诺?,javascript,jasmine,protractor,es6-promise,Javascript,Jasmine,Protractor,Es6 Promise,我正在学习量角器(也就是说,量角器使用的Jasmine),出于某种原因,我需要等待继续测试的承诺。然而,我不知道如何做到这一点 让我们从量角器教程中获取代码并添加一些代码,然后添加一个简单的承诺(ES6样式),它什么都不做 describe('angularjs homepage todo list', function() { it('should add a todo', function() { browser.get('https://angularjs.org');
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使用jasemine
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实际上是将expect中的承诺“链接”在一起,这很方便地解决了我的问题。我通过以下方式解决了这个问题:
- 首先,使用jasmine2作为框架,而不是writin使用jasemine
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); }); });