Testcafe Testing Library
简介
testcafe-testing-library
允许在
Testcafe 中使用 dom testing library 查
询,进行跨浏览器的端到端网络测试。
如果你是编写 testing-library 方法的新手,请查看关 于使用哪种查询的指南,以 及小抄。
安装
- npm
- Yarn
npm install --save-dev testcafe @testing-library/testcafe
yarn add --dev testcafe @testing-library/testcafe
使用
testcafe-testing-library
提供自定义选择器,允许你查询 dom。
在你的 .testcaferc.json
文件中添加以下内容:
"clientScripts": [
{ "module": "@testing-library/dom/dist/@testing-library/dom.umd.js" }
],
你现在可以导入 screen
,其中有所有 get[All]By*、query[All]By*、find[All]By*
选择器,你可以在测试中使用。
import {screen} from '@testing-library/testcafe'
关于 testcafe 中的查询的一个说明。Testcafe 有内置的等待, 由于这个原因,在 testcafe 测试库中
queryBy
和findBy
查询没有区别。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 测试库的 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`http://localhost: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 脚本之前出现。
"clientScripts": [
"./node_modules/@testing-library/dom/dist/@testing-library/dom.umd.js",
"./path/to/my-app-testcafe.config.js"
]
window.TestingLibraryDom.configure({testIdAttribute: 'data-automation-id'})
容器
默认情况下,选择器会预先绑定在 document.body
上,所以不需要提供一个容器。然而
,如果你想用一个容器来限制你的查询,你可以使用 within
,它可以接受一个字符串或
一个查询(get[All]By, query[All]By, find[All]By*)。
使用 within
的例子
import {within, screen} from '@testing-library/testcafe'
fixture`within`.page`http://localhost: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()
})