gpt4 book ai didi

javascript - 等待多个元素在 selenium webdriver JavaScript 绑定(bind)中可见

转载 作者:搜寻专家 更新时间:2023-10-31 23:35:00 25 4
gpt4 key购买 nike

JavaScript 的 selenium webdriver 绑定(bind)允许通过组合两个等待命令来等待元素可见,如下例所示:

const timeout = 1000;
const locator = webdriver.By.id('test');
driver.wait(webdriver.until.elementLocated(locator, timeout).then(function() {
that.findElement(locator).then(function(element) {
driver.wait(webdriver.until.elementIsVisible(element), timeout).then(function() {
// element is visible!
});
});
});

当我们需要等待一组元素一起可见时,是否有更简单的方法来做到这一点?如何做到这一点?

最佳答案

Promises 的一大优势是您可以使异步代码保持线性而不是嵌套(来自连续传递样式的回调 hell )。

Promises 给你 return 语句和错误抛出,你失去了 continuation passing 风格。

在你的情况下,你需要从你的 promise 返回函数中返回 promise ,这样你就可以链接你的 promise 。

示例:Promise.all 采用一系列 promise 并在所有 promise 都解决后解决,如果有任何被拒绝,则该数组被拒绝。

this.waitForElementsToBecomeVisible = function() {
return Promise.all([
driver.wait(webdriver.until.elementIsVisible(usernameTextField), 500),
driver.wait(webdriver.until.elementIsVisible(firstNameTextField), 500),
driver.wait(webdriver.until.elementIsVisible(lastNameTextField), 500),
driver.wait(webdriver.until.elementIsVisible(createEmployeeButton), 500)
]);
}

然后你可以链接你的 promise 。

driver.get('https://website.com/login').then(function () {
loginPage = new LoginPage(driver);
return loginPage.login('company.admin', 'password')
}).then(function () {
employeePage = new EmployeePage(driver);
return employeePage.clickAddEmployee()
}).then(function () {
addEmployeeForm = new AddEmployeeForm(driver);
/**
*
* Wait for elements to become visible
*/
return addEmployeeForm.waitForElementsToBecomeVisible();
}).then(function() {
return addEmployeeForm.completeForm(employee);
}).then(function() {
return addEmployeeForm.clickCreateEmployee();
}).then(function() {
return employeePage.searchEmployee(employee);
});

您可以看到上面的示例没有嵌套并且更易于维护。您返回一个 promise 并保持链接而不是嵌套。我希望这对您有所帮助,不会让您感到困惑。

关于javascript - 等待多个元素在 selenium webdriver JavaScript 绑定(bind)中可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com