使用异步forEach循环磁带"测试退出但不结束"错误

Oli*_*ull 7 javascript loops asynchronous node.js node.js-tape

我在做什么

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

我正在尝试使用browserstack,selenium-webdriver磁带设置自动前端测试.

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

为了只定义浏览器一次并处理测试,我创建了一个repo test-runner,应该添加dev-dependency到需要在给定设备和浏览器上测试的repos.在test-runner得到通过所有需要测试,开始第一个浏览器,运行于该浏览器的测试,一旦所有测试完成后关闭浏览器quit()和浏览器旁边被再次启动和测试.

测试亚军

/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()

}
Run Code Online (Sandbox Code Playgroud)

如果你想知道这个asyncForEach功能是如何工作的,我从这里得到它.

我的回购

/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 )
Run Code Online (Sandbox Code Playgroud)

/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()

    } )

  })

}
Run Code Online (Sandbox Code Playgroud)

/package.json

{
  ...
  "scripts": {
    "test": "NODE_ENV=development node test/front/ | tap-spec",
    "travis": "NODE_ENV=travis node test/front/ | tap-spec"
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

当我想运行测试时,我npm run test在my-repo中执行

请记住,我们只有一个测试(但也可能是多个测试)和两个浏览器定义,所以行为应该是:

  1. 启动浏览器1并导航(Chrome)
  2. 浏览器1(Chrome)上的一项测试
  3. 关闭浏览器1(Chrome)
  4. 启动浏览器2并导航(Safari)
  5. 浏览器2(Safari)上的一个测试
  6. 关闭浏览器2(Safari)
  7. DONE

问题

异步的东西似乎工作正常,浏览器按预期一个接一个地启动.问题是,即使我打电话,第一次测试也没有完成t.end(),我没有进行第二次测试(4次后失败).

在此输入图像描述

我尝试了什么

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

我也试图调整我的代码,就像这个问题的解决方案,但没有设法让它工作.

同时我还在磁带github页面上打开了一个问题.

因此,我希望这个问题不是太难以阅读,任何帮助将不胜感激.

Oli*_*ull 1

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

我发现,只要任何其他进程正在运行,该tape()进程就不能运行。.end()就我而言,是的browser。所以只要浏览器运行,我想tape就不能结束。

在我的示例存储库中,没有browser,但其他东西必须仍在运行才能防止tape结束。

所以我只能在一个过程中定义测试tape。因为我设法按顺序打开浏览器并测试,所以现在完全没问题。

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

capabilities我还从 a导入浏览器dependency,以便仅定义它们一次。

所以这是代码:

依赖主文件

{
  "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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

测试.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()

} )
Run Code Online (Sandbox Code Playgroud)