Skip to content
Permalink
Browse files
feat(protractor): navigate.to(url).withTimeout(duration)
This new interaction allows the actor to control how long they're willing to wait for an Angular app
to load. Please note that this interaction is Angular-specific as under the hood it uses
`protractor.browser.get(destination, timeout)`.

Closes #517
  • Loading branch information
jan-molak committed May 2, 2020
1 parent 1b5d0ad commit be23c6e4f2a00edad01a9c9ecc1734ec2eda4f4a
Showing 2 changed files with 72 additions and 15 deletions.
@@ -1,14 +1,16 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { endsWith, Ensure, equals } from '@serenity-js/assertions';
import { actorCalled } from '@serenity-js/core';
import { actorCalled, Duration } from '@serenity-js/core';
import { by } from 'protractor';

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

describe('Navigate', () => {

describe('to', () => {
describe('to(url)', () => {

/** @test {Navigate.to} */
it('allows the actor to navigate to a desired destination', () => actorCalled('Bernie').attemptsTo(
@@ -30,6 +32,15 @@ describe('Navigate', () => {
});
});

describe('to(url).withTimeout(duration)', function () {

/** @test {Navigate#toString} */
it('provides a sensible description of the interaction being performed', () => {
expect(Navigate.to(`https://serenity-js.org`).withTimeout(Duration.ofSeconds(5)).toString())
.to.equal(`#actor navigates to 'https://serenity-js.org' waiting up to 5s for Angular to load`);
});
});

describe('back', () => {

/** @test {Navigate.back} */
@@ -1,31 +1,31 @@
import { Answerable, AnswersQuestions, Interaction, UsesAbilities } from '@serenity-js/core';
import { Answerable, AnswersQuestions, Duration, Interaction, UsesAbilities } from '@serenity-js/core';
import { formatted } from '@serenity-js/core/lib/io';
import { promiseOf } from '../../promiseOf';
import { BrowseTheWeb } from '../abilities';

/**
* @desc
* Allows the {Actor} to navigate to a specific destination,
* Allows the {@link @serenity-js/core/lib/screenplay/actor~Actor} to navigate to a specific destination,
* as well as back and forth in the browser history.
*
* @abstract
* @extends {@serenity-js/core/lib/screenplay~Interaction}
*/
export abstract class Navigate extends Interaction {

static to(url: Answerable<string>) {
static to(url: Answerable<string>): NavigateToUrl {
return new NavigateToUrl(url);
}

static back() {
static back(): NavigateBack {
return new NavigateBack();
}

static forward() {
static forward(): NavigateForward {
return new NavigateForward();
}

static reloadPage() {
static reloadPage(): ReloadPage {
return new ReloadPage();
}

@@ -53,15 +53,25 @@ export abstract class Navigate extends Interaction {
}

/**
* @package
*
* @extends {Navigate}
*/
class NavigateToUrl extends Navigate {
constructor(private readonly url: Answerable<string>) {
super();
}

/**
* @desc
* Specifies timeout to wait for an Angular app to load.
* Please note that the timeout is ignored if you disable
* synchronisation with Angular.
*
* @param {Answerable<Duration>} duration
*/
withTimeout(duration: Answerable<Duration>): Interaction {
return new NavigateToUrlWithTimeout(this.url, duration);
}

/**
* @desc
* Makes the provided {@link @serenity-js/core/lib/screenplay/actor~Actor}
@@ -92,8 +102,46 @@ class NavigateToUrl extends Navigate {
}

/**
* @package
*
* @extends {Navigate}
*/
class NavigateToUrlWithTimeout extends Navigate {
constructor(private readonly url: Answerable<string>, private readonly timeout: Answerable<Duration>) {
super();
}

/**
* @desc
* Makes the provided {@link @serenity-js/core/lib/screenplay/actor~Actor}
* perform this {@link @serenity-js/core/lib/screenplay~Interaction}.
*
* @param {UsesAbilities & AnswersQuestions} actor
* @returns {Promise<void>}
*
* @see {@link @serenity-js/core/lib/screenplay/actor~Actor}
* @see {@link @serenity-js/core/lib/screenplay/actor~UsesAbilities}
* @see {@link @serenity-js/core/lib/screenplay/actor~AnswersQuestions}
*/
performAs(actor: UsesAbilities & AnswersQuestions): PromiseLike<void> {
return Promise.all([
actor.answer(this.url),
actor.answer(this.timeout),
]).then(([url, timeout]) =>
BrowseTheWeb.as(actor).get(url, timeout.inMilliseconds()),
);
}

/**
* @desc
* Generates a description to be used when reporting this {@link @serenity-js/core/lib/screenplay~Activity}.
*
* @returns {string}
*/
toString(): string {
return formatted `#actor navigates to ${ this.url } waiting up to ${ this.timeout } for Angular to load`;
}
}

/**
* @extends {Navigate}
*/
class NavigateBack extends Navigate {
@@ -126,8 +174,6 @@ class NavigateBack extends Navigate {
}

/**
* @package
*
* @extends {Navigate}
*/
class NavigateForward extends Navigate {
@@ -160,7 +206,7 @@ class NavigateForward extends Navigate {
}

/**
* @package
* @extends {Navigate}
*/
class ReloadPage extends Navigate {

0 comments on commit be23c6e

Please sign in to comment.