Jest environment for running Selenium WebDriver tests
Installation
I like using yarn for installations.
yarn add -D jest-environment-selenium
But npm works too!
npm install --save-dev jest-environment-selenium
Setup
Add this to the package.json
:
"jest": "testEnvironment": "jest-environment-selenium" "setupTestFrameworkScriptFile": "jest-environment-selenium/dist/setup.js"
By default tests will run against a local chromedriver
, but you can easily specify something else.
"jest": "testEnvironmentOptions": "capabilities": "browserName": "firefox" "server": "http://localhost:4444/wd/hub" "proxyType": "manual" "proxyOptions": "https": "http://127.0.0.1:3218"
Jest Environment Selenium
Tests will be initialized with a driver
according to the options (or a default chrome one)
;
cleanup
Kills the used session and starts a new one.
;
Failing to call cleanup
will result in non "idempotent" tests, which reuse the same WebDriver session (which can lead to difficult to debug errors in your tests).
Caveats
Since the tests are async make sure you return a Promise
so that jest
won't bail early
;
Matchers
Custom WebDriver matchers designed for ease of use with jest
expect.resolves[.not].toBePresent()
toBePresent
checks that an element appears on a page, it expects to receive a WebElementPromise
;
expect.resolves[.not].toBeChecked()
toBeChecked
checks that a checkbox is checked (many checks wow! 😱), it expects to receive a WebElementPromise
;
expect.resolves[.not].toBeEditable()
toBeEditable
checks that an input is editable (enabled and not readonly), it expects to receive a WebElementPromise
;
expect.resolves[.not].toHaveValue(value)
toHaveValue
checks that an input value is what you expect, it expects to receive a WebElementPromise
;
expect.resolves[.not].toHaveSelectedValue(value)
toHaveValue
checks that a select value is what you expect (will fail on other inputs), it expects to receive a WebElementPromise
;
expect.resolves[.not].toHaveText(value)
toHaveValue
checks that an element innetText
is what you expect, it expects to receive a WebElementPromise
;