简体   繁体   中英

Different browser behavior when pass .env variables in command for run tests

It's not actually a problem, but I do not fully understand, what happened and why.

I have this runner for my test. I test a React app.

let testcafe = null
const isCiEnv = process.env.CI === 'true'

const exit = async err => {
  console.log('Exiting...')
  if (testcafe) {
    console.log('Closing TestCafe...')
    testcafe.close()
  }
  console.log('Exiting process...')
  process.exit(err ? 1 : 0)
}

console.log('Is CI ENV: ', isCiEnv)
console.log('Creating TestCafe...')

createTestCafe('localhost', 1337, 1338)
  .then(tc => {
    testcafe = tc
  })
  .then(() => {
    console.log('Starting server...')
    return startServer()
  })
  .then(() => {
    console.log('Starting client...')
    return startClient()
  })
  .then(() => {
    console.log('Creating TestCafe Runner...')
    return testcafe.createRunner()
  })
  .then(runner => {
    console.log('About to start TestCafe Runner...')
    return runner
      .src([
        'test/e2e/fixtures/auth.js'
      ])
      .browsers({
        path: isCiEnv
          ? '/usr/bin/chromium-browser'
          : 'Chrome',
        cmd: isCiEnv
          ? '--no-sandbox --disable-gpu'
          : undefined
      })
      .screenshots('screenshots', true)
      .run({
        skipJsErrors: true,
        selectorTimeout: 25000,
        assertionTimeout: 25000
      })
  })
  .then(failedCount => {
    console.log('failed count:', failedCount)
    return exit(failedCount)
  })
  .catch(err => {
    console.error('ERR', err)
    return exit(err)
  })

In package.json i have this command for run test

"test:e2e": "HOST=0.0.0.0 NODE_ENV=test NODE_PATH=server babel-node test/e2e/index.js --presets stage-2"

But in the local environment, I run a test with this command

sudo REDIS_HOST=127.0.0.1 PORT=80 yarn test:e2e

That because on my local machine I have different config and I don't want to change it for everyone else.

Usually, test runs in a different, clear version of the browser, without any account data, plugins and etc. But in this case, tests run in a new browser window, but with all plugins and my account name. But, it's doesn't have cookie and session auth data from the browser window, in which I usually work (because I authorized on-site in the working browser and doesn't auth in test browser).

And if I change "Chrome" to "chrome" it stops run completely. Same behavior for Firefox and Safari.

Earlier, without passing REDIS_HOST and HOST , it works as usual and runs in a clean new browser window.

It's not a big problem, for now at least, but it's unexpected behavior and I don't understand, why it works this way.

I'm not very familiar with Node and React, and maybe this related to them.

Spec: macOS 10.12.5, Testcafe 0.20.3, Chrome 67

Specifying browsers using { path, cmd } is a legacy low-level option, you shouldn't use it. When a browser is specified in this way, TestCafe doesn't try to guess browser's type (Chrome, Firefox) and doesn't perform advanced initialization steps like creating a clean profile (because profile structure depends on browser's type). So it's better to use the following runner code:

.browsers(isCiEnv ? 'chromium --no-sandbox --disable-gpu' : 'chrome') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM