【发布时间】:2019-07-09 20:56:27
【问题描述】:
我正在量角器中切换我的打字稿代码以使用 async / await 并且我有一个 verifyRow 方法,如果没有 .then 承诺,我似乎无法工作
尝试在 browser.wait 中使用 await 并尝试在 browser.wait 中添加新的 Promise,它调用 asycn 方法
findRowByMultipleCellText 被定义为一个异步方法,它返回一个数字(即 rowIndex)
async verifyRow(columns: string[], values: string[], shouldFind = true, timeout = 60, refresh = false) {
let results: boolean = false;
const columnValueString: string = this.generateColumnValuesPrintString(columns, values);
// Wait for the row to exist based on shouldFind
// let rowIndex: number;
await this.driver.wait(() => {
return new Promise(resolve => {
this.findRowByMultipleCellText(columns, values).then((rowIndex: number) => {
if (rowIndex >= 0 && shouldFind === true) {
results = true;
logger.debug('Row EXISTS in the grid: \n' + columnValueString);
} else if (rowIndex <= -1 && shouldFind === false) {
results = true;
logger.debug("Row DOESN'T exist in the grid: \n" + columnValueString);
} else {
if (refresh === true) {
logger.info('Refreshing the vsphere web client main page');
new VClientMainPage().refreshButton.click();
}
}
});
resolve(results);
});
// return results;
}, timeout * 1000);
if (results === false) {
if (shouldFind === true) {
throw new Error("Row DOESN'T exist in the grid: \n" + columnValueString);
} else {
throw new Error('Row EXIST in the grid: \n' + columnValueString);
}
}
}
我也试过这个,但是await this.find 中存在错误,因为await 只能在异步函数中调用
await this.driver.wait(() => {
let rowIndex: number = await this.findRowByMultipleCellText(columns, values);
if (rowIndex >= 0 && shouldFind === true) {
results = true;
logger.debug('Row EXISTS in the grid: \n' + columnValueString);
} else if (rowIndex <= -1 && shouldFind === false) {
results = true;
logger.debug("Row DOESN'T exist in the grid: \n" + columnValueString);
} else {
if (refresh === true) {
logger.info('Refreshing the vsphere web client main page');
new VClientMainPage().refreshButton.click();
}
}
}, timeout * 1000);
if (results === false) {
if (shouldFind === true) {
throw new Error("Row DOESN'T exist in the grid: \n" + columnValueString);
} else {
throw new Error('Row EXIST in the grid: \n' + columnValueString);
}
}
如果我在等待循环之外调用它,我需要 findRowByMultipleCellTest 返回一个数字。如果它 >= 0,我们会跳出循环。
发生的事情是,当我在 new Promise 中有代码时,它会调用 this.find,但它永远不会到达返回值的地步.. 几乎就像代码被推到堆栈的末尾一样,所以我永远不会中断出去
【问题讨论】:
-
这是怎么回事??? 1. 不清楚你到底想得到什么。 2. 不清楚你得到了什么错误。 3. 不清楚 if .. else, if ... else - 为什么?你的问题完全搞砸了