本文へスキップ

Testcafe Testing Library

はじめに

testcafe-testing-library を使用すると、クロスブラウザのエンドツーエンドWebテストのために、Testcafe 内でDOMテストライブラリのクエリを使用できます。

テスト記述のためのテストライブラリのアプローチが初めての場合は、どのクエリを使用するかについてのこのガイドチートシートをご覧ください。

インストール

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

使用方法

testcafe-testing-libraryは、DOMをクエリできるカスタムセレクターを提供します。

.testcaferc.jsonファイルに以下を追加します。

  "clientScripts": [
{ "module": "@testing-library/dom/dist/@testing-library/dom.umd.js" }
],

これで、すべてのgetを含むscreenをインポートできます。[All]By、query[All]By、find[All]By*テストで使用できるセレクター。

import {screen} from '@testing-library/testcafe'

参照についてはクエリを参照してください。

Testcafeでのクエリの注意点。Testcafeにはビルトインの待機機能があるため、Testcafe Testing LibraryではqueryByfindByクエリに違いはありません。getByクエリは(設計どおり)例外をスローするため、すぐに失敗し、Testcafeが提供するビルトインの待機機能と連携しません。

いくつかの簡単な例を示します(https://github.com/testing-library/testcafe-testing-library/blob/master/tests/testcafe/selectors.tsから)

import {screen} from '@testing-library/testcafe'

test('getByPlaceHolderText', async t => {
await t.typeText(
screen.getByPlaceholderText('Placeholder Text'),
'Hello Placeholder',
)
})
test('getByText', async t => {
await t.click(screen.getByText('getByText'))
})

test('getByLabelText', async t => {
await t.typeText(
screen.getByLabelText('Label For Input Labelled By Id'),
'Hello Input Labelled By Id',
)
})

test('queryAllByText', async t => {
await t.expect(screen.queryAllByText('Button Text').exists).ok()
await t
.expect(screen.queryAllByText('Non-existing Button Text').exists)
.notOk()
})

設定

いくつかの異なる方法で、DOM Testing Libraryのconfigure関数を使用して、testIdAttributeをカスタマイズできます。

単一ページロード時に一度だけ:

import {configureOnce, getByTestId} from '@testing-library/testcafe'

test('can be configured once in a single page load', async t => {
await configureOnce({testIdAttribute: 'data-other-test-id'})
await t.click(screen.getByTestId('other-id'))
})

フィクスチャ内のすべてのテストとページロードごとに:

import {configure, screen} from '@testing-library/testcafe'

fixture`configure`.clientScripts(
configure({testIdAttribute: 'data-automation-id'}),
).page`https://#:13370`

test('supports alternative testIdAttribute', async t => {
await t.click(screen.getByTestId('image-with-random-alt-tag'))
})

test('still works after browser page load and reload', async t => {
await t.click(screen.getByText('Go to Page 2'))

await t.eval(() => location.reload(true))

await t
.click(screen.getByTestId('page2-thing'))
.expect(screen.getByText('second page').exists)
.ok()
})

clientScriptsを注入することで、すべてのフィクスチャ、テスト、ページロードに対してグローバルに設定します

注:dom-testing-libraryのUMDは、configureスクリプトの前に配置する必要があります。

.testcaferc.json
  "clientScripts": [
"./node_modules/@testing-library/dom/dist/@testing-library/dom.umd.js",
"./path/to/my-app-testcafe.config.js"
]
./path/to/my-app-testcafe.config.js
window.TestingLibraryDom.configure({testIdAttribute: 'data-automation-id'})

コンテナ

デフォルトでは、セレクターはdocument.bodyに事前にバインドされているため、コンテナを提供する必要はありません。ただし、コンテナを使用してクエリを制限する場合は、文字列またはクエリ(get[All]By、query[All]By、find[All]By*).

withinを使用した例

import {within, screen} from '@testing-library/testcafe'

fixture`within`.page`https://#:13370`

test('works with getBy* selectors', async t => {
await t
.expect(
within(screen.getByTestId('nested')).getByText('Button Text').exists,
)
.ok()
})

test('works with CSS selector strings', async t => {
const {getByText} = await within('#nested')
await t.click(getByText('Button Text')).ok()
})

test('works on any testcafe selector', async t => {
const nested = Selector('#nested')

await t.expect(within(nested).getByText('Button Text').exists).ok()
})

test('works with results from "byAll" query with index - regex', async t => {
const nestedDivs = screen.getAllByTestId(/nested/)
await t.expect(nestedDivs.count).eql(2)

await t
.expect(within(nestedDivs.nth(0)).getByText('Button Text').exists)
.ok()
.expect(
within(nestedDivs.nth(1)).getByText('text only in 2nd nested').exists,
)
.ok()
})