session_test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. const assert = require('../testing/assert');
  19. const chrome = require('../chrome');
  20. const edge = require('../edge');
  21. const firefox = require('../firefox');
  22. const ie = require('../ie');
  23. const opera = require('../opera');
  24. const phantomjs = require('../phantomjs');
  25. const safari = require('../safari');
  26. const test = require('../lib/test');
  27. const {Browser} = require('../lib/capabilities');
  28. const {Pages} = require('../lib/test');
  29. const {WebDriver} = require('..');
  30. test.suite(function(env) {
  31. var browsers = env.browsers;
  32. const BROWSER_MAP = new Map([
  33. [Browser.CHROME, chrome.Driver],
  34. [Browser.EDGE, edge.Driver],
  35. [Browser.FIREFOX, firefox.Driver],
  36. [Browser.INTERNET_EXPLORER, ie.Driver],
  37. [Browser.OPERA, opera.Driver],
  38. [Browser.PHANTOM_JS, phantomjs.Driver],
  39. [Browser.SAFARI, safari.Driver],
  40. ]);
  41. if (BROWSER_MAP.has(env.currentBrowser())) {
  42. describe('builder creates thenable driver instances', function() {
  43. let driver;
  44. after(() => driver && driver.quit());
  45. it(env.currentBrowser(), function() {
  46. driver = env.builder().build();
  47. const want = BROWSER_MAP.get(env.currentBrowser());
  48. assert(driver).instanceOf(want,
  49. `want ${want.name}, but got ${driver.name}`);
  50. assert(typeof driver.then).equalTo('function');
  51. return driver
  52. .then(
  53. d => assert(d)
  54. .instanceOf(want, `want ${want.name}, but got ${d.name}`))
  55. // Load something so the safari driver doesn't crash from starting and
  56. // stopping in short time.
  57. .then(() => driver.get(Pages.echoPage));
  58. });
  59. });
  60. }
  61. describe('session management', function() {
  62. var driver;
  63. test.before(function*() {
  64. driver = yield env.builder().build();
  65. });
  66. test.after(function() {
  67. return driver.quit();
  68. });
  69. test.it('can connect to an existing session', function*() {
  70. yield driver.get(Pages.simpleTestPage);
  71. yield assert(driver.getTitle()).equalTo('Hello WebDriver');
  72. return driver.getSession().then(session1 => {
  73. let driver2 = WebDriver.attachToSession(
  74. driver.getExecutor(),
  75. session1.getId());
  76. return assert(driver2.getTitle()).equalTo('Hello WebDriver')
  77. .then(_ => {
  78. let session2Id = driver2.getSession().then(s => s.getId());
  79. return assert(session2Id).equalTo(session1.getId());
  80. });
  81. });
  82. });
  83. });
  84. });