简体   繁体   English

在命令中传递 .env 变量以进行运行测试时的不同浏览器行为

[英]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.我测试了一个 React 应用程序。

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在 package.json 我有这个命令来运行测试

"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).但是,它没有来自浏览器窗口的 cookie 和会话身份验证数据,我通常在其中工作(因为我在工作浏览器中进行了现场授权,而没有在测试浏览器中进行身份验证)。

And if I change "Chrome" to "chrome" it stops run completely.如果我将“Chrome”更改为“chrome”,它会完全停止运行。 Same behavior for Firefox and Safari. Firefox 和 Safari 的行为相同。

Earlier, without passing REDIS_HOST and HOST , it works as usual and runs in a clean new browser window.早些时候,没有传递REDIS_HOSTHOST ,它像往常一样工作并在一个干净的新浏览器窗口中运行。

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.我对 Node 和 React 不是很熟悉,也许这与它们有关。

Spec: macOS 10.12.5, Testcafe 0.20.3, Chrome 67规格: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.使用{ path, cmd } 指定浏览器是一个传统的低级选项,您不应该使用它。 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).当以这种方式指定浏览器时,TestCafe 不会尝试猜测浏览器的类型(Chrome、Firefox),也不会执行诸如创建干净配置文件之类的高级初始化步骤(因为配置文件结构取决于浏览器的类型)。 So it's better to use the following runner code:所以最好使用以下运行器代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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