Executing Multiple IT blocks inside a Protractor For loop for web testing

时间:2019-01-18 18:32:59

标签: javascript jasmine protractor

We need to use for loop after login to a web page and perform multiple tests inside the for block on the page. My ideal test scenario should be like the snippet below. We have a table that have buttons on each of the row, and we will navigate to the next page for that particular button and validate data. currently we plugged all expects and assertions in a single IT block, but that is not a good solution. We need to split the sections of tests in different IT block.

require('..\\waitAbsent.js');
require("../node_modules/jasmine-expect/index.js");
var EC = protractor.ExpectedConditions;

describe('Student Enrollment Page Content Validation', function() {


beforeAll(function () {
    browser.driver.manage().window().maximize();
    browser.get(globalVariables.loginMain);
    globalVariables.Email_Input_box.sendKeys(globalVariables.Demo_User);
    globalVariables.Password_Input_Box.sendKeys(globalVariables.Demo_PWD);
    globalVariables.Submit_Button.click();
    browser.wait(EC.invisibilityOf(globalVariables.Submit_Button), 25000, 'submit button is not disappearing yet');
});

async function Row_Numbers() {

    const RowCount = (globalVariables.tableData_Dashboard.all(by.tagName("tr")).count()).then(function(RC){

        return RC;

    });

}

for (var i = 1; i<Row_Numbers(); i++){

    function tableData(n){
        var row_1 = globalVariables.tableData_Dashboard.all(by.tagName("tr")).get(n);

        // get cell values
        var cells = row_1.all(by.tagName("td"));

        it ('should return the data fo the first cell', function(){
            var Student_ID = cells.get(0).getText().then(function (SID) {
                console.log(SID);

                return SID;

            });

            expect(Student_ID.toEqual('Something'));
        });

        it ("should show the button in this row", async function(){

            const Button = globalVariables['Edit_Button_' + n];
            // console.log(Button)
            expect(await Button.isDisplayed());
            Button.click();
            // do some thing

        });
    }tableData(i)
}

});

When we run the test using this script, we get the following error:

E/launcher - Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined. This could be either because this is a non-angular page or because your test involve s client-side navigation, which can interfere with Protractor's bootstrapping. See https://github.com/angular/protractor/issues/2643 for details"

How can I use the for loop to achieve our goal?

1 个答案:

答案 0 :(得分:1)

对此我有几点评论。

首先,您的角度错误是因为您声明的Row_number函数在任何it块之外,因此在beforeAll运行之前运行。

接下来,您不需要tableData函数,因为它看起来像用循环中的i计数器替换参数n一样。

最后,如果您的代码需要跨多个页面才能完成这些测试,则最好使用数据驱动的方法并为每个测试编写单独的数据文件。这些表行的值是否发生变化或保持一致?

更新: 这种方法可能看起来像这样,但我尚未对此进行测试。

beforeAll(function () {
    browser.driver.manage().window().maximize();
    browser.get(globalVariables.loginMain);
    globalVariables.Email_Input_box.sendKeys(globalVariables.Demo_User);
    globalVariables.Password_Input_Box.sendKeys(globalVariables.Demo_PWD);
    globalVariables.Submit_Button.click();
    browser.wait(EC.invisibilityOf(globalVariables.Submit_Button), 25000, 'submit button is not disappearing yet');
});

it('test it', async () => {
    globalVariables.tableData_Dashboard.all(by.tagName("tr")).forEach((row) => {
        var cells = row.all(by.tagName("td"));

        var Student_ID = cells.get(0).getText().then(function (SID) {
            console.log(SID);
            return SID;
        });

        expect(Student_ID.toEqual('Something'), 'should return the data fo the first cell');

        const Button = globalVariables['Edit_Button_' + n];
        // console.log(Button)
        expect(Button.isDisplayed(), 'should show the button in this row').toBe(true);
        Button.click();
        // do some thing

    });
})