【问题标题】:Nested it in Protractor/Jasmine嵌套在量角器/茉莉花中
【发布时间】:2015-08-05 10:41:14
【问题描述】:

我可以在 Protractor/Jasmine 中创建一个嵌套它吗?

it("outer it", function () {
    it("inner it", function () {
        expect(1).toBe(1);
    });
});

我试图在一个循环中执行它的测试用例,并且在每次迭代中我都想运行一个测试,例如:

it("outer it", function () {
    for(var i=0;i<10;i++){
        it("inner it", function () {
            expect(1).toBe(1);
        });
    }
});

我想这样做的原因是我想初始化一个数组,然后以动态的方式循环遍历所有元素并运行一些“它”,例如:

describe ("[Components]", function() {
   var grid = new Grid();

   it("Initialize the grid for the history window", function () {
       grid.init();
   });

   for(var i=0;i<grid.length;i++){
       it("test 1", function () {
           expect(1).toBe(1);
       });
   }

});

for循环执行时grid.length等于0,我希望for循环在初始化“it”之后执行。

【问题讨论】:

    标签: jasmine protractor


    【解决方案1】:

    回答您的问题,您不能将嵌套在另一个中。尽管 Jasmine 框架没有抛出任何错误,但嵌套 it 中的代码不会执行。此外,我看不到嵌套 it 的任何用途,因为它们是独立运行以完成特定测试步骤的规范或函数。它还概述了当前正在执行的功能。如果你想在循环中运行某些东西,你可以创建另一个函数,然后在 for 循环中调用它,就像这样 -

    it("outer it", function () {
        var newFunction = function(){
            expect(1).toBe(1);
        };
        for(var i=0;i<10;i++){
            newFunction();
        };
    });
    

    希望这会有所帮助。更多关于 it 的信息可以在这里找到 - Jasmine Framework - it's

    【讨论】:

    • 嗨 Girish,我想使用 DOM 初始化一个对象,该初始化必须在它内部(尝试将它放在它之外,我得到一个错误),之后我想运行所有我创建的对象内部的元素,对于我需要运行多个测试的每个元素,我可以使用“expect”,但当其中一个测试失败时,我更喜欢使用“it”来获取更多信息。所以问题是所有的“它”都进入了一个队列,所有不在“它”中的代码都在之前执行,所以如果我有一个停止在“它”中初始化的值,长度为0。
    • 您好 user1684140,尝试在 newFunction 中初始化对象。它应该如上所示工作。基本上说 it 只是用来显示正在执行的步骤而不是用于循环,尽管您可以在 it 内循环。 it 总是串行执行,即初始化 it 在你的 test1 it 之前执行。如果要在初始化后执行 for 循环,请使用量角器中提供的 then 链接函数。
    【解决方案2】:
    1. 如前所述 - 不,您不能将 it 放在另一个 it 块中,但是您可以将整个 describe 块放在另一个块中
    2. 您还可以在 for 循环内运行 it 块,例如使 it 块有条件。

    您可以在下面找到一个真实的代码示例(我添加 for 循环只是为了演示)

    describe("E2E: Environment configuration test", function () {
    
        beforeAll(function () {
            logIn();
        });
    
        afterAll(function () {
            logOut();
        });
    
        describe("Members page configuration test", function() {
    
        for (let i=0; i<3; i++) {
            it("Members page - columns", function () {
                //code that verifies that all columns of the page are presented
            });
        }
    
            it( "Members page - filters", function() {            
                //code that verifies that all filters of the UI are presented as expected
            });
    
            it( "Members page - eligibility feature", function() {            
                //code that verifies that if the feature is set to true then additional column must be displayed
            });
    
        });
    
        describe("Providers page configuration test", function() {
    
        // use of conditional it blocks
        // if feature is turned on run one set it blocks
        // otherwise execute another set of it blocks
            if (envConfig.providerFeature) {
    
                it( "Organizations tab configuration test", function() {            
                    //code that verifies that all elements of the current tab are displayed according to configurations of all features of the application 
                });
    
                it( "Practitioners tab configuration test", function() {});
                    //code that verifies that all elements of the current tab are displayed according to configurations of all features of the application 
            
            } else {
    
                it( "Providers page - verification of the page being disabled", function() {
                    //code that verifies that both tabs are not present in the UI
                    console.log('Providers feature is set to FALSE for the environment by default configuration, the test case will be skipped');
                });
            }
    
        });
    
        it( "Users page configuration test", function() {
             //code that verifies that all elements of the current page are displayed according to configurations of all features of the application 
        });
    
        it( "Reports page configuration test", function() {
    
            if (!envConfig.disabledReportsFeature) {
             //code that verifies that all elements of the current page are displayed according to configurations of all features of the application 
            } else {
                console.log('Reports DISABLED_REPORTS_FEATURE is set to TRUE for the environment by default configuration, the test case will be skipped');
    
            }
        });
    });
    

    本例中的控制台输出看起来井井有条

    附:另外我会附上一张干净的控制台图片

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多