gpt4 book ai didi

javascript - 如何在 Protractor/ Jasmine 测试中等待 promise ?

转载 作者:行者123 更新时间:2023-11-28 06:47:14 25 4
gpt4 key购买 nike

我正在学习 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作为框架,而不是jasmine

框架:'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中的promise“链接”在一起,这很方便地解决了我的问题。

关于javascript - 如何在 Protractor/ Jasmine 测试中等待 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33304816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com