无法从其他文件调用的javascript函数中返回量角器框架中的promise

时间:2019-03-09 19:41:14

标签: javascript promise protractor ui-automation

我是量角器和javascript的新手。我正在建立一个测试框架,其中每个测试服都有对应的模块文件和定位器文件。在测试文件的测试用例中,通过传递参数来调用测试模块。       当我尝试使用嵌套嵌套调用的函数时,我得到“ TypeError:无法读取未定义的属性'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(){
        //some steps
        };

    })


})

下面是模块:

/**
 * UserPageModule.js
 */
var obj = require("../Locators/LoginPageLocators");
var landing = require("../Locators/LandingPageLocators");
var user = require("../Locators/UserPageLocators");

var usercreated = false;

function UserPageModule(){

    this.InputData = function(person){

        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

现在,我感到用Java和Selenium制作模块化测试框架是多么容易。

2 个答案:

答案 0 :(得分:1)

您在功能return的顶层错过了InputData

this.InputData = function(person){

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

答案 1 :(得分:0)

您遇到的错误很常见,并且可能是由于return函数中缺少beforeEach引起的;如果您要向其添加return,则很可能在browser.get()之前需要.then()。我不太确定,但是请尝试以下操作:

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