|
| 1 | +<div align="center"> |
| 2 | +<h1>webdriverio-testing-library</h1> |
| 3 | + |
| 4 | +</a> |
| 5 | + |
| 6 | +<p>WebdriverIO utilities that encourage good testing practices laid down by dom-testing-library.</p> |
| 7 | + |
| 8 | +<p><strong>Based heavily on the great work on <a href="https://github.com/testing-library/nightwatch-testing-library">nightwatch-testing-library</a></strong></p> |
| 9 | + |
| 10 | +</div> |
| 11 | + |
| 12 | +<hr /> |
| 13 | + |
| 14 | +[![Build Status][build-badge]][build] |
| 15 | +[![version][version-badge]][package] |
| 16 | +[![MIT License][license-badge]][license] |
| 17 | +[](https://github.com/semantic-release/semantic-release) |
| 18 | + |
| 19 | +[![PRs Welcome][prs-badge]][prs] |
| 20 | +[![Code of Conduct][coc-badge]][coc] |
| 21 | + |
| 22 | +<div align="center"> |
| 23 | +<a href="https://testingjavascript.com"> |
| 24 | +<img width="500" alt="TestingJavaScript.com Learn the smart, efficient way to test any JavaScript application." src="https://raw.githubusercontent.com/kentcdodds/cypress-testing-library/master/other/testingjavascript.jpg" /> |
| 25 | +</a> |
| 26 | +</div> |
| 27 | + |
| 28 | +## The problem |
| 29 | + |
| 30 | +You want to use [dom-testing-library](https://github.com/kentcdodds/dom-testing-library) methods in your [webdriverio][webdriverio] tests. |
| 31 | + |
| 32 | +## This solution |
| 33 | + |
| 34 | +Based heavily on [nightwatch-testing-library][nightwatch-testing-library] |
| 35 | + |
| 36 | +This allows you to use all the useful [dom-testing-library](https://github.com/kentcdodds/dom-testing-library) methods in your tests. |
| 37 | + |
| 38 | +## Table of Contents |
| 39 | + |
| 40 | +<!-- START doctoc generated TOC please keep comment here to allow auto update --> |
| 41 | +<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
| 42 | + |
| 43 | +- [Installation](#installation) |
| 44 | +- [Usage](#usage) |
| 45 | +- [Other Solutions](#other-solutions) |
| 46 | +- [LICENSE](#license) |
| 47 | + |
| 48 | +<!-- END doctoc generated TOC please keep comment here to allow auto update --> |
| 49 | + |
| 50 | +## Installation |
| 51 | + |
| 52 | +This module is distributed via [npm][npm] which is bundled with [node][node] and |
| 53 | +should be installed as one of your project's `devDependencies`: |
| 54 | + |
| 55 | +``` |
| 56 | +npm install --save-dev webdriverio-testing-library |
| 57 | +``` |
| 58 | + |
| 59 | +## Usage |
| 60 | + |
| 61 | +### setupBrowser |
| 62 | + |
| 63 | +Accepts a WebdriverIO BrowserObject and resolves with |
| 64 | +dom-testing-library queries modifed to return WebdriverIO Elements. All the |
| 65 | +queries are async, including queryBy and getBy variants, and are bound to |
| 66 | +`document.body` by default. |
| 67 | + |
| 68 | +``` |
| 69 | +const {setupBrowser} = require('webdriverio-testing-library'); |
| 70 | +
|
| 71 | +it('can click button', async () => { |
| 72 | + const {getByText} = await setupBrowser(browser) |
| 73 | +
|
| 74 | + const button = await getByText('Button Text'); |
| 75 | + await button.click(); |
| 76 | +
|
| 77 | + expect(await button.getText()).toEqual('Button Clicked') |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +Queries are also added to the BrowserObject and Elements as commands. The |
| 82 | +browser commands are scoped to the `document.body` as above and the Element |
| 83 | +commands are scoped to the element. |
| 84 | + |
| 85 | +``` |
| 86 | +it('adds queries as browser commands', async () => { |
| 87 | + await setupBrowser(browser); |
| 88 | +
|
| 89 | + expect(await browser.getByText('Page Heading')).toBeDefined() |
| 90 | +}) |
| 91 | +
|
| 92 | +it('adds queries as element commands scoped to element', async () => { |
| 93 | + await setupBrowser(browser); |
| 94 | +
|
| 95 | + const nested = await browser.$('*[data-testid="nested"]'); |
| 96 | + const button = await nested.getByText('Button Text') |
| 97 | + await button.click() |
| 98 | +
|
| 99 | + expect(await button.getText()).toEqual('Button Clicked') |
| 100 | +}) |
| 101 | +``` |
| 102 | + |
| 103 | +### within |
| 104 | + |
| 105 | +Returns queries scoped to a WebdriverIO element |
| 106 | + |
| 107 | +``` |
| 108 | +const {within} = require('webdriverio-testing-library') |
| 109 | +
|
| 110 | +it('within scopes queries to element', async () => { |
| 111 | + const nested = await browser.$('*[data-testid="nested"]'); |
| 112 | +
|
| 113 | + const button = await within(nested).getByText('Button Text'); |
| 114 | + await button.click(); |
| 115 | +
|
| 116 | + expect(await button.getText()).toEqual('Button Clicked') |
| 117 | +}); |
| 118 | +``` |
| 119 | + |
| 120 | +### configure |
| 121 | + |
| 122 | +Lets you pass a config to dom-testing-library |
| 123 | + |
| 124 | +``` |
| 125 | +const {configure} = require('webdriverio-testing-library') |
| 126 | +
|
| 127 | +beforeEach(() => { |
| 128 | + configure({testIdAttribute: 'data-automation-id'}) |
| 129 | +}) |
| 130 | +afterEach(() => { |
| 131 | + configure(null) |
| 132 | +}) |
| 133 | +
|
| 134 | +it('lets you configure queries', async () => { |
| 135 | + const {getByTestId} = await setupBrowser(browser) |
| 136 | +
|
| 137 | + expect(await getByTestId('testid-in-data-automation-id-attr')).toBeDefined() |
| 138 | +}) |
| 139 | +``` |
| 140 | + |
| 141 | +## Other Solutions |
| 142 | + |
| 143 | +I'm not aware of any, if you are please [make a pull request][prs] and add it |
| 144 | +here! |
| 145 | + |
| 146 | +## LICENSE |
| 147 | + |
| 148 | +MIT |
| 149 | + |
| 150 | +[npm]: https://www.npmjs.com/ |
| 151 | +[node]: https://nodejs.org |
| 152 | +[build-badge]: https://github.com/olivierwilkinson/webdriverio-testing-library/workflows/webdriverio-testing-library/badge.svg |
| 153 | +[build]: https://github.com/olivierwilkinson/webdriverio-testing-library/actions?query=branch%3Amaster+workflow%3Awebdriverio-testing-library |
| 154 | +[version-badge]: https://img.shields.io/npm/v/olivierwilkinson/webdriverio-testing-library.svg?style=flat-square |
| 155 | +[package]: https://www.npmjs.com/package/olivierwilkinson/webdriverio-testing-library |
| 156 | +[downloads-badge]: https://img.shields.io/npm/dm/olivierwilkinson/webdriverio-testing-library.svg?style=flat-square |
| 157 | +[npmtrends]: http://www.npmtrends.com/olivierwilkinson/webdriverio-testing-library |
| 158 | +[license-badge]: https://img.shields.io/npm/l/olivierwilkinson/webdriverio-testing-library.svg?style=flat-square |
| 159 | +[license]: https://github.com/olivierwilkinson/webdriverio-testing-library/blob/master/LICENSE |
| 160 | +[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square |
| 161 | +[prs]: http://makeapullrequest.com |
| 162 | +[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square |
| 163 | +[coc]: https://github.com/olivierwilkinson/webdriverio-testing-library/blob/master/other/CODE_OF_CONDUCT.md |
| 164 | +[dom-testing-library]: https://github.com/testing-library/dom-testing-library |
| 165 | +[webdriverio]: https://webdriver.io/ |
| 166 | +[nightwatch-testing-library]: https://github.com/testing-library/nightwatch-testing-library |
0 commit comments