selenium - mocha 控制台日志显示对象承诺
问题描述
开始掌握摩卡咖啡但我不明白的一件事,在下面的代码中
describe('03 Test recent recipe test', () => {
it('Test search async', async () => {
await driver.wait(until.elementLocated(By.name('selectit')));
var recipeName = driver.findElement(By.name('selectit')).getText();
driver.findElement(By.name('selectit')).click();
await driver.wait(until.elementLocated(By.id('name')));
var recipeLabel = driver.findElement(By.id('name')).getText();
await console.log(recipeName + " - " + recipeLabel);
expect(recipeName).to.contain(recipeLabel);
});
});
此测试作为通过返回,但 console.log 输出 - [object Promise] - [object Promise] 为什么会这样,期望测试很高兴它们匹配
解决方案
那是因为你误用了await
onconsole.log()
声明。
await
当你得到recipeName
andrecipeLabel
因为getText()
返回 a时,你应该这样做Promise
。
由于console.log()
不返回 aPromise
你不需要await
它。
作为旁注,通过await
ingconsole.log()
声明,它不会解决其中的承诺。
您的代码应如下所示:
describe('03 Test recent recipe test', () => {
it('Test search async', async () => {
await driver.wait(until.elementLocated(By.name('selectit')));
var recipeName = await driver.findElement(By.name('selectit')).getText();
await driver.findElement(By.name('selectit')).click();
await driver.wait(until.elementLocated(By.id('name')));
var recipeLabel = await driver.findElement(By.id('name')).getText();
console.log(recipeName + " - " + recipeLabel);
expect(recipeName).to.contain(recipeLabel);
});
});
要使用变量并使用它们进行打印,console.log()
您可以await
在其中的每一个变量中使用它们:
var recipeName = driver.findElement(By.name('selectit')).getText();
....
var recipeLabel = driver.findElement(By.id('name')).getText();
console.log(await recipeName + " - " + await recipeLabel);
推荐阅读
- java - SpringBoot + RabbitMQ 抛出错误:java.net.ConnectException:连接被拒绝
- vb.net - 使用复选框防止焦点更改到 Datagridview 中的下一行
- javascript - 显示错误“未知的自定义元素
" - jquery - 如何使用左侧文本和右侧图像 Bootstrap 4 制作 div
- ruby - BulkEnvelopesApi Docusign 请求数据格式
- hibernate - 如何转义作为 JPQL 中的实体名称的保留关键字 - Hibernate
- neural-network - 使用神经网络的模式识别
- css - React webpack 中的 scss 变量
- python-2.7 - 使用 pyplot 在 Python 2.7 上进行曲面绘图
- c# - ML.NET 无法在 uwp 上运行