2016-07-06 90 views
2

我是量角器的新手。任何人都可以請指導我使用量角器進行數據驅動測試。以下是代碼,配置文件和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" 
} 
] 

問題是不是獲取數據,它是寫在所有輸入框不確定。請幫助

+0

它看起來像一個對象數組您在您的測試數據得到了! –

回答

1

我假設它的Array對象,你可以遍歷每個數組元素和直接訪問其內容,你不需要testdata.forEach(),你可以嘗試這樣的事情 -

'use strict'; 

var testData = require('../example/Test Data/Test.json'); 

describe('LoginPage', function() { 

var loginData = require('../example/Test Data/Test.json'); 


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(); 

    }); 
    }); 
}); 

我沒有測試過上面的代碼,你應該使用頁面對象而不是直接在你的測試中使用它們!

+0

是的,顯然頁面對象應該是任何測試的方法。但我的重點是在我的測試中使用數據驅動方法,因此創建了一個簡單的方法。 嗯,非常感謝它爲我工作。 你能指導我從哪裏可以得到一些關於量角器的東西 –

4

我們有jasmine-data-provider軟件包,它可以幫助我們用量角器進行數據驅動測試。

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(); 
    }); 
    }); 
}); 

注意:如果茉莉數據提供程序包尚未安裝在您的機器,請之前要運行測試腳本運行以下命令來安裝它。

npm install jasmine-data-provider 
+0

嗨Suresh, 它給了我錯誤'模塊:茉莉花數據提供者找不到'。 您可以請幫忙 –

+0

@SahilSehgal,錯誤說茉莉花數據提供程序包尚未安裝在您的機器上。通過從命令提示符或終端運行命令「_npm install jasmine-data-provider_」來安裝jasmine-data-provider。它會工作。 –

+0

@SureshSalloju很好的包,不知道它存在。現在我們已經從使用'Array.map()'切換到使用'using'。謝謝! – alecxe

1

使用地圖功能更簡單的方法:

var testParams = testConfig.testArray; 
testParams.map(function(testdata) { 
     it('write your test here', function() { 
      console.log('Username: ', testData.username); 
     }); 
}); 
+0

不錯,簡單thx – danday74

+1

歡迎您! –

0

我覺得你的做法是比較合理的。你得到未定義的原因是因爲你把數據放在'done'參數中。它將數據設置爲當'it'函數調用正在定義的函數時傳遞的'done'對象。

testData.forEach(function (data) { it("data.description", function (data) {

應該

testData.forEach(function (data) { it("data.description", function() {