Testcafe Testing Library
Introduction
testcafe-testing-library
allows the use of dom testing library queries
within Testcafe for cross-browser
end-to-end web testing.
If you are new to the testing-library approach for writing tests, check out the this guide on which query to use as well as the cheat sheet.
Install
Usage
testcafe-testing-library
provides custom Selectors allowing you to query the
dom.
Add the following to your .testcaferc.json file:
You can now import screen
which has all the get[All]By, query[All]By,
find[All]By* selectors that you can use in your tests.
import { screen } from '@testing-library/testcafe'
See DOM Testing Library
API for reference
A note about queries in testcafe. Testcafe has built in waiting, for this reason, there's no difference between
queryBy
andfindBy
queries in testcafe testing library.getBy
queries will throw an exception (as designed) so they will fail immediately and do not work with the built in waiting that Testcafe provides.
Examples
To show some simple examples (from https://github.com/testing-library/testcafe-testing-library/blob/master/tests/testcafe/selectors.ts):
Configure
You can customize the testIdAttribute using the configure function of DOM Testing Library in a few different ways:
Once in a single page load:
For every test & page load in a fixture:
injecting clientScripts
Globally for all fixtures, tests and page loads byNote: the dom-testing-library umd must come before your configure script
Containers
By default the selectors come pre-bound to document.body
, so no need to
provide a container. However, if you want to restrict your query using a
container, you can use within
which can take either a string or a query
(get[All]By, query[All]By, find[All]By*).