如何在 JavaScript/CoffeeScript 中优雅地循环链式调用?
我正在使用 Soda 在 Node.js 中编写 Selenium 测试,我遇到了必须按的情况按向下键几次。
目前的代码如下所示:
browser
.chain
.setSpeed(200)
.session()
.open('/')
.click("id=save")
.focus(editor)
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
...
我怎样才能将其干燥?
仅使用这样的循环不适用于此库:
var b = browser.chain()
for (var i = 0; i < 10; i++) {
b.keyDown(editor, '\\40')
}
很棒的想法吗?
我可以使用 Soda 中的异步 API,例如 async-lib 来帮助我,但这不是我在这里问。它使其他一些事情变得丑陋。
I'm using Soda to write Selenium tests in Node.js and I have a situation where I have to press the down key several times.
The code currently looks like this:
browser
.chain
.setSpeed(200)
.session()
.open('/')
.click("id=save")
.focus(editor)
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
.keyDown(editor, '\\40')
...
How could I DRY this up?
Just using a loop like this does not work with this lib:
var b = browser.chain()
for (var i = 0; i < 10; i++) {
b.keyDown(editor, '\\40')
}
Awesome ideas?
I could use the async API in Soda and for example async-lib to help me out, but that's not what I'm asking here. It makes some other things ugly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你很接近了。您只需更改循环中的 b 即可正确链接。
You're close. You just have to change b in the loop so it chains correctly.
有一种名为
and
的方法,用于在命令链中间执行复杂的操作:请参阅自述文件以获取更多信息:https://github.com/learnboost/soda
There is a method called
and
for doing complicated things in the middle of a command chain:See the README for more information: https://github.com/learnboost/soda
您是否尝试替换循环中的
b
变量?Did you try replacing the
b
variable in your loop?