简体   繁体   中英

Checking for presence of elements using Protractor and chai as promised

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM