window_test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 Browser = require('..').Browser,
  19. By = require('..').By,
  20. assert = require('../testing/assert'),
  21. test = require('../lib/test');
  22. test.suite(function(env) {
  23. var driver;
  24. test.before(function*() { driver = yield env.builder().build(); });
  25. test.after(function() { return driver.quit(); });
  26. test.beforeEach(function() {
  27. return driver.switchTo().defaultContent();
  28. });
  29. test.it('can set size of the current window', function*() {
  30. yield driver.get(test.Pages.echoPage);
  31. yield changeSizeBy(-20, -20);
  32. });
  33. test.it('can set size of the current window from frame', function*() {
  34. yield driver.get(test.Pages.framesetPage);
  35. var frame = yield driver.findElement({css: 'frame[name="fourth"]'});
  36. yield driver.switchTo().frame(frame);
  37. yield changeSizeBy(-20, -20);
  38. });
  39. test.it('can set size of the current window from iframe', function*() {
  40. yield driver.get(test.Pages.iframePage);
  41. var frame = yield driver.findElement({css: 'iframe[name="iframe1-name"]'});
  42. yield driver.switchTo().frame(frame);
  43. yield changeSizeBy(-20, -20);
  44. });
  45. test.it('can switch to a new window', function*() {
  46. yield driver.get(test.Pages.xhtmlTestPage);
  47. let handle = yield driver.getWindowHandle();
  48. let originalHandles = yield driver.getAllWindowHandles();
  49. yield driver.findElement(By.linkText("Open new window")).click();
  50. yield driver.wait(forNewWindowToBeOpened(originalHandles), 2000);
  51. yield assert(driver.getTitle()).equalTo("XHTML Test Page");
  52. let newHandle = yield getNewWindowHandle(originalHandles);
  53. yield driver.switchTo().window(newHandle);
  54. yield assert(driver.getTitle()).equalTo("We Arrive Here");
  55. });
  56. test.it('can set the window position of the current window', function*() {
  57. let position = yield driver.manage().window().getPosition();
  58. yield driver.manage().window().setSize(640, 480);
  59. yield driver.manage().window().setPosition(position.x + 10, position.y + 10);
  60. // For phantomjs, setPosition is a no-op and the "window" stays at (0, 0)
  61. if (env.currentBrowser() === Browser.PHANTOM_JS) {
  62. position = yield driver.manage().window().getPosition();
  63. assert(position.x).equalTo(0);
  64. assert(position.y).equalTo(0);
  65. } else {
  66. var dx = position.x + 10;
  67. var dy = position.y + 10;
  68. return driver.wait(forPositionToBe(dx, dy), 1000);
  69. }
  70. });
  71. test.it('can set the window position from a frame', function*() {
  72. yield driver.get(test.Pages.iframePage);
  73. let frame = yield driver.findElement(By.name('iframe1-name'));
  74. yield driver.switchTo().frame(frame);
  75. let position = yield driver.manage().window().getPosition();
  76. yield driver.manage().window().setSize(640, 480);
  77. yield driver.manage().window().setPosition(position.x + 10, position.y + 10);
  78. // For phantomjs, setPosition is a no-op and the "window" stays at (0, 0)
  79. if (env.currentBrowser() === Browser.PHANTOM_JS) {
  80. return driver.manage().window().getPosition().then(function(position) {
  81. assert(position.x).equalTo(0);
  82. assert(position.y).equalTo(0);
  83. });
  84. } else {
  85. var dx = position.x + 10;
  86. var dy = position.y + 10;
  87. return driver.wait(forPositionToBe(dx, dy), 1000);
  88. }
  89. });
  90. function changeSizeBy(dx, dy) {
  91. return driver.manage().window().getSize().then(function(size) {
  92. return driver.manage().window()
  93. .setSize(size.width + dx, size.height + dy)
  94. .then(_ => {
  95. return driver.wait(
  96. forSizeToBe(size.width + dx, size.height + dy), 1000);
  97. });
  98. });
  99. }
  100. function forSizeToBe(w, h) {
  101. return function() {
  102. return driver.manage().window().getSize().then(function(size) {
  103. return size.width === w && size.height === h;
  104. });
  105. };
  106. }
  107. function forPositionToBe(x, y) {
  108. return function() {
  109. return driver.manage().window().getPosition().then(function(position) {
  110. return position.x === x &&
  111. // On OSX, the window height may be bumped down 22px for the top
  112. // status bar.
  113. // On Linux, Opera's window position will be off by 28px.
  114. (position.y >= y && position.y <= (y + 28));
  115. });
  116. };
  117. }
  118. function forNewWindowToBeOpened(originalHandles) {
  119. return function() {
  120. return driver.getAllWindowHandles().then(function(currentHandles) {
  121. return currentHandles.length > originalHandles.length;
  122. });
  123. };
  124. }
  125. function getNewWindowHandle(originalHandles) {
  126. // Note: this assumes there's just one new window.
  127. return driver.getAllWindowHandles().then(function(currentHandles) {
  128. return currentHandles.filter(function(i) {
  129. return originalHandles.indexOf(i) < 0;
  130. })[0];
  131. });
  132. }
  133. });