我将nightwatch与browserstack集成在一起。当我运行一个实例时,它运行正常。但是当第一个实例正在工作并且有人试图同时运行第二个测试时(在不同的浏览器上或者只是从项目的另一个分支)第一个实例得到错误“无法连接到页面”,所以可能是第一个selenium服务器关闭连接。这有什么办法吗? Ofc我在browserstack中有两个并行测试空间。 CONFIGS: nightwatch.conf.js
const chromedriver = require('chromedriver');
require('nightwatch-cucumber')({
cucumberArgs: [
'--require', './tests/step_definitions/hooks.js',
'--require', './tests/step_definitions',
'--format', 'json:reports/cucumber.json',
'--format-options', '{"colorsEnabled":true}',
'./tests/features'
]
});
module.exports = {
output_folder: 'reports',
custom_assertions_path: 'tests/assertions',
custom_commands_path: "./node_modules/nightwatch-commands/commands",
live_output: true,
disable_colors: false,
test_settings: {
default: {
launch_url: 'http://pptm_nightwatch:3000',
selenium_host: 'pptm_hub',
desiredCapabilities: {
browserName: 'chrome',
javascriptEnabled: true,
acceptSslCerts: true,
loggingPrefs: {
'browser': 'ALL'
}
},
selenium: {
cli_args: {
'webdriver.chrome.driver': chromedriver.path
}
},
},
chrome: {
desiredCapabilities: {
browserName: 'chrome',
javascriptEnabled: true,
acceptSslCerts: true,
loggingPrefs: {
'browser': 'ALL'
}
},
selenium: {
cli_args: {
'webdriver.chrome.driver': chromedriver.path
}
}
},
}
};
nightwatch.browserstack.conf.js
const defaultConfig = require('./nightwatch.conf');
const browserstackConfig = {
selenium: {
start_process: false,
host: 'hub-cloud.browserstack.com',
port: 80,
},
test_settings: {
default: {
desiredCapabilities: {
'browserstack.user': "bla",
'browserstack.key': "bla",
'browserstack.local': true,
},
globals: defaultConfig.test_settings.default.globals,
},
edge: {
desiredCapabilities: {
browser: 'edge',
},
},
chrome: {
desiredCapabilities: {
browserName: 'chrome',
javascriptEnabled: true,
acceptSslCerts: true,
loggingPrefs: {
'browser': 'ALL'
}
},
},
},
};
const nightwatchConfig = Object.assign({}, defaultConfig, browserstackConfig);
Object.keys(nightwatchConfig.test_settings).forEach((key) => {
const config = nightwatchConfig.test_settings[key];
config.selenium_host = nightwatchConfig.selenium.host;
config.selenium_port = nightwatchConfig.selenium.port;
config.desiredCapabilities = Object.assign(
{},
nightwatchConfig.test_settings.default.desiredCapabilities,
config.desiredCapabilities,
);
});
module.exports = nightwatchConfig;
server.js:
var _ = require('lodash'),
fs = require('fs'),
path = require('path'),
express = require('express'),
cons = require('consolidate'),
bodyParser = require('body-parser');
var PORT = 3000;
function runServer(done) {
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.engine('html', cons.lodash);
app.set('views', path.join(__dirname, './templates'));
app.set('view engine', 'html');
app.get('/page/:pageId', function(req, res){
res.status(200).render(req.params.pageId, TEMPLATE_OPTIONS);
});
app.post('/form-endpoint', function (req, res) {
res.status(200).send('<body>POST(' + JSON.stringify(req.body)+')</body>');
});
var server = app.listen(PORT, function () {
done()
});
return server;
}
module.exports = runServer;
if (require.main === module) {
runServer(function () {
console.log("Starting server on port " + PORT + ". Please visit e.g. http://localhost:" + PORT + "/page/formSimple.html");
});
}
答案 0 :(得分:0)
这表示当您的测试仍在执行时,BrowserStackLocal二进制文件正在断开连接。似乎问题是由于BrowserStackLocal二元生成多个具有相同修饰符的生成。让我用示例场景解释一下。
按顺序运行测试时,不会影响任何测试,因为上述情况不会出现。
为避免这种情况,请确保在启动BrowserStackLocal时使用 localIdentifer 参数。
如果您使用命令行启动二进制文件,请使用以下命令:
的Mac:
./BrowserStackLocal --key ACCESS_KEY --local-identifier RandomString
视窗:
BrowserStackLocal.exe --key ACCESS_KEY --local-identifier RandomString
在测试脚本中,请添加以下功能 的 'browserstack.localIdentifier': 'RandomString'强>
这应该使您的desiredCapabilities块看起来像这样:
desiredCapabilities: {
'browserstack.user': "bla",
'browserstack.key': "bla",
'browserstack.local': true,
'browserstack.localIdentifier':'RandomString'
}
此 RandomString 对于每个测试工程师应该是唯一的,以确保他们的测试连接不会重叠或影响其他工程师的连接。