Testing the contents of a temporary element with protractor

后端 未结 6 1734
清酒与你
清酒与你 2021-01-04 09:12

I\'m trying to test the login page on my site using protractor.

If you log in incorrectly, the site displays a \"toast\" message that pops up for 5 seconds, then dis

6条回答
  •  醉话见心
    2021-01-04 09:35

    I hope this can help who has some trouble with protractor, jasmine, angular and ngToast.

    I create a CommonPage to handle Toast in every pages without duplicate code. For example:

    var CommonPage = require('./pages/common-page');
    var commonPage = new CommonPage();
    decribe('Test toast', function(){
        it('should add new product', function () {
                browser.setLocation("/products/new").then(function () {
    
                    element(by.model("product.name")).sendKeys("Some name");    
                    var btnSave = element(by.css("div.head a.btn-save"));
                    browser.wait(EC.elementToBeClickable(btnSave, 5000));
                    btnSave.click().then(function () {
    
                        // this function use a callback to notify
                        // me when Toast appears
                        commonPage.successAlert(function (toast) {
                            expect(toast.isDisplayed()).toBe(true);
                        });
    
                    });
                });
            })
    });
    

    And this is my CommonPage:

    var _toastAlert = function (type, cb) {
        var toast = null;
    
        switch (type) {
            case "success":
                toast = $('ul.ng-toast__list div.alert-success');
                break;
            case "danger":
                toast = $('ul.ng-toast__list div.alert-danger');
                break;
        }
    
        if (!toast) {
            throw new Error("Unable to determine the correct toast's type");
        }
    
        browser.ignoreSynchronization = true;
        browser.sleep(500);
        browser.wait(EC.presenceOf(toast), 10000).then(function () {
            cb(toast);
            toast.click();
            browser.ignoreSynchronization = false;
        })
    
    
    }
    
    var CommonPage = function () {    
        this.successAlert = function (cb) {
            _toastAlert("success", cb);
        };
        this.dangerAlert = function(cb) {
            _toastAlert("danger", cb);
        }
    }
    
    module.exports = CommonPage;
    

提交回复
热议问题