nhrcpt
nhrcpt

Reputation: 872

Protractor shows variable value as "undefined"

We are defining variables from the elements on one page of the website, clicking on the edit button, which is opening the next page. In this page we need to assert that the data captured on the earlier page matches the data shown on the 2nd page. Our problem is, once the test moves to the 2nd page, it fails to recall the variables that we defined on the 1st page. below is our code snippets:

     it ('Student ID Validation', function(){

        // get rows
        var rows = tableData_Dashboard.all(by.tagName("tr"));

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

        var Student_ID = cells.get(0).getText().then(function(SID){
            console.log(SID);
        });
    Edit_Button_1.click();
    browser.sleep(2000);
    expect(Student_ID_on_Reg_Page.getAttribute('value')).toEqual(Student_ID);

after execution, we get the following error

 Message:
Expected '123456' to equal undefined.

We were suspecting that it may be due to asynchronization, but that is not the case. the test moves to page 2 after it stores the variable from page 1, so we are at a loss why this is happening. How can we fix this and use the variables for assertion purpose?

Upvotes: 1

Views: 251

Answers (2)

alecxe
alecxe

Reputation: 473833

The problem is that you've specified the then() callback where you just log the value but don't return it:

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

As nothing is returned, Student_ID would become a promise which would resolve into undefined.

You either need a return:

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

Or, remove the custom callback completely:

var Student_ID = cells.get(0).getText();

Upvotes: 3

nhrcpt
nhrcpt

Reputation: 872

actually, the following part is causing the problem. Once we removed this part, the test is working fine.

.then(function(SID){ console.log(SID); });

Upvotes: 0

Related Questions