如何在 JavaScript/CoffeeScript 中优雅地循环链接调用?

2022-01-24 00:00:00 javascript coffeescript dry

我正在使用 Soda 在 Node.js 中编写 Selenium 测试,我有一种情况,我必须按几次向下键.

目前的代码是这样的:

浏览器.链.setSpeed(200).会议().打开('/').click("id=save").focus(编辑).keyDown(编辑器,'\40').keyDown(编辑器,'\40').keyDown(编辑器,'\40').keyDown(编辑器,'\40').keyDown(编辑器,'\40').keyDown(编辑器,'\40').keyDown(编辑器,'\40').keyDown(编辑器,'\40').keyDown(编辑器,'\40').keyDown(编辑器,'\40')...

我怎么能把它弄干?

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

var b = browser.chain()for (var i = 0; i <10; i++) {b.keyDown(编辑器,'\40')}

好主意?

我可以使用 Soda 中的异步 API,例如 async-lib 来帮助我,但是这不是我在这里要问的.它使其他一些事情变得丑陋.

解决方案

在命令链的中间有一个方法叫做and 用来做复杂的事情:

浏览器.链.setSpeed(200).会议().打开('/').click("id=save").focus(编辑).and(函数(浏览器){for (var i = 0; i <10; i++) {browser.keyDown(编辑器,'\40')}})...

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

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.

解决方案

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

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

相关文章