我要在量角器中切换我的打字稿代码以使用async / await,并且我有一个verifyRow方法,如果没有.then promises,我似乎无法正常工作
尝试在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.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,则我们退出循环。
发生的事情是,当我在新的Promise中有代码时,它将调用this.find,但是它永远都不会返回值。.就像该代码被压入堆栈的末尾一样,所以我永不中断
答案 0 :(得分:0)
选项1)在Promise
中使用browser.wait()
,您应该在resolve(results)
内部而不是外部调用promise.then()
。
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);
// you should call resolve(..) inside then(),
// otherwise resolve() is executed prior to
// the completion of findRowByMultipleCellText()'s async execution.
});
});
// 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);
}
}
}
选项2)仅使用async/await
启用简明代码。
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(() => {
await rowIndex: number = 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');
await new VClientMainPage().refreshButton.click();
// Add await ahead of above click() too.
}
}
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);
}
}
}