Skip to content

Commit

Permalink
feat(web): accept Answerable as argument of ExecuteScript.from(source…
Browse files Browse the repository at this point in the history
…Url)

this makes it easier to inject scripts which URL becomes known only at runtime, e.g. scripts
parameterised with a query string
  • Loading branch information
jan-molak committed Oct 7, 2022
1 parent 9c9a6ec commit 0b06703
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Expand Up @@ -50,7 +50,7 @@ describe('ExecuteScriptFromUrl', function () {

it('provides a sensible description of the interaction being performed', () => {
expect(ExecuteScript.from('https://localhost/script.js').toString())
.to.equal(`#actor executes a script from https://localhost/script.js`);
.to.equal(`#actor executes a script from 'https://localhost/script.js'`);
});

it('correctly detects its invocation location', () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/web/src/screenplay/interactions/ExecuteScript.ts
Expand Up @@ -24,7 +24,7 @@ export class ExecuteScript {
* @param sourceUrl
* The URL to load the script from
*/
static from(sourceUrl: string): Interaction {
static from(sourceUrl: Answerable<string>): Interaction {
return new ExecuteScriptFromUrl(sourceUrl);
}

Expand Down Expand Up @@ -267,15 +267,16 @@ class ExecuteAsynchronousScript extends ExecuteScriptWithArguments {
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement
*/
class ExecuteScriptFromUrl extends Interaction {
constructor(private readonly sourceUrl: string) {
super(`#actor executes a script from ${ sourceUrl }`);
constructor(private readonly sourceUrl: Answerable<string>) {
super(d`#actor executes a script from ${ sourceUrl }`);
}

/**
* @inheritDoc
*/
async performAs(actor: UsesAbilities & AnswersQuestions): Promise<any> {
const page = await BrowseTheWeb.as(actor).currentPage();
const page = await BrowseTheWeb.as(actor).currentPage();
const sourceUrl = await actor.answer(this.sourceUrl);

return page.executeAsyncScript(
/* istanbul ignore next */
Expand All @@ -300,7 +301,7 @@ class ExecuteScriptFromUrl extends Interaction {
script.async = true;
document.head.append(script);
},
this.sourceUrl
sourceUrl
)
.then(errorMessage => {
if (errorMessage) {
Expand Down

0 comments on commit 0b06703

Please sign in to comment.