UNPKG

6 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree.
7 */
8/* eslint-env node */
9
10'use strict';
11
12// https://code.google.com/p/selenium/wiki/WebDriverJs
13var webdriver = require('selenium-webdriver');
14var chrome = require('selenium-webdriver/chrome');
15var firefox = require('selenium-webdriver/firefox');
16var edge = require('selenium-webdriver/edge');
17var fs = require('fs');
18
19var sharedDriver = null;
20
21function getBrowserVersion() {
22 var browser = process.env.BROWSER;
23 var browserChannel = process.env.BVER;
24
25 // Browser reg expressions and position to look for the milestone version.
26 var chromeExp = /\/chrome\/(\d+)\./;
27 var firefoxExp = /\/firefox\/(\d+)\./;
28
29 var browserVersion = function(expr) {
30 var symlink = './browsers/bin/' + browser + '-' + browserChannel;
31 var pathToBrowser = fs.readlinkSync(symlink);
32 var match = pathToBrowser.match(expr);
33 return match && match.length >= 1 && parseInt(match[1], 10);
34 };
35
36 switch (browser) {
37 case 'chrome':
38 return browserVersion(chromeExp);
39 case 'firefox':
40 return browserVersion(firefoxExp);
41 case 'safari':
42 return browserChannel;
43 default:
44 return 'non supported browser.';
45 }
46}
47
48function buildDriver() {
49 if (sharedDriver) {
50 return sharedDriver;
51 }
52 // Enable console logging. Add logging for firefox once it's supported
53 // properly-
54 var logging = webdriver.logging;
55 var prefs = new logging.Preferences();
56 prefs.setLevel(logging.Type.BROWSER, logging.Level.ALL);
57
58 // Firefox options.
59 // http://selenium.googlecode.com/git/docs/api/javascript/module_selenium-webdriver_firefox.html
60 var profile = new firefox.Profile();
61 profile.setPreference('media.navigator.streams.fake', true);
62 // This enables device labels for enumerateDevices when using fake devices.
63 profile.setPreference('media.navigator.permission.disabled', true);
64 // Currently the FF webdriver extension is not signed and FF 41 no longer
65 // allows unsigned extensions by default.
66 // TODO: Remove this once FF no longer allow turning this off and the
67 // selenium team starts making a signed FF webdriver extension.
68 // https://github.com/SeleniumHQ/selenium/issues/901.
69 profile.setPreference('xpinstall.signatures.required', false);
70
71 var firefoxOptions = new firefox.Options()
72 .setProfile(profile)
73 .setBinary('node_modules/.bin/start-firefox');
74
75 // Chrome options.
76 // http://selenium.googlecode.com/git/docs/api/javascript/module_selenium-webdriver_chrome_class_Options.html#addArguments
77 var chromeOptions = new chrome.Options()
78 .setChromeBinaryPath('node_modules/.bin/start-chrome')
79 .addArguments('allow-file-access-from-files')
80 .addArguments('use-fake-device-for-media-stream')
81 .addArguments('use-fake-ui-for-media-stream')
82 .addArguments('disable-translate')
83 .addArguments('no-process-singleton-dialog')
84 .addArguments('mute-audio')
85 .addArguments('no-sandbox')
86 .setLoggingPrefs(prefs);
87
88 // Only enable this for Chrome >= 49.
89 if (process.env.BROWSER === 'chrome' && getBrowserVersion() >= 49) {
90 chromeOptions.addArguments('--enable-experimental-web-platform-features');
91 }
92
93 var edgeOptions = new edge.Options();
94
95 sharedDriver = new webdriver.Builder()
96 .forBrowser(process.env.BROWSER)
97 .setFirefoxOptions(firefoxOptions)
98 .setChromeOptions(chromeOptions)
99 .setEdgeOptions(edgeOptions);
100
101 if (process.env.BROWSER === 'firefox' && getBrowserVersion() >= 47) {
102 sharedDriver.getCapabilities().set('marionette', true);
103 }
104 sharedDriver = sharedDriver.build();
105 return sharedDriver;
106}
107
108// Webdriver logging output only prints the first argument for console.log.
109// trace in common.js in the webrtc/samples prefixes a timestamp as a first
110// argument. This overrides this to ensure we can get full console logging.
111function overrideTrace(driver) {
112 driver.executeScript('window.trace = function(arg) { console.log(arg); };');
113}
114
115// A helper function to query stats from a PeerConnection.
116function getStats(driver, peerConnection) {
117 // Execute getStats on peerconnection named `peerConnection`.
118 driver.manage().timeouts().setScriptTimeout(1000);
119 return driver.executeAsyncScript(
120 'var callback = arguments[arguments.length - 1];' +
121 peerConnection + '.getStats(null).then(function(report) {' +
122 ' callback(report.entries ? [...report.entries()] : report);' +
123 '});'
124 ).then(function(entries) {
125 if (Array.isArray(entries)) {
126 return new Map(entries); // eslint-disable-line no-undef
127 }
128 return entries;
129 });
130}
131
132// Provide the webdriver driver and type of logging:
133// https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/logging_exports_Type.html
134// Browser console logs: webdriver.logging.Type.BROWSER
135// WebDriver driver logs: webdriver.logging.Type.DRIVER
136function getLogs(driver, type) {
137 // https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_Logs.html
138 driver.manage().logs().get(type)
139 .then(function(entries) {
140 return entries;
141 });
142}
143
144// Provide the webdriver driver and type of logging:
145// https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/logging_exports_Type.html
146// Browser console logs: webdriver.logging.Type.BROWSER
147// WebDriver driver logs: webdriver.logging.Type.DRIVER
148function printLogs(driver, type) {
149 // https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_Logs.html
150 driver.manage().logs().get(type)
151 .then(function(entries) {
152 entries.forEach(function(entry) {
153 console.log('[%s] %s', entry.level.name, entry.message);
154 });
155 });
156}
157
158module.exports = {
159 buildDriver: buildDriver,
160 getStats: getStats,
161 getLogs: getLogs,
162 overrideTrace: overrideTrace,
163 printLogs: printLogs
164};