簡體   English   中英

從量角器頁面正確返回值

[英]Returning values correctly from a protractor page Object

我在量角器中編寫一些pageobject來模塊化我的代碼。 我無法從函數返回正確的值。

以下功能為我提供了正確的輸出。

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,這是初始值。 反正把它做到一個里面嗎?

element.all().each()返回一個promise,其最終值為null。 因此,您的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都是異步的,並且會返回諾言。

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

    })

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM