Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): support for parameterising injected scripts with data structures containing PageElement objects #2364

Merged
merged 14 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ rules:
Prop: true
Props: true
props: true
Ref: true
Refs: true
ref: true
refs: true
TOC: true
toc: true
temp: true
Expand Down
1 change: 1 addition & 0 deletions integration/playwright-test/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules
# Build artifacts
lib
playwright-report
test-results
output
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Ensure, equals } from '@serenity-js/assertions';
import { actorCalled, Clock, Question, Serenity, serenity } from '@serenity-js/core';
import { ActivityFinished, ActivityRelatedArtifactGenerated, ActivityStarts, ArtifactGenerated, SceneFinishes, SceneStarts } from '@serenity-js/core/lib/events';
import { TextData } from '@serenity-js/core/lib/model';
import { By, ExecuteScript, LastScriptExecution, Navigate, PageElement, Switch, Value } from '@serenity-js/web';
import { By, ExecuteScript, LastScriptExecution, Navigate, PageElement, PageElements, Switch, Value } from '@serenity-js/web';

import { defaultCardScenario, sceneId } from '../../../stage/crew/photographer/fixtures';

Expand Down Expand Up @@ -118,7 +118,7 @@ describe('ExecuteAsynchronousScript', function () {
Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute an asynchronous script with a Target argument', () =>
it('allows the actor to execute an asynchronous script with a PageElement argument', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

Expand All @@ -136,6 +136,98 @@ describe('ExecuteAsynchronousScript', function () {
Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

describe('when working with page elements', () => {
it('allows the actor to execute a synchronous script with a PageElement argument', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.async(`
var name = arguments[0];
var field = arguments[1];
var callback = arguments[arguments.length - 1];

setTimeout(function () {
field.value = name;
callback();
}, 100);
`).withArguments(actorCalled('Joe').name, Sandbox.Input),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute a synchronous script with a record of PageElement objects', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.async(`
var name = arguments[0];
var field = arguments[1].include[0];
var callback = arguments[arguments.length - 1];

setTimeout(function () {
field.value = name;
callback();
}, 100);
`).withArguments(actorCalled('Joe').name, Question.fromObject({ include: [ Sandbox.Input ] })),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute a synchronous script with an array of PageElement objects', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.async(`
var name = arguments[0];
var field = arguments[1][0];
var callback = arguments[arguments.length - 1];

setTimeout(function () {
field.value = name;
callback();
}, 100);
`).withArguments(actorCalled('Joe').name, Question.fromArray([ Sandbox.Input ])),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute a synchronous script with a PageElements argument', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.async(`
var name = arguments[0];
var field = arguments[1][0];
var callback = arguments[arguments.length - 1];

setTimeout(function () {
field.value = name;
callback();
}, 100);
`).withArguments(actorCalled('Joe').name, PageElements.located(By.css('input')) ),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute a synchronous script with a PageElement determined by PEQL', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.async(`
var name = arguments[0];
var field = arguments[1];
var callback = arguments[arguments.length - 1];

setTimeout(function () {
field.value = name;
callback();
}, 100);
`).withArguments(actorCalled('Joe').name, PageElements.located(By.css('input')).first() ),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));
});

it('provides a sensible description of the interaction being performed when invoked without arguments', () => {
expect(ExecuteScript.async(`
arguments[arguments.length - 1]();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Ensure, equals, includes } from '@serenity-js/assertions';
import { actorCalled, Clock, Question, Serenity, serenity } from '@serenity-js/core';
import { ActivityFinished, ActivityRelatedArtifactGenerated, ActivityStarts, ArtifactGenerated, SceneFinishes, SceneStarts } from '@serenity-js/core/lib/events';
import { TextData } from '@serenity-js/core/lib/model';
import { By, ExecuteScript, LastScriptExecution, Navigate, PageElement, Switch, Value } from '@serenity-js/web';
import { By, ExecuteScript, LastScriptExecution, Navigate, PageElement, PageElements, Switch, Value } from '@serenity-js/web';

import { defaultCardScenario, sceneId } from '../../../stage/crew/photographer/fixtures';

Expand Down Expand Up @@ -100,19 +100,77 @@ describe('ExecuteSynchronousScript', function () {
Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute a synchronous script with a Target argument', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),
describe('when working with page elements', () => {
it('allows the actor to execute a synchronous script with a PageElement argument', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.sync(`
var name = arguments[0];
var field = arguments[1];
ExecuteScript.sync(`
var name = arguments[0];
var field = arguments[1];

field.value = name;
`).withArguments(actorCalled('Joe').name, Sandbox.Input),

field.value = name;
`).withArguments(actorCalled('Joe').name, Sandbox.Input),
Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));
it('allows the actor to execute a synchronous script with a record of PageElement objects', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.sync(`
var name = arguments[0];
var field = arguments[1].include[0];

field.value = name;
`).withArguments(actorCalled('Joe').name, Question.fromObject({ include: [ Sandbox.Input ] })),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute a synchronous script with an array of PageElement objects', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.sync(`
var name = arguments[0];
var field = arguments[1][0];

field.value = name;
`).withArguments(actorCalled('Joe').name, Question.fromArray([ Sandbox.Input ])),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute a synchronous script with a PageElements argument', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.sync(`
var name = arguments[0];
var field = arguments[1][0];

field.value = name;
`).withArguments(actorCalled('Joe').name, PageElements.located(By.css('input')) ),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));

it('allows the actor to execute a synchronous script with a PageElement determined by PEQL', () =>
actorCalled('Joe').attemptsTo(
Navigate.to('/screenplay/interactions/execute-script/input_field.html'),

ExecuteScript.sync(`
var name = arguments[0];
var field = arguments[1];

field.value = name;
`).withArguments(actorCalled('Joe').name, PageElements.located(By.css('input')).first() ),

Ensure.that(Value.of(Sandbox.Input), equals(actorCalled('Joe').name)),
));
});

it('provides a sensible description of the interaction being performed when invoked without arguments', () => {
expect(ExecuteScript.sync(`
Expand Down