Saturday, 27 May 2017

How to inheritance functions from a base js file in Protractor?

I moved from automation framework development with java to protractor & javascript so this is new to me. In my protractor framework I need to create a base screen js file with global functions that can be called from other screens js files. See the following example. How to make login.js inheritance all functions from base.js so the test in loginTest.js will work when calling base.js functions directly from login.js?

base.js

var base= function(){

    var that = {
        navigateToLogin: function(url){
        browser.get(url);
        browser.driver.manage().window().maximize();
        return require('login.js');
        },

        click: function(element, page){
            element.click();
            console.log('LOG: Clicked on element ' + element);
            return that || page;
        },

         insert: function(element, text){
            element.clear().then(function() {
                element.sendKeys(text);
                console.log('LOG: Insert text: ' +text);
            });
            return that;
        },
    };
    return that;
};
module.exports = new base();

login.js

var login = function(){

    var that = {

        func1: function() {
         // do something
                        return that 
        },

        func2: function() {
           // do something

                       return that;
        },
            };
    return that;
};
module.exports = new login();

loginTests.js

describe('Login tests - ', function() {

    var loginPage = require('login.js');

       describe('Success login: ', function () {

        beforeEach(function () {
            loginPage.navigateToLogin(“http://login.url”);
                   });

        it("Success login as admin",function(){
           loginPage.insert(“element(by.name("username"))”,”admin@mail”l)
                           .insert(“element(by.name("password"))”,”12345”)                             
                       .click(“element(by.name("loginButton"))”,“home.js”);
                  });
    });
});



via shaynl

No comments:

Post a Comment