如果我这样做,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);
});
});
Run Code Online (Sandbox Code Playgroud)
通过,但我不确定它是否正在测试正确的事情.将id更改为不存在的内容会失败.
如何在非角度应用程序上下文中正确测试与量角器存在的标记?
wli*_*gke 32
编辑2:
下面的Per Coding Smackdown,现在可以在量角器中找到更短的答案:
expect(element(by.id('recaptcha_image')).isPresent()).toBe(true);
编辑1:
我今天发现了isElementPresent(),它只是我下面描述的更易读的快捷方式.请参阅:http://www.protractortest.org/#/api
你的用法是:
driver.isElementPresent(by.id('recaptcha_image')).then(function(present){
expect(present).toBe(false);
})
Run Code Online (Sandbox Code Playgroud)
老答案(这可行,但上面的读者更友好)
一般情况下,如果您不确定标记是否存在,则应使用findElements(或$$,这是css的findElements的别名).然后测试数组长度.FindElement(和$)只会在找不到元素时抛出错误.
因此而不是
var img =driver.findElement(by.id('recaptcha_image'));
expect(img.getText()).not.toBe(null);
Run Code Online (Sandbox Code Playgroud)
使用:
driver.findElements(by.id('recaptcha_image')).then(function(array){
expect(array.length).not.toBe(0);
})
Run Code Online (Sandbox Code Playgroud)
此外,getText()返回一个promise,这就是你收到该错误的原因.
Cod*_*own 24
使用最新的Protractor构建,您可以将其缩短为以下内容:
expect(element(by.id('recaptcha_image')).isPresent()).toBe(true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20720 次 |
| 最近记录: |