OnetapInc / chromy

Chromy is a library for operating headless chrome. 🍺🍺🍺
MIT License
606 stars 41 forks source link

Chrome instance remaining open and crashing on next start #96

Closed cupcakearmy closed 5 years ago

cupcakearmy commented 6 years ago

Bug

If I start Chromy and the Mocha test somehow crashes, Chromy will not close the the browser and if i run the test again, aka firing up a new Chromy instance I get: Error: Failed to launch a browser.

This is on macOS 10.13.2 with Chrome Version 63.0.3239.132 (Official Build) (64-bit)

Details

I get this error whenever the Chrome instance fails to close after a test.

Error: Failed to launch a browser.
      at Chromy.start$ (node_modules/chromy/dist/index.js:184:21)
      at tryCatch (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at Generator.invoke [as _invoke] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:114:21)
      at tryCatch (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
      at invoke (node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:152:20)
      at node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:162:13
      at <anonymous>

This is my Mocha setup:

beforeEach(async() => {
    chromy = await new Chromy({
        chromeFlags: ['--window-size=1920,1080']
    })
})

afterEach(async() => {
    // Delete the session
    await chromy.goto(S.url.logout)

    await chromy.close()
})

after(Chromy.cleanup)

The solution i'm using right now is to call kill $(pgrep -f "Chrome.*--headless.*--window-size=1920,1080") before every test, but that seems very hacky.

// Current Fix
before(() => {
    pid = spawnSync('pgrep', ['-f', '"Chrome.*--headless.*--window-size=1920,1080"'], {
        shell: true
    }).stdout.toString()
    if (pid !== '')
        kill = spawnSync('kill', [pid], {
            shell: true
        })
})

I can send more details if needed.