编程语言
首页 > 编程语言> > javascript-从函数内的量角器Promise返回值

javascript-从函数内的量角器Promise返回值

作者:互联网

我正在尝试从页面获取文本,然后在规范中进一步使用该文本来声明另一个元素.

我粘贴了一个可以运行的非常简单的规范,该规范表明如果函数的return语句位于量角器promise return txt内,则无法从函数返回值; (第24行)…

describe('My Test', function () {
    var tempVariable;

    it('should go get some text from the page', function () {
        browser.get('https://angularjs.org/');
        tempVariable = getTextFromElement();    //it appears javascript immediately sets this variable before waiting for protractor to return the value
    });

    it('should do some random other stuff', function () {
        element.all(by.cssContainingText('a', 'Learn')).get(0).click();
        element.all(by.cssContainingText('a', 'Case Studies')).get(0).click();
        element.all(by.cssContainingText('a', ' Home')).get(0).click();
    });

    it('should be able to use the text from the first page in this test', function () {
        console.log('\ntempVariable: ' + tempVariable);    //this is undefined!
        expect(typeof tempVariable).not.toBe('undefined', 'failed: tempVariable was undefined!');
    });
});

function getTextFromElement() {
    $('a.learn-link').getText().then(function (txt) {
        console.log('\nInitial text:   ' + txt);
        return txt;     //how do we return this so it's available to other 'it' blocks?
    });
}

@alecxe答案和我的评论之后的更新代码段.

我正在尝试从页面上的各种文本构造对象,然后将其返回以在以后的页面中进行断言…

function getRandomProductFromList() {
    var Product = function (line, name, subname, units) {
        this.line       = line;
        this.name       = name;
        this.subname    = subname;
        this.units      = units;
    };

    var myProduct = new Product();

    myProduct.line = 'Ford';
    myProduct.units = 235;

    //select a random product on the page and add it to 'myProduct'
    var allProducts = element.all('div.product');
    allProducts.count().then(function (count) {
        var randomIndex = Math.floor(Math.random() * count);
        var productName = allProducts.get(randomIndex);

        productName.getText().then(function (prodName) {
            myProduct.name = prodName;
            productName.click();
        });
    });

    //If a sub-product can be chosen, select it and add it to 'myProduct'
    var subproduct = $('div.subproduct');
    subproduct.isDisplayed().then(function (subProductExists) {
        if (subProductExists) {
            subproduct.getText().then(function (subProductName) {
                myProduct.subname = subProductName;
            });
            subproduct.click();
        }
    }, function (err) {});

    return myProduct;
}

解决方法:

首先,您不会从该函数返回任何信息:

function getTextFromElement() {
    return $('a.learn-link').getText();
}

现在,此函数将向您返回一个承诺,您需要在使用前解决该承诺:

it('should be able to use the text from the first page in this test', function () {
    tempVariable.then(function (tempVariableValue) {
        console.log('\ntempVariable: ' + tempVariableValue);    
        expect(typeof tempVariableValue).not.toBe('undefined', 'failed: tempVariable was undefined!');
    });
});

另外,要确定是否定义了变量,我将使用jasmine-matchers的toBeDefined():

expect(tempVariableValue).toBeDefined();

标签:node-js,selenium-webdriver,testing,protractor,javascript
来源: https://codeday.me/bug/20191119/2039676.html