chai-webdriver-exec Build Status

Provides selenium-webdriver support for asserting executed scripts in Chai. This plugin is complementary to the chai-webdriver plugin which only adds support for dom-based assertions.

Assertions

All assertions use an exec mechanism and work with chai.expect():

chai.expect('return document.childElementCount').exec.to.equal(1)
chai.expect('return document.childElementCount').exec.to.exist

The script to be executed is the first argument to chai.expect() and will be passed to executeScript(). For the assertion to work the script must have a return value.

For a list of possible assertions see the test file. Basically all BDD assertions are supported except: arguments, itself, extensible, sealed, frozen, throw, respondTo, change, increase, decrease (due to the nature of executing a script remotely).

Asynchronous flow

All these assertions are presumed to be asynchronous (using selenium-webdriver’s promise chain). They can all take callbacks, or be chained with promises. For example:

expect(script).exec.to.have.ownProperty('string', function(){...})
expect(script).exec.to.have.ownProperty('string').then(function(){...})

Note that this usage is not required if using the selenium-webdriver/testing wrappers.

Usage

npm i --save-dev chai-webdriver-exec

and then in your test file:

var chaiWebdriverExec = require('chai-webdriver-exec')
chai.use(chaiWebdriverExec(driver))

Example

var webdriver = require('selenium-webdriver'),
  test = require('selenium-webdriver/testing')
var driver = new webdriver.Builder()
  .forBrowser('firefox')
  .build()

var chai = require('chai')
var chaiWebdriverExec = require('chai-webdriver-exec')
chai.use(chaiWebdriverExec(driver)) // here you hook it up

test.describe('some cool feature', function() {
  this.timeout(10000)
  
  test.it('should work as expected', function() {
    driver.get('http://github.com');
    chai.expect('return window.scrollX').to.be.a('number')
  })
})

Tests

npm test