Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Disable concurrency for tests * Add browser instance sharing * Bump concurrency and install chromedriver smoother * Fix config typo * Fix local test * Limit azure concurrency again * Add delay for empty-object-test * Make sure to use open port for chromedriver * Move browser closing to jest-environment * Re-enable concurrency for Azure * bump * Disable concurrency for Azure again * Re-enable concurrency for Azure * bump
- Loading branch information
Showing
9 changed files
with
78 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,9 @@ | ||
let globalTeardown = () => {} | ||
const browser = global.bsBrowser | ||
|
||
if (process.env.BROWSERSTACK) { | ||
globalTeardown = () => global.browserStackLocal.killAllProcesses(() => {}) | ||
} | ||
|
||
module.exports = async () => { | ||
if (browser) { | ||
// Close all remaining browser windows | ||
try { | ||
const windows = await browser.windowHandles() | ||
for (const window of windows) { | ||
if (!window) continue | ||
await browser.window(window) | ||
await browser.origClose() | ||
await browser.quit() | ||
await globalTeardown() | ||
} | ||
} catch (_) {} | ||
} else { | ||
await globalTeardown() | ||
} | ||
await globalTeardown() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1 @@ | ||
import wd from 'wd' | ||
import getPort from 'get-port' | ||
import waitPort from 'wait-port' | ||
|
||
const doHeadless = process.env.HEADLESS !== 'false' | ||
let driverPort = 9515 | ||
|
||
let webdriver = async function(appPort, pathname) { | ||
if (typeof appPort === 'undefined') { | ||
throw new Error('appPort is undefined') | ||
} | ||
|
||
const url = `http://localhost:${appPort}${pathname}` | ||
console.log(`> Start loading browser with url: ${url}`) | ||
|
||
// Sometimes browser won't initialize due to some random issues. | ||
// So, we need to timeout the initialization and retry again. | ||
for (let lc = 0; lc < 5; lc++) { | ||
try { | ||
const browser = await getBrowser(url, 5000) | ||
console.log(`> Complete loading browser with url: ${url}`) | ||
return browser | ||
} catch (ex) { | ||
console.warn(`> Error when loading browser with url: ${url}`) | ||
|
||
// Try restarting chromedriver max twice | ||
if (lc < 2) { | ||
const chromedriver = require('chromedriver') | ||
console.log('Trying to restart chromedriver with random port') | ||
driverPort = await getPort() | ||
chromedriver.stop() | ||
chromedriver.start([`--port=${driverPort}`]) | ||
// https://github.com/giggio/node-chromedriver/issues/117 | ||
await waitPort({ | ||
port: driverPort, | ||
timeout: 1000 * 30, // 30 seconds | ||
}) | ||
continue | ||
} | ||
|
||
if (ex.message === 'TIMEOUT') continue | ||
throw ex | ||
} | ||
} | ||
|
||
console.error(`> Tried 5 times. Cannot load the browser for url: ${url}`) | ||
throw new Error(`Couldn't start the browser for url: ${url}`) | ||
} | ||
|
||
function getBrowser(url, timeout) { | ||
const browser = wd.promiseChainRemote(`http://localhost:${driverPort}/`) | ||
|
||
return new Promise((resolve, reject) => { | ||
let timeouted = false | ||
const timeoutHandler = setTimeout(() => { | ||
timeouted = true | ||
const error = new Error('TIMEOUT') | ||
reject(error) | ||
}, timeout) | ||
|
||
browser | ||
.init({ | ||
browserName: 'chrome', | ||
...(doHeadless | ||
? { | ||
chromeOptions: { args: ['--headless'] }, | ||
} | ||
: {}), | ||
}) | ||
.get(url, err => { | ||
if (timeouted) { | ||
try { | ||
browser.close(() => { | ||
// Ignore errors | ||
}) | ||
} catch (err) { | ||
// Ignore | ||
} | ||
return | ||
} | ||
|
||
clearTimeout(timeoutHandler) | ||
|
||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
|
||
resolve(browser) | ||
}) | ||
}) | ||
} | ||
|
||
if (global.isBrowserStack) { | ||
webdriver = (...args) => global.bsWd(...args) | ||
} | ||
export default webdriver | ||
export default global.sharedWD |