Jurtchak
Jurtchak

Reputation: 25

Protractor getText() of expected result returning undefined

I trying to validate that data from JSON file equal to data that was sent in spec file. Test passing OK, but when I tried to console.log it, I received undefined, instead of 'TestFname'. 2 questions: 1. Why I receive undefined instead of entered in test data? 2. Why test pass OK if TestFname != undefined?

Spec file:

describe('Validating that record is present in table', function () {

  baseURL.navigateToURL('http://www.way2automation.com/angularjs-protractor/banking/#/manager/list');


  it('Validating that record is present in table', function () {

    const firstName = element(by.repeater('cust in Customers').row(0).column('cust.fName'));
    const lastName = element(by.repeater('cust in Customers').row(0).column('cust.lName'));
    const postCode = element(by.repeater('cust in Customers').row(0).column());
    const accountNumber = element(by.repeater('account in cust.accountNo').row(0).column());

    add_customer.gotoCustomerSearch();
    add_customer.validateCustomerRecords('TestFname', '', '', '');
    expect(firstName.getText()).toEqual(OR.locators.addcustomerdetailspage.testdata.fName1).then(function (text) {
      console.log(text);
    });
    browser.sleep(2000);

  });

})

PageObject used:

this.validateCustomerRecords = function (fname, lname, pcode, accountNum) {

    const searchCustomer = 
    element(by.model(OR.locators.customerData.searchCust)).clear();
    searchCustomer.sendKeys(fname);
    searchCustomer.sendKeys(lname);
    searchCustomer.sendKeys(pcode);
    searchCustomer.sendKeys(accountNum);

    return this;

  };

Upvotes: 1

Views: 698

Answers (2)

HaC
HaC

Reputation: 950

You need to resolve the promise for getText(), not toEqual()

firstName.getText().then(text=> {
    console.log(text);
    expect(text).toEqual(OR.locators.addcustomerdetailspage.testdata.fName1);
});

Upvotes: 1

Andy Jenkins
Andy Jenkins

Reputation: 181

Resolve the promise as above, but sometimes getText will just fail. An alternative approach that may work is:

myElement.getAttribute('innerText")

Upvotes: 0

Related Questions