[英]Looping on a protractor test with parameters
我有一套完全相同的煙幕測試。 我想將它們放入一個循環並循環一個參數數組。 但是,測試是異步運行的,因此循環在測試運行之前完成。 這導致測試在第8個參數上運行8次,而不是每個參數運行一次。
describe('Admin Console Campaigns', function() {
var ptor;
var adminUrl;
var testParams = [
{title: 'Dashboard', urlSuffix: '/communic8' },
{title: 'Campaign Report', urlSuffix: '/Reports/Campaign' },
{title: 'Partner Campaign Usage', urlSuffix: '/Reporting/PartnerCampaignUsage' },
{title: 'Campaign Template Usage', urlSuffix: '/Reporting/CampaignTemplateUsage' },
{title: 'Email Usage Report', urlSuffix: '/Reports/EmailUsage' },
{title: 'Campaign Templates', urlSuffix: '/CampaignTemplates' },
{title: 'Campaign Template Groups', urlSuffix: '/CampaignTemplateGroups' },
{title: 'New Template', urlSuffix: '/CampaignTemplates/Add' }
];
beforeEach(function() {
ptor = protractor.getInstance();
ptor.ignoreSynchronization = true;
var testParams = smokescreenTestConfig.adminCampaigns;
adminUrl = ptor.params.http + ptor.params.client + ptor.params.staging + ptor.params.sharedvue + ptor.params.admin;
});
afterEach(function(){
});
for(var i=0; i < testParams.length; i++){
var testParam = testParams[i];
it('should have a ' + testParam.title + ' tab', function() {
testUrl = adminUrl + testParam.urlSuffix;
basicTestFunctions.pageExists(testUrl, ptor, browser, testParam.title);
}, 60000);
};
});
有沒有人知道如何強制循環等待測試?
好的,不久之前想到這個,對不起,我忘了我已經發布在這里。 我們基本上在另一個文件中創建了一個配置數組,(雖然這不是必需的,只是讓代碼更容易閱讀),然后將該數組拉到我們想要重復的測試之上的var中。 然后我們在一個循環內部的函數中包圍測試,並在每個循環中傳入我們的數組中的行。
var testParams = testConfig.testArray;
for (var i = 0; i < testParams.length; i++) {
(function (testSpec) {
it('write your test here', function () {
//test code here
});
})(testParams[i]);
};
這里的關鍵是在函數結束時傳遞循環迭代的“testParams [i]”。 這迫使同步執行。
如果你想變得非常瘋狂,我們最終還是編寫了一個批處理文件,在我們所有的客戶中連續運行這個煙幕大約50次。 我們在大約10分鍾內將整個平台吸煙。
您還可以使用specs創建一個數組:
var testParams = testConfig.testArray;
testParams.forEach(function(testSpec) {
it('write your test here', function() {
//test code here
});
});
這應該像Robert McCraw提出的解決方案一樣工作。
使用一些異步代碼,您可以很容易地解決這個問題。
而不是運行it('description', function () { // my test });
使用it('description', function (done) { // my test with a call to done() at the end }
您可以使用此代碼段作為示例:
for (var i = 0; i < 10; i++) {
it('should work for ' + i, function (done) {
setTimeout(done, 1000);
});
}
預期產量:
✓ should work for 0 (1000ms)
✓ should work for 1 (1001ms)
✓ should work for 2 (1001ms)
✓ should work for 3 (1001ms)
✓ should work for 4 (1001ms)
✓ should work for 5 (1001ms)
✓ should work for 6 (1002ms)
✓ should work for 7 (1001ms)
✓ should work for 8 (1001ms)
✓ should work for 9 (1002ms)
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.