1

When trying to assert the result from getText() keep getting errors that indicate getText() is returning an object instead of a string.

I'm quite new to automated testing but my (experienced) colleague is getting the same and we've had a JS dev look at it, who is also baffled.

Using selenium-cucumber-js as a test framework. (this is the only framework we've been able to get working on the client's network).

I added console.log in the test to prove that the text is retrievable, it logs it fine but the assert still fails.

Have also tried all the various suggestions in this question but still cannot get it to work Protractor: element.getText() returns an object and not String

Page Obj:

zeroResults: function () {
    return driver.wait(until.elementsLocated(by.css(...)), 10000)
        .then(function () {
            return driver.findElement(by.css(...))
                .getText().then(function (searchOutcome) {
                   console.log(searchOutcome); //this was just to prove the text can be found
                    return searchOutcome;

//I have also tried variations on this (e.g. removing the final return)

Step Def:

this.Then(/^I should see text "Zero results found"$/, function () {
    var searchOutcome = page.xx.zeroResults();
    expect(searchOutcome).to.equal('Zero results found');

Expect test to pass but instead get:

AssertionError: expected { Object (flow_, stack_, ...) } to equal 'Zero results found'

If I change the assert to:

return Promise.resolve(searchOutcome).should.eventually.equal('Zero results found');

I get:

TypeError: Cannot read property 'eventually' of undefined

These are just some of the attempts but all tries have similar results. This is affecting more than one user, has been proven on multiple test cases and using different asserts/expects.

Any help appreciated.

RobM.CW
  • 11
  • 2
  • Welcome to SO. Do you see the text printed in console as part of zeroResults function? – supputuri May 01 '19 at 13:33
  • Hi, thanks for the welcome. Yes the text does print – RobM.CW May 01 '19 at 14:14
  • What happens if you print `searchOutcome` using `console.log`? – demouser123 May 01 '19 at 14:39
  • Not sure if you mean something different to what I already have? When I run the test as is I get: √ And I enter "xxxx" in the search field × Then I should see text "Zero results found" Zero results found The 'zero results found' being the result of the console log. – RobM.CW May 01 '19 at 15:02

2 Answers2

0

Try the below.

Page Obj:

zeroResults: function () {
    return driver.wait(until.elementsLocated(by.css(...)), 10000)
        .then(function () {
            return driver.findElement(by.css(...))
                .getText().then((searchOutcome) => {
                    return searchOutcome;
                   });

Step Def:

this.Then(/^I should see text "Zero results found"$/, function () {
    var searchOutcome = page.xx.zeroResults();
    #debug here and see what is the value in searchOutCome
    expect(searchOutcome).to.equal('Zero results found');
supputuri
  • 13,644
  • 2
  • 21
  • 39
0

It appears that this has been caused by the promises not being resolved. Had another Dev look at it, he amended the code to the following and it has worked:

Page Obj (unaltered)

zeroResults: function () {
    return driver.wait(until.elementsLocated(by.css(...)), 10000)
        .then(function () {
            return driver.findElement(by.css(...))
                .getText().then(function (searchOutcome) {
                   console.log(searchOutcome);
                    return searchOutcome;

Step Def (amended with an additional console.log)

this.Then(/^I should see text "Zero results found"$/, function () {
    var searchOutcome = page.xx.zeroResults();
    console.log(typeof (searchOutcome));
    searchOutcome.then(function (value) {
    console.log(value);
    expect(value).to.equal('Zero results found');

We have the issue on some other steps so going to try to add .then and see if that works.

Thanks for all the responses on this.

RobM.CW
  • 11
  • 2