我在做什么
编辑:我用我的问题的简化版本创建了一个repo,重现了这个问题。
我正在尝试使用browserstack、selenium-webdriver和tape设置自动化前端测试。
这个想法是定义多个浏览器和设备,它们必须通过 X 数量的给定测试一个接一个地进行测试。在下面的示例中,我在 OSX 上只定义了一个测试和两个浏览器。
为了只定义一次浏览器并处理测试,我创建了一个存储库test-runner
,该存储库应该添加dev-dependency
到需要在给定设备和浏览器上测试的存储库中。获得所有需要的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()
}
如果你想知道这个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 )
/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"
}
...
}
当我想运行我npm run test
在 my-repo 中执行的测试时
请记住,我们只有一个测试(但也可以是多个测试)并定义了两个浏览器,因此行为应该是:
- 启动浏览器 1 并导航(Chrome)
- 在浏览器 1 (Chrome) 上进行一项测试
- 关闭浏览器 1 (Chrome)
- 启动浏览器 2 并导航 (Safari)
- 在浏览器 2 (Safari) 上进行一项测试
- 关闭浏览器 2 (Safari)
- 完毕
问题
异步的东西似乎工作得很好,浏览器按预期一个接一个地启动。问题是,即使我打电话,第一个测试也没有完成,t.end()
而且我没有进行第二个测试(在 4 之后失败。)。
我试过的
我尝试使用t.pass()
并运行 CLI,NODE_ENV=development tape test/front/ | tap-spec
但没有帮助。我还注意到,当我没有resolve()
参加test.js
考试时,结果很好,但当然我没有参加下一个考试。
我还尝试像解决这个问题一样调整我的代码,但没有设法让它工作。
同时,我还在磁带 github 页面上打开了一个问题。
所以我希望这个问题阅读起来不会太痛苦,任何帮助都将不胜感激。