nhrcpt
nhrcpt

Reputation: 872

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

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?

Upvotes: 1

Views: 1021

Answers (1)

DublinDev
DublinDev

Reputation: 2348

I have a couple of comments about this.

Firstly, Your angular error is because the Row_number function you declare is outside of any it block and therefore running before your beforeAll has run.

Next there should be no need for your tableData function as it appears replacing it's parameter n with the i counter from the loop would have the same effect.

Finally if your code needs to go across multiple pages to achieve these tests it will likely be much better to use a data-driven approach and write separate data files for each test. Do the values of these tables row change or would they be consistent?

Update: This approach may look something like this but I have not tested this.

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

    });
})

Upvotes: 1

Related Questions