diff --git a/.testcaferc.json b/.testcaferc.json index 222efa3..e429314 100644 --- a/.testcaferc.json +++ b/.testcaferc.json @@ -1,6 +1,8 @@ { "src": "tests/testcafe/**/*.js", - "browsers": ["chromium"], - + "browsers": [ + "chrome:headless", + "firefox:headless" + ], "concurrency": 1 -} +} \ No newline at end of file diff --git a/README.md b/README.md index 8d17e28..558317d 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,6 @@

Testcafe selectors and utilities that encourage good testing practices laid down by dom-testing-library.

-**_Please go upvote this issue in testcafe to help make this library better_** https://github.com/DevExpress/testcafe/issues/3758 - [**Read the docs**](https://testing-library.com/docs/testcafe-testing-library/intro) | [Edit the docs](https://github.com/alexkrolick/testing-library-docs) diff --git a/src/index.js b/src/index.js index b72fba3..1e3f372 100644 --- a/src/index.js +++ b/src/index.js @@ -5,26 +5,58 @@ import path from 'path' import { ClientFunction, Selector } from 'testcafe' import { queries } from 'dom-testing-library' -const LIBRARY_UMD_PATH = path.join( +const DOM_TESTING_LIBRARY_UMD_PATH = path.join( './node_modules', 'dom-testing-library/dist/dom-testing-library.umd.js', ) -const LIBRARY_UMD_CONTENT = fs.readFileSync(LIBRARY_UMD_PATH).toString() +const DOM_TESTING_LIBRARY_UMD = fs.readFileSync(DOM_TESTING_LIBRARY_UMD_PATH).toString() -export const addTestcafeTestingLibrary = async t => { + + + +export async function configure(options, t) { + const configFunction = + ` + window.DomTestingLibrary.configure(${JSON.stringify(options)}); +`; + await ClientFunction(new Function(configFunction))(); + + if (t) { + t.testRun.injectable.scripts.push('/testcafe-testing-library-config.js'); + t.testRun.session.proxy.GET('/testcafe-testing-library-config.js', { content: configFunction, contentType: 'application/x-javascript' }) + } + +} + +export async function addTestcafeTestingLibrary(t) { + // inject for 1st pageload. Then just use injectables for subsequent page loads. // eslint-disable-next-line const inject = ClientFunction( () => { // eslint-disable-next-line no-undef window.eval(script) - window.TestCafeTestingLibrary = {} }, { - dependencies: { script: LIBRARY_UMD_CONTENT }, + dependencies: { script: DOM_TESTING_LIBRARY_UMD }, }, ) - await inject.with({ boundTestRun: t })() + await inject.with({ boundTestRun: t })(); + + //and for subsequent pageloads: + t.testRun.injectable.scripts.push('/dom-testing-library.js'); + t.testRun.session.proxy.GET('/dom-testing-library.js', { content: DOM_TESTING_LIBRARY_UMD, contentType: 'application/x-javascript' }) + + if (addTestcafeTestingLibrary.options) { + await configure(addTestcafeTestingLibrary.options, t); + } + +} + +// eslint-disable-next-line no-shadow +addTestcafeTestingLibrary.configure = function configure(options) { + addTestcafeTestingLibrary.options = { ...options }; + return addTestcafeTestingLibrary; } Object.keys(queries).forEach(queryName => { @@ -42,8 +74,10 @@ export const within = async sel => { await ClientFunction( new Function( ` - const elem = document.querySelector("${sanitizedSel}"); - window.TestCafeTestingLibrary["within_${sanitizedSel}"] = DomTestingLibrary.within(elem); + + window.TestcafeTestingLibrary = window.TestcafeTestingLibrary || {} + const elem = document.querySelector("${sanitizedSel}"); + window.TestcafeTestingLibrary["within_${sanitizedSel}"] = DomTestingLibrary.within(elem); `, ), @@ -53,7 +87,7 @@ export const within = async sel => { Object.keys(queries).forEach(queryName => { container[queryName] = Selector( new Function( - `return window.TestCafeTestingLibrary["within_${sanitizedSel}"].${queryName}(...arguments)`, + `return window.TestcafeTestingLibrary["within_${sanitizedSel}"].${queryName}(...arguments)`, ), ) }) @@ -61,11 +95,3 @@ export const within = async sel => { return container } - -export const configure = async options => { - await ClientFunction(new Function( - ` - window.DomTestingLibrary.configure(${JSON.stringify(options)}); - ` - )) -} \ No newline at end of file diff --git a/test-app/index.html b/test-app/index.html index 49024d5..ec1d9d1 100644 --- a/test-app/index.html +++ b/test-app/index.html @@ -56,11 +56,20 @@

configure

+
+

configure standalone

+ + +

getAllByText

+
+

navigate

+ Go to Page 2 +