Skip to content
Permalink
Browse files
fix(protractor): Corrected the Text.of and Text.ofAll questions
  • Loading branch information
jan-molak committed Feb 2, 2019
1 parent aefe521 commit 7f558f002a1824529b450e8d802801472c0917ce
@@ -3,7 +3,7 @@
exports.config = {
chromeDriver: require(`chromedriver/lib/chromedriver`).path,

// SELENIUM_PROMISE_MANAGER: false,
SELENIUM_PROMISE_MANAGER: false,

onPrepare: function() {
return browser.waitForAngularEnabled(false);
@@ -37,6 +37,5 @@ describe('Enter', () => {
Enter.theValue(Bernie.name).into(Form.Field),

Ensure.that(Value.of(Form.Field), equals(Bernie.name)),
Ensure.that(Text.of(Form.Result), equals(Bernie.name)),
));
});
@@ -0,0 +1,22 @@
import { Ensure, equals } from '@serenity-js/assertions';
import { Actor } from '@serenity-js/core';
import { by, protractor } from 'protractor';

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

describe('Attribute', () => {

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

/** @test {Attribute} */
it('allows the actor to read an attribute of a DOM element', () => Bernie.attemptsTo(
Navigate.to(pageFromTemplate(`
<html lang="en" />
`)),

Ensure.that(Attribute.of(Target.the('DOM').located(by.tagName('html'))).called('lang'), equals('en')),
));
});
@@ -0,0 +1,51 @@
import { Ensure, equals } from '@serenity-js/assertions';
import { Actor } from '@serenity-js/core';
import { by, protractor } from 'protractor';

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

describe('Text', function() {

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

describe('of', () => {

/** @test {Text} */
/** @test {Text.of} */
it('allows the actor to read the text of the DOM element matching the locator', () => Bernie.attemptsTo(
Navigate.to(pageFromTemplate(`
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
`)),

Ensure.that(Text.of(Target.the('header').located(by.tagName('h1'))), equals('Hello World!')),
));
});

describe('ofAll', () => {

/** @test {Text} */
/** @test {Text.ofAll} */
it('allows the actor to read the text of all DOM elements matching the locator', () => Bernie.attemptsTo(
Navigate.to(pageFromTemplate(`
<html>
<body>
<h1>Shopping list</h1>
<ul>
<li>milk</li>
<li>oats</li>
</ul>
</body>
</html>
`)),

Ensure.that(Text.ofAll(Target.all('shopping list items').located(by.css('li'))), equals(['milk', 'oats'])),
));
});
});
@@ -0,0 +1,32 @@
import { Ensure, equals } from '@serenity-js/assertions';
import { Actor } from '@serenity-js/core';
import { by, protractor } from 'protractor';

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

describe('Value', function() {

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

describe('of', () => {

/** @test {Text} */
/** @test {Text.of} */
it(`allows the actor to read the 'value' attribute of a DOM element matching the locator`, () => Bernie.attemptsTo(
Navigate.to(pageFromTemplate(`
<html>
<body>
<form>
<input name="username" value="jan-molak" />
</form>
</body>
</html>
`)),

Ensure.that(Value.of(Target.the('username field').located(by.tagName('input'))), equals('jan-molak')),
));
});
});
@@ -0,0 +1,38 @@
import { Ensure, equals } from '@serenity-js/assertions';
import { Actor } from '@serenity-js/core';
import { protractor } from 'protractor';

import { BrowseTheWeb, Navigate, Website } from '../../../src';
import { pageFromTemplate } from '../../fixtures';

describe('Website', () => {

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

describe('title', () => {
/** @test {Attribute} */
it('allows the actor to read an attribute of a DOM element', () => Bernie.attemptsTo(
Navigate.to(pageFromTemplate(`
<html>
<head>
<title>Hello World</title>
</head>
</html>
`)),

Ensure.that(Website.title(), equals(`Hello World`)),
));
});

describe('url', () => {

/** @test {Attribute} */
it('allows the actor to read an attribute of a DOM element', () => Bernie.attemptsTo(
Navigate.to(`chrome://accessibility/`),

Ensure.that(Website.url(), equals(`chrome://accessibility/`)),
));
});
});
@@ -159,4 +159,14 @@ export class BrowseTheWeb implements Ability {
getCurrentUrl(): Promise<string> {
return promiseOf(this.browser.getCurrentUrl());
}

/**
* @desc
* Pause the actor flow for a specified number of milliseconds.
*
* @returns {Promise<string>}
*/
sleep(millis: number): Promise<void> {
return promiseOf(this.browser.sleep(millis));
}
}
@@ -2,6 +2,7 @@ import { AnswersQuestions, Question, UsesAbilities } from '@serenity-js/core';
import { ElementArrayFinder, ElementFinder } from 'protractor';
import { WebElement } from 'selenium-webdriver';

import { promiseOf } from '../promiseOf';
import { Target } from './Target';

export abstract class Text<T extends WebElement, R> implements Question<Promise<R>> {
@@ -22,7 +23,7 @@ export abstract class Text<T extends WebElement, R> implements Question<Promise<

class TextOfSingleElement extends Text<ElementFinder, string> {
answeredBy(actor: AnswersQuestions & UsesAbilities): Promise<string> {
return actor.answer(this.target).then(el => el.getText());
return promiseOf(this.target.answeredBy(actor).getText()) as any;
}

toString() {
@@ -34,7 +35,7 @@ class TextOfMultipleElements extends Text<ElementArrayFinder, string[]> {
answeredBy(actor: AnswersQuestions & UsesAbilities): Promise<string[]> {
// protractor ignores type definitions for the ElementArrayFinder, hence the Promise<any>
// https://github.com/angular/protractor/blob/c3978ec166760ac07db01e700c4aaaa19d9b5c38/lib/element.ts#L92
return actor.answer(this.target).then(el => el.getText() as Promise<any>);
return promiseOf(this.target.answeredBy(actor).getText()) as any;
}

toString() {

0 comments on commit 7f558f0

Please sign in to comment.