2

I am having trouble testing for the absence of elements in my node.js app.

I have an enterBtn button that, when clicked, it displays resultsTable and a clearBtn. The enterBtn is always present.

I am trying to test that the resultsTable disappears when I click the clearBtn and I am having trouble with that.

'use strict';

const chai = require('chai');
chai.use(require('chai-as-promised'));
chai.should();
const expect = chai.expect;

require('./lib/test-helper');

const until = protractor.ExpectedConditions;

describe('My App', function() {
    it('should clear resultsTable clearBtn is clicked', function(){
        var resultsTable = element(by.id('results-table'));
        clearBtn.click();

        expect(resultsTable.isPresent()).to.eventually.be.false;
      }); 
});

I also tried to do this:

resultsTable.isPresent().then(function(bln) {
     expect(bln).to.equal(false);
});

And that didn't work either:

"Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test."

But if I try to test for the presence of the enterBtn, which is always present, using the code below, it works.

var enterBtn = element(by.id('enter'));
expect(enterBtn.isPresent()).to.eventually.be.true;

I'm not sure what's going on...

Any help would be much appreciated! Thanks.

11
  • 1
    Can you show the full test? Apr 29, 2017 at 21:41
  • 1
    Check this answer out for Mocha. Important to check which dependencies you use / config setup. Once that's verified, include more information about what kind of app you are testing and the protractor config. stackoverflow.com/questions/42839202/…
    – cnishina
    Apr 29, 2017 at 21:47
  • @MariaInesParnisari I added more details, I hope that makes more sense. Apr 29, 2017 at 22:05
  • Shouldn't you put the line that goes var resultsTable = element(by.id('results-table')); after the call to click()? Apr 29, 2017 at 22:13
  • @MariaInesParnisari Actually, on my test script, I defined a bunch of variables right below the describe. I defined the enter button there as well and that works in the positive cases. It's only where I am expecting a false scenario that doesn't work o.O Apr 29, 2017 at 22:15

1 Answer 1

0

After clicking the clearBtn, does the element disappear immediately?
If no, try putting a sleep after clearBtn.click() to ensure that object will be gone before expect.

You can also simplify your expect by using
expect(resultsTable.isPresent()).toBe(false)

describe('My App', function() {
    it('should clear resultsTable clearBtn is clicked', function(){
        var resultsTable = element(by.id('results-table'));
        clearBtn.click();
        browser.sleep(2000);
        resultsTable.isPresent().then(function(bln) {
            expect(bln).toBe(false);
        });
    }); 
});

If object might take more than 2 seconds, you cna try using this recursive function https://stackoverflow.com/a/43679616/7761311

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.