【问题标题】:Protractor: use variable from helper file in spec file量角器:在规范文件中使用帮助文件中的变量
【发布时间】:2017-03-09 22:15:21
【问题描述】:

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

两个页面文件:

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

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

【问题讨论】:

  • 为什么您的帮助文件中有describeitexpect 块?
  • 因为目前我在一个规范中将它们称为助手,但会将它们更改为规范文件。

标签: protractor global-variables helper


【解决方案1】:

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

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

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

请参考我的回答@Protractor: initialise Global variable in one js and use the same var in other js

【讨论】:

  • 谢谢!就是这个! ;) 正是我想要的。
  • 只是一个简单的问题;)我可以按照你写的方式存储任何东西?
  • @jurijk .. 欢迎!是的,你可以存储任何东西......复杂的对象,引用任何东西
猜你喜欢
  • 2020-04-25
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多