Groovy Syntax for multiple list element declaration


#1

Ooh look! Now I get to use “Find a specialist”! (Like I didn’t already know them :wink: )

Callout to @Brandon_Hein @kazurayam @Marek_Melocik @Mate_Mrse (re Java/Groovy)

Is there a groovy-way to do this?

List myList = [“first”, “item” * 3, “last”]

The multiplier is (of course) bound to the string, producing “itemitemitem”. Is there a way to get [“first”, “item”, “item”, “item”, “last”] such that this notional method could be coded and get what I wish?

def thing(int mult) {
  List myList = ["first", "item" * mult, "last"]
}

#2
assert ['item'] * 3 == ['item', 'item', 'item']

assert ['first', ['middle'], 'last'].flatten() == ['first', 'middle', 'last']

def thing(int mult) {
	List myList = ['first', ['item'] * mult, 'last'].flatten()
}

assert thing(3) == ['first', 'item', 'item', 'item', 'last']

#3

image

image


#4

Dang, kaz beat me to it…


#5

Yei! :slightly_smiling_face:


#6

For posterity (and anyone wondering what the hell I wanted that for) …

  /**
   * Select an item from an ACE dropdown.
   * @param selector (String) CSS selector of the target control containing the ACE.
   * @param count (int) The number of downarrow keys to send to the ACE.
   * @param filter (String) Optional prefix to filter the ACE. (e.g. "Wayne").
   */
  void chooseACEItem(String selector, int count, String filter = "") {
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement elem = driver.findElement(By.cssSelector(selector))
    List keys = [Keys.RIGHT, [Keys.DOWN] * count, Keys.ENTER].flatten();
    type(selector, filter)
    for(def key in keys) { 
      Thread.sleep(400)
      elem.sendKeys(key)
    }
  }

ACE = AutoComplete Extender

http://www.ajaxcontroltoolkit.com/AutoComplete/AutoComplete.aspx


#7

#8

groovy way:

keys.each {
      Thread.sleep(400)
      elem.sendKeys(it)
}

:stuck_out_tongue: