想知道是否有可能使用量角器+ jasmine进行数据驱动测试以并行执行它。
我有以下内容: storeList.json - 一个带有输入参数的数组,用于测试单个商店。我们有大约40家商店 - 记录。
[
{
"storeId": "Store_ID_1",
"storeController": "Store_Controller_1"
},
{
"storeId": "Store_ID_2",
"storeController": "Store_Controller_2"
}
]
ordering.js - 代码(量角器),它从上面的json获取每个元素并将其作为单独的测试执行。
describe('Ordering', function () {
all(require('../../assets/data/storeList'), (storeData) => {
it(`Add all items with all modifiers to cart and checkout on ${storeData.storeId}`, async function () {
let user = await userGenerator.Registered(storeData);
await shoppingCartActions.fillCart(storeData,user);
await homePageActions.openCart();
await shoppingCartActions.validateCartMath();
await shoppingCartActions.proceedToCheckout();
await recommendedActions.continueToDeliveryOptions();
await deliveryAndTipsActions.pickupAnd15PercentTip();
await deliveryAndTipsActions.validateCartMath();
await deliveryAndTipsActions.continueToAddressConfirmation();
await pickupAddressConfirmationActions.continueToPaymentMethod();
await paymentActions.fillCardData(storeData);
await paymentActions.pay();
});
});});
all.js - 使orders.js有点数据驱动的片段
module.exports = (data, test) => {
const rows = Array.isArray(data) ? data : [data];
rows.forEach((row, index) => {
test(row, index + 1)
})};
config.js
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
baseUrl : 'localhost',
restartBrowserBetweenTests: true,
maxSessions: 3,
multiCapabilities: [{
'browserName': 'chrome'
}, {
'browserName': 'chrome'
}, {
'browserName': 'chrome'
}],
specs: ['./testsuite/ordering/*.js'],
allScriptsTimeout: 20000,
framework: 'jasmine2',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 600000,
isVerbose: true,
},
onPrepare: () => {
global.all = require('./all');
}};
使用这个设置我得到3个chrome实例,每个执行orders.js测试,其中包含来自storeList的所有可用数据输入。所以我执行了6次测试,但实际上我愿意并行执行2次测试。
我尝试过的另一个选择是为数据输入提供多个json,以及排序js的副本,但这似乎是一种不好的做法。
有人能指出正确的方向,并行执行量角器数据驱动测试。
答案 0 :(得分:1)
你需要知道量角器与spec文件并行运行,而不是it
块跨所有spec文件。因此,解决方案是为数据行创建许多spec文件(每个spec文件每行)。
//量角器conf.js
var fs = require('fs');
var util = require('util');
var uuidv1 = require('uuid/v1');
function splitData2Sepc(datarows) {
datarows.forEach(function(row) {
let id = uuidv1();
let dataFilePath = './data/data_'+id + '.json';
let specFilePath = './specs/spec_parallel_'+id + '.js';
// read the spec template
let templatePath = './specs/template_01.js';
let template = fs.readFileSync(templatePath, 'utf8');
// generate data file for per row
fs.writeFileSync(dataFilePath,JSON.stringify(row));
// generate spec file for per row
let content = util.format('var row = require("%s");', dataFilePath);
fs.writeFileSync(specFilePath,[content, template].join('\n\n'));
});
}
var datarows = require('./data/all.js');
splitData2Sepc(datarows);
exports.config = {
capabilities: {
browserName: 'chrome',
shardTestFiles: true,
maxInstances: 3,
},
specs: [
'./specs/spec_parallel*.js'
]
};
// specs / template_01.js
describe('', function(row){
it(`Add all items with all modifiers to cart and checkout on ${row.storeId}`, async function () {
let user = await userGenerator.Registered(row);
await shoppingCartActions.fillCart(row.user);
...
});
});
// data / all.js
module.exports = [
{storeId: 1, user: 'user_01', ...},
{storeId: 2, user: 'user_02', ...},
...
];