angular / protractor

E2E test framework for Angular apps
http://www.protractortest.org
MIT License
8.75k stars 2.31k forks source link

Is that possible to encapsulate a spec.js file to a function? #1154

Closed jasonnjit closed 10 years ago

jasonnjit commented 10 years ago

Hi, I am writing the spec.js files for our project. Inside our project, we have many widgets(or panels) on the dashboard, and for different users, we have 3 different dashboards. But some of the widgets are the same on these 3 dashboards. So, I was wondering if that is possible to encapsulate every widget-spec.js and invoke it in different users' spec.js files? I tried to encapsulate it like below. But it doesn't work, it shows 0 test, 0 assertions, 0 failures. Sounds like no test scripts found, but it worked fine without the first line (var headerWidget = function() { ) and the last line ( }; exports.headerWidget = headerWidget;). Is that possible to encapsulate the describe, it (everything from Jasmine) to one function?

Many thanks.


var headerWidget = function() {
    var landingPage = require("./page-objects/landing_page.js");
    var ptor = protractor.getInstance();
//var baseUrl = 'http://localhost:8082';
////var baseUrl = 'https://entrpt-web.dev.globalpay.com';
//TODO: Do we have to test the url every time when we click the menu or button on the header?
//TODO: when 9 different users login, do they see the same url? No, but all have /#/landing in their urls

    describe('Landing Page - Common', function () {
        beforeEach(function () {
            landingPage.visitCommon();
        });

        describe('Default language shows when you login again', function () {
            it('should show French when you login again if you choose French last time', function () {
                element(by.id('localeMenu')).click();
                element(by.id('fr_CA')).click();
                element(by.id('userMenu')).click();
                element(by.id('logout')).click();
                expect(ptor.getCurrentUrl()).toBe(baseUrl + '/#/login');
                expect(element(by.id('signin-message')).getText()).toBe('SVP ouvrir une session');
                element(by.model("user.username")).sendKeys('sandy');
                element(by.model("user.password")).sendKeys('sandy');
                element(by.id("login")).click();
                expect(element(by.id('home')).getText()).toBe('Accueuil');
                expect(element(by.id('current_locale')).getAttribute('src')).toBe(baseUrl + '/img/fr_CA.png');
            });
        });

        describe('Landing Page - Header', function () {
//TODO: welcome profile bar
//TODO: search bar and print button
//TODO: Reporting drop-down, find a way, not in Sprint 1
//TODO: add two text labels 'welcome nameOfPerson'  'Report Date as of todaysDate'
//        it('should show label Welcome user', function () {
////            expect(protractor(by.xpath('/html/body/#wrap/div/div/div/div/div/div#welcome-widget.row.ng-scope/div.col-md-6/strong.ng.binding')).getAttribute('class')).toContain('Welcome');
//        expect(ptor.by.binding("{{}}"));
//        });

//Menu (Header)
            it('should present a menu and a page with widgets, after log in', function () {
                expect(element(by.id('menu')).isDisplayed()).toBe(true);
                landingPage.menu.click;
                expect(browser.getCurrentUrl()).toBe(baseUrl + '/#/landing/isoManager');
            });
            it('should have GPN logo with correct url in the header', function () {
                expect(element(by.id('logo')).isDisplayed()).toBe(true);
                expect(element(by.tagName('img')).getAttribute('src')).toBe(baseUrl + '/img/GPlogo.png'); //should have better way
            });
            it('should redirect to GPN worldwide homepage when click the GPN logo', function () {
                element(by.id('gpnHome')).click();
                ptor.sleep(1000);  //10000
                var expectedUrl = browser.driver.getCurrentUrl();   //browser.driver.getCurrentUrl()
                expect(expectedUrl).toEqual('https://www.globalpaymentsinc.com/worldwide.html');
            });
            it('should be at the landing page when click "Home"', function () {
                expect(element(by.id('home')).isDisplayed()).toBe(true);
                landingPage.menu.home();
                ptor.sleep(1000);
                expect(browser.getCurrentUrl()).toBe(baseUrl + '/#/landing/isoManager');
            });
//TODO: Administration will redirect us to legacy ERadmin page
//      it('should be at the landing page after clicking on "Administrative"', function() {
//          expect(element(by.id('administration')).isDisplayed()).toBe(true);   // no id for Administrative  FIXME
//          landingPage.menu.home();
//          expect(browser.getCurrentUrl()).toContain("/#/landing");
//      });
            it('should be at the landing page when click "reportingMenu" and show drop-down menu', function () {
                expect(element(by.id('reportingMenu')).isDisplayed()).toBe(true);
                browser.findElement(by.id('reportingMenu')).click();
                ptor.sleep(1000);
                expect(browser.getCurrentUrl()).toBe(baseUrl + '/#/landing/isoManager');
                expect(element(by.id('reportingDropMenu')).isDisplayed()).toBe(true);
            });
            it('should be at the landing page when click on "userMenu"', function () {
                expect(element(by.id('userMenu')).isDisplayed()).toBe(true);
                element(by.id('userMenu')).click();
                expect(browser.getCurrentUrl()).toBe(baseUrl + '/#/landing/isoManager');
            });
            it('should show a drop down menu when click on "userMenu"', function () {
                element(by.id('userMenu')).click();
                expect(element(by.id('help')).isDisplayed()).toBe(true);
                expect(element(by.id('profile')).isDisplayed()).toBe(true);
                expect(element(by.id('logout')).isDisplayed()).toBe(true);
            });
//TODO: The Help should redirect us to a new url
//TODO: The Profile redirect us to a new url
            it('should logout when we click logout in userMenu', function () {
                element(by.id('userMenu')).click();
                element(by.id('logout')).click();
                expect(browser.getCurrentUrl()).toBe(baseUrl + '/#/login');
            });
            it('should be at the landing page when click "localeMenu"', function () {
                expect(element(by.id('current_locale')).getAttribute('src')).toBe(baseUrl + '/img/en_US.png');
                expect(element(by.id('localeMenu')).isDisplayed()).toBe(true);
                element(by.id('localeMenu')).click();
                expect(browser.getCurrentUrl()).toBe(baseUrl + '/#/landing/isoManager');
            });
            it('should have two flags and correct text when click localeMenu', function () {
                element(by.id('localeMenu')).click();
                expect(element(by.id('en_US')).isDisplayed()).toBe(true);
                expect(element(by.id('fr_CA')).isDisplayed()).toBe(true);
                expect(element(by.id('en_US')).getText('src')).toBe('English (U.S.A.)'); //Maybe wrong way, it got the text in <a>
                expect(element(by.id('fr_CA')).getText('src')).toBe('French (Canada)');
            });
            it('should redirect to French version', function () {
                element(by.id('localeMenu')).click();
                element(by.id('fr_CA')).click();
                expect(element(by.id('home')).isDisplayed()).toBe(true);
//TODO: Maybe need more evidence to prove it's French
                expect(element(by.id('home')).getText()).toBe('Accueuil');
            });
        });
    });
};
exports.headerWidget = headerWidget;
elgalu commented 10 years ago

Read the How to use it section of that stackoverflow answer.

juliemr commented 10 years ago

Thanks @elgalu, good reference. Closing this as an answered question.