我是 Protractor 的新手,我想创建一个这样的期望:
expect(elementIsVisible).toBe(true);
我看到 Protractor 有 EC (expected conditions) ,即 EC.visibilityOf
这似乎是我正在寻找的。但是,我不完全确定 visibilityOf
会返回什么。
我找到了 the docs非常晦涩:
RETURNS
+-----------+-------------------------------------------------------------------------------------------+
| Type | Description |
+-----------+-------------------------------------------------------------------------------------------+
| !function | An expected condition that returns a promise representing whether the element is visible. |
+-----------+-------------------------------------------------------------------------------------------+
它返回什么? Promise
还是预期条件?
好吧,考虑到链接 .then
触发 then 不是函数
,它似乎返回了一个预期条件。但那是什么?
在所有 Protractor 文档示例中,此返回值用于 browser.wait
函数中。
我不想那样使用它,我想在我的 expect
条件中有一个 true
/false
值。
如果我尝试从 Selenium 的示例中查找更多信息,Protractor(一个 javascript 实现)将重定向到 Java documentation ...
最佳答案
visibilityOf
和所有其他 ExpectedConditions 返回函数。你可以调用这个函数,你会得到Promise<boolean>
.基本上所有 ExpectedConditions 都是谓词 - 函数,当被调用时 return promise 解析为 bool 值(不应该抛出异常)。所以基本上你可以尝试使用这样的东西:
let shouldBeVisible = protractor.ExpectedConditions.visibilityOf
expect(
shouldBeVisible($('div.button'))() // Notice () - this where we are manually calling predicate function to get boolean result
).toBeTruthy('Element div.button should be visible');
但幸运的是 - 如果您使用的是 JasmineJS - 您可以尝试我的库来断言元素的可见性: https://github.com/Xotabu4/jasmine-protractor-matchers
因此,您不仅会检查元素可见性,而且匹配器会自动等待一段时间以使元素变得可见。检查这个:
expect($('div.button')).toAppear()
更多示例在 README.MD 中
关于javascript - 如何手动使用 Protractor ExpectedCondition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50042693/