[英]Using promises in selenium webdriver
I'm using webdriverJS to do some automated testing in chrome. 我正在使用webdriverJS在chrome中进行一些自动化测试。
I'm currently waiting for an element to be displayed on a webpage before clicking it, although the issue I'm having is the element takes a few seconds to display before being clickable, so I need the driver to sleep for a few seconds before clicking the element. 我目前正在等待某个元素在单击之前显示在网页上,尽管我遇到的问题是该元素需要几秒钟才能显示出来,然后才能被单击,因此我需要驱动程序先休眠几秒钟再单击元素。 I was wondering how I could use promises with webdriverJS to achieve this.
我想知道如何在webdriverJS中使用promises来实现这一目标。
return driver.wait(until.elementLocated(By.className('elementName')), 5000)
.then(element => {
driver.sleep(2000);
element.click();
});
Since javascript is asynchronous the element is trying to be clicked before the sleep so I was wondering how I can incorporate promises in webdriverJS so that the click only happens after the sleep has finished. 由于javascript是异步的,因此该元素正试图在睡眠之前被单击,因此我想知道如何将promises合并到webdriverJS中,以便仅在睡眠完成后才进行单击。
According to the webdriver documentation , the sleep method returns a promise which is resolved when the time is reached. 根据webdriver文档 ,sleep方法将返回一个promise,该promise将在到达时间时解决。
You can handle the asynchronous part with this code : 您可以使用以下代码处理异步部分:
let element
return driver.wait(until.elementLocated(By.className('elementName')), 5000)
.then(_element => {
element = _element
return driver.sleep(2000)
})
.then(() => element.click())
With the async/await feature : 使用异步/等待功能:
const func = async driver => {
const element = await driver.wait(until.elementLocated(By.className('elementName')), 5000)
await driver.sleep(2000)
element.click()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.