UNPKG

@rentready/flutter-selenium-bridge

Version:

A tool to enable automated Selenium WebDriver testing for Flutter Web applications compiled with CanvasKit.

83 lines (82 loc) 4.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlutterSeleniumBridge = void 0; const selenium_webdriver_1 = require("selenium-webdriver"); class FlutterSeleniumBridge { constructor(driver) { this.driver = driver; } enableAccessibility() { return __awaiter(this, arguments, void 0, function* (timeout = 30000) { const glassPaneSelector = 'flt-glass-pane'; const enableAccessibilitySelector = '[aria-label="Enable accessibility"]'; let success = false; let attempts = 0; const maxAttempts = 10; while (!success && attempts < maxAttempts) { try { yield this.driver.wait(selenium_webdriver_1.until.elementLocated(selenium_webdriver_1.By.css(glassPaneSelector)), timeout); const glassPane = yield this.driver.findElement(selenium_webdriver_1.By.css(glassPaneSelector)); const shadowRoot = yield this.driver.executeScript('return arguments[0].shadowRoot', glassPane); const enableAccessibilityButton = yield shadowRoot.findElement(selenium_webdriver_1.By.css(enableAccessibilitySelector)); yield this.driver.wait(selenium_webdriver_1.until.elementIsVisible(enableAccessibilityButton), 3000); yield this.driver.wait(selenium_webdriver_1.until.elementIsEnabled(enableAccessibilityButton), 3000); yield this.driver.executeScript('arguments[0].click();', enableAccessibilityButton); success = true; } catch (error) { console.error(error); console.log('Attempt to click on "EnableAccessibility" button failed. Retrying...'); yield this.driver.sleep(3000); attempts++; } } if (!success) { throw new Error('Unable to click on "EnableAccessibility" button after multiple attempts.'); } }); } activateInputField(locator_1) { return __awaiter(this, arguments, void 0, function* (locator, timeout = 30000) { let element = yield this.driver.wait(selenium_webdriver_1.until.elementLocated(locator), timeout); const tagName = yield element.getTagName(); // If the element is a flt-semantics, find the first child input if (tagName.toLowerCase() === 'flt-semantics') { const inputChildren = yield element.findElements(selenium_webdriver_1.By.css('input, textarea')); if (inputChildren.length === 0) { throw new Error(`No input or textarea element found as a child of flt-semantics.`); } element = inputChildren[0]; // Assuming we want the first input or textarea child } else if (tagName.toLowerCase() !== 'input' && tagName.toLowerCase() !== 'textarea') { throw new Error(`The located element is neither an input, a textarea, nor a flt-semantics element.`); } if ((yield element.getTagName()).toLowerCase() === 'input') { const mimicFocus = ` const textInput = arguments[0]; // Dispatch a focus event manually const focusEvent = new FocusEvent('focus', { bubbles: false, // Focus events do not bubble cancelable: true }); textInput.dispatchEvent(focusEvent); `; yield this.driver.executeScript(mimicFocus, element); } // Introduce a delay yield this.driver.sleep(500); return element; }); } } exports.FlutterSeleniumBridge = FlutterSeleniumBridge; exports.default = FlutterSeleniumBridge;