项目作者: applitools

项目描述 :
Jest environment for running Selenium WebDriver tests
高级语言: JavaScript
项目地址: git://github.com/applitools/jest-environment-selenium.git
创建时间: 2018-04-24T11:37:48Z
项目社区:https://github.com/applitools/jest-environment-selenium

开源协议:Apache License 2.0

下载


jest-environment-selenium · npm version Build Status License

Jest environment for running Selenium WebDriver tests

Installation

I like using yarn for installations.

  1. yarn add -D jest-environment-selenium

But npm works too!

  1. npm install --save-dev jest-environment-selenium

Setup

Add this to the package.json:

  1. "jest": {
  2. "testEnvironment": "jest-environment-selenium",
  3. "setupTestFrameworkScriptFile": "jest-environment-selenium/dist/setup.js"
  4. }

By default tests will run against a local chromedriver, but you can easily specify something else.

  1. "jest": {
  2. "testEnvironmentOptions": {
  3. "capabilities": {
  4. "browserName": "firefox"
  5. },
  6. "server": "http://localhost:4444/wd/hub",
  7. "proxyType": "manual",
  8. "proxyOptions": {
  9. "https": "http://127.0.0.1:3218"
  10. }
  11. }
  12. }

Jest Environment Selenium

Tests will be initialized with a driver according to the options (or a default chrome one)

  1. test('load wikipedia', () => {
  2. driver.get('https://en.wikipedia.org/wiki/Base64');
  3. });

cleanup

Kills the used session and starts a new one.

  1. afterEach(async () => (cleanup()));

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

  1. test('load wikipedia', () => {
  2. driver.get('https://en.wikipedia.org/wiki/Base64');
  3. return driver.getTitle().then(title => {expect(title).toBeDefined();});
  4. });

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

  1. test('link appears in the page', () => {
  2. driver.get('https://en.wikipedia.org/wiki/Base64');
  3. return expect(driver.findElements(By.linkText("binary-to-text encoding"))).resolves.toBePresent();
  4. });

expect.resolves[.not].toBeChecked()

toBeChecked checks that a checkbox is checked (many checks wow! :scream:), it expects to receive a WebElementPromise

  1. test('a checkbox is checked', () => {
  2. driver.get('somewhere');
  3. return expect(driver.findElements(By.css('input[type="checkbox"]'))).resolves.toBeChecked();
  4. });

expect.resolves[.not].toBeEditable()

toBeEditable checks that an input is editable (enabled and not readonly), it expects to receive a WebElementPromise

  1. test('an input is editable', () => {
  2. driver.get('somewhere');
  3. return expect(driver.findElements(By.css('input'))).resolves.toBeEditable();
  4. });

expect.resolves[.not].toHaveValue(value)

toHaveValue checks that an input value is what you expect, it expects to receive a WebElementPromise

  1. test('an input has the value', () => {
  2. driver.get('somewhere');
  3. return expect(driver.findElements(By.css('input'))).resolves.toHaveValue('test');
  4. });

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

  1. test('a select has the right value', () => {
  2. driver.get('somewhere');
  3. return expect(driver.findElements(By.css('select'))).resolves.toHaveSelectedValue('test');
  4. });

expect.resolves[.not].toHaveText(value)

toHaveValue checks that an element innetText is what you expect, it expects to receive a WebElementPromise

  1. test('the paragraph has the correct text', () => {
  2. driver.get('somewhere');
  3. return expect(driver.findElements(By.css('p'))).resolves.toHaveText('some nice text, maybe lorem ipsum');
  4. });