Selenium webdriver 使用Thread.sleep()或显式等待再单击SeleniumWeb驱动程序中的任何元素是最佳做法吗
我是web驱动程序新手,我为web应用程序编写了一个selenium脚本,其中包含backbone.js和select2 我过去经常遇到NoTouchElementException和元素不可单击的异常。因此,我决定编写如下脚本, -在单击任何元素之前,它将使用显式等待等待元素的存在。 i、 e在单击任何元素之前,它将等待该元素加载Selenium webdriver 使用Thread.sleep()或显式等待再单击SeleniumWeb驱动程序中的任何元素是最佳做法吗,selenium-webdriver,automation,selenium-grid,Selenium Webdriver,Automation,Selenium Grid,我是web驱动程序新手,我为web应用程序编写了一个selenium脚本,其中包含backbone.js和select2 我过去经常遇到NoTouchElementException和元素不可单击的异常。因此,我决定编写如下脚本, -在单击任何元素之前,它将使用显式等待等待元素的存在。 i、 e在单击任何元素之前,它将等待该元素加载 在单击之前等待每个元素是否是最佳做法 显式等待特定元素及其特定状态是selenium webdriver中的最佳实践。睡眠从来都不是一个好主意,因为你的睡眠超时可能
在单击之前等待每个元素是否是最佳做法 显式等待特定元素及其特定状态是selenium webdriver中的最佳实践。睡眠从来都不是一个好主意,因为你的睡眠超时可能会更少或更多,从而使你的测试不一致和不确定 使用WebDriver是同步问题的最佳解决方案。所以在JS中,类似这样的东西
var until = webdriver.until;
var searchBox =
driver.wait(until.elementIsEnabled(driver.findElement(webdriver.By.name('q'))),5000,'Search button is not enabled');
建议使用显式等待来等待对webelement执行任何操作,而不是使用Thread.sleep()作为最佳实践 Selenium中的显式等待相当于具有指定条件的Thread.sleep()。这意味着,即使使用显式或隐式等待,也会间接使用Thread.sleep()。不同之处在于,您指定了等待的条件,并且知道在等待超时时何时抛出错误 如果您知道等待线程的确切时间,可以使用sleep(),但最好避免使用它。如果等待时间较长,测试可能会减慢,如果等待时间较短,测试可能会失败
- 类使用Sleeper类实现该目的
- 类实现
/* waitForTitle
* Before doing anything, make sure the actual title on the page we are visiting is the expected Title.
* @param {int} timeout
* @param {string} expectedTitle
*/
async waitForTitle(timeout, expectedTitle) {
let isTitle;
this.printLog('pageBase: wait for expected Page Title to be present when visiting a Page');
await this.driver.wait(async () => {
const actualTitle = await this.driver.getTitle();
isTitle = actualTitle;
return actualTitle.includes(expectedTitle);
}, timeout);
return isTitle.includes(expectedTitle);
}
//Now continue your test and do your assertion.