Test.clientScripts Method
Injects scripts into all pages visited during the test.
test.clientScripts( script[, script2[, ...[, scriptN]]] ) → this
Parameter | Type | Description |
---|---|---|
script , script2 , scriptN |
String | Object | Array | Scripts to inject into the tested pages. See Provide Scripts to Inject for information on how to specify scripts. |
test('My test', async () => {
/* ... */
}).clientScripts({
module: 'async',
});
test('My test', async () => {
/* ... */
}).clientScripts({
page: /\/testcafe\/example\//,
content: 'Geolocation.prototype.getCurrentPosition = success => success({latitude: 0, longitude: 0});',
});
Note
Relative paths are resolved against the test file location.
You can use the page option to specify pages into which scripts should be injected. Otherwise, TestCafe injects scripts into all pages visited during the test.
Inject a JavaScript File
Specify the JavaScript file path to inject the content of this file into the tested pages. You can pass a string or object with the path
property.
test.clientScripts(filePath | { path: filePath })
test.clientScripts(filePath | { path: filePath }, ...)
test.clientScripts([ filePath | { path: filePath } ])
Argument | Type | Description |
---|---|---|
filePath |
String | The path to the JavaScript file whose content should be injected. |
Note
You cannot combine the path
, module and content properties in a single object. To inject multiple items, pass several arguments or an array.
Example
test('My test', async () => {
/* ... */
}).clientScripts('./script.js');
Inject a Module
Specify the Node.js module’s name to inject its content into the tested pages. Use an object with the module
property.
test.clientScripts( { module: moduleName } )
test.clientScripts( { module: moduleName }, ... )
test.clientScripts([ { module: moduleName } ])
Argument | Type | Description |
---|---|---|
moduleName |
String | The module name. |
Note
You cannot combine the module
, path and content properties in a single object. To inject multiple items, pass several arguments or an array.
TestCafe uses Node.js mechanisms to search for the module’s entry point and injects its content into the tested page.
The browser must be able to execute the injected module. For example, modules that implement the UMD API can run in most modern browsers.
Note
If the injected module has dependencies, ensure that the dependencies can be loaded as global variables and these variables are initialized in the page’s code.
Example
test('My test', async () => {
/* ... */
}).clientScripts({
module: 'lodash',
});
Inject Script Code
You can pass an object with the content
property to provide the injected script as a string.
test.clientScripts({ content: code })
test.clientScripts({ content: code }, ...)
test.clientScripts([ { content: code } ])
Argument | Type | Description |
---|---|---|
code |
String | JavaScript that should be injected. |
Note
You cannot combine the content
, path and module properties in a single object. To inject multiple items, pass several arguments or an array.
Example
const mockDate = `
Date.prototype.getTime = function () {
return 42;
};
`;
test('My test', async () => {
/* ... */
}).clientScripts({ content: mockDate });
Provide Scripts for Specific Pages
You can also specify pages into which a script should be injected. This will allow you to mock browser API on specified pages and use the default behavior everywhere else.
To specify target pages for a script, add the page
property to the object you pass to clientScripts
.
test.clientScripts({
page: url,
path: filePath | module: moduleName | content: code
})
test.clientScripts({
page: url,
path: filePath | module: moduleName | content: code
}, ...)
test.clientScripts([
{
page: url,
path: filePath | module: moduleName | content: code
}
])
Property | Type | Description |
---|---|---|
url |
String | RegExp | Specify a page URL to add scripts to a page, or a regular expression to add scripts to pages whose URLs match this expression. |
Note
If the target page redirects to a different URL, ensure that the page
property matches the destination URL. Otherwise, scripts are not injected.
Example
test('My test', async () => {
/* ... */
}).clientScripts({
page: /\/user\/profile\//,
path: './script.js',
});