Script parameterization

Q7, 20.12.2012 by Ivan Inozemtsev View Comments

Note: this post is outdated and the same functionality can be achieved in more efficient way by using supercontexts.

One of the most asked features for Q7 is to execute a same test with different parameters. I’m going to demonstrate how it is possible to achieve the same effect using ECL. As an example, let’s test error messages in JDT New Class Wizard. We start with a Java project with a single class:

At first, let’s record an initial script which launches new class wizard, sets package and class name and asserts an error message:
get-view "Package Explorer" | get-tree | select example | get-menu "New/Class" | click
with [get-window "New Java Class"] {
    get-editbox -after [get-label "Package:"] | set-text "org.example"
    get-editbox -after [get-label "Name:"] | set-text Class1
    get-editbox -after [get-label "Java Class"] | get-property text |
       equals "Type already exists." | verify-true
}

get-window "New Java Class" | get-button Cancel | click
Now, what we really want is to type various values into Package and Name fields and make sure that error message changes accordingly. As a next step, let’s parameterize a script using get-param ande set-param commands:
get-view "Package Explorer" | get-tree | select example | get-menu "New/Class" | click

set-param package "org.example"
set-param class "Class1"
set-param message "Type already exists."

with [get-window "New Java Class"] {
    get-editbox -after [get-label "Package:"] | set-text [get-param package]
    get-editbox -after [get-label "Name:"] | set-text [get-param class]
    get-editbox -after [get-label "Java Class"] | get-property text |
       equals [get-param message] | verify-true
}
get-window "New Java Class" | get-button Cancel | click
So there are two logical blocks now: the first one sets parameters, the second one performs parameterized actions. So ideally we want to have a several “parameter” blocks and execute an “action” block after each of them. In ECL we can do that using two commands – emit and repeat-with. The first one just takes an arbitrary number of arguments of any type and writes them into an output pipe, so we can pass several “parameter” scripts to it, and it will write them into output. The repeat-with is more tricky and does the following:
  1. accepts a script from an argument and reads scripts from input pipe
  2. for each script from input pipe, executes an input script followed by the argument script
  3. writes aggregated output
Therefore, we can emit multiple parameter blocks and repeat them with a given action block:
emit {
   set-param package "org.example"
   set-param class "Class1"
   set-param message "Type already exists."
} {
   set-param package ""
   set-param class "Class1"
   set-param message "The use of the default package is discouraged."
} | repeat-with {
   with [get-window "New Java Class"] {
      get-editbox -after [get-label "Package:"] | set-text [get-param package]
      get-editbox -after [get-label "Name:"] | set-text [get-param class]
      get-editbox -after [get-label "Java Class"] | get-property text |
       equals [get-param message] | verify-true
   }
}
get-window "New Java Class" | get-button Cancel | click
The example above uses only two paramerer blocks, but there can be as many such blocks as necessary, take a look at this video:

blog comments powered by Disqus

Functional and UI Testing for Eclipse-based Applications

Test automation solution, handcrafted for Eclipse. Modern and easy to learn IDE.

Product Health Analytics and Support Automation Platform

End-user and product analytics solution that gives you rich insights into your application health & performance.

Eclipse-based IDE for the Fantom Programming Language

Full-featured Integrated Development Environment for emerging Fantom programming language.