磁带 "test exited without ending" 异步 forEach 循环错误

Tape "test exited without ending" error with asynchronous forEach loops

我在做什么

编辑:我创建了一个 repo,其中包含重现问题的简化版本。

我正在尝试使用 browserstack, selenium-webdriver and tape 设置自动化前端测试。

这个想法是定义多个浏览器和设备,这些浏览器和设备必须用 X 数量的给定测试一个接一个地进行测试。在下面的示例中,我只在 OSX.

上定义了一个测试和两个浏览器

为了仅定义一次浏览器并处理测试,我创建了一个回购协议 test-runner,应将其作为 dev-dependency 添加到需要在给定设备和浏览器上进行测试的回购协议。 test-runner 通过所有需要的测试,启动第一个浏览器,运行 在该浏览器上进行测试,完成所有测试后浏览器关闭 quit() 并启动下一个浏览器,再次测试。

测试-运行ner

/index.js

const webdriver = require( 'selenium-webdriver' )

// ---
// default browser configs
// ---
const defaults = {
  "os" : "OS X",
  "os_version" : "Mojave",
  "resolution" : "1024x768",
  "browserstack.user" : "username",
  "browserstack.key" : "key",
  "browserstack.console": "errors",
  "browserstack.local" : "true",
  "project" : "element"
}

// ---
// browsers to test
// ---
const browsers = [
  {
    "browserName" : "Chrome",
    "browser_version" : "41.0"
  },
  {
    "browserName" : "Safari",
    "browser_version" : "10.0",
    "os_version" : "Sierra"
  }
]

module.exports = ( tests, url ) => {

  // ---
  // Asynchronous forEach loop
  // helper function
  // ---
  async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
      await callback(array[index], index, array)
    }
  }

  // ---
  // runner
  // ---
  const run = async () => {

    // ---
    // Iterate through all browsers and run the tests on them
    // ---
    await asyncForEach( browsers, async ( b ) => {

      // ---
      // Merge default configs with current browser
      // ---
      const capabilities = Object.assign( {}, defaults, b )

      // ---
      // Start and connect to remote browser
      // ---
      console.info( '-- Starting remote browser hang on --', capabilities.browserName )
      const browser = await new webdriver.Builder().
        usingServer( 'http://hub-cloud.browserstack.com/wd/hub' ).
        withCapabilities( capabilities ).
        build()

      // ---
      // Navigate to page which needs to be checked (url)
      // ---
      console.log('-- Navigate to URL --')
      await browser.get( url )

      // ---
      // Run the tests asynchronously
      // ---
      console.log( '-- Run tests --- ' )
      await asyncForEach( tests, async ( test ) => {
        await test( browser, url, capabilities, webdriver )
      } )

      // ---
      // Quit the remote browser when all tests for this browser are done
      // and move on to next browser
      // Important: if the browser is quit before the tests are done
      // the test will throw an error beacause there is no connection
      //  anymore to the browser session
      // ---
      browser.quit()

    } )

  }

  // ---
  // Start the tests
  // ---
  run()

}

如果您想知道这个 asyncForEach 函数是如何工作的,我是从 here 那里得到的。

我的仓库

/test/front/index.js

const testRunner = require( 'test-runner' )
const url = ( process.env.NODE_ENV == 'development' ) ? 'http://localhost:8888/element/...' : 'https://staging-url/element/...'

// tests to run
const tests = [
  require('./test.js')
]

testRunner( tests, url )

/test/front/test.js

const tape = require( 'tape' )

module.exports = async ( browser, url, capabilities, driver ) => {

  return new Promise( resolve => {

    tape( `Frontend test ${capabilities.browserName} ${capabilities.browser_version}`, async ( t ) => {

      const myButton = await browser.wait( driver.until.elementLocated( driver.By.css( 'my-button:first-of-type' ) ) )

      myButton.click()

      const marked = await myButton.getAttribute( 'marked' )
      t.ok(marked == "true", 'Button marked')

      //---
      // Test should end now
      //---
      t.end()

      resolve()

    } )

  })

}

/package.json

{
  ...
  "scripts": {
    "test": "NODE_ENV=development node test/front/ | tap-spec",
    "travis": "NODE_ENV=travis node test/front/ | tap-spec"
  }
  ...
}

当我想要 运行 我在 my-reponpm run test 中执行的测试时

请记住,我们只有一个测试(但也可以是多个测试)并且定义了两个浏览器,因此行为应该是:

  1. 启动浏览器 1 并导航(Chrome)
  2. 在浏览器 1 上进行一次测试 (Chrome)
  3. 关闭浏览器 1 (Chrome)
  4. 启动浏览器 2 并导航 (Safari)
  5. 在浏览器 2 (Safari) 上进行一次测试
  6. 关闭浏览器 2 (Safari)
  7. 完成

问题

异步的东西似乎工作得很好,浏览器按预期一个接一个地启动。 问题是,即使我调用 t.end(),第一个测试也没有完成,而且我没有进行第二个测试(在 4 之后立即失败。).

我试过的

我尝试使用 t.pass() 并 运行 将 CLI 与 NODE_ENV=development tape test/front/ | tap-spec 结合使用,但没有帮助。 我还注意到,当我在 test.js 中不 resolve() 时,测试结束得很好,但当然我不会进入下一个测试。

我也尝试像 this issue 中的解决方案一样调整我的代码,但没能成功。

同时我也打开了一个 issue on tapes github 页面。

所以我希望这个问题读起来不会太痛苦,我们将不胜感激任何帮助。

似乎 tape 不能很好地处理异步代码。在 Github 问题页面上查看这些讨论:

https://github.com/substack/tape/issues/223
https://github.com/substack/tape/issues/160

解决方案似乎是在开始时用 tape.add 声明您的测试,然后再调用任何异步代码。

如果您只是按顺序打开浏览器,我还会尝试重构一些可能不需要的异步代码。

我会首先尝试简化测试的编写和执行方式:

  1. 您是否尝试过 运行 使用磁带二进制文件进行测试?例如tape test/front/test.js
  2. 同时简化 test/front/test/js(您必须弄清楚如何以其他方式传递参数;也许您可以仅出于调试目的对它们进行硬编码? )
const tape = require( 'tape' )

tape( `your test outline`, ( t ) => {

  const alwaysEnd = () => t.end();

  new Promise((resolve, reject) => {
    // your async stuff here...
    // resolve() or reject() at the end
  }).then(alwaysEnd, alwaysEnd);
})

所以不幸的是,我还没有得到现有设置的答案,并设法让事情以稍微不同的方式工作。

我发现,tape() 个进程不能 .end() 只要任何其他进程是 运行。在我的例子中是 browser。所以只要浏览器运行,我觉得tape就可以结束。

在我的 example repo 中没有 browser 但其他东西必须仍然 运行 以防止 tape 结束。

所以我不得不在一个 tape 过程中定义测试。由于我设法按顺序打开浏览器并测试它现在完全没问题。

如果有很多不同的东西要测试,我会把这些东西分成不同的文件,然后将它们导入到主测试文件中。

我还从 dependency 导入浏览器 capabilities,以便只定义一次。

代码如下:

依赖主文件

{
  "browsers": [{
      "browserName": "Chrome",
      "browser_version": "41",
      "os": "Windows",
      "os_version": "10",
      "resolution": "1024x768",
      "browserstack.user": "username",
      "browserstack.key": "key"
    },
    }
      "browserName": "Safari",
      "browser_version": "10.0",
      "os": "OS X",
      "os_version": "Sierra",
      "resolution": "1024x768",
      "browserstack.user": "username",
      "browserstack.key": "key"
    }
  ]
}

test.js

const tape = require( "tape" )
const { Builder, By, until } = require( 'selenium-webdriver' );
const { browsers } = require( "dependency" )
const browserStack = 'http://hub-cloud.browserstack.com/wd/hub'

tape( "Browsers", async ( t ) => {

  await Promise.all( browsers.map( async ( capa ) => {

    const { browserName, browser_version, os } = capa

    const browser = new Builder().usingServer( browserStack ).withCapabilities( capa ).build();

    await browser.get( 'http://someurl.com' )

    const myButton = await browser.wait( until.elementLocated( By.css( 'my-button:first-of-type' ) ) )

    myButton.click()

    const marked = await myButton.getAttribute( 'marked' )

    t.ok(marked == "true", `${browserName} ${browser_version} ${os}`)

    await browser.quit()

  } ) )

  t.end()

} )