Skip to content

Global-localhost/webdriverio-testing-library

 
 

Repository files navigation

webdriverio-testing-library

spider

WebdriverIO utilities that encourage good testing practices laid down by dom-testing-library.

Based heavily on the great work on nightwatch-testing-library


Build Status version MIT License semantic-release

PRs Welcome Code of Conduct

The problem

You want to use dom-testing-library methods in your webdriverio tests.

This solution

Based heavily on nightwatch-testing-library

This allows you to use all the useful dom-testing-library methods in your tests.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev @testing-library/webdriverio

Usage

setupBrowser

Accepts a WebdriverIO BrowserObject and returns dom-testing-library queries modifed to return WebdriverIO Elements. All the queries are async, including queryBy and getBy variants, and are bound to document.body by default.

const {setupBrowser} = require('@testing-library/webdriverio');

it('can click button', async () => {
  const {getByText} = setupBrowser(browser)

  const button = await getByText('Button Text');
  await button.click();

  expect(await button.getText()).toEqual('Button Clicked')
})

Queries are also added to the BrowserObject and Elements as commands. The browser commands are scoped to the document.body as above and the Element commands are scoped to the element.

it('adds queries as browser commands', async () => {
  setupBrowser(browser);

  expect(await browser.getByText('Page Heading')).toBeDefined()
})

it('adds queries as element commands scoped to element', async () => {
  setupBrowser(browser);

  const nested = await browser.$('*[data-testid="nested"]');
  const button = await nested.getByText('Button Text')
  await button.click()

  expect(await button.getText()).toEqual('Button Clicked')
})

within

Returns queries scoped to a WebdriverIO element

const {within} = require('@testing-library/webdriverio')

it('within scopes queries to element', async () => {
  const nested = await browser.$('*[data-testid="nested"]');

  const button = await within(nested).getByText('Button Text');
  await button.click();

  expect(await button.getText()).toEqual('Button Clicked')
});

configure

Lets you pass a config to dom-testing-library

const {configure} = require('@testing-library/webdriverio')

beforeEach(() => {
  configure({testIdAttribute: 'data-automation-id'})
})
afterEach(() => {
  configure(null)
})

it('lets you configure queries', async () => {
  const {getByTestId} = setupBrowser(browser)

  expect(await getByTestId('testid-in-data-automation-id-attr')).toBeDefined()
})

Typescript

All the above methods are fully typed. To use the Browser and Element commands added by setupBrowser the global WebdriverIO namespace will need to be modified. Add the following to a typescript module:

import {WebdriverIOQueries} from '@testing-library/webdriverio';

declare global {
  namespace WebdriverIO {
    interface Browser extends WebdriverIOQueries {}
    interface Element extends WebdriverIOQueries {}
  }
}

If you are using the @wdio/sync framework you will need to use the WebdriverIOQueriesSync type to extend the interfaces:

import {WebdriverIOQueriesSync} from '@testing-library/webdriverio';

declare global {
  namespace WebdriverIO {
    interface Browser extends WebdriverIOQueriesSync {}
    interface Element extends WebdriverIOQueriesSync {}
  }
}

Other Solutions

I'm not aware of any, if you are please make a pull request and add it here!

LICENSE

MIT

About

No description, website, or topics provided.

Resources

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 48.6%
  • JavaScript 39.0%
  • HTML 12.4%