firefox_test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 path = require('path');
  19. var firefox = require('../../firefox'),
  20. io = require('../../io'),
  21. test = require('../../lib/test'),
  22. assert = require('../../testing/assert'),
  23. Context = require('../../firefox').Context,
  24. error = require('../..').error;
  25. var {consume} = require('../../lib/promise');
  26. var JETPACK_EXTENSION = path.join(__dirname,
  27. '../../lib/test/data/firefox/jetpack-sample.xpi');
  28. var NORMAL_EXTENSION = path.join(__dirname,
  29. '../../lib/test/data/firefox/sample.xpi');
  30. var WEBEXTENSION_EXTENSION = path.join(__dirname,
  31. '../../lib/test/data/firefox/webextension.xpi');
  32. test.suite(function(env) {
  33. describe('firefox', function() {
  34. describe('Options', function() {
  35. let driver;
  36. beforeEach(function() {
  37. driver = null;
  38. });
  39. afterEach(function() {
  40. if (driver) {
  41. return driver.quit();
  42. }
  43. });
  44. /**
  45. * @param {...string} extensions the extensions to install.
  46. * @return {!firefox.Profile} a new profile.
  47. */
  48. function profileWithExtensions(...extensions) {
  49. let profile = new firefox.Profile();
  50. profile.setPreference('xpinstall.signatures.required', false);
  51. extensions.forEach(ext => profile.addExtension(ext));
  52. return profile;
  53. }
  54. /**
  55. * Runs a test that requires Firefox Developer Edition. The test will be
  56. * skipped if dev cannot be found on the current system.
  57. */
  58. function runWithFirefoxDev(options, testFn) {
  59. return firefox.Channel.AURORA.locate().then(exe => {
  60. options.setBinary(exe);
  61. driver = env.builder()
  62. .setFirefoxOptions(options)
  63. .build();
  64. return driver.call(testFn);
  65. }, err => {
  66. console.warn(
  67. 'Skipping test: could not find Firefox Dev Edition: ' + err);
  68. });
  69. }
  70. describe('can start Firefox with custom preferences', function() {
  71. function runTest(opt_dir) {
  72. return consume(function*() {
  73. let profile = new firefox.Profile(opt_dir);
  74. profile.setPreference('general.useragent.override', 'foo;bar');
  75. let options = new firefox.Options().setProfile(profile);
  76. driver = env.builder().
  77. setFirefoxOptions(options).
  78. build();
  79. yield driver.get('data:text/html,<html><div>content</div></html>');
  80. var userAgent = yield driver.executeScript(
  81. 'return window.navigator.userAgent');
  82. assert(userAgent).equalTo('foo;bar');
  83. });
  84. }
  85. test.it('profile created from scratch', function() {
  86. return runTest();
  87. });
  88. test.it('profile created from template', function() {
  89. return io.tmpDir().then(runTest);
  90. });
  91. });
  92. test.it('can start Firefox with a jetpack extension', function() {
  93. let profile = profileWithExtensions(JETPACK_EXTENSION);
  94. let options = new firefox.Options().setProfile(profile);
  95. return runWithFirefoxDev(options, function*() {
  96. yield loadJetpackPage(driver,
  97. 'data:text/html;charset=UTF-8,<html><div>content</div></html>');
  98. let text =
  99. yield driver.findElement({id: 'jetpack-sample-banner'}).getText();
  100. assert(text).equalTo('Hello, world!');
  101. });
  102. });
  103. test.it('can start Firefox with a normal extension', function() {
  104. let profile = profileWithExtensions(NORMAL_EXTENSION);
  105. let options = new firefox.Options().setProfile(profile);
  106. return runWithFirefoxDev(options, function*() {
  107. yield driver.get('data:text/html,<html><div>content</div></html>');
  108. let footer =
  109. yield driver.findElement({id: 'sample-extension-footer'});
  110. let text = yield footer.getText();
  111. assert(text).equalTo('Goodbye');
  112. });
  113. });
  114. test.it('can start Firefox with a webextension extension', function() {
  115. let profile = profileWithExtensions(WEBEXTENSION_EXTENSION);
  116. let options = new firefox.Options().setProfile(profile);
  117. return runWithFirefoxDev(options, function*() {
  118. yield driver.get(test.Pages.echoPage);
  119. let footer =
  120. yield driver.findElement({id: 'webextensions-selenium-example'});
  121. let text = yield footer.getText();
  122. assert(text).equalTo('Content injected by webextensions-selenium-example');
  123. });
  124. });
  125. test.it('can start Firefox with multiple extensions', function() {
  126. let profile =
  127. profileWithExtensions(JETPACK_EXTENSION, NORMAL_EXTENSION);
  128. let options = new firefox.Options().setProfile(profile);
  129. return runWithFirefoxDev(options, function*() {
  130. yield loadJetpackPage(driver,
  131. 'data:text/html;charset=UTF-8,<html><div>content</div></html>');
  132. let banner =
  133. yield driver.findElement({id: 'jetpack-sample-banner'}).getText();
  134. assert(banner).equalTo('Hello, world!');
  135. let footer =
  136. yield driver.findElement({id: 'sample-extension-footer'})
  137. .getText();
  138. assert(footer).equalTo('Goodbye');
  139. });
  140. });
  141. function loadJetpackPage(driver, url) {
  142. // On linux the jetpack extension does not always run the first time
  143. // we load a page. If this happens, just reload the page (a simple
  144. // refresh doesn't appear to work).
  145. return driver.wait(function() {
  146. driver.get(url);
  147. return driver.findElements({id: 'jetpack-sample-banner'})
  148. .then(found => found.length > 0);
  149. }, 3000);
  150. }
  151. });
  152. describe('binary management', function() {
  153. var driver1, driver2;
  154. test.ignore(env.isRemote).
  155. it('can start multiple sessions with single binary instance', function*() {
  156. var options = new firefox.Options().setBinary(new firefox.Binary);
  157. env.builder().setFirefoxOptions(options);
  158. driver1 = yield env.builder().build();
  159. driver2 = yield env.builder().build();
  160. // Ok if this doesn't fail.
  161. });
  162. test.afterEach(function*() {
  163. if (driver1) {
  164. yield driver1.quit();
  165. }
  166. if (driver2) {
  167. yield driver2.quit();
  168. }
  169. });
  170. });
  171. describe('context switching', function() {
  172. var driver;
  173. test.beforeEach(function*() {
  174. driver = yield env.builder().build();
  175. });
  176. test.afterEach(function() {
  177. if (driver) {
  178. return driver.quit();
  179. }
  180. });
  181. test.it('can get context', function() {
  182. return assert(driver.getContext()).equalTo(Context.CONTENT);
  183. });
  184. test.it('can set context', function*() {
  185. yield driver.setContext(Context.CHROME);
  186. let ctxt = yield driver.getContext();
  187. assert(ctxt).equalTo(Context.CHROME);
  188. yield driver.setContext(Context.CONTENT);
  189. ctxt = yield driver.getContext();
  190. assert(ctxt).equalTo(Context.CONTENT);
  191. });
  192. test.it('throws on unknown context', function() {
  193. return driver.setContext("foo").then(assert.fail, function(e) {
  194. assert(e).instanceOf(error.InvalidArgumentError);
  195. });
  196. });
  197. });
  198. });
  199. }, {browsers: ['firefox']});