UNPKG

3.94 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2015, Groupon, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of GROUPON nor the names of its contributors may be
17 * used to endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33'use strict';
34
35const Bluebird = require('bluebird');
36const debug = require('debug')('testium-core');
37const _ = require('lodash');
38
39const Config = require('./config');
40const initTestium = require('./init');
41const resolveDriver = require('./driver-factory');
42
43const initTestiumOnce = _.once(initTestium);
44const getConfig = _.once(Config.load);
45
46const DEFAULT_PAGE_SIZE = { height: 768, width: 1024 };
47
48// For backwards-compatibility
49module.exports = initTestium;
50
51function clearCookies(testium) {
52 return Bluebird.try(_.bindKey(testium.browser, 'clearCookies'))
53 .then(_.partial(debug, 'Cookies cleared'))
54 .then(_.constant(testium));
55}
56
57function 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
65function 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
72function 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}
105module.exports.getTestium = getTestium;
106
107function getBrowser(options) {
108 return getTestium(options).then(_.property('browser'));
109}
110module.exports.getBrowser = getBrowser;
111
112module.exports.getConfig = getConfig;