预期结果的量角器getText()返回undefined

时间:2018-04-18 15:16:58

标签: typescript protractor

我尝试验证来自JSON文件的数据是否等于在spec文件中发送的数据。测试传递OK,但是当我尝试console.log时,我收到了undefined,而不是'TestFname'。 2个问题: 1.为什么我收到未定义而不是输入测试数据? 2.如果TestFname!= undefined,为什么测试通过OK?

规格文件:

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:

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;

  };

2 个答案:

答案 0 :(得分:1)

您需要解决getText()的承诺,而不是toEqual()

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

答案 1 :(得分:0)

如上所述解决承诺,但有时getText会失败。另一种可行的方法是:

myElement.getAttribute('innerText")