-
Notifications
You must be signed in to change notification settings - Fork 10
/
ait.js
192 lines (161 loc) · 5.48 KB
/
ait.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (C) 2013, GoodData(R) Corporation. All rights reserved.
var au = require('auquery');
// Exported AIT instance
var AIT = {
$: null,
browser: null
};
// Export PageFragments and friends to be globally accessible
var fragment = require('./lib/fragment.js');
// export Page(Fragment|Object|*) to AIT
fragment.exportClasses(AIT);
/**
* @property AIT.options
*
* Set from within the AIT.init where it merges its `options` argument with the
* result of .aitrc file.
**/
/**
* @property AIT.options.browserName
*
* Browser name to configure the WebDriver with. The following were tested:
* 'chrome', 'firefox', 'phantomjs'.
**/
/**
* @property AIT.options.implicitWaitTimeout
*
* implicitWaitTimeout setting for WebDriver
**/
/**
* Initialize the AIT environment. Sets the AuQuery instance to AIT.$ and
* an initialized WebDriver to AIT.browser variable.
*
* @method init
* @param {Object} options Configuration object
* @param {Function} callback A callback to call upon completion
**/
AIT.init = function aitInit(options, callback) {
if (!callback) {
callback = options;
options = null;
}
options = Em.Object.create(options || {});
// read the config file
var path = require('path'),
fs = require('fs');
var rcFname = path.resolve(process.env.aitrc || '.aitrc');
var rc = fs.existsSync(rcFname) && fs.readFileSync(rcFname, 'utf8');
if (rc) {
var sandbox = {
__filename: rcFname,
__dirname: path.dirname(rcFname),
// module: aitrc,
require: require,
process: process,
console: console
};
var cfg = require('vm').runInNewContext(rc, sandbox, rcFname);
options.setProperties(cfg);
}
if (!options.get('browserName')) {
options.set('browserName', process.env.browser);
}
AIT.options = options;
function cb($, browser){
var options = AIT.options;
// initialized $ and browser into the global.AIT namespace
AIT.$ = $;
AIT.browser = browser;
// provide unconditional wait() method
browser.wait = this.wait.bind(this);
//
// $().wait(timeout) method addition which calls WD.js: waitForElement()
//
var _wait = au.auQuery.fn.wait = function(timeout) {
if (!this.selector) console.trace('waitFor');
var context = this.context || browser;
try {
context.waitForElement(this.by || 'css selector', this.selector, timeout);
} catch(e) {
console.error('Error: wait(', this.selector, this.by, ')', e.stack);
throw e;
}
return this;
};
//
// Patch to mitigate the unmerged
// https://github.com/cyrjano/AuQuery/pull/2
//
var _init = au.auQuery.fn.init;
au.auQuery.fn.init = function(selector, context, by, browser) {
_init.call(this, selector, context, by, browser);
this.by = by || 'css selector';
return this;
};
au.auQuery.fn.init.prototype = _init.prototype;
// initialize the browser instance
browser.init({ browserName: options.browserName || 'phantomjs' });
// convenience aliases
browser.screenshot = function screenshot(filename) {
var data = browser.takeScreenshot();
filename = filename || 'ait-screenshot-' + new Date().getTime() + '.png';
var dir = options.screenshotsDir || 'ait-screenshots';
var fs = require('fs');
if (!path.existsSync(dir)) fs.mkdirSync(dir);
fs.writeFileSync(dir + '/' + filename, data, 'base64');
};
// Implicit wait timeout defaults to 100s
browser.implicitWaitTimeout = options.implicitWaitTimeout || 100000;
// initializing the timeout
browser.setImplicitWaitTimeout(browser.implicitWaitTimeout);
/**
* @method create
*
* Factory method to instantiate PageFragment using the configured
* connected WebDriver client instance.
**/
browser.create = function(Clazz, selector, context, by) {
if (Clazz) { // fragment type, create the fragment
if (by || context) {
selector = {
sel: selector,
by: by,
context: context
};
}
if (Clazz.create) {
return Clazz.create({ root: selector });
} else {
var res = new Clazz({ root: selector });
return res;
}
}
return selector;
};
// use the initialized $ and browser
fragment.init(AIT);
// mocha expects the first argument to be an Error in its done()
// async functions
callback(null, $, browser);
}
var b = new au.browser(require('wd').remote());
b.drive(cb, function(err, res){
if(!err) return;
var browser = AIT.browser;
try {
browser.screenshot('ait-error-' + new Date().getTime() + '.png');
browser.quit();
} catch(e) {
// quietly finalize
}
throw err;
});
};
/**
* @method destroy
* Ensure the AIT instance is cleaned up eventually.
**/
AIT.destroy = function aitDestroy(callback) {
AIT.browser.quit(callback);
};
module.exports = AIT;