gpt4 book ai didi

javascript - 如何将 yield (generators) 与 selenium webdriver promises 一起使用?

转载 作者:行者123 更新时间:2023-11-30 17:47:40 25 4
gpt4 key购买 nike

我正尝试在 node 0.11.x 中使用生成器,让我编写 Selenium 测试的生活更轻松一些。我的问题是我不知道如何正确使用它们。我几乎 100% 肯定这一定是语法问题。

我正在使用官方 selenium-webdriver 模块(2.37.0 版)和 co(2.1.0 版)来创建我的生成器。

这是一个没有生成器/ yield 魔法的常规测试:

driver.isElementPresent(wd.By.css('.form-login')).then(function (isPresent) {
console.log(isPresent); // true
});

这里有 2 次尝试使用 yield/generator 魔法获得相同的结果:

var isPresent = yield browser.isElementPresent(wd.By.css('.form-login'));
console.log(isPresent); // undefined

var isPresent = yield browser.isElementPresent(wd.By.css('.form-login')).then(function (isPresent) {
console.log(isPresent); // true
});
console.log(isPresent); // undefined

如您所见,isPresent 始终是 undefined,除非在 promise 的 then() 回调中。我必须承认,我对生成器或 promise 都不太熟悉,所以我可能遗漏了一些非常明显的东西。

最佳答案

我想出了以下解决方案。它有效,但我不认为它是理想的。我感觉有更好/更简单的方法。

describe("The login form", function() {
it("should have an email, password and remember me fields and a submit button", function *() {

var results = [];
yield browser.isElementPresent(wd.By.css('.form-login'))
.then(function (isPresent) {
results.push(isPresent);
});
yield browser.isElementPresent(wd.By.css('.form-login input[name="email"]'))
.then(function (isPresent) {
results.push(isPresent);
});
yield browser.isElementPresent(wd.By.css('.form-login input[name="password"]'))
.then(function (isPresent) {
results.push(isPresent);
});
yield browser.isElementPresent(wd.By.css('.form-login button[type="submit"]'))
.then(function (isPresent) {
results.push(isPresent);
});

results.forEach( function (result) {
result.must.be.true();
});

});

});

关于javascript - 如何将 yield (generators) 与 selenium webdriver promises 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19785092/

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