【问题标题】:'then' block is getting executed before called function which returns promise'then' 块在返回 promise 的被调用函数之前执行
【发布时间】:2019-08-01 00:02:01
【问题描述】:

我是量角器和 javascript 的新手。我正在构建一个测试框架,其中每个测试套件都有相应的模块文件和定位器文件。在测试套件文件的测试用例中,通过传递参数调用测试模块。当我尝试将模块中的调用函数嵌套在“then”块中时;然后块在被调用函数完成之前开始执行。参考以下代码文件:

 UserPage.js // Test suit from where modules are called

var d = require("../Data/TestCaseData.js");
var login = require("../Modules/LoginPageModule.js");
var user = require("../Modules/UserPageModule.js");
var userl = require("../Locators/UserPageLocators");

describe('first test suit',function(){

    beforeEach(function(){

           browser.get(d.iManUrl).then(function(){
            browser.manage().window().maximize();
            login.OSPLogin(d.username,d.password);
        })
    });

    it ('Creating user',function(){
        var person = {'Username' : 'u6' , 'Context' : 'novell' , 'Last name' : 'last' , 'Password' : 'n'};

        user.InputData(person).then(function(){
        element(by.buttonText("Create")).click().then(function(){
            browser.sleep(5000);
        });
    })

    })


})

下面是模块:

var usercreated = false;

function UserPageModule(){

    this.InputData = function(person){

        return this.InputUserPasswordContext(person).then(function(person){

            this.fill =function(data){
                for (var key in data) {
                      console.log(key);
                      element(by.xpath(user.identificationAddValueButtonXpath.replace("Input Field", key))).click();
                      element(by.css(user.identificationValueInputCss)).sendKeys(person [ key ]);
                      element(by.buttonText(user.identificationAcceptValueButtonText)).click();               
                    }
            }
            return new Promise(function(resolve,request){
                 return resolve(this.fill(person));
            })
        });
    }

    /*this.InputData = function(person){
        this.InputUserPasswordContext(person).then(function(person){
            this.InputFields(person);
        });
    }*/


    this.InputUserPasswordContext = function(person){
            return new Promise(function(resolve,reject){
                //Navigating to user page
                element(by.css(landing.userLinkCss)).click();

                //Clicking on add button
                element(by.css(user.createuserButtonCss)).click();

                //Inputting Username
                element(by.css(user.usernameInputCss)).sendKeys(person.Username).then(function(){
                    delete person.Username;
                });

                //Input password
                element(by.css(user.identificationPasswordInputCss)).sendKeys(person.Password);
                element(by.css(user.identificationRetypeInputCss)).sendKeys(person.Password).then(function(){
                    delete person.Password;
                });

                //Inputting context
                element(by.css(user.contextInputCss)).sendKeys(person.Context).then(function(){
                    delete person.Context;
                }).then(function(){
                    resolve(person);
                })
            })                  
    }




}

module.exports = new UserPageModule

所有步骤都按顺序运行,直到“for循环”。但是在 for 循环之前执行块。我也没有收到任何错误。

【问题讨论】:

    标签: javascript promise protractor ui-automation


    【解决方案1】:

    对于 Javascript,每个 Function 都有一个隐含的 this。使用嵌套函数时,使用this很容易出错。

    function UserPageModule(){
    
        this.InputData = function(person){
    
            return this.InputUserPasswordContext(person).then(function abc(person){
    
    
              for (let key in data) {
                  console.log(key);
                  element(by.xpath(user.identificationAddValueButtonXpath.replace("Input Field", key))).click();
                  element(by.css(user.identificationValueInputCss)).sendKeys(person [ key ]);
                  element(by.buttonText(user.identificationAcceptValueButtonText)).click();               
              }
    
    
            }).catch(function(err){
               console.log('err: ' + err);
            });
        }
    
        this.InputUserPasswordContext = function(person){
          ...
        }
    
    }
    

    【讨论】:

    • 此解决方案无效。观察:1 - “return resolve();”返回的任何内容在“user.InputData(person).then(function(value){ element(by.buttonText(“Create”)).click(); }) 块之后执行函数 'xyz' 的块,这是测试的一部分有问题的案例块
    • 附加信息:在 beforeEach 部分 'login.OSPLogin(d.username,d.password);'不返回承诺。但我想,这不应该是问题。
    • 尝试将for (var key in data) {更改为for (let key in data) {
    • 我尝试改变但行为相同。即使我返回简单的语句或变量而不是“xyz”方法; 'then' 块首先被执行。
    • 我删除了return new Promise() 并添加了catch,以防 InputUserPasswordContext() 中出现错误。请重试。
    猜你喜欢
    • 2019-02-22
    • 2016-05-13
    • 1970-01-01
    • 2021-05-15
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2018-12-31
    相关资源
    最近更新 更多