跳到主要內容

Testcafe Testing Library

簡介

testcafe-testing-library 允許在 Testcafe 中使用 dom testing library 查詢,以進行跨瀏覽器的端對端網頁測試。

如果您是測試程式庫方法的新手,請查看此關於使用哪個查詢的指南以及速查表

安裝

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

用法

testcafe-testing-library 提供自訂的 Selectors,允許您查詢 dom。

將以下內容新增至您的 .testcaferc.json 檔案

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

您現在可以匯入 screen,它具有所有 get[所有]By、query[All]By、find[所有]By*您可以於測試中使用的 selectors。

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

請參閱 Queries 以取得參考

關於 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 的設定函數,以幾種不同的方式自訂 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'))
})

在 fixture 中,針對每個測試和頁面載入:

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

fixture`configure`.clientScripts(
configure({testIdAttribute: 'data-automation-id'}),
).page`https://127.0.0.1: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 全域套用於所有 fixture、測試和頁面載入:

注意:dom-testing-library umd 必須在您的設定指令碼之前

.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'})

容器

預設情況下,selectors 會預先繫結至 document.body,因此無需提供容器。但是,如果您想使用容器來限制查詢,您可以使用 within,它可以接受字串或查詢(get[所有]By、query[All]By、find[所有]By*).

使用 within 的範例

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

fixture`within`.page`https://127.0.0.1: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()
})