@serenity-js/webdriverio
Version:
Adapter that integrates @serenity-js/web with the latest stable version of WebdriverIO, enabling Serenity/JS reporting and using the Screenplay Pattern to write web and mobile test scenarios
89 lines • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebdriverIOModalDialogHandler = void 0;
require("webdriverio");
const web_1 = require("@serenity-js/web");
const WebdriverProtocolErrorCode_js_1 = require("./WebdriverProtocolErrorCode.js");
/**
* WebdriverIO-specific implementation of [`ModalDialogHandler`](https://serenity-js.org/api/web/class/ModalDialogHandler/),
* used with the [WebDriver protocol](https://webdriver.io/docs/api/webdriver).
*
* @group Models
*/
class WebdriverIOModalDialogHandler extends web_1.ModalDialogHandler {
browser;
defaultHandler = async (dialog) => {
const message = dialog.message();
await dialog.dismiss();
this.modalDialog = new web_1.DismissedModalDialog(message);
};
currentHandler;
dialog;
constructor(browser) {
super();
this.browser = browser;
this.currentHandler = this.defaultHandler;
this.browser.on('dialog', this.onDialog);
}
onDialog = this.tryToHandleDialog.bind(this);
async tryToHandleDialog(dialog) {
try {
await this.currentHandler(dialog);
}
catch (error) {
if (error.name === WebdriverProtocolErrorCode_js_1.WebdriverProtocolErrorCode.UnexpectedAlertOpenError) {
await dialog.dismiss();
return;
}
if (error.message.includes(WebdriverProtocolErrorCode_js_1.WebdriverProtocolErrorCode.NoSuchAlertError)) {
this.modalDialog = new web_1.AbsentModalDialog();
return;
}
throw error;
}
}
async acceptNext() {
this.currentHandler = async (dialog) => {
const message = dialog.message();
await this.browser.acceptAlert();
this.modalDialog = new web_1.AcceptedModalDialog(message);
};
}
async acceptNextWithValue(text) {
this.currentHandler = async (dialog) => {
const message = dialog.message();
await dialog.accept(String(text));
this.modalDialog = new web_1.AcceptedModalDialog(message);
};
}
async dismissNext() {
this.currentHandler = async (dialog) => {
const message = dialog.message();
await dialog.dismiss();
this.modalDialog = new web_1.DismissedModalDialog(message);
};
}
async dismiss() {
if (!this.dialog) {
return;
}
const message = this.dialog.message();
await this.dialog.dismiss();
this.modalDialog = new web_1.DismissedModalDialog(message);
}
async reset() {
this.modalDialog = new web_1.AbsentModalDialog();
this.currentHandler = this.defaultHandler;
}
/**
* @override
*/
async last() {
return this.modalDialog;
}
async discard() {
this.browser.off('dialog', this.onDialog);
}
}
exports.WebdriverIOModalDialogHandler = WebdriverIOModalDialogHandler;
//# sourceMappingURL=WebdriverIOModalDialogHandler.js.map