@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
77 lines • 3.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProtractorModalDialogHandler = void 0;
const web_1 = require("@serenity-js/web");
const protractor_1 = require("protractor");
const promised_1 = require("../promised");
/**
* Protractor-specific implementation of [`ModalDialogHandler`](https://serenity-js.org/api/web/class/ModalDialogHandler/).
*
* @group Models
*/
class ProtractorModalDialogHandler extends web_1.ModalDialogHandler {
browser;
errorHandler;
defaultHandler = async () => {
const message = await (0, promised_1.promised)(this.browser.switchTo().alert().getText());
await (0, promised_1.promised)(this.browser.switchTo().alert().dismiss());
this.modalDialog = new web_1.DismissedModalDialog(message);
};
currentHandler;
constructor(browser, errorHandler) {
super();
this.browser = browser;
this.errorHandler = errorHandler;
this.currentHandler = this.defaultHandler;
this.errorHandler.setHandlerFor(protractor_1.error.UnexpectedAlertOpenError.name, error_ => this.tryToHandleDialog());
}
async tryToHandleDialog() {
try {
await this.currentHandler();
}
catch (error) {
if (error.name === protractor_1.error.NoSuchAlertError.name) {
this.modalDialog = new web_1.AbsentModalDialog();
return;
}
throw error;
}
}
async acceptNext() {
this.currentHandler = async () => {
const message = await (0, promised_1.promised)(this.browser.switchTo().alert().getText());
await (0, promised_1.promised)(this.browser.switchTo().alert().accept());
this.modalDialog = new web_1.AcceptedModalDialog(message);
};
}
async acceptNextWithValue(text) {
this.currentHandler = async () => {
await (0, promised_1.promised)(this.browser.switchTo().alert().sendKeys(String(text)));
const message = await (0, promised_1.promised)(this.browser.switchTo().alert().getText());
await (0, promised_1.promised)(this.browser.switchTo().alert().accept());
this.modalDialog = new web_1.AcceptedModalDialog(message);
};
}
async dismissNext() {
this.currentHandler = async () => {
const message = await (0, promised_1.promised)(this.browser.switchTo().alert().getText());
await (0, promised_1.promised)(this.browser.switchTo().alert().dismiss());
this.modalDialog = new web_1.DismissedModalDialog(message);
};
}
async reset() {
this.modalDialog = new web_1.AbsentModalDialog();
this.currentHandler = this.defaultHandler;
}
/**
* @override
*/
async last() {
if (this.modalDialog instanceof web_1.AbsentModalDialog) {
await this.tryToHandleDialog();
}
return this.modalDialog;
}
}
exports.ProtractorModalDialogHandler = ProtractorModalDialogHandler;
//# sourceMappingURL=ProtractorModalDialogHandler.js.map