Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Using javascript prototype (inheritance) concept in Protractor using jasmine

Hi i am trying to use POM concept ,first file basefile.JS my code is

var basefile = function() {
};
basefile.prototype.elementClick = function(locator) {
 element(by.xpath(locator)).click();
};
//module.exports = new basefile();

This file contains common methods that a user can perform on any web application. Second js file tempPageOne.js my code is

require("./basefile.js");

var tempPageOne = function(){

 var BaseMethod = new basefile();
 this.ddselection = function(locOne,locTwo){
  BaseMethod.elementClick(locOne);
  BaseMethod.elementClick(locTwo);
 };
};
module.exports = new tempPageOne();

here i am calling my basefile.JS and using methods defined there in thirdd js file testMethod.js my code is

describe("sample to check inheritance",function(){
 var testOne = require("./tempPageOne.js");

 it("working with inheritance",function(){
  browser.get("http://www.protractortest.org/#/");
  testOne.ddselection("html/body/nav/div/div[2]/div/ul/li[3]/a","html/body/nav/div/div[2]/div/ul/li[3]/ul/li[4]/a");
  console.log("Working fine");
 });

});

this is my specification file for a simple test but getting error do not know what to do Failures: 1) sample to check inheritance encountered a declaration exception Message: ReferenceError: basefile is not defined Stack: ReferenceError: basefile is not defined at new tempPageOne (D:\eclipseProject\JavaScriptInheritance\tempPageOne.js:4:23) at Object. (D:\eclipseProject\JavaScriptInheritance\tempPageOne.js:10:18) at require (module.js:385:17) at Suite. (D:\eclipseProject\JavaScriptInheritance\testMethod.js:3:16) at addSpecsToSuite (C:\Users\rajnish\AppData\Roaming\npm\node_modules\protractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:743:25)

Upvotes: 2

Views: 495

Answers (2)

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Keep everything same no change in any Js file only change tempPageOne.js like this and it is working

   var basefile = require("./basefile.js");

var tempPageOne = function(){

/*  // Comination One : works this way also
    var BaseMethod = Object.create(basefile);
    this.ddselection = function(locOne,locTwo){
        BaseMethod.elementClick(locOne);
        BaseMethod.elementClick(locTwo);
    };*/


    /*
    // Comination Two :works this way also
    var BaseMethod = basefile;
    this.ddselection = function(locOne,locTwo){
        BaseMethod.elementClick(locOne);
        BaseMethod.elementClick(locTwo);
    };
    */

    // Comination Three :works this way also
    //var BaseMethod = basefile;
    this.ddselection = function(locOne,locTwo){
        basefile.elementClick(locOne);
        basefile.elementClick(locTwo);
    };

};

module.exports = new tempPageOne();

Note for Comination Four Please look @Brine answer below

Upvotes: 1

Brine
Brine

Reputation: 3731

You could also extend basefile thusly...

var basefile = function() {
};

basefile.prototype.elementClick = function(locator) {
  element(by.xpath(locator)).click();
};  
module.exports = new basefile();

Then...

var basefile = require("./basefile.js");

var tempPageOne = function(){
  this.ddselection = function(locOne,locTwo){
    this.elementClick(locOne);
    this.elementClick(locTwo);
  };
};
tempPageOne.prototype = basefile; // extend basefile
module.exports = new tempPageOne();

Upvotes: 1

Related Questions