【问题标题】:How can I inspect the contents of a DropDown List with Protractor?如何使用量角器检查下拉列表的内容?
【发布时间】:2016-03-23 01:39:23
【问题描述】:

我正在尝试编写一个可重用的量角器函数,该函数将确定具有指定文本的选项是否存在于下拉列表中。这是我在尝试检查选项时创建的函数。

public expectDropdownListOptionPresence(dropdownListID: string, isPresent: boolean, optionText: string) {
    // Determine if the desired option text is present or missing from the target list.
    // Set isPresent to true when it should appear, and false when it shouldn't appear.

    // Attempt 1
    element.all(by.id(dropdownListID)).filter(function (lists) {
        // Make sure the list is visible.
        return lists.isDisplayed().then(function (isVisible) {
            return isVisible;
        }).then(function (visibleLists) {
            // Nope, "Property 'all' does not exist on type 'Boolean'."
            // visibleLists.all.getItem(by.css('option')) 
        })
    });

    // Attempt 2
    element(by.id(dropdownListID)).$('option').filter(function (listOptions) {
        // Nope, "Property 'filter' does not exist on type 'ElementFinder'."
    });
}

您可以看到几次尝试,我已经在我尝试过的一些事情上包含了 WebStorm 发出的警告。

我已经调查了这篇关于选择选项的 SO 帖子。 (How to select option in drop down protractorjs e2e tests) 与那篇帖子不同,我的目标不是选择选项,而只是检查下拉列表并确定列表中是否存在选项,或者列表中是否不存在。

=== 2016 年 12 月 16 日编辑 ===

这是我根据@alecxe 提供的解决方案改编的代码。我更改了函数名称以使其更具可读性。这可以按需要工作。谢谢!

public expectDropdownListContains(dropdownListID: string, isPresent: boolean, optionText: string) {
    // Determine if the desired option text is present or missing from the target list.
    // Set isPresent to true when it should appear, and false when it shouldn't appear.
    var foundOptions = element(by.id(dropdownListID)).$$('option').filter(function (option) {
        return option.getText().then(function (text) {
            return text === optionText;
        });
    });

    if (isPresent === true) {
        expect(foundOptions.count()).toBeGreaterThan(0);
    } else {
        expect(foundOptions.count()).toEqual(0);
    }
}

【问题讨论】:

    标签: typescript protractor


    【解决方案1】:

    您可以采用不同的方法,但这里有一个想法 - 使用 map() 获取所有选项文本,然后使用 toContain() jasmine 匹配器检查是否有所需的选项:

    public expectDropdownListOptionPresence(dropdownListID: string, optionText: string) {
        var allOptions = element(by.id(dropdownListID)).$$('option').map(function (option) {
            return option.getText();
        });
    
        expect(allOptions).toContain(optionText);
    }
    

    或者,您可以filter() 通过文本输出所需的选项,然后期待您找到一些东西:

    public expectDropdownListOptionPresence(dropdownListID: string, optionText: string) {
        var foundOptions = element(by.id(dropdownListID)).$$('option').filter(function (option) {
            return option.getText().then(function (text) {
                return text === optionText;
            });
        });
    
        expect(foundOptions.count()).toBeGreaterThan(0);
    }
    

    还有Select -> option abstraction,在这里可能也有帮助。

    【讨论】:

    • 好主意。我会试一试。谢谢@alecxe!
    • 第二种方法效果很好!谢谢。
    猜你喜欢
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多