Chai 断言库提供 selenium-webdriver 的语法糖。允许您创建富有表现力的集成测试

expect('.frequency-field').dom.to.contain.text('One time')
expect('.toggle-pane').dom.to.eventually.not.be.visible()

我们可以做哪些断言?

所有断言都以一个 Sizzle 兼容的 CSS 选择器 开头,例如

expect('.list')
expect('div > h1')
expect('a[href=http://google.com]')

然后我们添加 dom 标志,如下所示

expect(selector).dom

最后,我们可以将断言添加到链中

文本

将 dom 的文本值与提供的字符串进行测试。仅限完全匹配。

expect(selector).dom.to.have.text('string')

文本(包含)

将 dom 的文本值与提供的字符串进行测试。允许部分匹配。

expect(selector).dom.to.contain.text('string')

匹配

将 dom 的文本值与正则表达式进行测试。

expect(selector).dom.to.match(/regex/)

文本(正则表达式)

将 dom 的文本值与正则表达式进行测试。(与上面的 match 相同)。

expect(selector).dom.to.have.text(/regex/)

显示

检查元素是否显示(可以滚动到屏幕外)

expect(selector).dom.to.be.displayed()

可见

检查元素是否在屏幕上可见

expect(selector).dom.to.be.visible()

禁用

检查表单元素是否禁用

expect(selector).dom.to.be.disabled()

计数

测试 dom 中使用提供的选择器存在的元素数量

expect(selector).dom.to.have.count(number)

样式

测试元素的 CSS 样式(精确字符串匹配)。

expect(selector).dom.to.have.style('property', 'value')

将表单字段的值与提供的字符串进行测试。

expect(selector).dom.to.have.value('string')

HTML 类

测试元素是否具有 warning 作为其类属性之一。

expect(selector).dom.to.have.htmlClass('warning')

属性

测试元素的属性值。仅限完全匹配。通过省略 value,测试仅检查属性是否存在。

expect(selector).dom.to.have.attribute('attribute', 'value')

您也可以始终添加一个 not 来否定断言

expect(selector).dom.not.to.have.style('property', 'value')

更大更小

几个断言方法支持 largersmaller 属性,它们允许数字比较。例如,对于 value()

测试数字值是否大于等于 0。

expect('input[type=number]').dom.to.have.larger.value(0)

测试数字值是否小于等于 0。

expect('input[type=number]').dom.to.have.smaller.value(0)

测试数字值是否小于 0。

expect('input[type=number]').dom.not.to.have.larger.value(0)

测试数字值是否大于 0。

expect('input[type=number]').dom.not.to.have.smaller.value(0)

其他支持 largersmaller 的方法

测试文本长度是否大于等于 0。

expect(selector).dom.to.have.larger.text(0)

测试与 selector 匹配的元素数量是否大于等于 0。

expect(selector).dom.to.have.larger.count(0)

测试 css 属性值是否大于等于 0(忽略单位)。

expect(selector).dom.to.have.larger.style('width', 0)

测试属性值是否大于等于 0。

expect(selector).dom.to.have.larger.attribute('offsetWidth', 0)

最终

您还可以添加一个 eventually 来告诉 chai-webdriver-promised 轮询所需状态,直到配置的超时时间(见下文设置)

expect(selector).dom.to.eventually.have.htmlClass('warning')

所有内容都返回一个 promise

所有这些断言都返回一个 Q promise,因此如果您使用的是 mocha,您只需返回 promise 即可。

设置

设置非常简单。只需


// Start with a webdriver instance:
var sw = require('selenium-webdriver');
var driver = new sw.Builder()
  .withCapabilities(sw.Capabilities.chrome())
  .build()

//optional timeout in ms to use with eventually (defaults to 1000)
var timeout = 15000;
//optional interval in ms to use when polling (defaults to 200)
var interval = 100;

// And then...
var chai = require('chai');
var chaiWebdriver = require('chai-webdriver-promised');
chai.use(chaiWebdriver(driver, timeout, interval));

// And you're good to go!
chai.describe('kitty test', function() {
  chai.before(function(done) {
    driver.get('http://github.com').then(done);
  });
  it('should not find a kitty', function() {
    return chai.expect('#site-container h1.heading').dom.to.not.contain.text("I'm a kitty!");
  });
});

贡献

很容易。

$EDITOR index.js      # edit index.js
npm test              # run the specs

许可证

MIT。