【问题标题】:Cannot create async method which return ElementArrayFinder for E2E tests无法创建为 E2E 测试返回 ElementArrayFinder 的异步方法
【发布时间】:2017-11-07 20:47:49
【问题描述】:

我有一个类,我想在其中创建一个返回 ElementArrayFinder 的异步方法。我正是需要这种类型,而不是 ElementFinder[],因为我的服务员基于它,我可以等待 DOM 中现在不存在的元素。您可以在下面找到该结构的简单示例。在行'return this.collection;'我有一个错误:

[ts] 类型“any[]”不可分配给类型“ElementArrayFinder”。 类型“any[]”中缺少属性“browser_”。

我尝试以不同的方式转换结果并尝试使用 Promise.resolve,但没有成功。有人可以帮我处理这个案子吗?

export class Test {
    private grid1: ElementFinder;
    private grid2: ElementFinder;

    get collection1(): ElementArrayFinder {
        return this.grid1
            .element(by.css('tbody'))
            .all(by.tagName('tr'));
    }

    get collection2(): ElementArrayFinder {
        return this.grid2
            .element(by.css('tbody'))
            .all(by.tagName('tr'));
    }


    public async getCollection(): Promise<ElementArrayFinder> {

        if (await this.collection.count() === 0) {
            return this.collection1;
        }
        return this.collection2;
    }
}

错误报告

  • 节点版本:v8.2.1
  • 量角器版本:5.2.0

【问题讨论】:

  • 你试过没有grids 吗? ...return this.element(by.css('tbody')).all(by.css('tr')); ...我不确定,如果你想/需要在你的函数中包含一个ElementFinder
  • 感谢您的回答。不幸的是,结果将是相同的。原因在下面的解决方案中描述。

标签: async-await protractor e2e-testing


【解决方案1】:

我在文件 element.d.ts 文件中找到了答案。

/**
 * Retrieve the elements represented by the ElementArrayFinder. The input
 * function is passed to the resulting promise, which resolves to an
 * array of ElementFinders.
 *
 * @alias element.all(locator).then(thenFunction)
 * @view
 * <ul class="items">
 *   <li>First</li>
 *   <li>Second</li>
 *   <li>Third</li>
 * </ul>
 *
 * @example
 * element.all(by.css('.items li')).then(function(arr) {
 *   expect(arr.length).toEqual(3);
 * });
 *
 * // Or using the shortcut $$() notation instead of element.all(by.css()):
 *
 * $$('.items li').then(function(arr) {
 *   expect(arr.length).toEqual(3);
 * });
 *
 * @param {function(Array.<ElementFinder>)} fn
 * @param {function(Error)} errorFn
 *
 * @returns {!webdriver.promise.Promise} A promise which will resolve to
 *     an array of ElementFinders represented by the ElementArrayFinder.
 */
then<T>(fn?: (value: ElementFinder[] | any[]) => T | wdpromise.IThenable<T>, errorFn?: (error: any) => any): wdpromise.Promise<T>;

根据本文档,ElementArrayFinder 类型由 .then 解析为 ElementFinder[]。这就是为什么我不能在 promise 中创建返回 ElementArrayFinder 类型的方法,但可以创建只返回 ElementArrayFinder 类型的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2013-05-03
    • 2012-12-20
    相关资源
    最近更新 更多