perivoje
perivoje

Reputation: 3

Protractor: Continue with test if element is not displayed

Using protractor, I'm testing if copied files,if there are any, are listed when clicking on double files icon. So i want to create test in which I will check for double files icon and click on it and expect that list is opened. But, if there are no double files icon, i want to log message.

I've tried if/else and try/catch isDisplayed(), ispresent(), (in)visibilityOf() and always same outcome: If element is displayed, test pass. If element is not displayed test fails

it('should check if assignment has double files', () => {
    try {
        element.dubleFilesIcon.isDisplayed().then(function() {
            console.log('There ARE double files for this assignment!!!');
            assignmentHeader.doubleFiles();
            expect(element.doubleFilesindicator.isDisplayed()).toBe(true);
        }, function(err) {
            console.error('error' + err);
            throw err;
        });
    } catch (err) {
        console.log('There ARE NO double files for this assignment!!!');
        expect(element.doubleFilesindicator).toBe(false);
    }
});

This is error: 1) Asignment header test cases should check if assignment has double files - Failed: No element found using locator: By(css selector, '')

I'm obviously doing something wrong, but I can't figure out what

Upvotes: 0

Views: 507

Answers (2)

perivoje
perivoje

Reputation: 3

Finnaly I've manged to do it: just added done.call() in else block. It is working. Not sure how, but it does:

it('should check if assignment has double files', (done) => {
    element.dubleFilesIcon.isPresent().then(function(present) {
        if (present) {
            console.log('There ARE double files for this assignment!!!');
            assignmentHeader.doubleFiles();
            expect(element.doubleFilesindicator.isDisplayed()).toBe(true);
            done();
        } else {
            done.call(expect(element.doubleFilesindicator.isPresent().toBe(false));)
        }
    });

});

Upvotes: 0

Joaquin Casco
Joaquin Casco

Reputation: 734

isDisplayed() is used for when you know the element is present in the DOM. You need to check for its presence first with isPresent()

element.dubleFilesIcon.isPresent().then(function() {
    console.log('There ARE double files for this assignment!!!');
    assignmentHeader.doubleFiles();
    expect(element.doubleFilesindicator.isDisplayed()).toBe(true);
});

Additionally, if you want the test to fail if its not present/displayed, you can use this:

it('should check if assignment has double files', (done) => {
    element.dubleFilesIcon.isPresent().then(function(present) {
        if (present) {
            console.log('There ARE double files for this assignment!!!');
            assignmentHeader.doubleFiles();
            expect(element.doubleFilesindicator.isDisplayed()).toBe(true);
            done();
        } else {
            // Here I think you want to test if dubleFilesIcon is NOT present
            // Then expect the files indicator not to be present, which is a bit 
            // redundant. But anyway:  
            expect(element.doubleFilesindicator.isPresent()).toBe(false);
        }
    });

});

Upvotes: 0

Related Questions