UNPKG

@serenity-js/protractor

Version:

Adapter that integrates @serenity-js/web with Protractor, enabling Serenity/JS reporting and using the Screenplay Pattern to write end-to-end test scenarios

99 lines (75 loc) 3.12 kB
import type { ModalDialog} from '@serenity-js/web'; import { AbsentModalDialog, AcceptedModalDialog, DismissedModalDialog, ModalDialogHandler } from '@serenity-js/web'; import type * as protractor from 'protractor'; import { error as errors } from 'protractor'; import { promised } from '../promised'; import type { ProtractorErrorHandler } from './ProtractorErrorHandler'; /** * Protractor-specific implementation of [`ModalDialogHandler`](https://serenity-js.org/api/web/class/ModalDialogHandler/). * * @group Models */ export class ProtractorModalDialogHandler extends ModalDialogHandler { private readonly defaultHandler: () => Promise<void> = async () => { const message = await promised(this.browser.switchTo().alert().getText()); await promised(this.browser.switchTo().alert().dismiss()); this.modalDialog = new DismissedModalDialog(message); } private currentHandler: () => Promise<void>; constructor( private readonly browser: protractor.ProtractorBrowser, private readonly errorHandler: ProtractorErrorHandler, ) { super(); this.currentHandler = this.defaultHandler; this.errorHandler.setHandlerFor(errors.UnexpectedAlertOpenError.name, error_ => this.tryToHandleDialog()); } async tryToHandleDialog(): Promise<void> { try { await this.currentHandler() } catch (error) { if (error.name === errors.NoSuchAlertError.name) { this.modalDialog = new AbsentModalDialog(); return; } throw error; } } async acceptNext(): Promise<void> { this.currentHandler = async () => { const message = await promised(this.browser.switchTo().alert().getText()); await promised(this.browser.switchTo().alert().accept()); this.modalDialog = new AcceptedModalDialog(message); }; } async acceptNextWithValue(text: string | number): Promise<void> { this.currentHandler = async () => { await promised(this.browser.switchTo().alert().sendKeys(String(text))); const message = await promised(this.browser.switchTo().alert().getText()); await promised(this.browser.switchTo().alert().accept()); this.modalDialog = new AcceptedModalDialog(message); }; } async dismissNext(): Promise<void> { this.currentHandler = async () => { const message = await promised(this.browser.switchTo().alert().getText()); await promised(this.browser.switchTo().alert().dismiss()); this.modalDialog = new DismissedModalDialog(message); } } async reset(): Promise<void> { this.modalDialog = new AbsentModalDialog(); this.currentHandler = this.defaultHandler; } /** * @override */ async last(): Promise<ModalDialog> { if (this.modalDialog instanceof AbsentModalDialog) { await this.tryToHandleDialog(); } return this.modalDialog; } }