量角器:在规范文件中使用帮助文件中的变量

Protractor: use variable from helper file in spec file

我可能遇到不寻常的情况,我必须从辅助文件中的元素中获取文本,然后在规范文件中比较该文本。例如:

两个页面文件:

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

帮助文件:

        // 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);

                   ???
                });
            });
        }

如何将 globalPrice 存储为可以传递给规范文件的变量?

规格文件:

// 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(???);
    });
});

如何从 spec 文件中的帮助文件中读取全局变量?

您可以将全局所需的任何值转储到 Protractor 全局对象上 - browser

假设 .. 在您需要存储值的帮助文件中。然后这样做 - browser.globalPrice = text

然后这个值将在您的规范文件中可用。像任何其他值一样从 browser 对象访问它 expect(home.matchText(home.price)).toContain(browser.globalPrice);

请参考我的回答@