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.

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
learningruby347
  • 211
  • 1
  • 4
  • 12
  • 1
    Can you show the full test? – Maria Ines Parnisari Apr 29 '17 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. http://stackoverflow.com/questions/42839202/protractormocha-fails-suite-with-typeerror-before-browser-loads-sut/42853148#42853148 – cnishina Apr 29 '17 at 21:47
  • @MariaInesParnisari I added more details, I hope that makes more sense. – learningruby347 Apr 29 '17 at 22:05
  • Shouldn't you put the line that goes `var resultsTable = element(by.id('results-table'));` _after_ the call to `click()`? – Maria Ines Parnisari Apr 29 '17 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 – learningruby347 Apr 29 '17 at 22:15
  • Yeah but like you said, the `enterBtn` doesn't really change. Can you try what I suggested? – Maria Ines Parnisari Apr 29 '17 at 22:16
  • @MariaInesParnisari I tried it, same error :( – learningruby347 Apr 29 '17 at 22:17
  • `isPresent` checks if an element is in the DOM, `isDisplayed` checks if an element is present in the DOM *AND* is also visible. What should happen when you click on the button, should the table be hidden? If so, you should use `isDisplayed` – wswebcreation Apr 30 '17 at 12:31
  • @wswebcreation I tried using isDisplayed and get the same error... :/ – learningruby347 Apr 30 '17 at 13:18
  • Can you give more info like a HTML example – wswebcreation Apr 30 '17 at 13:22
  • So once I click clearBtn, the results-table disappears from the DOM. This is what I tried to do as you suggested: expect(resultsTable.isDisplayed()).to.eventually.be.false; – learningruby347 Apr 30 '17 at 13:26

1 Answers1

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

Community
  • 1
  • 1
Paul Co
  • 447
  • 2
  • 9