2016-09-24 18:52:37 +02:00
|
|
|
/*
|
2017-01-06 19:11:18 +01:00
|
|
|
* Copyright (c) 2016-2017 Martin Donath <martin.donath@squidfunk.com>
|
2016-09-24 18:52:37 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-10-23 10:21:37 +02:00
|
|
|
import selenium from "selenium-standalone"
|
|
|
|
|
2016-09-24 18:52:37 +02:00
|
|
|
/* ----------------------------------------------------------------------------
|
2016-10-23 10:21:37 +02:00
|
|
|
* Locals
|
2016-09-24 18:52:37 +02:00
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
2016-10-23 10:21:37 +02:00
|
|
|
/* Selenium server */
|
|
|
|
let server = null
|
2016-09-24 18:52:37 +02:00
|
|
|
|
2016-10-23 10:21:37 +02:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Definition
|
|
|
|
* ------------------------------------------------------------------------- */
|
2016-09-24 18:52:37 +02:00
|
|
|
|
2017-02-01 23:56:57 +01:00
|
|
|
/**
|
|
|
|
* Start Selenium
|
|
|
|
*
|
|
|
|
* @param {Function} done - Resolve callback
|
|
|
|
*/
|
2016-10-23 10:21:37 +02:00
|
|
|
export const start = done => {
|
|
|
|
selenium.start({}, (err, proc) => {
|
2017-01-28 20:25:30 +01:00
|
|
|
|
|
|
|
/* Register signal handlers */
|
|
|
|
for (const signal of ["SIGTERM", "SIGINT", "exit"])
|
|
|
|
process.on(signal, stop)
|
2016-10-23 10:21:37 +02:00
|
|
|
if (err) {
|
2016-09-24 18:52:37 +02:00
|
|
|
|
2016-10-23 10:21:37 +02:00
|
|
|
/* Install selenium, if not present */
|
|
|
|
if (/^Missing(.*)chromedriver$/.test(err.message)) {
|
2017-01-28 20:25:30 +01:00
|
|
|
new Promise(resolve => {
|
|
|
|
selenium.install({}, resolve)
|
2016-10-23 10:21:37 +02:00
|
|
|
})
|
2016-09-24 18:52:37 +02:00
|
|
|
|
2017-01-28 20:25:30 +01:00
|
|
|
/* Start selenium again */
|
|
|
|
.then(() => {
|
|
|
|
selenium.start({}, (err_, proc_) => {
|
|
|
|
server = proc_
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-10-23 10:21:37 +02:00
|
|
|
/* Otherwise, throw error */
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2016-09-24 18:52:37 +02:00
|
|
|
|
2016-10-23 10:21:37 +02:00
|
|
|
/* Remember process handle */
|
2017-01-28 20:25:30 +01:00
|
|
|
server = proc
|
2016-10-23 10:21:37 +02:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
}
|
2016-09-24 18:52:37 +02:00
|
|
|
|
2017-02-01 23:56:57 +01:00
|
|
|
/**
|
|
|
|
* Stop Selenium
|
|
|
|
*
|
|
|
|
* @param {Function} done - Resolve callback
|
|
|
|
*/
|
2017-01-28 20:25:30 +01:00
|
|
|
export const stop = done => {
|
|
|
|
if (server) {
|
|
|
|
if (typeof done === "function")
|
|
|
|
server.on("exit", done)
|
2016-10-23 10:21:37 +02:00
|
|
|
server.kill()
|
2017-01-28 20:25:30 +01:00
|
|
|
server = null
|
|
|
|
}
|
|
|
|
}
|