【发布时间】: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