Skip to content

Commit d73975c

Browse files
jgrahamwhimboo
andauthored
Add support for preload scripts (#292)
* Add support for "preload scripts" These are scripts which are run when a realm (initially only for realms with a Window global) is created. They allow a test harness to inject functionality that's guaranteed to be available for any content scripts that are subsequently loaded, and before any later scripts that WebDriver injects into the context. Registering a load script happens with the script.addPreloadScript command. This returns a handle to the added script. Load scripts can later be removed by passing the handle to script.removePreloadScript. Preload scripts can either be run directly in the newly created Window, or in a named sandbox. Co-authored-by: Henrik Skupin <[email protected]>
1 parent 14731db commit d73975c

File tree

1 file changed

+181
-10
lines changed

1 file changed

+181
-10
lines changed

index.bs

Lines changed: 181 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,6 @@ ErrorResponse = {
289289
Extensible
290290
}
291291

292-
ErrorCode = {
293-
"invalid argument" /
294-
"no such alert" /
295-
"no such frame" /
296-
"session not created" /
297-
"unknown command" /
298-
"unknown error" /
299-
"unsupported operation"
300-
}
301-
302292
ResultData = (
303293
EmptyResult //
304294
SessionResult //
@@ -424,6 +414,28 @@ The <dfn export for=command>set of all command names</dfn> is a set containing
424414
all the defined [=command names=], including any belonging to [=extension
425415
modules=].
426416

417+
## Errors ## {#errors}
418+
419+
WebDriver BiDi extends the set of [=error codes=] from [[WEBDRIVER|WebDriver]]
420+
with the following additional codes:
421+
422+
<dl>
423+
<dt><dfn>No such script</dfn>
424+
<dd>Tried to remove an unknown [=preload script=].
425+
</dl>
426+
427+
<pre class="cddl local-cddl">
428+
ErrorCode = ("invalid argument" /
429+
"invalid session id" /
430+
"no such alert" /
431+
"no such frame" /
432+
"no such script" /
433+
"session not created" /
434+
"unknown command" /
435+
"unknown error" /
436+
"unsupported operation")
437+
</pre>
438+
427439
## Events ## {#events}
428440

429441
An <dfn export>event</dfn> is a notification, sent by the [=remote
@@ -3792,17 +3804,20 @@ relating to script realms and execution.
37923804

37933805
<pre class="cddl remote-cddl">
37943806
ScriptCommand = (
3807+
script.AddPreloadScriptCommand //
37953808
script.CallFunction //
37963809
script.Disown //
37973810
script.Evaluate //
37983811
script.GetRealms
3812+
script.RemovePreloadScriptCommand
37993813
)
38003814
</pre>
38013815

38023816
[=local end definition=]
38033817

38043818
<pre class="cddl local-cddl">
38053819
ScriptResult = (
3820+
script.AddPreloadScriptResult //
38063821
script.EvaluateResult //
38073822
script.GetRealmsResult
38083823
)
@@ -3813,6 +3828,55 @@ ScriptEvent = (
38133828
)
38143829
</pre>
38153830

3831+
### Preload Scripts ### {#preload-scripts}
3832+
3833+
A <dfn>Preload script</dfn> is one which runs on creation of a new {{Window}},
3834+
before any author-defined script have run.
3835+
3836+
TODO: Extend this to scripts in other kinds of realms.
3837+
3838+
A [=BiDi session=] has a <dfn>preload script map</dfn> which is a map in which the
3839+
keys are [[!RFC4122|UUID]]s, and the values are [=structs=] with an <a
3840+
for=struct>item</a> named <code>expression</code>, which is a string, and an
3841+
item named <code>sandbox</code> which is a string or null.
3842+
3843+
<div algorithm>
3844+
To <dfn export>run WebDriver BiDi preload scripts</dfn> given |environment settings|:
3845+
3846+
1. Let |document| be |environment settings|' [=relevant global object=]'s
3847+
<a>associated <code>Document</code></a>.
3848+
3849+
1. Let |browsing context| be |document|'s [=/browsing context=].
3850+
3851+
1. For each |session| in [=active BiDi sessions=]:
3852+
3853+
1. For each |preload script| in |session|'s [=preload script map=]'s
3854+
[=values=]:
3855+
3856+
1. If |preload script|'s <code>sandbox</code> is not null, let |realm| be [=get
3857+
or create a sandbox realm=] with |preload script|'s <code>sandbox</code> and
3858+
|browsing context|. Otherwise let |realm| be |environment settings|'
3859+
[=realm execution context=]'s Realm component.
3860+
3861+
1. Let |source| be |preload script|'s <code>expression</code>.
3862+
3863+
1. Let |options| be the [=default classic script fetch options=].
3864+
3865+
1. Let |base URL| be the [=API base URL=] of |environment settings|.
3866+
3867+
1. Let |script| be the result of [=create a classic script=] with |source|,
3868+
|environment settings|, |base URL|, and |options|.
3869+
3870+
1. [=Prepare to run script=] with |environment settings|.
3871+
3872+
1. Let |evaluation status| be [=ScriptEvaluation=](|script|'s record).
3873+
3874+
1. If |evaluation status| is an [=abrupt completion=], then [=report the
3875+
exception=] given by |evaluation status|.\[[Value]] for |script|.
3876+
3877+
1. [=Clean up after running script=] with |environment settings|.
3878+
3879+
</div>
38163880

38173881
### Types ### {#module-script-types}
38183882

@@ -3867,6 +3931,17 @@ To <dfn>get exception details</dfn> given a |realm|, a [=completion record=]
38673931

38683932
</div>
38693933

3934+
#### The script.PreloadScript type #### {#type-script-PreloadScript}
3935+
3936+
[=Remote end definition=]
3937+
3938+
<pre class="cddl remote-cddl local-cddl">
3939+
script.PreloadScript = text;
3940+
</pre>
3941+
3942+
The <code>script.PreloadScript</code> type represents a handle to a script that will run
3943+
on realm creation.
3944+
38703945
#### The script.Realm type #### {#type-script-Realm}
38713946

38723947
[=Remote end definition=] and [=local end definition=]
@@ -4323,6 +4398,57 @@ Issue: This has the wrong error code
43234398

43244399
### Commands ### {#module-script-commands}
43254400

4401+
#### The script.addPreloadScript Command #### {#command-script-addPreloadScript}
4402+
4403+
The <dfn export for=commands>script.addPreloadScript</dfn> command adds a [=preload
4404+
script=].
4405+
4406+
<dl>
4407+
<dt>Command Type</dt>
4408+
<dd>
4409+
<pre class="cddl remote-cddl">
4410+
script.AddPreloadScriptCommand = {
4411+
method: "script.addPreloadScript",
4412+
params: script.AddPreloadScriptParameters
4413+
}
4414+
4415+
script.AddPreloadScriptParameters = {
4416+
expression: text;
4417+
?sandbox: text
4418+
}
4419+
</pre>
4420+
</dd>
4421+
<dt>Return Type</dt>
4422+
<dd>
4423+
<pre class="cddl local-cddl">
4424+
script.AddPreloadScriptResult = {
4425+
script: script.PreloadScript
4426+
}
4427+
</pre>
4428+
</dd>
4429+
</dl>
4430+
4431+
<div algorithm="remote end steps for script.addPreloadScript">
4432+
The [=remote end steps=] given |session| and |command parameters| are:
4433+
4434+
1. Let |expression| be the <code>expression</code> field of |command
4435+
parameters|.
4436+
4437+
1. Let |script| be the string representation of a [[!RFC4122|UUID]].
4438+
4439+
1. Let |sandbox| be the value of the "<code>sandbox</code>" field in |command
4440+
parameters|, if present, or null otherwise.
4441+
4442+
1. Let |preload script map| be |session|'s [=preload script map=].
4443+
4444+
1. Set |preload script map|[|script|] to a struct with <code>expression</code>
4445+
|expression| and <code>sandbox</code> |sandbox|.
4446+
4447+
1. Return a new map matching the <code>script.AddPreloadScriptResult</code> with the
4448+
<code>script</code> field set to |script|.
4449+
4450+
</div>
4451+
43264452
#### The script.disown Command #### {#command-script-disown}
43274453

43284454
The <dfn export for=commands>script.disown</dfn> command disowns the given handles.
@@ -4714,6 +4840,51 @@ Issue: We might want to have a more sophisticated filter system than just a
47144840

47154841
</div>
47164842

4843+
#### The script.removePreloadScript Command #### {#command-script-removePreloadScript}
4844+
4845+
The <dfn export for=commands>script.removePreloadScript</dfn> command removes a
4846+
[=preload script=].
4847+
4848+
<dl>
4849+
<dt>Command Type</dt>
4850+
<dd>
4851+
<pre class="cddl remote-cddl">
4852+
script.RemovePreloadScriptCommand = {
4853+
method: "script.removePreloadScript",
4854+
params: script.RemovePreloadScriptParameters
4855+
}
4856+
4857+
script.RemovePreloadScriptParameters = {
4858+
script: script.PreloadScript
4859+
}
4860+
</pre>
4861+
</dd>
4862+
<dt>Return Type</dt>
4863+
<dd>
4864+
<pre class="cddl">
4865+
EmptyResult
4866+
</pre>
4867+
</dd>
4868+
</dl>
4869+
4870+
<div algorithm="remote end steps for script.removePreloadScript">
4871+
The [=remote end steps=] given |session| and |command parameters| are:
4872+
4873+
1. Let |script| be the value of the "<code>script</code>" field in |command
4874+
parameters|.
4875+
4876+
1. Let |preload script map| be |session|'s [=preload script map=].
4877+
4878+
1. If |preload script map| does not <a for=map>contain</a> |script|, return
4879+
[=error=] with [=error code=] [=no such script=].
4880+
4881+
1. <a for=set>Remove</a> |script| from |preload script map|.
4882+
4883+
1. Return null
4884+
4885+
</div>
4886+
4887+
47174888
### Events ### {#module-script-events}
47184889

47194890
#### The script.realmCreated Event #### {#event-script-realmCreated}

0 commit comments

Comments
 (0)