Javascript 异步函数的承诺未等待实现
在过去的两天里,我一直在努力解决承诺和异步/等待的问题。我正在尝试配置我的dragrator.conf.js,它将在套装开始时获取浏览器名称,并与套装名称合并。我已经以定制的方式编写了jasmine allure reporter代码,这样我就可以异步获取浏览器名称,然后和套装名称一起使用。但是没有什么能正常工作。在我尝试过的代码中,我只得到了西装名称。浏览器名称在几秒钟后。因此,我无法在套装名称中使用该浏览器名称。下面是我的详细代码 已编辑Javascript 异步函数的承诺未等待实现,javascript,promise,async-await,jasmine,allure,Javascript,Promise,Async Await,Jasmine,Allure,在过去的两天里,我一直在努力解决承诺和异步/等待的问题。我正在尝试配置我的dragrator.conf.js,它将在套装开始时获取浏览器名称,并与套装名称合并。我已经以定制的方式编写了jasmine allure reporter代码,这样我就可以异步获取浏览器名称,然后和套装名称一起使用。但是没有什么能正常工作。在我尝试过的代码中,我只得到了西装名称。浏览器名称在几秒钟后。因此,我无法在套装名称中使用该浏览器名称。下面是我的详细代码 已编辑 var AllureReporter = fu
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结束
我想在“Test Suites&Specs:”行后面获得浏览器名称,并想用suit name添加名称。要使用wait的函数应该是异步的。 我给你举了个小例子。希望能有所帮助
//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();
我假设,在这一点上,我的意思是,问题的产生是因为你用括号将
wait Promise()
括起来,以便在之后获得browserName
。将此内容分成两行,等待承诺,然后在下一行访问等待的结果,并询问姓名,尝试一下。再说一次,我完全可以wrong@Dellirium你能给我一个例子,这就是我理解你想要的密钥外卖,在异步函数中,你需要返回一个value@Dellirium我已根据您的答案编辑了代码,但现在发现了新错误。我把我的新错误说了出来question@Ido_Cohen我已经编辑了我的代码并发现了我在问题中描述的新错误。您的作用域仍然不是异步的。正如我所说。要使用wait,整个作用域/函数应为异步函数类型。var AllureReporter=异步函数CustomJasmine2AllureReporter(userDefinedConfig,AllureReporter)。。。