Permalink
Browse files

fix(protractor): executeScript and ExecuteAsyncScript will accept any…

… type of arguments (not only Ta

affects: serenity-js
  • Loading branch information...
jan-molak committed Oct 22, 2017
1 parent 2dc4a49 commit 3778a32b597d8e33b52e31343aded929d5144cc7
@@ -14,7 +14,7 @@ class Newsletter {
static Result = Target.the('newsletter result').located(by.css('label[for="checkbox"] pre'));
}
describe ('When working with an Angular app, a test scenario', function() {
describe ('When working with an Angular app, a test scenario can execute', function() {
this.timeout(10000);
@@ -25,39 +25,68 @@ describe ('When working with an Angular app, a test scenario', function() {
before(() => james.attemptsTo(Open.browserOn(app.demonstrating('angular_internals'))));
after(app.stop());
it ('can execute a script within its context', () =>
james.attemptsTo(
Execute.script('arguments[0].click();').on(Newsletter.Checkbox),
).then(() => Promise.all([
expect(james.toSee(Value.of(Newsletter.Checkbox))).eventually.equal('on'),
])));
it ('can execute an asynchronous script within its context', () =>
james.attemptsTo(
Execute.asyncScript(
'var callback = arguments[arguments.length - 1];',
'arguments[0].click();',
'callback();',
).on(Newsletter.Checkbox),
).then(() => Promise.all([
expect(james.toSee(Value.of(Newsletter.Checkbox))).eventually.equal('on'),
])));
it ('can execute an asynchronous script with arguments', () =>
james.attemptsTo(
Execute.asyncScript(
'var callback = arguments[arguments.length - 1];',
'angular.element(arguments[0]).val(arguments[1]).triggerHandler("input");',
'callback();',
).on(Username.Field).withArguments('James'),
).then(() => Promise.all([
expect(james.toSee(Text.of(Username.Result))).eventually.equal('James'),
])));
it ('can evaluate an expression in the context of an Angular $scope', () =>
james.attemptsTo(
Evaluate.script('text.username = "James"; $apply();').on(Username.Field),
).then(() => Promise.all([
expect(james.toSee(Text.of(Username.Result))).eventually.equal('James'),
])));
describe('a script that', () => {
it ('runs within its context', () =>
james.attemptsTo(
Execute.script('arguments[0].click();').on(Newsletter.Checkbox),
).then(() => Promise.all([
expect(james.toSee(Value.of(Newsletter.Checkbox))).eventually.equal('on'),
])));
it ('can be called with arguments', () =>
james.attemptsTo(
Execute.script('angular.element(arguments[0]).val(arguments[1]).triggerHandler("input");').withArguments(Username.Field, 'Jimmy'),
).then(() => Promise.all([
expect(james.toSee(Text.of(Username.Result))).eventually.equal('Jimmy'),
])));
});
describe('a function that', () => {
it ('runs within its context', () =>
james.attemptsTo(
Execute.fn(function() { arguments[0].click(); }).on(Newsletter.Checkbox),
).then(() => Promise.all([
expect(james.toSee(Value.of(Newsletter.Checkbox))).eventually.equal('on'),
])));
});
describe('an asynchronous script that', () => {
it ('runs within its context', () =>
james.attemptsTo(
Execute.asyncScript(
'var callback = arguments[arguments.length - 1];',
'arguments[0].click();',
'callback();',
).on(Newsletter.Checkbox),
).then(() => Promise.all([
expect(james.toSee(Value.of(Newsletter.Checkbox))).eventually.equal('on'),
])));
it ('can be called with arguments', () =>
james.attemptsTo(
Execute.asyncScript(
'var callback = arguments[arguments.length - 1];',
'angular.element(arguments[0]).val(arguments[1]).triggerHandler("input");',
'callback();',
).on(Username.Field).withArguments('James'),
).then(() => Promise.all([
expect(james.toSee(Text.of(Username.Result))).eventually.equal('James'),
])));
});
describe('an asynchronous function that', () => {
it ('runs within its context', () =>
james.attemptsTo(
Execute.asyncFn(function(element, callback) {
element.click();
callback();
}).on(Newsletter.Checkbox),
).then(() => Promise.all([
expect(james.toSee(Value.of(Newsletter.Checkbox))).eventually.equal('on'),
])));
});
});
@@ -101,14 +101,20 @@ export class BrowseTheWeb implements Ability {
return this.browser.waitForAngularEnabled(enable);
}
executeScript(script: string, target: Target): PromiseLike<any> {
return this.browser.executeScript(script, target.resolveUsing(this.browser.element));
executeScript(script: string | Function, ...args: any[]): PromiseLike<any> {
return this.browser.executeScript(script, ...args.map(arg => this.resolveTargets(arg)));
}
executeAsyncScript(script: string | Function, target: Target, ...args: any[]): PromiseLike<any> {
return this.browser.executeAsyncScript(script, target.resolveUsing(this.browser.element), ...args);
executeAsyncScript(script: string | Function, ...args: any[]): PromiseLike<any> {
return this.browser.executeAsyncScript(script, ...args.map(arg => this.resolveTargets(arg)));
}
constructor(private browser: ProtractorBrowser) {
}
private resolveTargets(maybeTarget: Target | any) {
return maybeTarget instanceof Target
? maybeTarget.resolveUsing(this.browser.element)
: maybeTarget;
}
}
@@ -4,31 +4,32 @@ import { BrowseTheWeb } from '../abilities/browse_the_web';
import { Target } from '../ui/target';
export class Execute {
static script = (...lines: string[]) => ({
on: (target: Target): Interaction => new ExecuteScript(lines, target),
})
static asyncScript = (...lines: string[]) => ({
on: (target: Target): ExecuteAsyncScript => new ExecuteAsyncScript(lines, target),
})
static script = (...lines: string[]) => new ExecuteScript(lines.join('\n'));
static fn = (fn: Function) => new ExecuteScript(fn);
static asyncScript = (...lines: string[]) => new ExecuteAsyncScript(lines.join('\n'));
static asyncFn = (fn: Function) => new ExecuteAsyncScript(fn);
}
class ExecuteScript implements Interaction {
export class ExecuteScript implements Interaction {
performAs(actor: UsesAbilities): PromiseLike<any> {
return BrowseTheWeb.as(actor).executeScript(this.lines.join('\n'), this.target);
return BrowseTheWeb.as(actor).executeScript(this.script, ...this.args);
}
constructor(private lines: string[], private target: Target) {
on = (target: Target) => new ExecuteScript(this.script, [target].concat(this.args));
withArguments = (...args: any[]) => new ExecuteScript(this.script, this.args.concat(args));
constructor(private readonly script: string | Function, private readonly args: any[] = []) {
}
}
export class ExecuteAsyncScript implements Interaction {
performAs(actor: UsesAbilities): PromiseLike<any> {
return BrowseTheWeb.as(actor).executeAsyncScript(this.lines.join('\n'), this.target, ...this.args);
return BrowseTheWeb.as(actor).executeAsyncScript(this.script, ...this.args);
}
withArguments = (...args: any[]): Interaction => new ExecuteAsyncScript(this.lines, this.target, args);
on = (target: Target) => new ExecuteAsyncScript(this.script, [target].concat(this.args));
withArguments = (...args: any[]) => new ExecuteAsyncScript(this.script, this.args.concat(args));
constructor(private lines: string[], private target: Target, private args: any[] = []) {
constructor(private readonly script: string | Function, private args: any[] = []) {
}
}

0 comments on commit 3778a32

Please sign in to comment.