{ closure -> Groovy Closures }

A closure in Groovy is an open, anonymous, block of code that can :
 > take arguments
 > return a value and
 > be assigned to a variable
 Before we deep dive into the concept, here is an example
Normal method

Closure   
  








                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
A method in groovy can even take closure as an argument .For instance the println method takes squareMe as an agument.
Closure definition: squareMe={num -> num*num}
i.e. { [closureParameters -> ] statements }
Parameters could be comma delimited list and is optional  and statements are 0 or more groovy statements
Calling closures .call()
 
                                                                                                                     
Assertion


















Implementation in Geb for browser automation



















The Page superclass defines a static closure property called content that describes the page content.
i.e static content={page objects}
Geb uses jquery $() to identify page objects. In cases where the attributes are to be parametarized as at line 13, it can be done by passing parameter to the closure "name" where 'ids' is the parameter. Similarly compare the line 17 and 18. When parametarized, line 33 can be used to feed "pushbutton-wide" attribute to line 17

This could be really helpful in cases when user would want to interact with 2nd item in a dropdown list, closure would provide leverage of specifying the index of the item.

Worth reading interact closures :Interact closures (or Actions) can be used to perform behaviors that are more complicated than clicking buttons and anchors or typing in input fields.

Comments

Popular Posts