简体   繁体   English

具有异步功能的承诺已迫在眉睫

[英]Promise with async function is not waiting to be fulfilled

I am struggling with the issue of Promise and async/await for last two days. 最近两天,我在Promise和async / await问题上苦苦挣扎。 I am trying to configure my protractor.conf.js that would get the browser name just at the starting of the suit and will join with the suit name. 我正在尝试配置我的protractor.conf.js,它将在诉讼开始时获得浏览器名称,并将与诉讼名称一起加入。 I have written jasmine allure reporter code in customized way so that I can get browser name in asynchronously and then use with the suit name. 我以定制的方式编写了茉莉花诱惑记者代码,这样我就可以异步获取浏览器名称,然后与西装名称一起使用。 But nothing working properly. 但是没有任何工作正常。 In the code I have tried, I get only suit name. 在我尝试过的代码中,我仅获得西装名称。 Browser name in few seconds later. 几秒钟后浏览器名称。 As a result I could not use that browser name in suit name. 结果,我无法在西装名称中使用该浏览器名称。 Here is my code in detail 这是我的详细代码

Edited 编辑

   var AllureReporter = function CustomJasmine2AllureReporter(userDefinedConfig, allureReporter) {


    let browser = {
     getCapabilities: function() {
        return new Promise(resolve => {
            setTimeout(() => {
                resolve({
                    get: str => str
                 });
            }, 2000);
        });
    }
};

    var result;
    let bName = (async () => {
        try {
            var result = (await browser.getCapabilities()).get('Browser Name');
            return result;
        } catch (err) {
            return "Error or smth"
        }
        })();

        this.suiteStarted = function(suite) {
                this.allure.startSuite(suite.fullName + result);
                console.log(suite.fullName + result);

        };

        // other methods like spec done,, spec description.

    }

the index code from Allure that can be changed is Allure可以更改的索引代码是

'use strict';
var assign = require('object-assign'),
            Suite = require('./beans/suite'),
            Test = require('./beans/test'),
            Step = require('./beans/step'),
            Attachment = require('./beans/attachment'),
            util = require('./util'),
            writer = require('./writer');

function Allure() {
    this.suites = [];
      this.options = {
            targetDir: 'allure-results'
            };
        }
    Allure.prototype.setOptions = function(options) {
            assign(this.options, options);
        };

        Allure.prototype.getCurrentSuite = function() {
            return this.suites[0];
        };



        Allure.prototype.startSuite = function(suiteName, timestamp) {

        this.suites.unshift(new Suite(suiteName,timestamp));
        };


    module.exports = Allure;

and the Suit.js class 和Suit.js类

    function Suite(name, timestamp) {
        this.name = name;
        this.start = timestamp || Date.now();
        this.testcases = [];
    }
    Suite.prototype.end = function(timestamp) {
        this.stop = timestamp || Date.now();
    };


    Suite.prototype.addTest = function(test) {
        this.testcases.push(test);
    };

    Suite.prototype.toXML = function() {
        var result = {
            '@': {
                'xmlns:ns2' : 'urn:model.allure.qatools.yandex.ru',
                start: this.start
            },
            name: this.name,
            title: this.name,
            'test-cases': {
                'test-case': this.testcases.map(function(testcase) {
                    return testcase.toXML();
                })
            }
        };


        if(this.stop) {
            result['@'].stop = this.stop;
        }

        return result;
    };

    module.exports = Suite;

I am getting this output after edited the question.the result is undefined in the suit name 编辑问题后得到此输出。结果在西装名称中未定义

Executing 7 defined specs...

Test Suites & Specs:
Test for correct login undefined

1. Test for correct login 
(node:9764) [DEP0005] DeprecationWarning: Buffer() is deprecated due to 
security and usability issues. Please use the Buffer.alloc(), 
Buffer.allocUnsafe(), or Buffer.from() methods instead.
√ Navigate to the login page (5520ms)
√ Click onto language button (406ms)
√ English Language is selected (417ms)
√ Correct user name is written into email field (609ms)
√ Correct password is written into password field (486ms)
√ Login button is clicked and home page is opened with Machine on left top 

menu (5622ms) √ Logout button is clicked and redirect to login page (4049ms) 菜单(5622ms)√单击注销按钮并重定向到登录页面(4049ms)

7 specs, 0 failures Finished in 17.127 seconds 7个规格,0个故障在17.127秒内完成

I want to get browser name after the the line 'Test Suites & Specs:' and want to add the name with suit name. 我想在“测试套件和规格:”行之后获取浏览器名称,并想添加带有西装名称的名称。

The function where you want to use await should be async. 您要使用await的函数应该是异步的。 I have made a small example for you. 我为你做了一个小例子。 hope it will help 希望对你有帮助

//The function we want to use wait in should be async!
async function myFunction() {
    //Using callback
    thisTakeSomeTime().then((res) => console.log(res)); //Will fire when time out is done. but continue to the next line

    //Using await
    let a = await thisTakeSomeTime();
    console.log(a);//will fire after waiting. a will be defined with the result.
}

function thisTakeSomeTime() {
    return new Promise((res) => {
        setTimeout(()=>{res("This is the result of the promise")}, 5000)
    })
}

myFunction();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM