Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
feat(protractor): EXPERIMENTAL: Custom extensions can be mixed into B…
…rowseTheWeb Custom Extensions can be mixed into the BrowseTheWeb Ability to modify the protractor.browser before the ability is used by the actor, or to provide additional features BrowseTheWeb itself doesn't provide. Please note that this feature is experimental, so its APIs can change without affecting the major version number of the framework. See https://serenity-js.org/handbook/integration/versioning.html#experimental-apis
- Loading branch information
Showing
12 changed files
with
281 additions
and
22 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Extension } from './Extension'; | ||
import { ExtensionType } from './ExtensionType'; | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
export interface Extendable<Subject> { | ||
extendedWith(extension: Extension<Subject>): this; | ||
|
||
extension<E extends Extension<Subject>>(extensionType: ExtensionType<Subject, E>): E; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @experimental | ||
*/ | ||
export interface Extension<Subject> { | ||
applyTo(subject: Subject): Promise<Subject> | Subject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Extension } from './Extension'; | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
export type ExtensionType<Subject, ET extends Extension<Subject> = Extension<Subject>> = new (...args: any[]) => ET |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
export * from './Discardable'; | ||
export * from './Extendable'; | ||
export * from './Extension'; | ||
export * from './ExtensionType'; | ||
export * from './Initialisable'; | ||
export * from './TakeNotes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'mocha'; | ||
import { expect } from '@integration/testing-tools'; | ||
import { Ensure, isTrue } from '@serenity-js/assertions'; | ||
import { Actor, actorCalled, actorInTheSpotlight, Cast, ConfigurationError, configure, Discardable, engage, Extension, Interaction, Question } from '@serenity-js/core'; | ||
import { protractor, ProtractorBrowser } from 'protractor'; | ||
import { BrowseTheWeb } from '../../../src'; | ||
|
||
describe('BrowseTheWeb', () => { | ||
|
||
class TroubleMaker implements Extension<ProtractorBrowser> { | ||
applyTo(subject: ProtractorBrowser): ProtractorBrowser | Promise<ProtractorBrowser> { | ||
return subject; | ||
} | ||
|
||
someMethod(): void { | ||
return void 0; | ||
} | ||
} | ||
|
||
describe('when extended', () => { | ||
|
||
class SupportForAngularEnabled implements Discardable, Extension<ProtractorBrowser>{ | ||
private browser: ProtractorBrowser; | ||
|
||
applyTo(subject: ProtractorBrowser): ProtractorBrowser | Promise<ProtractorBrowser> { | ||
this.browser = subject; | ||
|
||
return Promise.resolve() | ||
.then(() => subject.waitForAngularEnabled(true)) | ||
.then(() => this.browser); | ||
} | ||
|
||
synchronisationEnabled(): boolean { | ||
return ! this.browser.ignoreSynchronization; | ||
} | ||
|
||
discard(): Promise<void> { | ||
return Promise.resolve() | ||
.then(() => { | ||
this.browser.waitForAngularEnabled(false) | ||
}) | ||
} | ||
} | ||
|
||
class Actors implements Cast { | ||
prepare(actor: Actor): Actor { | ||
return actor.whoCan(BrowseTheWeb.using(protractor.browser).extendedWith(new SupportForAngularEnabled())); | ||
} | ||
} | ||
|
||
const AngularSupportEnabled = () => | ||
Question.about('Protractor ignoreSynchronization setting', actor => | ||
BrowseTheWeb.as(actor).extension(SupportForAngularEnabled).synchronisationEnabled() | ||
); | ||
|
||
const CauseTrouble = () => | ||
Interaction.where(`#actor tries to use an extension that hasn't been provided`, actor => { | ||
BrowseTheWeb.as(actor).extension(TroubleMaker).someMethod() | ||
}); | ||
|
||
beforeEach(() => engage(new Actors())); | ||
|
||
/** @test {BrowseTheWeb} */ | ||
/** @test {BrowseTheWeb#with} */ | ||
it('initialises the extensions correctly', () => | ||
actorCalled('Bernie').attemptsTo( | ||
Ensure.that(AngularSupportEnabled(), isTrue()) | ||
)); | ||
|
||
/** @test {BrowseTheWeb} */ | ||
/** @test {BrowseTheWeb#with} */ | ||
it('complains if the extension is not available', () => | ||
expect(actorCalled('Bernie').attemptsTo( | ||
CauseTrouble(), | ||
)).to.be.rejectedWith(ConfigurationError, `BrowseTheWeb doesn't have the TroubleMaker extension`)); | ||
|
||
// Note that you don't have to manually dismiss the actors in your tests | ||
// as Serenity/JS will do this for you. | ||
// | ||
// In the case of this scenario, however, I have to dismiss them explicitly | ||
// because it's executed via plain Mocha rather than | ||
// the Serenity/JS runner. | ||
afterEach(() => | ||
actorCalled('Bernie').dismiss() | ||
); | ||
}); | ||
|
||
/** @test {BrowseTheWeb} */ | ||
/** @test {BrowseTheWeb#with} */ | ||
it('complains if the extension is registered more than once', () => { | ||
expect(() => { | ||
BrowseTheWeb.using(protractor.browser) | ||
.extendedWith(new TroubleMaker()) | ||
.extendedWith(new TroubleMaker()) | ||
}).to.throw(ConfigurationError, `BrowseTheWeb already has the TroubleMaker extension, so you don't need to assign it again`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters