How can I set up common functions that are available for my test suites with Protractor / Selenium?
To reuse code i use the page object pattern. I put the page object in a separate file and in a module.
For example, the pages.js
file contains some page objects.
'use strict';(function() { var Application = function() { var app = this; browser.get('http://localhost:9003/'); app.login = function() { element(by.buttonText('login')).click(); return new LoginPage(); }; var LoginPage = function() { var loginPage = this; loginPage.withCredentials = function(login, password) { element(by.css('.loginField')).Keys(login); element(by.css('.passwordField')).Keys(password); element(by.buttonText('login')).click(); return new WelcomePage(); }; }; var WelcomePage = function() { var welcomePage = this; welcomePage.getGreetings = function() { return element(by.css('.greetings')).getText(); }; }; }; module.exports = function() { return new Application(); };}());
and i import them in my acceptance test using require
:
'use strict';var Application = require('./pages.js');describe('The application', function() { it('should let you log into the application', function() { var application = new Application(); var welcomePage = application.login().withCredentials('Jean', '!sz3sk,dz'); expect(welcomePage.getGreetings()).toEqual('Welcome Jean'); });});