Skip to content
Permalink
Browse files
feat(protractor): Press.the(key).into(field) interaction
  • Loading branch information
jan-molak committed Feb 12, 2019
1 parent 7d133f0 commit 44a97b27daaccf3ef97f2c6d916341bfa602dd63
@@ -0,0 +1,87 @@
import { expect } from '@integration/testing-tools';
import { Ensure, equals } from '@serenity-js/assertions';
import { Actor } from '@serenity-js/core';
import { given } from 'mocha-testdata';
import { by, Key, protractor } from 'protractor';

import { BrowseTheWeb, Navigate, Press, Target, Value } from '../../../src';
import { pageFromTemplate } from '../../fixtures';

describe('Press', () => {

const Bernie = Actor.named('Bernie').whoCan(
BrowseTheWeb.using(protractor.browser),
);

const Form = {
Text_Field: Target.the('text field').located(by.name('text')),
};

// todo: add check.whether

const page = pageFromTemplate(`
<html>
<body>
<form>
<input type="text" name="text" />
</form>
</body>
</html>
`);

describe('single keys', () => {

/** @test {Press} */
/** @test {Press.the} */
it('allows the actor to enter keys individually into a field', () => Bernie.attemptsTo(
Navigate.to(page),

Press.the('a').into(Form.Text_Field),
Press.the('A').into(Form.Text_Field),

Ensure.that(Value.of(Form.Text_Field), equals('aA')),
));

});

describe('keyboard shortcuts', function() {

/** @test {Press} */
/** @test {Press.the} */
it('allows the actor to use keyboard shortcuts', () => Bernie.attemptsTo(
Navigate.to(page),

Press.the(Key.SHIFT, 'a').into(Form.Text_Field),

Ensure.that(Value.of(Form.Text_Field), equals(`A`)),
));

});

given([
{
description: 'single key',
interaction: Press.the('a').into(Form.Text_Field),
expected: `#actor types A in the text field`,
},
{
description: 'sequence of keys',
interaction: Press.the('a', 'b', 'c').into(Form.Text_Field),
expected: `#actor types A, B, C in the text field`,
},
{
description: 'keyboard shortcut',
interaction: Press.the(Key.CONTROL, 'a').into(Form.Text_Field),
expected: `#actor types Control-A in the text field`,
},
{
description: 'complex shortcut',
interaction: Press.the(Key.COMMAND, Key.ALT, 'a').into(Form.Text_Field),
expected: `#actor types Command-Alt-A in the text field`,
},
]).
/** @test {Press#toString} */
it(`provides a sensible description of the interaction being performed`, ({ interaction, expected }) => {
expect(interaction.toString()).to.equal(expected);
});
});
@@ -0,0 +1,60 @@
import { AnswersQuestions, Interaction, UsesAbilities } from '@serenity-js/core';
import { ElementFinder, Key } from 'protractor';
import { Target } from '../questions';

/**
* @desc
* Note that modifier keys, such as Command, won't work on Mac - https://github.com/angular/protractor/issues/690
*/
export class Press implements Interaction {

static the(...keys: string[]) {
return {
into: (field: Target<ElementFinder>) => new Press(keys, field),
};
}

constructor(
private readonly keys: string[],
private readonly field: Target<ElementFinder>,
) {
}

performAs(actor: UsesAbilities & AnswersQuestions): PromiseLike<any> {
return Promise.all(this.keys.map(key => actor.answer(key)))
.then(keys => this.field.answeredBy(actor).sendKeys(...keys));
}

toString() {
return `#actor types ${ describeSequenceOf(this.keys) } in ${ this.field.toString() }`;
}
}

function describeSequenceOf(keys: string[]) {
return keys.map(key => [
capitalised(nameOf(key)),
isModifier(key) ? '-' : ', ',
]).
reduce((acc, current) => acc.concat(current), []).
slice(0, keys.length * 2 - 1).
join('');
}

function isModifier(key: string) {
return !! ~ [ Key.ALT, Key.COMMAND, Key.CONTROL, Key.SHIFT ].indexOf(key);
}

function nameOf(key: string) {

for (const candidate in Key) {
if (Key.hasOwnProperty(candidate) && Key[ candidate ] === key) {
return candidate;
}
}

return key;
}

function capitalised(name: string) {
return name.charAt(0).toLocaleUpperCase() + name.slice(1).toLocaleLowerCase();
}
@@ -3,4 +3,5 @@ export * from './Click';
export * from './DoubleClick';
export * from './Enter';
export * from './Navigate';
export * from './Press';
export * from './Wait';

0 comments on commit 44a97b2

Please sign in to comment.