chai-webdriver Build Status Dependency Status NPM version

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 作為其 class 屬性之一。
  • 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。