UNPKG

@progress/kendo-e2e

Version:

Kendo UI end-to-end test utilities.

207 lines 10 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Browser = exports.WebElementCondition = exports.WebElement = exports.until = exports.Key = exports.By = void 0; const webdriverjs_1 = __importDefault(require("@axe-core/webdriverjs")); const selenium_webdriver_1 = require("selenium-webdriver"); const logging_1 = require("selenium-webdriver/lib/logging"); const driver_manager_1 = require("./driver-manager"); const web_app_1 = require("./web-app"); var selenium_webdriver_2 = require("selenium-webdriver"); Object.defineProperty(exports, "By", { enumerable: true, get: function () { return selenium_webdriver_2.By; } }); Object.defineProperty(exports, "Key", { enumerable: true, get: function () { return selenium_webdriver_2.Key; } }); Object.defineProperty(exports, "until", { enumerable: true, get: function () { return selenium_webdriver_2.until; } }); Object.defineProperty(exports, "WebElement", { enumerable: true, get: function () { return selenium_webdriver_2.WebElement; } }); Object.defineProperty(exports, "WebElementCondition", { enumerable: true, get: function () { return selenium_webdriver_2.WebElementCondition; } }); function isDriver(obj) { return obj && typeof obj.getSession === 'function'; } /** * Extends the {@link WebApp} with browser based capabilities. * Essentially, we test browsers (that have nice things like {@link navigateTo}, {@link refresh} etc.), * and electron apps, that render within a webview (but have no shell with address bar, refresh button, tabs, etc), * and VSCode extensions... which is an electron app. */ class Browser extends web_app_1.WebApp { /** * Creates an instance of the Browser class. * * @param {ThenableWebDriver | BrowserOptions} [driverOrOptions] - Either a WebDriver instance or an options object. * If a WebDriver instance is provided, it will be used as the driver. If an options object is provided, a new driver will be created with those options. * @param {Object} [mobileEmulation] - (Optional) Mobile emulation options, used only if the first parameter is a WebDriver instance. * Mobile options can be an object with either a `deviceName` or `width`, `height`, and `pixelRatio`. * @param {boolean} [enableBidi] - (Optional) Enables BiDi (Bidirectional communication) if set to `true`. * * __Usage Examples:__ * * __Example 1: Using a Pre-configured Device__ * ```typescript * const mobileEmulation = { deviceName: "iPhone 14 Pro Max" }; * const browser = new Browser(mobileEmulation); * ``` * * __Example 2: Using Custom Screen Configuration__ * ```typescript * const mobileEmulation = { deviceMetrics: { width: 360, height: 640, pixelRatio: 3.0 }, userAgent: 'My Agent' }; * const browser = new Browser(mobileEmulation); * ``` * * __Example 3: Providing a WebDriver Instance With Mobile Emulation__ * ```typescript * const driver = new Builder().forBrowser('chrome').build(); * const mobileEmulation = { deviceName: "iPhone 14 Pro Max" }; * const browser = new Browser(driver, mobileEmulation); * ``` * * __Example 4: Enabling BiDi Mode__ * ```typescript * const browser = new Browser({ enableBidi: true }); * ``` * * __Example 5: Combining Mobile Emulation and BiDi Mode__ * ```typescript * const browser = new Browser({ mobileEmulation: { deviceName: "iPhone 14 Pro Max" }, enableBidi: true }); * ``` * * [em]: https://chromedriver.chromium.org/mobile-emulation * [devem]: https://developer.chrome.com/devtools/docs/device-mode */ constructor(driverOrOptions, mobileEmulation, enableBidi) { var _a; let driver; // If the first parameter is a driver instance, use it directly. if (driverOrOptions && isDriver(driverOrOptions)) { driver = driverOrOptions; } else { const options = driverOrOptions || {}; if (mobileEmulation && !options.mobileEmulation) { options.mobileEmulation = mobileEmulation; } if (enableBidi && options.enableBidi === undefined) { options.enableBidi = enableBidi; } driver = (_a = options.driver) !== null && _a !== void 0 ? _a : new driver_manager_1.DriverManager().getDriver({ mobileEmulation: options.mobileEmulation, enableBidi: options.enableBidi }); } super(driver); } close() { return __awaiter(this, void 0, void 0, function* () { yield this.driver.quit(); }); } navigateTo(url) { return __awaiter(this, void 0, void 0, function* () { yield this.driver.navigate().to(url); }); } getRect() { return __awaiter(this, void 0, void 0, function* () { return yield this.driver.manage().window().getRect(); }); } setRect(rect) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const currentRect = yield this.driver.manage().window().getRect(); this.driver.manage().window().setRect({ width: (_a = rect.width) !== null && _a !== void 0 ? _a : currentRect.width, height: (_b = rect.height) !== null && _b !== void 0 ? _b : currentRect.height, x: (_c = rect.x) !== null && _c !== void 0 ? _c : currentRect.x, y: (_d = rect.y) !== null && _d !== void 0 ? _d : currentRect.y }); }); } resizeToDocumentScrollHeight() { return __awaiter(this, void 0, void 0, function* () { const originalRect = yield this.getRect(); const viewportHeight = yield this.driver.executeScript("return window.innerHeight"); const documentHeight = yield this.driver.executeScript("return document.body.scrollHeight"); yield this.setRect({ height: documentHeight + originalRect.height - viewportHeight }); }); } refresh() { return __awaiter(this, void 0, void 0, function* () { yield this.driver.navigate().refresh(); }); } switchToIFrame(elementLocator) { return __awaiter(this, void 0, void 0, function* () { const iframe = yield this.find(elementLocator); yield this.driver.switchTo().frame(iframe); }); } getCurrentUrl() { return __awaiter(this, void 0, void 0, function* () { return yield this.driver.getCurrentUrl(); }); } getBrowserName() { return __awaiter(this, void 0, void 0, function* () { const capabilities = (yield (yield this.driver).getCapabilities()); const browserName = capabilities.getBrowserName().toLowerCase(); return browserName; }); } getAccessibilityViolations() { return __awaiter(this, arguments, void 0, function* (cssSelector = "html", disableRules = ["color-contrast"]) { yield this.find(selenium_webdriver_1.By.css(cssSelector)); const axe = new webdriverjs_1.default(this.driver) .include(cssSelector) .disableRules(disableRules); const result = yield axe.analyze(); return result.violations; }); } clearLogs() { return __awaiter(this, void 0, void 0, function* () { yield this.driver.manage().logs().get(logging_1.Type.BROWSER); }); } getErrorLogs() { return __awaiter(this, arguments, void 0, function* (excludeList = ["favicon.ico"], logLevel = logging_1.Level.SEVERE) { const errors = []; const capabilities = (yield (yield this.driver).getCapabilities()); const platform = capabilities.getPlatform().toLowerCase(); const browserName = capabilities.getBrowserName().toLowerCase(); // Can not get FF logs due to issue. // Please see: // https://github.com/mozilla/geckodriver/issues/284#issuecomment-477677764 // // Note: Logs are not supported on mobile platforms too! if (browserName === "chrome" && platform !== "android" && platform !== "iphone") { const logs = yield this.driver.manage().logs().get(logging_1.Type.BROWSER); for (const entry of logs) { // Check if the entry's level is greater than or equal to the provided logLevel and collect all included logs if (entry.level.value >= logLevel.value) { errors.push(entry.message); } } } // Filter errors let filteredErrors = errors; for (const excludeItem of excludeList) { filteredErrors = filteredErrors.filter((error) => { return error.toLowerCase().indexOf(excludeItem.toLowerCase()) < 0; }); } return filteredErrors; }); } } exports.Browser = Browser; //# sourceMappingURL=browser.js.map