[英]Async code execution in protractor jasmine test
I have the below jasmine test case where I would like to store the values returned from the function getAllUsageCategoriesDropListElements into an array so that I can access the array inside the test case and evaluate it contents with another array. 我在下面的茉莉花测试用例中,我想将函数getAllUsageCategoriesDropListElements返回的值存储到一个数组中,这样我就可以访问测试用例中的数组并用另一个数组评估它的内容。
it('Validate the Usage Category droplist values matches with the Usage Categories Table',() => {
let allUsageCategoryDropListElements: string[] = [];
additionalCostsPage.getAllUsageCategoriesDropListElements(
element => {
console.log("Text from the usage Cateory Droplist elements " + element);
allUsageCategoryDropListElements.push(element);
console.log(" from inside " + allUsageCategoryDropListElements.length);
}
);
console.log("Size of the array is " +allUsagCategoryDropListElements.length );
});
Method is below: 方法如下:
getAllUsageCategoriesDropListElements(evaluationFunc: (element: string) => void) : void { getAllUsageCategoriesDropListElements(evaluationFunc:(element:string)=> void):void {
E2EUtil.click(this.addAdditionalCostDialogue.usageCategoryField);
E2EUtil.waitFor(this.addAdditionalCostDialogue.usageCategoryDropListContainer);
browser.sleep(2000);
var usageCategoryFromPage: string[] = [];
element.all(by.xpath("//*[@id='usageCategory']/div/div[3]/div/ul/li[*]/span"))
.each(function (element, index) {
element.getText().then(function (text){
// console.log("text extracted is " + text);
usageCategoryFromPage.push(text);
})
})
.then(function(){
usageCategoryFromPage.forEach(evaluationFunc);
});
}
Size of the array printed inside the function increments properly but when printed outside it 0. I think it is because of async code execution. 函数内部打印的数组大小正确增加,但在函数外部打印时大小为0。我认为这是由于执行异步代码所致。 Can any one please help ? 有人可以帮忙吗? I am very new to this typescript world. 我对这个打字稿世界很陌生。
To run your code with an await you will need to add the async
and await
keywords correctly. 要使用await运行代码,您将需要正确添加async
和await
关键字。 Try: 尝试:
it('Validate the Usage Category droplist values matches with the Usage Categories Table', async () => {
let allUsageCategoryDropListElements: string[] = [];
await additionalCostsPage.getAllUsageCategoriesDropListElements(
element => {
console.log("Text from the usage Cateory Droplist elements " + element);
allUsageCategoryDropListElements.push(element);
console.log(" from inside " + allUsageCategoryDropListElements.length);
}
);
console.log("Size of the array is " +allUsagCategoryDropListElements.length );
});
EDIT: Your getAllUsageCategoriesDropListElements
is not asynchronous but you are using promises. 编辑:您的getAllUsageCategoriesDropListElements
不是异步的,但您正在使用getAllUsageCategoriesDropListElements
。 You can update it to make it asynchronous and then your await/async
in the calling function will work correctly. 您可以对其进行更新以使其异步,然后调用函数中的await/async
将正常工作。
Try: 尝试:
async getAllUsageCategoriesDropListElements(evaluationFunc: (element: string) => void) : void {
E2EUtil.click(this.addAdditionalCostDialogue.usageCategoryField);
E2EUtil.waitFor(this.addAdditionalCostDialogue.usageCategoryDropListContainer);
browser.sleep(2000);
var usageCategoryFromPage: string[] = [];
let elements = element.all(by.xpath("//*[@id='usageCategory']/div/div[3]/div/ul/li[*]/span"));
for(var i = 0; i < elements.length; i++) {
let element = elements[i];
let text = await element.getText();
// console.log("text extracted is " + text);
usageCategoryFromPage.push(text);
}
usageCategoryFromPage.forEach(evaluationFunc);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.