Skip to content

Commit

Permalink
[docs] Enhancements for client scripts (#4110)
Browse files Browse the repository at this point in the history
* Fixes for client script docs

* Address remarks
  • Loading branch information
VasilyStrelyaev committed Aug 6, 2019
1 parent 837d176 commit 74b3322
Showing 1 changed file with 27 additions and 5 deletions.
Expand Up @@ -7,11 +7,22 @@ permalink: /documentation/using-testcafe/common-concepts/inject-scripts-into-tes

TestCafe allows you to inject custom client scripts into pages visited during the tests. You can add scripts that mock browser API or provide helper functions.

* [Add Client Scripts to All Tests](#add-client-scripts-to-all-tests)
* [Add Client Scripts to Specific Tests](#add-client-scripts-to-specific-tests)
* [Provide Scripts to Inject](#provide-scripts-to-inject)
* [Inject a JavaScript File](#inject-a-javascript-file)
* [Inject a Module](#inject-a-module)
* [Inject Script Code](#inject-script-code)
* [Provide Scripts for Specific Pages](#provide-scripts-for-specific-pages)
* [Inject Scripts Into Iframes](#inject-scripts-into-iframes)
* [Specify Multiple Scripts](#specify-multiple-scripts)
* [Access DOM in the Injected Scripts](#access-dom-in-the-injected-scripts)

Use [test run options](#add-client-scripts-to-all-tests) to add client scripts to all tests, or [test API](#add-client-scripts-to-specific-tests) to add them to specific fixtures or tests.

## Add Client Scripts to All Tests

Use either of the following to inject scripts into pages visited during all the tests:
Use either of the following options to inject scripts into pages visited during all the tests:

* the [--cs (--client-scripts)](../command-line-interface.md#--cs-pathpath2---client-scripts-pathpath2) command line option

Expand Down Expand Up @@ -57,7 +68,7 @@ You can pass the following arguments to specify the scripts to inject:
* [module name](#inject-a-module)
* [script code](#inject-script-code)

You can also [inject scripts into specific pages](#provide-scripts-for-specific-pages).
You can also [inject scripts into specific pages](#provide-scripts-for-specific-pages) and [iframes](#inject-scripts-into-iframes).

> Note that the API methods and configuration option support [multiple arguments](#specify-multiple-scripts).
Expand Down Expand Up @@ -138,7 +149,7 @@ runner.clientScripts({ path: 'assets/jquery.js' });
}
```

> You cannot combine the `path`, [module](#inject-a-module) and [content](#inject-script-code) properties.
> You cannot combine the `path`, [module](#inject-a-module) and [content](#inject-script-code) properties in a single object. To inject multiple items, pass several arguments or an array.
### Inject a Module

Expand Down Expand Up @@ -206,7 +217,7 @@ fixture `My fixture`
}
```

> You cannot combine the `module`, [path](#inject-a-javascript-file) and [content](#inject-script-code) properties.
> You cannot combine the `module`, [path](#inject-a-javascript-file) and [content](#inject-script-code) properties in a single object. To inject multiple items, pass several arguments or an array.
### Inject Script Code

Expand Down Expand Up @@ -271,7 +282,7 @@ test
}
```

> You cannot combine the `content`, [path](#inject-a-javascript-file) and [module](#inject-a-module) properties.
> You cannot combine the `content`, [path](#inject-a-javascript-file) and [module](#inject-a-module) properties in a single object. To inject multiple items, pass several arguments or an array.
### Provide Scripts for Specific Pages

Expand Down Expand Up @@ -385,6 +396,17 @@ runner.clientScripts({

> If the target page redirects to a different URL, ensure that the `page` property matches the destination URL. Otherwise, scripts are not injected.
### Inject Scripts Into Iframes

To inject a script into an iframe, specify the iframe URL in the [page](#provide-scripts-for-specific-pages) property.

```js
runner.clientScripts({
path: 'scripts/helpers.js',
page: 'https://example.com/iframe/'
}));
```

### Specify Multiple Scripts

You can pass multiple arguments or an array to the `clientScripts` methods:
Expand Down

0 comments on commit 74b3322

Please sign in to comment.