How to loop chained calls elegantly in JavaScript/

2019-04-15 06:03发布

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.

3条回答
看我几分像从前
2楼-- · 2019-04-15 06:52

You're close. You just have to change b in the loop so it chains correctly.

var b = browser.chain()                                                      
for (var i = 0; i < 10; i++) {                                               
  b = b.keyDown(editor, '\\40')                                                  
}    
查看更多
放荡不羁爱自由
3楼-- · 2019-04-15 06:53

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

查看更多
虎瘦雄心在
4楼-- · 2019-04-15 06:55

Did you try replacing the b variable in your loop?

var b = browser.chain()                                                     
for (var i = 0; i < 10; i++) {                                              
  b = b.keyDown(editor, '\\40')                                                 
}                                                                           
查看更多
登录 后发表回答