量角器中的数据驱动测试
Data-Driven Testing in Protractor
我是量角器的新手。谁能指导我使用量角器进行数据驱动测试。下面是代码、配置文件和 testdata.json 文件。
'use strict';
var testData = require('../example/Test Data/Test.json');
describe('LoginPage', function() {
var loginData = require('../example/Test Data/Test.json');
testData.forEach(function (data) {
it("data.description", function (data) {
browser.get("http://127.0.0.1:8080/#/login");
element(by.model("username")).sendKeys(data.username);
element(by.model("password")).sendKeys(data.passwordField);
element(by.buttonText("Authenticate")).click();
});
});
});
配置文件:
// An example configuration file.
exports.config = {
directConnect: true,
//seleniumAddress: 'http://localhost:4444/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['Testpage.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
};
Json 文件:
[
{
"username": "admin",
"passwordField": "admin"
},
{
"username": "admin1",
"passwordField": "admin2"
}
]
问题是它没有获取数据,而是在所有输入框中写入未定义。请帮忙
我假设它是一个对象数组,你可以迭代每个数组元素并直接访问它的内容,你不需要testdata.forEach()
,你可以试试像这样 -
'use strict';
var testData = require('../example/Test Data/Test.json');
describe('LoginPage', function() {
it("data.description", function () {
browser.get("http://127.0.0.1:8080/#/login");
element(by.model("username")).sendKeys(testData[0].username);
element(by.model("password")).sendKeys(testData[0].passwordField);
element(by.buttonText("Authenticate")).click();
});
});
});
我没有测试过上面的代码,你应该使用页面对象而不是直接在你的测试中使用它们!
我们有 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*/
function arrayOfData() {
return [
{
"username": "admin",
"passwordField": "admin"
},
{
"username": "admin1",
"passwordField": "admin2"
}
]
//or return loginData json object here
} /*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 包,请在转到 [=18= 之前通过下面的命令 运行 安装它] 测试脚本。
npm install jasmine-data-provider
使用 map 函数的更简单方法:
var testParams = testConfig.testArray;
testParams.map(function(testdata) {
it('write your test here', function() {
console.log('Username: ', testData.username);
});
});
我觉得你的做法很合理。您未定义的原因是因为您将数据放在 'done' 参数中。它将数据设置为 'done' 对象,当 'it' 函数调用您正在定义的函数时传递该对象。
testData.forEach(function (data) {
it("data.description", function (data) {
应该是
testData.forEach(function (data) {
it("data.description", function () {
第二个答案比较突出。只是在这里补充一点,如果您想从 Excel sheet 读取数据,然后执行数据驱动测试,那么这个视频非常有用:
https://www.youtube.com/watch?v=vzvC4dYE84Q
我是量角器的新手。谁能指导我使用量角器进行数据驱动测试。下面是代码、配置文件和 testdata.json 文件。
'use strict';
var testData = require('../example/Test Data/Test.json');
describe('LoginPage', function() {
var loginData = require('../example/Test Data/Test.json');
testData.forEach(function (data) {
it("data.description", function (data) {
browser.get("http://127.0.0.1:8080/#/login");
element(by.model("username")).sendKeys(data.username);
element(by.model("password")).sendKeys(data.passwordField);
element(by.buttonText("Authenticate")).click();
});
});
});
配置文件:
// An example configuration file.
exports.config = {
directConnect: true,
//seleniumAddress: 'http://localhost:4444/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['Testpage.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
};
Json 文件:
[
{
"username": "admin",
"passwordField": "admin"
},
{
"username": "admin1",
"passwordField": "admin2"
}
]
问题是它没有获取数据,而是在所有输入框中写入未定义。请帮忙
我假设它是一个对象数组,你可以迭代每个数组元素并直接访问它的内容,你不需要testdata.forEach()
,你可以试试像这样 -
'use strict';
var testData = require('../example/Test Data/Test.json');
describe('LoginPage', function() {
it("data.description", function () {
browser.get("http://127.0.0.1:8080/#/login");
element(by.model("username")).sendKeys(testData[0].username);
element(by.model("password")).sendKeys(testData[0].passwordField);
element(by.buttonText("Authenticate")).click();
});
});
});
我没有测试过上面的代码,你应该使用页面对象而不是直接在你的测试中使用它们!
我们有 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*/
function arrayOfData() {
return [
{
"username": "admin",
"passwordField": "admin"
},
{
"username": "admin1",
"passwordField": "admin2"
}
]
//or return loginData json object here
} /*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 包,请在转到 [=18= 之前通过下面的命令 运行 安装它] 测试脚本。
npm install jasmine-data-provider
使用 map 函数的更简单方法:
var testParams = testConfig.testArray;
testParams.map(function(testdata) {
it('write your test here', function() {
console.log('Username: ', testData.username);
});
});
我觉得你的做法很合理。您未定义的原因是因为您将数据放在 'done' 参数中。它将数据设置为 'done' 对象,当 'it' 函数调用您正在定义的函数时传递该对象。
testData.forEach(function (data) {
it("data.description", function (data) {
应该是
testData.forEach(function (data) {
it("data.description", function () {
第二个答案比较突出。只是在这里补充一点,如果您想从 Excel sheet 读取数据,然后执行数据驱动测试,那么这个视频非常有用: https://www.youtube.com/watch?v=vzvC4dYE84Q