From 0bd61b0d10688fbe1fe781773873060e94742528 Mon Sep 17 00:00:00 2001 From: Ben Monro Date: Sun, 26 May 2019 17:37:56 -0700 Subject: [PATCH 1/5] feat: no more having to re-add DomTestingLibrary after page load! :party: --- src/index.js | 25 +++++++++++++++++-------- test-app/index.html | 4 ++++ test-app/page2.html | 1 + tests/testcafe/selectors.js | 9 +++++++++ 4 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 test-app/page2.html diff --git a/src/index.js b/src/index.js index b72fba3..4accc3e 100644 --- a/src/index.js +++ b/src/index.js @@ -5,26 +5,33 @@ 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 => { + // 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' }) + + + } Object.keys(queries).forEach(queryName => { @@ -42,8 +49,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 +62,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)`, ), ) }) diff --git a/test-app/index.html b/test-app/index.html index 49024d5..9f16d84 100644 --- a/test-app/index.html +++ b/test-app/index.html @@ -61,6 +61,10 @@

getAllByText

+
+

navigate

+ Go to Page 2 +