UNPKG

15.1 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 fail = require('assert').fail;
21
22var Browser = require('..').Browser,
23 By = require('..').By,
24 error = require('..').error,
25 until = require('..').until,
26 promise = require('../lib/promise'),
27 test = require('../lib/test'),
28 assert = require('../testing/assert'),
29 Pages = test.Pages;
30
31
32test.suite(function(env) {
33 var browsers = env.browsers;
34
35 var driver;
36
37 test.before(function*() {
38 driver = yield env.builder().build();
39 });
40
41 after(function() {
42 return driver.quit();
43 });
44
45 describe('finding elements', function() {
46 test.it(
47 'should work after loading multiple pages in a row',
48 function*() {
49 yield driver.get(Pages.formPage);
50 yield driver.get(Pages.xhtmlTestPage);
51 yield driver.findElement(By.linkText('click me')).click();
52 yield driver.wait(until.titleIs('We Arrive Here'), 5000);
53 });
54
55 describe('By.id()', function() {
56 test.it('should work', function*() {
57 yield driver.get(Pages.xhtmlTestPage);
58 yield driver.findElement(By.id('linkId')).click();
59 yield driver.wait(until.titleIs('We Arrive Here'), 5000);
60 });
61
62 test.it('should fail if ID not present on page', function*() {
63 yield driver.get(Pages.formPage);
64 return driver.findElement(By.id('nonExistantButton')).
65 then(fail, function(e) {
66 assert(e).instanceOf(error.NoSuchElementError);
67 });
68 });
69
70 test.it(
71 'should find multiple elements by ID even though that is ' +
72 'malformed HTML',
73 function*() {
74 yield driver.get(Pages.nestedPage);
75
76 let elements = yield driver.findElements(By.id('2'));
77 assert(elements.length).equalTo(8);
78 });
79 });
80
81 describe('By.linkText()', function() {
82 test.it('should be able to click on link identified by text', function*() {
83 yield driver.get(Pages.xhtmlTestPage);
84 yield driver.findElement(By.linkText('click me')).click();
85 yield driver.wait(until.titleIs('We Arrive Here'), 5000);
86 });
87
88 test.it(
89 'should be able to find elements by partial link text',
90 function*() {
91 yield driver.get(Pages.xhtmlTestPage);
92 yield driver.findElement(By.partialLinkText('ick me')).click();
93 yield driver.wait(until.titleIs('We Arrive Here'), 5000);
94 });
95
96 test.it('should work when link text contains equals sign', function*() {
97 yield driver.get(Pages.xhtmlTestPage);
98 let el = yield driver.findElement(By.linkText('Link=equalssign'));
99
100 let id = yield el.getAttribute('id');
101 assert(id).equalTo('linkWithEqualsSign');
102 });
103
104 test.it('matches by partial text when containing equals sign',
105 function*() {
106 yield driver.get(Pages.xhtmlTestPage);
107 let link = yield driver.findElement(By.partialLinkText('Link='));
108
109 let id = yield link.getAttribute('id');
110 assert(id).equalTo('linkWithEqualsSign');
111 });
112
113 test.it('works when searching for multiple and text contains =',
114 function*() {
115 yield driver.get(Pages.xhtmlTestPage);
116 let elements =
117 yield driver.findElements(By.linkText('Link=equalssign'));
118
119 assert(elements.length).equalTo(1);
120
121 let id = yield elements[0].getAttribute('id');
122 assert(id).equalTo('linkWithEqualsSign');
123 });
124
125 test.it(
126 'works when searching for multiple with partial text containing =',
127 function*() {
128 yield driver.get(Pages.xhtmlTestPage);
129 let elements =
130 yield driver.findElements(By.partialLinkText('Link='));
131
132 assert(elements.length).equalTo(1);
133
134 let id = yield elements[0].getAttribute('id');
135 assert(id).equalTo('linkWithEqualsSign');
136 });
137
138 test.it('should be able to find multiple exact matches',
139 function*() {
140 yield driver.get(Pages.xhtmlTestPage);
141 let elements = yield driver.findElements(By.linkText('click me'));
142 assert(elements.length).equalTo(2);
143 });
144
145 test.it('should be able to find multiple partial matches',
146 function*() {
147 yield driver.get(Pages.xhtmlTestPage);
148 let elements =
149 yield driver.findElements(By.partialLinkText('ick me'));
150 assert(elements.length).equalTo(2);
151 });
152
153 test.ignore(browsers(Browser.SAFARI)).
154 it('works on XHTML pages', function*() {
155 yield driver.get(test.whereIs('actualXhtmlPage.xhtml'));
156
157 let el = yield driver.findElement(By.linkText('Foo'));
158 return assert(el.getText()).equalTo('Foo');
159 });
160 });
161
162 describe('By.name()', function() {
163 test.it('should work', function*() {
164 yield driver.get(Pages.formPage);
165
166 let el = yield driver.findElement(By.name('checky'));
167 yield assert(el.getAttribute('value')).equalTo('furrfu');
168 });
169
170 test.it('should find multiple elements with same name', function*() {
171 yield driver.get(Pages.nestedPage);
172
173 let elements = yield driver.findElements(By.name('checky'));
174 assert(elements.length).greaterThan(1);
175 });
176
177 test.it(
178 'should be able to find elements that do not support name property',
179 function*() {
180 yield driver.get(Pages.nestedPage);
181 yield driver.findElement(By.name('div1'));
182 // Pass if this does not return an error.
183 });
184
185 test.it('shoudl be able to find hidden elements by name', function*() {
186 yield driver.get(Pages.formPage);
187 yield driver.findElement(By.name('hidden'));
188 // Pass if this does not return an error.
189 });
190 });
191
192 describe('By.className()', function() {
193 test.it('should work', function*() {
194 yield driver.get(Pages.xhtmlTestPage);
195
196 let el = yield driver.findElement(By.className('extraDiv'));
197 yield assert(el.getText()).startsWith('Another div starts here.');
198 });
199
200 test.it('should work when name is first name among many', function*() {
201 yield driver.get(Pages.xhtmlTestPage);
202
203 let el = yield driver.findElement(By.className('nameA'));
204 yield assert(el.getText()).equalTo('An H2 title');
205 });
206
207 test.it('should work when name is last name among many', function*() {
208 yield driver.get(Pages.xhtmlTestPage);
209
210 let el = yield driver.findElement(By.className('nameC'));
211 yield assert(el.getText()).equalTo('An H2 title');
212 });
213
214 test.it('should work when name is middle of many', function*() {
215 yield driver.get(Pages.xhtmlTestPage);
216
217 let el = yield driver.findElement(By.className('nameBnoise'));
218 yield assert(el.getText()).equalTo('An H2 title');
219 });
220
221 test.it('should work when name surrounded by whitespace', function*() {
222 yield driver.get(Pages.xhtmlTestPage);
223
224 let el = yield driver.findElement(By.className('spaceAround'));
225 yield assert(el.getText()).equalTo('Spaced out');
226 });
227
228 test.it('should fail if queried name only partially matches', function*() {
229 yield driver.get(Pages.xhtmlTestPage);
230 return driver.findElement(By.className('nameB')).
231 then(fail, function(e) {
232 assert(e).instanceOf(error.NoSuchElementError);
233 });
234 });
235
236 test.it('should implicitly wait', function*() {
237 var TIMEOUT_IN_MS = 1000;
238 var EPSILON = TIMEOUT_IN_MS / 2;
239
240 yield driver.manage().timeouts().implicitlyWait(TIMEOUT_IN_MS);
241 yield driver.get(Pages.formPage);
242
243 var start = new Date();
244 return driver.findElement(By.id('nonExistantButton')).
245 then(fail, function(e) {
246 var end = new Date();
247 assert(e).instanceOf(error.NoSuchElementError);
248 assert(end - start).closeTo(TIMEOUT_IN_MS, EPSILON);
249 });
250 });
251
252 test.it('should be able to find multiple matches', function*() {
253 yield driver.get(Pages.xhtmlTestPage);
254
255 let elements = yield driver.findElements(By.className('nameC'));
256 assert(elements.length).greaterThan(1);
257 });
258
259 test.it('permits compound class names', function() {
260 return driver.get(Pages.xhtmlTestPage)
261 .then(() => driver.findElement(By.className('nameA nameC')))
262 .then(el => el.getText())
263 .then(text => assert(text).equalTo('An H2 title'));
264 });
265 });
266
267 describe('By.xpath()', function() {
268 test.it('should work with multiple matches', function*() {
269 yield driver.get(Pages.xhtmlTestPage);
270 let elements = yield driver.findElements(By.xpath('//div'));
271 assert(elements.length).greaterThan(1);
272 });
273
274 test.it('should work for selectors using contains keyword', function*() {
275 yield driver.get(Pages.nestedPage);
276 yield driver.findElement(By.xpath('//a[contains(., "hello world")]'));
277 // Pass if no error.
278 });
279 });
280
281 describe('By.tagName()', function() {
282 test.it('works', function*() {
283 yield driver.get(Pages.formPage);
284
285 let el = yield driver.findElement(By.tagName('input'));
286 yield assert(el.getTagName()).equalTo('input');
287 });
288
289 test.it('can find multiple elements', function*() {
290 yield driver.get(Pages.formPage);
291
292 let elements = yield driver.findElements(By.tagName('input'));
293 assert(elements.length).greaterThan(1);
294 });
295 });
296
297 describe('By.css()', function() {
298 test.it('works', function*() {
299 yield driver.get(Pages.xhtmlTestPage);
300 yield driver.findElement(By.css('div.content'));
301 // Pass if no error.
302 });
303
304 test.it('can find multiple elements', function*() {
305 yield driver.get(Pages.xhtmlTestPage);
306
307 let elements = yield driver.findElements(By.css('p'));
308 assert(elements.length).greaterThan(1);
309 // Pass if no error.
310 });
311
312 test.it(
313 'should find first matching element when searching by ' +
314 'compound CSS selector',
315 function*() {
316 yield driver.get(Pages.xhtmlTestPage);
317
318 let el =
319 yield driver.findElement(By.css('div.extraDiv, div.content'));
320 yield assert(el.getAttribute('class')).equalTo('content');
321 });
322
323 test.it('should be able to find multiple elements by compound selector',
324 function*() {
325 yield driver.get(Pages.xhtmlTestPage);
326 let elements =
327 yield driver.findElements(By.css('div.extraDiv, div.content'));
328
329 return Promise.all([
330 assertClassIs(elements[0], 'content'),
331 assertClassIs(elements[1], 'extraDiv')
332 ]);
333
334 function assertClassIs(el, expected) {
335 return assert(el.getAttribute('class')).equalTo(expected);
336 }
337 });
338
339 // IE only supports short version option[selected].
340 test.ignore(browsers(Browser.IE)).
341 it('should be able to find element by boolean attribute', function*() {
342 yield driver.get(test.whereIs(
343 'locators_tests/boolean_attribute_selected.html'));
344
345 let el = yield driver.findElement(By.css('option[selected="selected"]'));
346 yield assert(el.getAttribute('value')).equalTo('two');
347 });
348
349 test.it(
350 'should be able to find element with short ' +
351 'boolean attribute selector',
352 function*() {
353 yield driver.get(test.whereIs(
354 'locators_tests/boolean_attribute_selected.html'));
355
356 let el = yield driver.findElement(By.css('option[selected]'));
357 yield assert(el.getAttribute('value')).equalTo('two');
358 });
359
360 test.it(
361 'should be able to find element with short boolean attribute ' +
362 'selector on HTML4 page',
363 function*() {
364 yield driver.get(test.whereIs(
365 'locators_tests/boolean_attribute_selected_html4.html'));
366
367 let el = yield driver.findElement(By.css('option[selected]'));
368 yield assert(el.getAttribute('value')).equalTo('two');
369 });
370 });
371
372 describe('by custom locator', function() {
373 test.it('handles single element result', function*() {
374 yield driver.get(Pages.javascriptPage);
375
376 let link = yield driver.findElement(function(driver) {
377 let links = driver.findElements(By.tagName('a'));
378 return promise.filter(links, function(link) {
379 return link.getAttribute('id').then(id => id === 'updatediv');
380 }).then(links => links[0]);
381 });
382
383 yield assert(link.getText()).matches(/Update\s+a\s+div/);
384 });
385
386 test.it('uses first element if locator resolves to list', function*() {
387 yield driver.get(Pages.javascriptPage);
388
389 let link = yield driver.findElement(function() {
390 return driver.findElements(By.tagName('a'));
391 });
392
393 yield assert(link.getText()).isEqualTo('Change the page title!');
394 });
395
396 test.it('fails if locator returns non-webelement value', function*() {
397 yield driver.get(Pages.javascriptPage);
398
399 let link = driver.findElement(function() {
400 return driver.getTitle();
401 });
402
403 return link.then(
404 () => fail('Should have failed'),
405 (e) => assert(e).instanceOf(TypeError));
406 });
407 });
408
409 describe('switchTo().activeElement()', function() {
410 // SAFARI's new session response does not identify it as a W3C browser,
411 // so the command is sent in the unsupported wire protocol format.
412 test.ignore(browsers(Browser.SAFARI)).
413 it('returns document.activeElement', function*() {
414 yield driver.get(Pages.formPage);
415
416 let email = yield driver.findElement(By.css('#email'));
417 yield driver.executeScript('arguments[0].focus()', email);
418
419 let ae = yield driver.switchTo().activeElement();
420 let equal = yield driver.executeScript(
421 'return arguments[0] === arguments[1]', email, ae);
422 assert(equal).isTrue();
423 });
424 });
425 });
426});