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.

4
  • Welcome to SO. Do you see the text printed in console as part of zeroResults function? Commented May 1, 2019 at 13:33
  • Hi, thanks for the welcome. Yes the text does print Commented May 1, 2019 at 14:14
  • What happens if you print searchOutcome using console.log? Commented May 1, 2019 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. Commented May 1, 2019 at 15:02

2 Answers 2

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');
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.