我是Protractor的新手。我将测试数据存储在excel中的多行中。我想为excel中的每一行多次运行相同的规范。有可能吗?
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
baseUrl: 'https:somewebsite.com',
capabilities: {
'browserName': (process.env.TEST_BROWSER_NAME || workbook.Sheets[sheetNamelist[sheetNumber]]['N2'].v)
, 'version': (process.env.TEST_BROWSER_VERSION || 'ANY')
, 'shardTestFiles': false
, },
onPrepare: function () {
browser.ignoreSynchronization = true;
},
framework: 'custom'
, frameworkPath: require.resolve('protractor-cucumber-framework'),
specs: [
'../Features/Availity_Login.feature'
]
, exclude: '../Features/database.feature'
, cucumberOpts: {
monochrome: true
, strict: true
, plugin: ["pretty"]
, require: ['../StepDefinitions/*.js', '../Support/*.js']
, tags: '@AllureScenario,@Regression,@ProtractorScenario,~@DatabaseTest' // @DatabaseTest scenario can be included when the username & password of DB have been configured in Support/database.js
}
};
在上面的脚本中我想多次运行规范,因为我在excel中有多个测试数据。我可以使用模块' xlsjs'来读取excel值。循环使用规范只会在第一时间运行规范。
答案 0 :(得分:3)
使用多组数据测试相同的功能只不过是 数据驱动 方法。为此我们有 jasmine-data-provider 包,它将帮助您使用Protractor进行数据驱动测试。
Code Snippet:
var using = require(‘jasmine-data-provider);
var loginData = require('../example/Test Data/Test.json');
describe('Data driven test spec', function () {
/*define sets of input data as array in method called arrayOfData*/
//OR retrieve all test data and stored into array and the follow below
//approach
function arrayOfData() {
return [
{
"username": "admin",
"passwordField": "admin"
},
{
"username": "admin1",
"passwordField": "admin2"
}
]
} /*below one will loop the test case based on data size and pass single
data set every time till complete the end of array*/
using(arrayofData, function (inputData) {
it('test case logic to be executed for each set of data', function ()
{
browser.get("http://127.0.0.1:8080/#/login");
element(by.model("username")).sendKeys(inputData.username);
element(by.model("password")).sendKeys(inputData.passwordField);
element(by.buttonText("Authenticate")).click();
});
});
});
注意:如果您的计算机上尚未安装jasmine-data-provider软件包,请在运行测试脚本之前运行以下命令进行安装。
npm install jasmine-data-provider
答案 1 :(得分:0)
您似乎已在使用protractor-cucumber-framework
。 Cucumber为您提供了对多个数据运行相同测试的方法。您不需要增加从excel读取数据的复杂性。重用框架提供的功能。例如,在.feature文件中,您可以执行以下操作。因此,此方案将作为单独的测试运行每一行
Scenario Outline:
Given I have "<firstNumber>" and "<secondNumber>"
When I add them together
Then the total is "<sum>"
Scenarios:
| firstNumber | secondNumber | sum |
| 5 | 7 | 12 |
| -5 | 7 | 2 |
| 5 | -7 | -2 |