chai-webdriver  
  
 
为 Chai 断言库提供 selenium-webdriver 的语法糖。允许你创建表达力强的集成测试。
expect('.frequency-field').dom.to.contain.text('One time')
expect('.toggle-pane').dom.to.not.be.visible()
我们能进行哪些类型的断言?
所有断言都以一个 Sizzle 兼容的 CSS 选择器 开始,例如
- expect('.list')
- expect('div > h1')
- expect('a[href=http://google.com]')
然后我们添加 dom 标志,如下所示
- expect(selector).dom
最后,我们可以将断言添加到链中。
- expect(selector).dom.to.have.text('string')- 将 dom 的文本值与提供的字符串进行测试。仅精确匹配。
- expect(selector).dom.to.contain.text('string')- 将 dom 的文本值与提供的字符串进行测试。允许部分匹配。
- expect(selector).dom.to.match(/regex/)- 将 dom 的文本值与正则表达式进行测试。
- expect(selector).dom.to.have.text(/regex/)- 将 dom 的文本值与正则表达式进行测试。(与上面的- match相同)。
- expect(selector).dom.to.be.visible()- 检查元素是否正在渲染。
- expect(selector).dom.to.be.disabled()- 检查表单元素是否被禁用。
- expect(selector).dom.to.have.count(number)- 测试 dom 中使用提供的选择器存在的元素数量。
- expect(selector).dom.to.have.style('property', 'value')- 测试元素的 CSS 样式。不幸的是,目前仅支持精确匹配。
- expect(selector).dom.to.have.value('string')- 将表单字段的值与提供的字符串进行测试。
- expect(selector).dom.to.have.htmlClass('warning')- 测试元素是否具有- warning作为其类属性之一。
- expect(selector).dom.to.have.attribute('attribute', 'value')- 将元素的- attribute与- value作为精确匹配进行测试。通过省略- value,测试仅检查属性是否存在。
- expect(selector).dom.to.have.attribute('attribute', /regex/)- 将元素的- attribute与正则表达式进行测试。
你也可以随时添加一个 not 来否定断言。
- expect(selector).dom.not.to.have.style('property', 'value')
异步流程
请注意,所有这些断言都被假定为是异步的(使用 selenium-webdriver 的 promise 链)。它们都可以接受回调,或者与 promise 相连。例如
- expect(selector).dom.to.have.text('string', function(){...})
- expect(selector).dom.to.have.text('string').then(function(){...})
设置
设置非常简单。只需
// Start with a webdriver instance:
var sw = require('selenium-webdriver');
var driver = new sw.Builder()
  .withCapabilities(sw.Capabilities.chrome())
  .build()
// And then...
var chai = require('chai');
var chaiWebdriver = require('chai-webdriver');
chai.use(chaiWebdriver(driver));
// And you're good to go!
driver.get('http://github.com');
chai.expect('#site-container h1.heading').dom.to.not.contain.text("I'm a kitty!");
贡献
非常简单。
npm install           # download the necessary development dependencies
npm run-script build  # compile coffee-script into javascript
npm test              # build and run the specs
许可
MIT。