Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set up common functions that are available for my test suites with Protractor / Selenium?

I am working on an AngularJS protractor test suite. I have a conf file looking like this:

exports.config = {

    seleniumAddress: 'http://localhost:4444/wd/hub',
    baseUrl: 'http://127.0.0.1:17315/',
    capabilities: {
        browserName: 'chrome',
        'chromeOptions': {
            args: ['--test-type']
        }
    },

    suites: {
        login: ['LoginPage/login.js'],
        homePage: ['Homepage/homepage.js',
                   'Homepage/city_page.js',
                   'Homepage/admin_page.js'],
        adminPage: ['AdminPage/exam.js',
                    'AdminPage/location.js'

.. 

Inside these .js files I use some functions that I would like to share amongst all of my files. For example:

describe('xxx', function () {

    it('xxx', function () {
        commonFunction(123);
    });

I would like to place these common functions in their own file but I am not sure how to do this so that I can make them accessible from the other javascript files. I guess what I need is something like an "inport" which I don't think exists. Can anyone give me some advice on where I can put these common functions and on how I can access them from each of the *.js files in the test suites?

like image 969
Alan2 Avatar asked Jun 15 '14 18:06

Alan2


1 Answers

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');
  });
});
like image 60
gontard Avatar answered Oct 22 '22 22:10

gontard