UNPKG

@progress/kendo-e2e

Version:

Kendo UI end-to-end test utilities.

263 lines 10.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DriverManager = void 0; const selenium_webdriver_1 = require("selenium-webdriver"); const chrome_1 = require("selenium-webdriver/chrome"); const edge_1 = require("selenium-webdriver/edge"); const firefox_1 = require("selenium-webdriver/firefox"); const safari_1 = require("selenium-webdriver/safari"); const browserstack_settings_1 = require("../settings/browserstack-settings"); const settings_1 = require("../settings/settings"); /** * Factory class for creating and configuring Selenium WebDriver instances. * * Handles browser-specific configuration and provides pre-configured drivers for different browsers. * Automatically applies settings from environment variables and Settings class. * * **Supported browsers:** * - Chrome (with mobile emulation and BiDi support) * - Edge * - Firefox * - Safari * - BrowserStack (cloud testing) * * @example * ```typescript * // Get default driver (from Settings.browserName) * const manager = new DriverManager(); * const driver = manager.getDriver(); * * // Get driver with mobile emulation * const mobileDriver = manager.getDriver({ * mobileEmulation: { deviceName: 'iPhone 14 Pro Max' } * }); * * // Get driver with BiDi enabled * const bidiDriver = manager.getDriver({ enableBidi: true }); * * // Get specific browser driver * const chromeDriver = manager.getChromeDriver(); * const firefoxDriver = manager.getFirefoxDriver(); * ``` */ class DriverManager { constructor() { /** * Default command-line arguments for Chromium-based browsers (Chrome, Edge). * * These options ensure consistent test behavior: * - Fixed window size and scale factor for consistent screenshots * - Disabled extensions and notifications to avoid interference * - Reduced logging noise * - Certificate error handling * - Disabled search engine choice screen */ this.DEFAULT_CHROMIUM_OPTIONS = [ `--window-size=${settings_1.Settings.browserWidth},${settings_1.Settings.browserHeight}`, '--force-device-scale-factor=1', '--log-level=1', '--disable-extensions', '--disable-notifications', '--disable-search-engine-choice-screen', '--ignore-certificate-errors' ]; } /** * Creates a WebDriver instance based on Settings.browserName. * * Automatically selects the appropriate browser driver based on configuration. * Supports mobile emulation and BiDi protocol for Chrome. * * @param options - Optional driver configuration * @param options.mobileEmulation - Mobile device emulation settings (Chrome only) * @param options.enableBidi - Enable BiDi protocol for advanced features (Chrome only) * @returns Configured WebDriver instance * * @example * ```typescript * const manager = new DriverManager(); * * // Basic driver * const driver = manager.getDriver(); * * // With mobile emulation * const mobile = manager.getDriver({ * mobileEmulation: { deviceName: 'Pixel 5' } * }); * * // With BiDi for CDP features * const advanced = manager.getDriver({ enableBidi: true }); * ``` */ getDriver(options = {}) { switch (settings_1.Settings.browserName) { case selenium_webdriver_1.Browser.CHROME: { // Pass mobileOptions as second parameter, not as the options instance. return this.getChromeDriver(options); } case selenium_webdriver_1.Browser.EDGE: { return this.getEdgeDriver(); } case selenium_webdriver_1.Browser.FIREFOX: { return this.getFirefoxDriver(); } case selenium_webdriver_1.Browser.SAFARI: { return this.getSafariDriver(); } default: { return this.getBrowserStackDriver(); } } } /** * Creates Chrome-specific options with custom arguments and settings. * * Configures Chrome with optimal settings for testing, including headless mode * support, Docker compatibility, mobile emulation, and BiDi protocol. * * @param args - Command-line arguments for Chrome (default: DEFAULT_CHROMIUM_OPTIONS) * @param options - Driver configuration options * @returns Configured ChromeOptions instance * * @example * ```typescript * const manager = new DriverManager(); * * // Get default options * const options = manager.getChromeOptions(); * * // Custom arguments * const customOptions = manager.getChromeOptions([ * '--window-size=1920,1080', * '--disable-gpu' * ]); * * // With mobile emulation * const mobileOptions = manager.getChromeOptions( * manager.DEFAULT_CHROMIUM_OPTIONS, * { mobileEmulation: { deviceName: 'iPhone 12' } } * ); * ``` */ getChromeOptions(args = this.DEFAULT_CHROMIUM_OPTIONS, options = {}) { const chromeOptions = new chrome_1.Options(); args.forEach(argument => { chromeOptions.addArguments(argument); }); if (settings_1.Settings.headless) { chromeOptions.addArguments('--headless=new'); } // We must disable the Chrome sandbox when running Chrome inside Docker. // Chrome's sandbox needs more permissions than Docker allows by default. // See: // - https://github.com/GoogleChrome/puppeteer/issues/560 // - https://github.com/microsoft/playwright/issues/1977#issuecomment-619397496 if (process.env["CI_CONTAINER"]) { chromeOptions.addArguments('--no-sandbox'); chromeOptions.addArguments('--disable-setuid-sandbox'); } if (options.mobileEmulation) { chromeOptions.setMobileEmulation(options.mobileEmulation); } if (options.enableBidi) { chromeOptions.enableBidi(); } return chromeOptions; } /** * Creates a Chrome WebDriver instance. * * @param options - Either pre-configured ChromeOptions or DriverOptions * @returns Chrome WebDriver instance * * @example * ```typescript * const manager = new DriverManager(); * * // Basic Chrome driver * const driver = manager.getChromeDriver(); * * // With custom options * const options = manager.getChromeOptions(); * options.addArguments('--start-maximized'); * const driver = manager.getChromeDriver(options); * ``` */ getChromeDriver(options) { let chromeOptions; if (options instanceof chrome_1.Options) { chromeOptions = options; } else { chromeOptions = this.getChromeOptions(this.DEFAULT_CHROMIUM_OPTIONS, options); } return new selenium_webdriver_1.Builder().forBrowser(selenium_webdriver_1.Browser.CHROME).setChromeOptions(chromeOptions).build(); } getEdgeOptions(args = this.DEFAULT_CHROMIUM_OPTIONS) { const options = new edge_1.Options(); args.forEach(argument => { options.addArguments(argument); }); if (settings_1.Settings.headless) { options.addArguments('--headless'); } return options; } getEdgeDriver(options = this.getEdgeOptions()) { return new selenium_webdriver_1.Builder().forBrowser(selenium_webdriver_1.Browser.EDGE).setEdgeOptions(options).build(); } getFirefoxOptions() { const options = new firefox_1.Options(); options.addArguments(`--width=${settings_1.Settings.browserWidth}`); options.addArguments(`--height=${settings_1.Settings.browserHeight}`); options.setLoggingPrefs(selenium_webdriver_1.logging.Level.SEVERE); if (settings_1.Settings.headless) { options.addArguments('--headless'); } return options; } getFirefoxDriver(options = this.getFirefoxOptions()) { const prefs = new selenium_webdriver_1.logging.Preferences(); prefs.setLevel(selenium_webdriver_1.logging.Type.BROWSER, selenium_webdriver_1.logging.Level.ALL); const service = new firefox_1.ServiceBuilder().setStdio("inherit"); return new selenium_webdriver_1.Builder() .forBrowser(selenium_webdriver_1.Browser.FIREFOX) .setLoggingPrefs(prefs) .setFirefoxOptions(options) .setFirefoxService(service) .build(); } getSafariOptions() { const options = new safari_1.Options(); return options; } getSafariDriver(options = this.getSafariOptions()) { const driver = new selenium_webdriver_1.Builder().forBrowser(selenium_webdriver_1.Browser.SAFARI).setSafariOptions(options).build(); const size = { width: settings_1.Settings.browserWidth, height: settings_1.Settings.browserHeight, x: 0, y: 0 }; driver.manage().window().setRect(size); return driver; } getBrowserStackDriver() { if (browserstack_settings_1.BSSettings.accessKey === undefined || browserstack_settings_1.BSSettings.userName === undefined) { throw Error("Please set BS_USER_NAME and BS_ACCESS_KEY variables."); } const url = "http://hub-cloud.browserstack.com/wd/hub"; const capabilities = { 'bstack:options': { "osVersion": browserstack_settings_1.BSSettings.osVersion, "deviceName": browserstack_settings_1.BSSettings.deviceName, "realMobile": browserstack_settings_1.BSSettings.realMobile, "local": browserstack_settings_1.BSSettings.local, "userName": browserstack_settings_1.BSSettings.userName, "accessKey": browserstack_settings_1.BSSettings.accessKey }, "browserName": browserstack_settings_1.BSSettings.browserName, "name": browserstack_settings_1.BSSettings.buildName, "build": browserstack_settings_1.BSSettings.buildNumber, "nativeWebTap": "true" }; return new selenium_webdriver_1.Builder().usingServer(url).withCapabilities(capabilities).build(); } } exports.DriverManager = DriverManager; //# sourceMappingURL=driver-manager.js.map