Path: blob/trunk/javascript/selenium-webdriver/test/driver_factory.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'18const fs = require('node:fs')19const os = require('node:os')20const path = require('node:path')21const { Browser } = require('selenium-webdriver/index')22const { Environment } = require('selenium-webdriver/testing')23const chrome = require('selenium-webdriver/chrome')24const firefox = require('selenium-webdriver/firefox')25const { runfiles } = require('@bazel/runfiles')2627function GetBrowserForTests() {28let browser = process.env.SELENIUM_BROWSER2930// If we have no browser set, fail the build31if (!browser) {32throw new Error('SELENIUM_BROWSER env var not set')33}3435if (browser.indexOf(',') !== -1) {36throw new Error('SELENIUM_BROWSER env var must only be a single browser')37}3839/** @type !TargetBrowser */40const targetBrowser = { name: browser, capabilities: undefined }41const builder = new Environment(targetBrowser).builder()42builder.disableEnvironmentOverrides()43let binary = process.env.BROWSER_BINARY44let driverBinary = process.env.DRIVER_BINARY4546let resolvedBinary = binary ? runfiles.resolve(driverBinary) : undefined47let resolvedDriver = driverBinary ? runfiles.resolve(binary) : undefined4849// Create a temporary directory we can use as a home dir50// process.env["USER"] = "nobody"51process.env['HOME'] = fs.mkdtempSync(path.join(os.tmpdir(), 'jasmine-test'))5253switch (browser) {54case 'chrome':55builder.forBrowser(Browser.CHROME)56if (resolvedDriver) {57let sb = new chrome.ServiceBuilder(resolvedDriver)58sb.enableVerboseLogging()59sb.setStdio('inherit')60builder.setChromeService(sb)61}62if (resolvedBinary) {63let options = new chrome.Options()64options.setChromeBinaryPath(resolvedBinary)65options.setAcceptInsecureCerts(true)66options.addArguments('disable-infobars', 'disable-breakpad', 'disable-dev-shm-usage', 'no-sandbox')67builder.setChromeOptions(options)68}69break7071// case 'edge':72// builder = builder.forBrowser(webdriver.Browser.EDGE)73// break74//75case 'firefox':76builder.forBrowser(Browser.FIREFOX)77if (resolvedDriver) {78let sb = new firefox.ServiceBuilder(resolvedDriver)79sb.enableVerboseLogging(true)80sb.setStdio('inherit')81builder.setFirefoxService(sb)82}83if (resolvedBinary) {84let options = new firefox.Options()85options.setBinary(resolvedBinary)86options.enableDebugger()87builder.setFirefoxOptions(options)88}89break9091case 'safari':92builder.forBrowser(Browser.SAFARI)93break9495default:96throw new Error('SELENIUM_BROWSER does not list a supported browser')97}9899return builder.build()100}101102module.exports = {103GetBrowserForTests,104}105106107