En renvoyant une valeur de un à partir d'un rapporteur promesse à l'intérieur d'une fonction

Je vais essayer d'obtenir le texte d'une page, et ensuite utiliser ce texte plus bas dans la spec à faire valoir sur un autre élément.

Je l'ai collé un très simple spec, vous pouvez exécuter qui montre que vous ne pouvez pas retourner une valeur à partir d'une fonction si la fonction de l'instruction return est à l'intérieur d'un rapporteur promesse return txt; (ligne 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?
    });
}

Mise à jour de l'extrait de code suivant @alecxe réponse et mon commentaire.

Je tente de contruct un objet de diverses texte sur une page et de le renvoyer à faire valoir sur une page ultérieure...

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;
}
Placer le code que vous voulez exécuter après le retour à l'intérieur d'une fonction anonyme et de le passer à getTextFromElements en tant que paramètre. Puis il suffit d'appeler ce paramètre comme une fonction de l'endroit où vous êtes en essayant de retourner la valeur.

OriginalL'auteur luker02 | 2015-08-10