Element.getText() in Protractor [duplicate]

2019-06-08 16:47发布

问题:

This question already has an answer here:

  • Protractor: element.getText() returns an object and not String 3 answers

I want to write a test case in which I compare the value of an element clicked in previous page to that of an element's value in the current page.

I am using the following code -

validateText = SoftwaresUnmappedPage_POM.checkFirstSoftwareName.getText();

expect(validateText+"*").toBe(SoftwareSummary_POM.softwareName.getText());

For example - If I click on a button (ABC), then it loads a page. This page has elements indicating the element i clicked. But it displays the name as ABC*. So i want to verify if the element I clicked on and the page I am landing at is the same.

Error being caused - Expected '[object Object]' to be 'ABC'.

Can someone please help me resolve this problem? Thanks.

回答1:

This won't work because Protractor works with promisses. You will first need to resolve the promise to be able to do what you want to test.

This will work

SoftwaresUnmappedPage_POM.checkFirstSoftwareName.getText()
  // Get the resolved text from the promise
  .then(function (validateText){
    // Compare the result
    expect(validateText+"*").toBe(SoftwareSummary_POM.softwareName.getText());
  });

Hope this helps



回答2:

In addition to above answer, you can also do something like below,

 var validateText = SoftwaresUnmappedPage_POM.checkFirstSoftwareName.getText().then(function(validateText){
    return validateText+"*";
});
 expect(validateText).toBe(SoftwareSummary_POM.softwareName.getText());