options_test.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. var fs = require('fs');
  19. var webdriver = require('../..'),
  20. chrome = require('../../chrome'),
  21. symbols = require('../../lib/symbols'),
  22. proxy = require('../../proxy'),
  23. assert = require('../../testing/assert');
  24. var test = require('../../lib/test');
  25. describe('chrome.Options', function() {
  26. describe('fromCapabilities', function() {
  27. it('should return a new Options instance if none were defined',
  28. function() {
  29. var options = chrome.Options.fromCapabilities(
  30. new webdriver.Capabilities());
  31. assert(options).instanceOf(chrome.Options);
  32. });
  33. it('should return options instance if present', function() {
  34. var options = new chrome.Options();
  35. var caps = options.toCapabilities();
  36. assert(caps).instanceOf(webdriver.Capabilities);
  37. assert(chrome.Options.fromCapabilities(caps)).equalTo(options);
  38. });
  39. it('should rebuild options from wire representation', function() {
  40. var expectedExtension = fs.readFileSync(__filename, 'base64');
  41. var caps = webdriver.Capabilities.chrome().set('chromeOptions', {
  42. args: ['a', 'b'],
  43. extensions: [__filename],
  44. binary: 'binaryPath',
  45. logPath: 'logFilePath',
  46. detach: true,
  47. localState: 'localStateValue',
  48. prefs: 'prefsValue'
  49. });
  50. var options = chrome.Options.fromCapabilities(caps);
  51. var json = options[symbols.serialize]();
  52. assert(json.args.length).equalTo(2);
  53. assert(json.args[0]).equalTo('a');
  54. assert(json.args[1]).equalTo('b');
  55. assert(json.extensions.length).equalTo(1);
  56. assert(json.extensions[0]).equalTo(expectedExtension);
  57. assert(json.binary).equalTo('binaryPath');
  58. assert(json.logPath).equalTo('logFilePath');
  59. assert(json.detach).equalTo(true);
  60. assert(json.localState).equalTo('localStateValue');
  61. assert(json.prefs).equalTo('prefsValue');
  62. });
  63. it('should rebuild options from incomplete wire representation',
  64. function() {
  65. var caps = webdriver.Capabilities.chrome().set('chromeOptions', {
  66. logPath: 'logFilePath'
  67. });
  68. var options = chrome.Options.fromCapabilities(caps);
  69. var json = options[symbols.serialize]();
  70. assert(json.args).isUndefined();
  71. assert(json.binary).isUndefined();
  72. assert(json.detach).isUndefined();
  73. assert(json.excludeSwitches).isUndefined();
  74. assert(json.extensions).isUndefined();
  75. assert(json.localState).isUndefined();
  76. assert(json.logPath).equalTo('logFilePath');
  77. assert(json.prefs).isUndefined();
  78. assert(json.minidumpPath).isUndefined();
  79. assert(json.mobileEmulation).isUndefined();
  80. assert(json.perfLoggingPrefs).isUndefined();
  81. });
  82. it('should extract supported WebDriver capabilities', function() {
  83. var proxyPrefs = proxy.direct();
  84. var logPrefs = {};
  85. var caps = webdriver.Capabilities.chrome().
  86. set(webdriver.Capability.PROXY, proxyPrefs).
  87. set(webdriver.Capability.LOGGING_PREFS, logPrefs);
  88. var options = chrome.Options.fromCapabilities(caps);
  89. assert(options.proxy_).equalTo(proxyPrefs);
  90. assert(options.logPrefs_).equalTo(logPrefs);
  91. });
  92. });
  93. describe('addArguments', function() {
  94. it('takes var_args', function() {
  95. var options = new chrome.Options();
  96. assert(options[symbols.serialize]().args).isUndefined();
  97. options.addArguments('a', 'b');
  98. var json = options[symbols.serialize]();
  99. assert(json.args.length).equalTo(2);
  100. assert(json.args[0]).equalTo('a');
  101. assert(json.args[1]).equalTo('b');
  102. });
  103. it('flattens input arrays', function() {
  104. var options = new chrome.Options();
  105. assert(options[symbols.serialize]().args).isUndefined();
  106. options.addArguments(['a', 'b'], 'c', [1, 2], 3);
  107. var json = options[symbols.serialize]();
  108. assert(json.args.length).equalTo(6);
  109. assert(json.args[0]).equalTo('a');
  110. assert(json.args[1]).equalTo('b');
  111. assert(json.args[2]).equalTo('c');
  112. assert(json.args[3]).equalTo(1);
  113. assert(json.args[4]).equalTo(2);
  114. assert(json.args[5]).equalTo(3);
  115. });
  116. });
  117. describe('addExtensions', function() {
  118. it('takes var_args', function() {
  119. var options = new chrome.Options();
  120. assert(options.extensions_.length).equalTo(0);
  121. options.addExtensions('a', 'b');
  122. assert(options.extensions_.length).equalTo(2);
  123. assert(options.extensions_[0]).equalTo('a');
  124. assert(options.extensions_[1]).equalTo('b');
  125. });
  126. it('flattens input arrays', function() {
  127. var options = new chrome.Options();
  128. assert(options.extensions_.length).equalTo(0);
  129. options.addExtensions(['a', 'b'], 'c', [1, 2], 3);
  130. assert(options.extensions_.length).equalTo(6);
  131. assert(options.extensions_[0]).equalTo('a');
  132. assert(options.extensions_[1]).equalTo('b');
  133. assert(options.extensions_[2]).equalTo('c');
  134. assert(options.extensions_[3]).equalTo(1);
  135. assert(options.extensions_[4]).equalTo(2);
  136. assert(options.extensions_[5]).equalTo(3);
  137. });
  138. });
  139. describe('serialize', function() {
  140. it('base64 encodes extensions', function() {
  141. var expected = fs.readFileSync(__filename, 'base64');
  142. var wire = new chrome.Options()
  143. .addExtensions(__filename)
  144. [symbols.serialize]();
  145. assert(wire.extensions.length).equalTo(1);
  146. assert(wire.extensions[0]).equalTo(expected);
  147. });
  148. });
  149. describe('toCapabilities', function() {
  150. it('returns a new capabilities object if one is not provided', function() {
  151. var options = new chrome.Options();
  152. var caps = options.toCapabilities();
  153. assert(caps.get('browserName')).equalTo('chrome');
  154. assert(caps.get('chromeOptions')).equalTo(options);
  155. });
  156. it('adds to input capabilities object', function() {
  157. var caps = webdriver.Capabilities.firefox();
  158. var options = new chrome.Options();
  159. assert(options.toCapabilities(caps)).equalTo(caps);
  160. assert(caps.get('browserName')).equalTo('firefox');
  161. assert(caps.get('chromeOptions')).equalTo(options);
  162. });
  163. it('sets generic driver capabilities', function() {
  164. var proxyPrefs = {};
  165. var loggingPrefs = {};
  166. var options = new chrome.Options().
  167. setLoggingPrefs(loggingPrefs).
  168. setProxy(proxyPrefs);
  169. var caps = options.toCapabilities();
  170. assert(caps.get('proxy')).equalTo(proxyPrefs);
  171. assert(caps.get('loggingPrefs')).equalTo(loggingPrefs);
  172. });
  173. });
  174. });
  175. test.suite(function(env) {
  176. var driver;
  177. test.afterEach(function() {
  178. return driver.quit();
  179. });
  180. describe('Chrome options', function() {
  181. test.it('can start Chrome with custom args', function*() {
  182. var options = new chrome.Options().
  183. addArguments('user-agent=foo;bar');
  184. driver = yield env.builder()
  185. .setChromeOptions(options)
  186. .build();
  187. yield driver.get(test.Pages.ajaxyPage);
  188. var userAgent =
  189. yield driver.executeScript('return window.navigator.userAgent');
  190. assert(userAgent).equalTo('foo;bar');
  191. });
  192. });
  193. }, {browsers: ['chrome']});