if I do expect(img).not.toBe(null)
然后我得到一个错误:Error: expect called with WebElement argment, expected a Promise. Did you mean to use .getText()?
。我不想获取 img 中的文本,我只想知道页面上是否存在该标签。
describe('company homepage', function() {
it('should have a captcha', function() {
var driver = browser.driver;
driver.get('http://dev.company.com/');
var img =driver.findElement(by.id('recaptcha_image'));
expect(img.getText()).not.toBe(null);
});
});
通过,但我不确定它正在测试正确的东西。将 id 更改为不存在的内容确实会失败。
如何在非角度应用程序上下文中正确测试标签是否与量角器一起存在?
Edit 2:
根据下面的 Coding Smackdown,量角器中现在提供了更短的答案:
expect(element(by.id('recaptcha_image')).isPresent()).toBe(true);
Edit 1:
我今天发现 isElementPresent() 这只是我下面描述的一个更易读的快捷方式。看:http://www.protractortest.org/#/api http://www.protractortest.org/#/api
您的用法是:
driver.isElementPresent(by.id('recaptcha_image')).then(function(present){
expect(present).toBe(false);
})
旧答案(这有效,但上面的内容对读者更友好)
一般来说,如果您不确定标签是否存在,则应该使用 findElements (或 $$,它是 css 的 findElements 的别名)。然后测试数组长度。如果 FindElement (和 $)找不到该元素,它只会抛出错误。
因此而不是
var img =driver.findElement(by.id('recaptcha_image'));
expect(img.getText()).not.toBe(null);
use:
driver.findElements(by.id('recaptcha_image')).then(function(array){
expect(array.length).not.toBe(0);
})
另外, getText() 返回一个承诺,这就是您收到该错误的原因。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)