3

Should we write callbacks/promises for the re-usable methods in Page Object Pattern based testing in Protractor?

For example .. I have the below test code and Page Objects and its working fine without issues. But should I add callbacks for re-usable methods in page class?

describe('This is a test suite for Login cases',function(){
    beforeEach(function() {
        LoginPage.goHome();
        LoginPage.doLogin();
    });
    afterEach(function() {
        LoginPage.doLogout();
    });
    it('Scenario1_Login_VerifyFirstName',function(){
            //Some Test step      
    });

Page Class:

var Login = {

    PageElements: {
        emailInput: element(by.css('.email')),
        passwordInput: element(by.css('.password')),
        loginForm: element(by.css('.form')),
        logout: element(by.linkText('LOG OUT'))
    },
    goHome: function goHome() {
        browser.get('/signin');
        browser.driver.manage().window().maximize();
    },
    doLogin: function doLogin() {
        this.PageElements.emailInput.sendKeys(UserName);
        this.PageElements.passwordInput.sendKeys(Password);
        this.PageElements.loginForm.submit();
    },
    doLogout: function doLogout() {
        browser.wait(EC.elementToBeClickable(this.PageElements.profileLink));
        this.PageElements.profileLink.click();
        this.PageElements.logout.click();
    }
};

module.exports = Login;
Hikaryu
  • 317
  • 5
  • 17
AdityaReddy
  • 3,625
  • 12
  • 25
  • you do not need to add callback until you a specific reason – Optimworks Aug 25 '16 at 11:19
  • @SureshSalloju Right now we have only webdriverJs commands and they get chained due to ControlFlow. But what if we have non webdriverjs actions. Also, Is it possible to return values without callbacks? – AdityaReddy Aug 26 '16 at 09:13

1 Answers1

0

Yes you can.

By simply returning values or promises:

goHome: function() {
    browser.get('/home');
    return browser.getTitle();
},

And should resolve them on spec level inside "it" blocks like below:

it('Page should have a title', function() {
    expect(Page.goHome()).toEqual('Home Page');
  });
Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23