This repository was archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
186 lines (169 loc) · 6.41 KB
/
index.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
const { Print } = require('@ianwalter/print')
let print
let seleniumStandalone
const webdriverVersion = '3.141.59'
const hasBsl = cap => cap['bstack:options'] && cap['bstack:options'].local
const shouldUseBsl = ({ browserstackLocal, capabilities: cap }) =>
browserstackLocal !== false &&
(Array.isArray(cap) ? cap.some(hasBsl) : hasBsl(cap))
module.exports = {
webdriverVersion,
async before (context) {
print = new Print(context.log)
try {
// Set the WebDriver version if not already configured.
context.webdriver.version = context.webdriver.version || webdriverVersion
print.debug('Using WebDriver version', context.webdriver.version)
if (context.webdriver.standalone) {
print.debug('Starting Selenium Standalone')
return new Promise((resolve, reject) => {
const standalone = require('selenium-standalone')
const spawnOptions = { stdio: 'inherit' }
const { version, drivers } = context.webdriver || {}
// Start the Selenium Standalone server.
standalone.start({ spawnOptions, version, drivers }, (err, child) => {
if (err) {
if (child) {
// If there was an error but a child process was still created,
// kill the child process.
child.kill()
}
reject(err)
} else {
// Assign the child process to the seleniumStandalone variable so
// that it can be killed later when the after hook runs.
seleniumStandalone = child
resolve()
}
})
})
} else if (shouldUseBsl(context.webdriver)) {
print.debug('Starting BrowserStack Local')
const { start } = require('@ianwalter/bsl')
// Start the BrowserStack Local tunnel.
await start(context.webdriver.browserstackLocal)
}
} catch (err) {
print.error(err)
}
},
registration (file, context) {
print = new Print(context.log)
try {
// Extract the WebDriver capabilities from the test configuration.
const capabilities = Array.isArray(context.webdriver.capabilities)
? context.webdriver.capabilities
: [context.webdriver.capabilities]
// Go through the browser tests and split them up by capability so that
// they can be run individually/in parallel.
context.augmentTests = tests => tests.reduce(
(acc, test) => acc.concat(capabilities.map(capability => {
let name = test.name
if (capabilities.length > 1) {
// Modify the test name to contain the name of the browser it's
// being tested in.
name = `${test.name} in ${capability.browserName}`
// Modify the test name to contain the version of the browser it's
// being tested in, if configured.
if (capability.browserVersion) {
name += ` ${capability.browserVersion}`
}
}
// Return the test with it's modified name and capability
// configuration.
return { ...test, name, capability }
})),
[]
)
} catch (err) {
print.error(err)
}
},
async beforeEach (file, context) {
print = new Print(context.log)
try {
print.debug('Adding WebDriver integrations')
const BrowserStackIntegration = require('./integrations/browserstack')
const ZaleniumIntegration = require('./integrations/zalenium')
const AppiumIntegration = require('./integrations/appium')
// Add enabled integrations to the integrations array so they can be used
// later.
context.webdriver.integrations = context.webdriver.integrations || []
BrowserStackIntegration.integrate(context)
ZaleniumIntegration.integrate(context)
AppiumIntegration.integrate(context)
// Go through each enabled integration and allow it to enahance the
// webdriver capability.
const enhanceCapability = i => i.enhanceCapability(context.testContext)
context.webdriver.integrations.forEach(enhanceCapability)
} catch (err) {
print.error(err)
}
try {
print.debug('Creating WebdriverIO browser instance')
// Set up the browser instance and add it to the test context.
const { remote } = require('webdriverio')
context.testContext.browser = await remote({
path: '/wd/hub',
...context.webdriver,
logLevel: context.webdriver.logLevel || context.log.level,
capabilities: context.testContext.capability
})
// Add the expect instance to the browser instance so that the user can
// more easily create commands that involve making assertions.
context.testContext.browser.expect = (...args) => (
context.testContext.expect(...args)
)
} catch (err) {
print.error(err)
}
},
async afterEach (file, context) {
try {
// Go through each enabled integration and report results to it, etc.
print.debug('Running WebDriver integration reporting')
const toReport = async integration => {
if (integration.report) {
integration.report(context)
}
}
await Promise.all(context.webdriver.integrations.map(toReport))
} catch (err) {
print.error(err)
}
try {
if (context.testContext.browser) {
// Tell Selenium to delete the browser session once the test is over.
print.debug('Terminating WebdriverIO browser instance')
await context.testContext.browser.deleteSession()
}
} catch (err) {
print.error(err)
}
},
async after (context) {
try {
if (seleniumStandalone) {
// Kill the Selenium Standalone child process.
print.write('\n')
print.log('👉', 'bff-webdriver: Stopping Selenium Standalone')
seleniumStandalone.kill()
// Run cleanup in case there are any orphaned processes hanging around.
if (context.err) {
print.write('\n')
print.log('👉', 'bff-webdriver: Running manual cleanup')
const cleanup = require('./cleanup')
await cleanup()
}
} else if (shouldUseBsl(context.webdriver)) {
// Stop the BrowserStack Local tunnel.
print.write('\n')
print.log('👉', 'bff-webdriver: Stopping BrowserStack Local')
const { stop } = require('@ianwalter/bsl')
await stop()
}
} catch (err) {
print.error(err)
}
}
}