|
| 1 | +--- |
| 2 | +uid: NamedScriptsArticle |
| 3 | +--- |
| 4 | +# Named JavaScripts |
| 5 | + |
| 6 | +Sometimes it is neccesary to execute JavaScript in the web browser. |
| 7 | +This might be to work around a limitation in a WebDriver implementation, such as [web browsers which cannot enter values into date fields]. |
| 8 | +At other times, JavaScript might be used to perform large-scale operations which would perform very poorly if conducted through many WebDriver interactions. |
| 9 | +This is particularly relevant where a Remote WebDriver is being used, in which each individual interaction suffers the latency and overhead of a remote network request/response. |
| 10 | + |
| 11 | +Named scripts provide a mechanism by which developers may build a library of reusable pre-defined scripts and give them human-readable names. |
| 12 | +This way, when those scripts are used, they produce clear and concise entries in [the Screenplay report]. |
| 13 | +Additionally, named scripts provide .NET type safety via generic type parameters. |
| 14 | + |
| 15 | +[web browsers which cannot enter values into date fields]: xref:CSF.Screenplay.Selenium.BrowserQuirks.CannotSetInputTypeDateWithSendKeys |
| 16 | +[the Screenplay report]: ../../GettingReports.md |
| 17 | + |
| 18 | +## The classes |
| 19 | + |
| 20 | +There are many script classes available, differing primarily in the number of paramaters which they accept. |
| 21 | +Here is a table listing them all. |
| 22 | +Pick the one which returns/does not return a result and which has the correct number of parameters for your script. |
| 23 | + |
| 24 | +| Params | No result | With result | |
| 25 | +| ------ | --------- | ----------- | |
| 26 | +| 0 | [`NamedScript`] | [`NamedScriptWithResult<TResult>`] | |
| 27 | +| 1 | [`NamedScript<T1>`] | [`NamedScriptWithResult<T1,TResult>`] | |
| 28 | +| 2 | [`NamedScript<T1,T2>`] | [`NamedScriptWithResult<T1,T2,TResult>`] | |
| 29 | +| 3 | [`NamedScript<T1,T2,T3>`] | [`NamedScriptWithResult<T1,T2,T3,TResult>`] | |
| 30 | +| 4 | [`NamedScript<T1,T2,T3,T4>`] | [`NamedScriptWithResult<T1,T2,T3,T4,TResult>`] | |
| 31 | +| 5 | [`NamedScript<T1,T2,T3,T4,T5>`] | [`NamedScriptWithResult<T1,T2,T3,T4,T5,TResult>`] | |
| 32 | +| 6 | [`NamedScript<T1,T2,T3,T4,T5,T6>`] | [`NamedScriptWithResult<T1,T2,T3,T4,T5,T6,TResult>`] | |
| 33 | +| 7 | [`NamedScript<T1,T2,T3,T4,T5,T6,T7>`] | [`NamedScriptWithResult<T1,T2,T3,T4,T5,T6,T7,TResult>`] | |
| 34 | + |
| 35 | +[`NamedScript`]: xref:CSF.Screenplay.Selenium.NamedScript |
| 36 | +[`NamedScriptWithResult<TResult>`]: xref:CSF.Screenplay.Selenium.NamedScriptWithResult`1 |
| 37 | +[`NamedScript<T1>`]: xref:CSF.Screenplay.Selenium.NamedScript`1 |
| 38 | +[`NamedScriptWithResult<T1,TResult>`]: xref:CSF.Screenplay.Selenium.NamedScriptWithResult`2 |
| 39 | +[`NamedScript<T1,T2>`]: xref:CSF.Screenplay.Selenium.NamedScript`2 |
| 40 | +[`NamedScriptWithResult<T1,T2,TResult>`]: xref:CSF.Screenplay.Selenium.NamedScriptWithResult`3 |
| 41 | +[`NamedScript<T1,T2,T3>`]: xref:CSF.Screenplay.Selenium.NamedScript`3 |
| 42 | +[`NamedScriptWithResult<T1,T2,T3,TResult>`]: xref:CSF.Screenplay.Selenium.NamedScriptWithResult`4 |
| 43 | +[`NamedScript<T1,T2,T3,T4>`]: xref:CSF.Screenplay.Selenium.NamedScript`4 |
| 44 | +[`NamedScriptWithResult<T1,T2,T3,T4,TResult>`]: xref:CSF.Screenplay.Selenium.NamedScriptWithResult`5 |
| 45 | +[`NamedScript<T1,T2,T3,T4,T5>`]: xref:CSF.Screenplay.Selenium.NamedScript`5 |
| 46 | +[`NamedScriptWithResult<T1,T2,T3,T4,T5,TResult>`]: xref:CSF.Screenplay.Selenium.NamedScriptWithResult`6 |
| 47 | +[`NamedScript<T1,T2,T3,T4,T5,T6>`]: xref:CSF.Screenplay.Selenium.NamedScript`6 |
| 48 | +[`NamedScriptWithResult<T1,T2,T3,T4,T5,T6,TResult>`]: xref:CSF.Screenplay.Selenium.NamedScriptWithResult`7 |
| 49 | +[`NamedScript<T1,T2,T3,T4,T5,T6,T7>`]: xref:CSF.Screenplay.Selenium.NamedScript`7 |
| 50 | +[`NamedScriptWithResult<T1,T2,T3,T4,T5,T6,T7,TResult>`]: xref:CSF.Screenplay.Selenium.NamedScriptWithResult`8 |
| 51 | + |
| 52 | +## Using a script |
| 53 | + |
| 54 | +Execute scripts within your performables using the builder method [`PerformableBuilder.ExecuteAScript`] or one of its many same-named overloads. |
| 55 | +Generic type inference will offer the appropriate number and types of parameters and will expose a strongly-typed result if applicable. |
| 56 | + |
| 57 | +Within the JavaScript body, the parameters are accessible via an `arguments` object. |
| 58 | +Each parameter is assigned to this object using a zero-based index. |
| 59 | +So, the first parameter value is `arguments[0]`, the second `arguments[1]` and so on. |
| 60 | + |
| 61 | +[`PerformableBuilder.ExecuteAScript`]: xref:CSF.Screenplay.Selenium.PerformableBuilder.ExecuteAScript(CSF.Screenplay.Selenium.NamedScript) |
0 commit comments