如何在JavaScript / CoffeeScript中优雅地循环链接调用? [英] How to loop chained calls elegantly in JavaScript/CoffeeScript?

查看:48
本文介绍了如何在JavaScript / CoffeeScript中优雅地循环链接调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Soda 在Node.js中编写Selenium测试,但遇到这种情况必须多次按下向下键。

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.

当前代码如下:

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')                                                  

  ...                                                                       

我该如何干燥呢?

仅使用像这样的循环不适用于此库:

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')                                                 
}                                                                           

很棒的主意吗?

我可以在Soda中使用异步API,例如 async -lib 来帮助我,但这不是我要问的。

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.

推荐答案

有一种方法叫做在命令链中间执行复杂的操作:

There is a method called and for doing complicated things in the middle of a command chain:

browser
  .chain
  .setSpeed(200)
  .session()
  .open('/')
  .click("id=save")
  .focus(editor)
  .and(function (browser) {
    for (var i = 0; i < 10; i++) {
      browser.keyDown(editor, '\\40')
    }
  })
  ...

有关详细信息,请参见自述文件: https://github.com/learnboost/soda

See the README for more information: https://github.com/learnboost/soda

这篇关于如何在JavaScript / CoffeeScript中优雅地循环链接调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆