Rafael C.
Rafael C.

Reputation: 2345

Use the value of a variable in another file

Currently I am using protractor and using page object, so there is a file that I get the value of an element in a variable, but I need to call this value in another file.

vehiclePage.js

/*jshint esversion: 6 */

var basePage = require('./basePage.js');
var homePage = require('./homePage.js');

var VehiclePage = function() {

    this.storeVehicleData = function() {

        this.pessengersRuntValue = element(by.id('preview_ocupantes_runt')).getText();
    };
};

VehiclePage.prototype = basePage; // extend basePage...

module.exports = new VehiclePage();

Now I need to use the value of the above variables in another file

checkoutPage.js

/*jshint esversion: 6 */

var basePage = require('./basePage.js');
var homePage = require('./homePage.js');

var CheckoutPage = function() {

    this.getRuntValue = element(by.css('.mb10'));

    this.compareValues = function() {

        expect(this.getRuntValue.getText()).toContain(this.pessengersRuntValue);
    };

};

CheckoutPage.prototype = basePage; // extend basePage...

module.exports = new CheckoutPage();

How can I make it work?

Upvotes: 0

Views: 1716

Answers (2)

danggrianto
danggrianto

Reputation: 371

If you are following Page Object Design Pattern, I would say that the test should not be on the page object. I will write something like this.

VehiclePage.js

var VehiclePage = function(){
  // if this is a browser testing something like this
  browser.get('/vehicle');
};

VehiclePage.prototype = Object.create({}, {
  runt: {
    get: function(){
      return element(by.id('preview_ocupantes_runt'));
    }
  }
});
module.export = VehiclePage;

CheckOutPage.js

var CheckOutPage = function(){
  // if this is a browser testing something like this
  browser.get('/checkout');
};

CheckOutPage.prototype = Object.create({}, {
  runt: {
    get: function(){
      return element(by.css('.mb10'));
    }
  }
});
module.export = CheckOutPage;

TheTest.js

var VehiclePage = require('VehiclePage');
var CheckOutPage = require('CheckOutPage');


describe('testing something', () => {
  var vehicle = new VehiclePage();
  var checkout = new CheckOutPage();

  it('should contain', () => {
    expect(checkout.runt.getText()).toContains(vehicle.runt.getText());
  });

});

Upvotes: 1

Ryan Villanueva
Ryan Villanueva

Reputation: 116

One way to do this would be to pass a state object to both pages.

var VehiclePage = require('./vehiclePage.js');
var CheckoutPage = require('./checkoutPage.js');

class StateStorage {
  constructor(){
    this.savedVariable = null;
  }
}

var state = new StateStorage();
var vehiclePage = new VehiclePage(state);
var checkoutPage = new CheckoutPage(state);

Then you can manipulate and access the state from both new pages.

Upvotes: 0

Related Questions