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

时间:2019-01-14 11:12:20

标签: javascript promise async-await jasmine allure

最近两天,我在Promise和async / await问题上苦苦挣扎。我正在尝试配置我的protractor.conf.js,它将在诉讼开始时获得浏览器名称,并将与诉讼名称一起加入。我以定制的方式编写了茉莉花诱惑记者代码,这样我就可以异步获取浏览器名称,然后与西装名称一起使用。但是没有任何工作正常。在我尝试过的代码中,我仅获得西装名称。几秒钟后浏览器名称。结果,我无法在西装名称中使用该浏览器名称。这是我的详细代码

已编辑

   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.

    }

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;

和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;

编辑问题后,我得到此输出。结果在西装名称中未定义

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 

菜单(5622ms)     √单击注销按钮,并重定向到登录页面(4049ms)

7个规格,0个故障 在17.127秒内完成

我想在“测试套件和规格:”行之后获取浏览器名称,并想添加带有西装名称的名称。

1 个答案:

答案 0 :(得分:0)

您要使用await的函数应该是异步的。 我为你做了一个小例子。希望对您有帮助

//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();