簡體   English   中英

從函數中的量角器promise中返回一個值

[英]Returning a value from a from a protractor promise inside a function

我正試圖從頁面獲取文本,然后在規范中進一步使用該文本來斷言另一個元素。

我已經粘貼了一個非常簡單的規范,你可以運行,如果函數的return語句在一個量角器promise promise return txt;你就不能從函數返回一個值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();

感謝@alecxe讓我從右腳開始。

我發現我現在用相當多的解決方案看完這個

通過引用傳遞對象,您可以動態添加屬性並在以后的規范中使用它們。

例:

describe('My Test', function () {
    var tempObject = {};

    it('should go get some text from the page', function () {
        browser.get('https://angularjs.org/');
        getTextFromElement(tempObject);    //pass an object by reference to populate with page text
    });

    it('should do some random other stuff', function () {
        $('div.someDiv').click();
    });

    it('should be able to use the text from the first page in this test', function () {
        console.log(tempObject.textFromFirstPage); //works!
    });
});

function getTextFromElement(tempObject) {
    $('a.some-link').getText().then(function (txt) {
        tempObject.textFromFirstPage = txt;
    });
}

以上都不適合我。 這對我有用:

    var item = element.all(by.xpath("some xpath here"));


    this.methodToGetTheText = function () {
       return Promise.resolve(item.getText().then(function (text) {
           return text;
       }));
    }

你打電話methodToGetTheText().then(從你的規范呢?值內.then()函數應該包含您的實際網頁的文本

暫無
暫無

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

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