1

I have perhaps unusual situation where I have to get text out of an element in a helper file and then compare this text in spec file. For example:

Both page files:

this.matchText = function(elem) {
    return elem.getText();
};

Helper file:

        // page objects
        var calculationPage = require('../calculation_page/calculation_page.js');

        // variables
        var globalPrice = "";

        module.exports = exports = function() {
            describe('...
                it('...
                    // initialize page object
                    var calculation = new calculationPage();

                    // store price into global variable
                    calculation.matchText(calculation.price).then(function(text) {
                        globalPrice = text;
                    });

                   // verify price equals expected
                   expect(calculation.matchText(calculation.priceSum)).toContain(globalPrice);

                   ???
                });
            });
        }

How to store globalPrice as a variable that could be passed to spec file?

Spec file:

// page objects
var homePage = require('../home_page/home_page.js');

// helpers
var getPrice = require('../helpers/get_price.js');

describe('...
    it('...

        // helper - get price
        getPrice();

        // initialize page object
        var home = new homePage();

        // verify if price equals expected
        expect(home.matchText(home.price)).toContain(???);
    });
});

How to read global variable from helper file in spec file?

jurijk
  • 309
  • 8
  • 22

1 Answers1

1

You can dump any values you need globally onto Protractor global object - browser.

Lets say .. in helper file you need to store the value. Then do this - browser.globalPrice = text

And then this value would be available in your spec file. Access it from the browser object like any other value expect(home.matchText(home.price)).toContain(browser.globalPrice);

Please refer my answer @Protractor: initialise Global variable in one js and use the same var in other js

Community
  • 1
  • 1
AdityaReddy
  • 3,625
  • 12
  • 25