1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * Provide polling-based "wait for" functionality, and defines some useful
13 /** @suppress {duplicate} */
14 var browserTest
= browserTest
|| {};
17 browserTest
.Timeout
= {
23 browserTest
.Predicate = function() {};
25 /** @return {boolean} */
26 browserTest
.Predicate
.prototype.evaluate = function() {};
28 /** @return {string} */
29 browserTest
.Predicate
.prototype.description = function() {};
32 * @param {browserTest.Predicate} predicate
33 * @param {number=} opt_timeout Timeout in ms.
36 browserTest
.waitFor = function(predicate
, opt_timeout
) {
38 * @param {function():void} fulfill
39 * @param {function(Error):void} reject
41 return new Promise(function (fulfill
, reject
) {
42 if (opt_timeout
=== undefined) {
43 opt_timeout
= browserTest
.Timeout
.DEFAULT
;
46 var timeout
= /** @type {number} */ (opt_timeout
);
47 var end
= Number(Date
.now()) + timeout
;
48 var testPredicate = function() {
49 if (predicate
.evaluate()) {
50 console
.log(predicate
.description() + ' satisfied.');
52 } else if (Date
.now() >= end
) {
53 reject(new Error('Timed out (' + opt_timeout
+ 'ms) waiting for ' +
54 predicate
.description()));
56 console
.log(predicate
.description() + ' not yet satisfied.');
57 window
.setTimeout(testPredicate
, 500);
66 * @return {browserTest.Predicate}
68 browserTest
.isVisible = function(id
) {
69 var pred
= new browserTest
.Predicate();
70 pred
.evaluate = function() {
71 /** @type {HTMLElement} */
72 var element
= document
.getElementById(id
);
73 browserTest
.expect(Boolean(element
), 'No such element: ' + id
);
74 return element
.getBoundingClientRect().width
!== 0;
76 pred
.description = function() {
77 return 'isVisible(' + id
+ ')';
84 * @return {browserTest.Predicate}
86 browserTest
.isEnabled = function(id
) {
87 var pred
= new browserTest
.Predicate();
88 pred
.evaluate = function() {
89 /** @type {Element} */
90 var element
= document
.getElementById(id
);
91 browserTest
.expect(Boolean(element
), 'No such element: ' + id
);
92 return !element
.disabled
;
94 pred
.description = function() {
95 return 'isEnabled(' + id
+ ')';