繁体   English   中英

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

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

这实际上不是问题,但我不完全理解发生了什么以及为什么。

我有这个跑步者用于我的测试。 我测试了一个 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)
  })

在 package.json 我有这个命令来运行测试

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

但是在本地环境下,我用这个命令运行了一个测试

sudo REDIS_HOST=127.0.0.1 PORT=80 yarn test:e2e

那是因为在我的本地机器上我有不同的配置,我不想为其他人更改它。

通常,测试在不同的、清晰的浏览器版本中运行,没有任何帐户数据、插件等。但在这种情况下,测试在新的浏览器窗口中运行,但带有所有插件和我的帐户名。 但是,它没有来自浏览器窗口的 cookie 和会话身份验证数据,我通常在其中工作(因为我在工作浏览器中进行了现场授权,而没有在测试浏览器中进行身份验证)。

如果我将“Chrome”更改为“chrome”,它会完全停止运行。 Firefox 和 Safari 的行为相同。

早些时候,没有传递REDIS_HOSTHOST ,它像往常一样工作并在一个干净的新浏览器窗口中运行。

至少就目前而言,这不是一个大问题,但这是意外行为,我不明白为什么会这样。

我对 Node 和 React 不是很熟悉,也许这与它们有关。

规格:macOS 10.12.5、Testcafe 0.20.3、Chrome 67

使用{ path, cmd } 指定浏览器是一个传统的低级选项,您不应该使用它。 当以这种方式指定浏览器时,TestCafe 不会尝试猜测浏览器的类型(Chrome、Firefox),也不会执行诸如创建干净配置文件之类的高级初始化步骤(因为配置文件结构取决于浏览器的类型)。 所以最好使用以下运行器代码:

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

暂无
暂无

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

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