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 | 'use strict';
|
34 |
|
35 | const Bluebird = require('bluebird');
|
36 | const debug = require('debug')('testium-core');
|
37 | const _ = require('lodash');
|
38 |
|
39 | const Config = require('./config');
|
40 | const initTestium = require('./init');
|
41 | const resolveDriver = require('./driver-factory');
|
42 |
|
43 | const initTestiumOnce = _.once(initTestium);
|
44 | const getConfig = _.once(Config.load);
|
45 |
|
46 | const DEFAULT_PAGE_SIZE = { height: 768, width: 1024 };
|
47 |
|
48 |
|
49 | module.exports = initTestium;
|
50 |
|
51 | function clearCookies(testium) {
|
52 | return Bluebird.try(_.bindKey(testium.browser, 'clearCookies'))
|
53 | .then(_.partial(debug, 'Cookies cleared'))
|
54 | .then(_.constant(testium));
|
55 | }
|
56 |
|
57 | function primingLoad(testium) {
|
58 | return Bluebird.try(
|
59 | _.bindKey(testium.browser, 'navigateTo', testium.getInitialUrl())
|
60 | )
|
61 | .then(_.partial(debug, 'Browser was primed'))
|
62 | .then(_.constant(testium));
|
63 | }
|
64 |
|
65 | function resetViewport(testium) {
|
66 | const pageSize = testium.config.get('defaultPageSize', DEFAULT_PAGE_SIZE);
|
67 | return Bluebird.try(_.bindKey(testium.browser, 'setPageSize', pageSize))
|
68 | .then(_.partial(debug, 'View reset to default size', pageSize))
|
69 | .then(_.constant(testium));
|
70 | }
|
71 |
|
72 | function getTestium(options) {
|
73 | const config = getConfig();
|
74 | const localConfig = config.createShallowChild(options);
|
75 |
|
76 | const reuseSession = localConfig.get('reuseSession', true);
|
77 | const keepCookies = localConfig.get('keepCookies', false);
|
78 | const driverFactory = resolveDriver(localConfig.get('driver', 'wd'));
|
79 |
|
80 | const isExistingSession = reuseSession && !!driverFactory.instance;
|
81 | const skipPriming = isExistingSession;
|
82 | if (skipPriming) {
|
83 | debug('Skipping priming load');
|
84 | }
|
85 |
|
86 | function generateDriverError(error) {
|
87 | const logName =
|
88 | config.get('browser') === 'phantomjs' ? 'phantomjs.log' : 'selenium.log';
|
89 |
|
90 | error.message = [
|
91 | `Failed to initialize WebDriver. Check ${logName}.`,
|
92 | error.message,
|
93 | ].join('\n');
|
94 |
|
95 | throw error;
|
96 | }
|
97 |
|
98 | return initTestiumOnce(config)
|
99 | .then(reuseSession ? driverFactory.once : driverFactory.create)
|
100 | .then(resetViewport)
|
101 | .then(keepCookies ? _.identity : clearCookies)
|
102 | .then(skipPriming ? _.identity : primingLoad)
|
103 | .catch(generateDriverError);
|
104 | }
|
105 | module.exports.getTestium = getTestium;
|
106 |
|
107 | function getBrowser(options) {
|
108 | return getTestium(options).then(_.property('browser'));
|
109 | }
|
110 | module.exports.getBrowser = getBrowser;
|
111 |
|
112 | module.exports.getConfig = getConfig;
|