从量角器页面Object正确返回值

问题描述 投票:0回答:1

我在量角器中编写了一些pageobjects来模块化我的代码。我无法从函数返回正确的值。

以下功能为我提供了正确的输出。

this.functionName = function(){
        var1 = '';
       return this.locator.getText().then((locatorText)=>{
            locatorNumber = locatorText;
            return locatorNumber;
        })

    }

但是这个功能不是

this.functionName = function(parameter){
    return this.locator1.each((locator1Each, index)=>{

           return locator1Each.getText().then((locator1EachText)=>{

                if (locator1EachText.toLowerCase().includes(parameter.toLowerCase())) {

                    return this.locator1Count.get(index).getText().then((locator1CountText)=>{

                        locator1CountString = "Showing " + locator1CountText + " results"

                        console.log(locator1CountString);

                        return locator1CountText;
                    })
                }
            }) 
        })     

我无法从此函数获取返回值。

var x = common.functionName('mapreduce').then((functionNameTextV)=>{
            console.log('functionNameText = ' + functionNameTextV);
})

任何帮助,将不胜感激

 it('description', function() {
    x = 0;
    y = 0;  
    z = 0;
    z1 = 0;
    locator.getText().then(function(var1){
      x = var1;
    }) 
    locator1.getText().then(function(var2){
      y = var2;
    }) 
//this is what the sum of the above two variables will be compared against
    locator2.getText().then(function(var3){
      z1=var3;
    })
    z = x + y;

    expect(z).toEqual(z1);
    });

我无法做到这一点。值始终为0,这是初始值。无论如何要在单一里面完成这个吗?

javascript protractor pageobjects
1个回答
0
投票

element.all().each()返回一个最终值为null的promise。所以你的functionNameTextV应该为null。

this.functionName= function(parameter){

    return this.locator1.getText().then(function(txts) {
        var index = txts.findIndex(function(txt){
            return txt.toLowerCase().includes(parameter.toLowerCase());
        });

        if(index > -1) {
            return locator1Count.get(index).getText().then((locator1CountText)=>{
                var locator1CountString = "Showing " + 
                                   locator1CountText + " results";
                console.log(locator1CountString);
                return locator1CountText;
            )};
        }

        return 'Not found such element: ' + parameter;
    })
}

common.functionName('mapreduce').then((functionNameTextV)=>{
   console.log('functionNameText = ' + functionNameTextV);
});

您需要注意所有Protractor API都是异步并返回promise。

it('description', function () {
    x = 0;
    y = 0;
    z = 0;
    z1 = 0;
    locator.getText().then(function (var1) {
        x = var1;
    })
    locator1.getText().then(function (var2) {
        y = var2;
    })
    //this is what the sum of the above two variables will be compared against
    locator2.getText().then(function (var3) {
        z1 = var3;
    })
    z = x + y;

    expect(z).toEqual(z1);

    // The `getText()` internal implement is Async, `getText()`
    // will return a promise as the execute result of `locator.getText()`
    // then Nodejs start to execute next code line.
    // the '.then(function callback())' is used to register a callback
    // to the promise which returned by previous `getText()`. 
    // And the registered callbac be invoked automatically when 
    // the Async execution inside 'getText()'completed.

    // Nodejs won't wait the callback to be invoked when it execute
    // the code line `locator.getText().then(...)`. 

    // Thus when `z = x + y` be executed, the variable x, y are still
    // with init value 0, the new assignment value inside then() have
    // not happened yet, because the getText() internal async execution
    // have not complete.
});

您可以使用Promise.all()使代码看起来简洁。

it('description', function () {
    x = 0;
    y = 0;
    z = 0;
    z1 = 0;

    Promise.all([
        locator.getText(),
        locator1.getText(),
        locator2.getText()
    ])
    .then(function (datas) {
        x = datas[0];
        y = datas[1]; 
        z1 = datas[2];

        z = x + y;
        expect(z).toEqual(z1);
    });  

});

或者使用嵌套的then()

it('description', function () {
    x = 0;
    y = 0;
    z = 0;
    z1 = 0;
    locator.getText().then(function (var1) {
        x = var1;

        locator1.getText().then(function (var2) {
            y = var2;
            locator2.getText().then(function (var3) {
                z1 = var3;
                z = x + y;
                expect(z).toEqual(z1);
            })
        })

    })

})
© www.soinside.com 2019 - 2024. All rights reserved.