Erik
Erik

Reputation: 3198

conditional expectations in Jasmine?

I am testing a page that can have random overlays occur..

These HTML/CSS elements block the entire screen and force you to close them if they are displaying.

How do I write a Jasmine test it("....") , such that I can conditionally

without using expect since this popup isn't always present, and the testcase would/could fail.

Here's what I currently have, and it fails if the dialog overlay isn't present..

Jasmine Testcase description

 describe("close header warning if opened", function () {
    it("should check for header warning", function () {
        writeTitleToLog("close header warning if opened");
        element(by.css('.header-warning-container')).isDisplayed().then(function (displayed) {
            if(displayed) {
                element(by.css('.close-container')).click();
            }
        });
    });

    it("should not have the header warning", function () {
        expect(element(by.css('.header-warning-container')).isPresent()).toBeFalsy();
    });
});

Test Output

landing page
    should check for header warning, and close it - fail
    should not have the header warning, once it is closed - pass

Upvotes: 1

Views: 1304

Answers (2)

Erik
Erik

Reputation: 3198

Turns out.. I cannot use isDisplayed() if an element is not even present.. it will throw an error. Here is the solution that now correctly passes

describe("close header warning if opened", function () {

    it("should check for header warning", function () {
        writeTitleToLog("close header warning if opened");
        var headerElm = by.css('.header-warning-container');
        element(headerElm).isPresent().then(function (isPresent) {
            if(isPresent) {
                element(by.css('.close-container')).click();
            }
        });
    });

    it("should not have the header warning", function () {
        expect(element(by.css('.header-warning-container')).isPresent()).toBeFalsy();
    });
});

Upvotes: 1

user2020347
user2020347

Reputation: 883

See if this works for you.

As you are doing, use isDisplayed to get a check. Then, if the element is there. click the close. Wait for it to close. Then do your expect afterwards, still inside the if block, to make sure it closed. This shouldn't fail if the warning is not present, because it will skip over the expect.

    overlays.headerWarning.isDisplayed().then(function(result){
        if(result){
            overlays.headerWarningCloseButton.click();
            browser.wait(protractor.until.elementIsNotVisible(overlays.headerWarningCloseButton), 3000, 'Header warning overlay did not close');
            expect(overlays.headerWarning.isDisplayed()).toBeFalsy('Header was displayed after attempted close');
        } else {
            //do nothing
        }
    });

Upvotes: 1

Related Questions