Skip to content

Latest commit

 

History

History
 
 

client-scripts

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Inject Custom Client Scripts

These examples show how to inject custom scripts into tested webpages.

Inject a Node.js module into a tested page

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.