UNPKG

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