【问题标题】:Unable to pass values from one js file to another js file protractor e2e testing无法将值从一个 js 文件传递​​到另一个 js 文件量角器 e2e 测试
【发布时间】:2015-07-14 16:04:39
【问题描述】:

下面是我无法将值从 GenericUtil.js 返回到 homepage.js 的代码

我正在尝试实现 Page 对象以及混合驱动框架。

//GenericUtil.js:

var blnFlag;
GenericUtilities = function(){
    this.objClick = function(objLocator){
        element.all(objLocator).then(function(items) {
            if (items.length == 1) {
                element(objLocator).click();
                blnFlag='True';
                console.log("Inside if" + blnFlag);
            }
            else {
                blnFlag='False';
                console.log("inside else" + blnFlag);
            };
        });
        return blnFlag;
    };
};
module.exports = new GenericUtilities();

//home_page.js:

var blnFlag;
var gu = require("../GenericUtilities/GenericUtil.js");
var home_page = function(){
    this.clickContinue = function(){
        blnFlag = gu.objClick(home_page_obj.btnContinue);
        console.log("After return" + blnFlag );
    };
};
module.exports = new home_page();

//The value is being returned as undefined.

【问题讨论】:

    标签: javascript angularjs protractor pageobjects


    【解决方案1】:

    试试这样的:

    //GenericUtil.js:
    var blnFlag,
        GenericUtilities = function(){
           this.objClick = function(objLocator){
               element.all(objLocator).then(function(items) {
                   if (items.length == 1) {
                       element(objLocator).click();
                       blnFlag='True';
                       console.log("Inside if" + blnFlag);
                   }
                   else {
                       blnFlag='False';
                       console.log("inside else" + blnFlag);
                   };
               });
               return blnFlag;
           };
       };
    
    
    module.exports = GenericUtilities;
    
    //home_page.js:
    
    var blnFlag,
        GenericUtilities = require("../GenericUtilities/GenericUtil.js"),
        gu = new GenericUtilities(),
        home_page = function(){
            this.clickContinue = function(){
               blnFlag = gu.objClick(home_page_obj.btnContinue);
               console.log("After return" + blnFlag );
            };
        };
    
    
    
    module.exports = new home_page();
    
    //The value is being returned as undefined.
    

    编辑:这就是我当时所做的

    //GenericUtil.js:
    var GenericUtilities = function () {
       this.bnlFlag = 'False';
       this.action = function () {
           this.bnlFlag = 'True';
       };
    };
    
    module.exports = new GenericUtilities();
    
    
    //home_page.js:
    var bnlFlag,
        gu = require("./generic.js"),
        home_page = function(){
            this.clickContinue = function(){
                bnlFlag = gu.bnlFlag;
                console.log("Before return " + bnlFlag );
                gu.action();
                bnlFlag = gu.bnlFlag;
                console.log("After return " + bnlFlag );
            };
        };
    
    module.exports = new home_page();
    
    // test in an other file
    var home = require('./home.js');
    home.clickContinue();
    

    这是有效的:

    Before return False
    After return True
    

    【讨论】:

    • 不走运。它仍然返回 undefined。我想将 GenericUtilities.js 中的 blnFlag 值返回给 home_page.js。
    • 但是如果你看到我在 objClick 函数中编写的代码,那就不一样了。在 objClick 函数中,我试图验证元素的计数,如果计数为 1 并且需要对其执行一些操作并且应该返回 true,否则它应该返回 false。我相信 .then(function.........) 是异步函数调用,它不允许返回值。请给我建议。
    猜你喜欢
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2018-07-23
    • 2020-07-17
    • 1970-01-01
    • 2021-09-05
    • 2021-12-10
    • 2015-09-07
    相关资源
    最近更新 更多