These examples show how to inject custom scripts into tested webpages.
Test Code: inject-a-nodejs-module.js
TestCafe allows you to add third-party Node.js modules to the tested page's code during test execution.
This example shows how to inject the lodash module into the tested page and use the defaults method in client-side code.
The fixture.clientScripts method is used to inject the module into all pages visited during the fixture execution. The module
option passed to fixture.clientScripts
specifies the module name. TestCafe searches for the module's entry point with Node.js mechanisms and injects its content.
Then, the t.eval method runs client-side code that uses the defaults method from lodash
. The result is checked in test code with the ok assertion.
Browsers can execute the
lodash
module because it implements the UMD API. You can use TestCafe to inject any UMD-compatible module into the tested pages.