简体   繁体   English

循环使用参数的量角器测试

[英]Looping on a protractor test with parameters

I have a set of smokescreen tests that are all pretty much identical. 我有一套完全相同的烟幕测试。 I would like to put them into a loop and loop at an array of parameters. 我想将它们放入一个循环并循环一个参数数组。 However, the tests are running asynchronously and so the loop completes before the tests are run. 但是,测试是异步运行的,因此循环在测试运行之前完成。 This results in the test being run 8 times on the 8th parameter instead of once for each parameter. 这导致测试在第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);
    };
});

Does anyone have an idea of how to force the loop to wait on the tests? 有没有人知道如何强制循环等待测试?

Ok figured this one out a while ago, sorry I forgot I had posted here. 好的,不久之前想到这个,对不起,我忘了我已经发布在这里。 We basically created a configuration array in another file, (though this is not necessary, just makes the code easier to read) and then pulled that array into a var right above the test we wanted to repeat. 我们基本上在另一个文件中创建了一个配置数组,(虽然这不是必需的,只是让代码更容易阅读),然后将该数组拉到我们想要重复的测试之上的var中。 Then we surrounded the test in a function inside of a loop and passed the lines from our array in with each loop. 然后我们在一个循环内部的函数中包围测试,并在每个循环中传入我们的数组中的行。

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

};

The key here is that "testParams[i]" at the end of the function passing in the iteration of the loop. 这里的关键是在函数结束时传递循环迭代的“testParams [i]”。 This forces synchronous execution. 这迫使同步执行。

If you want to get really crazy we also ended up writing a batch file that runs this smokescreen about 50 times consecutively across all of our clients. 如果你想变得非常疯狂,我们最终还是编写了一个批处理文件,在我们所有的客户中连续运行这个烟幕大约50次。 We smokescreen our entire platform in about 10 minutes. 我们在大约10分钟内将整个平台吸烟。

You can also create an array with specs: 您还可以使用specs创建一个数组:

var testParams = testConfig.testArray;

testParams.forEach(function(testSpec) {
    it('write your test here', function() {
        //test code here
    });
});

This should work like the solution proposed by Robert McCraw. 这应该像Robert McCraw提出的解决方案一样工作。

With some asynchronous code, you can solve this quite easily. 使用一些异步代码,您可以很容易地解决这个问题。

instead of running it('description', function () { // my test }); 而不是运行it('description', function () { // my test }); use it('description', function (done) { // my test with a call to done() at the end } 使用it('description', function (done) { // my test with a call to done() at the end }

You can use this snippet as an example : 您可以使用此代码段作为示例:

for (var i = 0; i < 10; i++) {
  it('should work for ' + i, function (done) {
    setTimeout(done, 1000);
  });
}  

The expected output : 预期产量:

✓ 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)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM