Skip to content
/ .. / .. /
Custom Phone Allocation Stepdef





Custom Phone Allocation Stepdef

Now that we have configured our Glue path and deactivated the Built-in Telephony Stepdefs, we will implement the custom step Given phones as A and B:.

1. Creating a Steps File

Create a Steps file by right-clicking on the lang directory -> New -> Steps:

alt text

Name the Steps file CustomTelephonyStepdefs. intaQt Studio will append the .steps extension to the file.

Replace the template text with the code below:

1
2
3
stepdef "a? ?phones? as {phoneTags}:" / ids, details /

end

The Stepdef keyword needs a regular expression (Regex) and a list of parameters.

2. Regular Expression

The regular expression defines the text that this Stepdef uses. The question marks ? indicate optional characters. In this case, all the phrases below would match the regular expression:

  • a phone as A

  • phones as A and B

  • phone as B

Note
The leading Given, When, and Then must not be part of the Regex definition.

3. Parameters

This Stepdef is special, because it ends with a colon :. This means that it is a Compound Step that has detail clauses: lines starting with asterisks *. Those details clauses are mapped to the last parameter details that does not correspond to a pair of curly braces `{ }' in the Regex.

The Stepdef takes two parameters, ids and details:

  • The position of the parameters in the text phrase are indicated by the curly braces {}.

  • The first parameter, ids holds the phone identifiers (characters such as A or B).

  • The phoneTags keyword enclosed by the curly braces has a special meaning: it implicitly creates the given phones as VirtualPhones.

4. Revise the Stepdef and Feature File

Remove the phoneTags keyword form the braces and change our Stepdef. Now it only holds the phone allocation step:

1
2
3
4
stepdef "a? ?phones? as {}:" / ids, details /
    println("ids=" + ids)
    println("details=" + details)
end

Change the Feature File to the following:

1
2
3
4
5
6
Feature: A phone can dial another phone and connect within 30 seconds

  Scenario: Phone connection

    Given phones as A and B:
      * of type Android

This Step does not do anything useful aside from taking the two parameters ids and details and printing them.

Running the current Feature File, will display the error below:

1
2
3
4
5
6
7
12:54:23.581 [INFO]: Step started: phones as A and B:
      * of type Android
12:54:23.75 [INFO]: Step ended with status: failed
12:54:23.751 [INFO]: [Error: unresolvable property or identifier: A]
[Near : {... A and B ....}]
            ^
[Line: 1, Column: 1]

The elements A and B are not defined, meaning, they are not part of the scenario execution context.

Important!
An empty pair of curly braces { } in the Stepdef Regex tries to evaluate the corresponding expression against the scenario execution context before passing it on to the Stepdef. Since A and B are not there, the evaluation fails and the test will not run.

5. Fix the Stepdef

Add the phoneTags keyword to the custom Stepdef to fix the issued faced in Step 4:

1
2
3
4
stepdef "a? ?phones? as {phoneTags}:" / ids, details /
    println("ids=" + ids)
    println("details=" + details)
end

Run the test case again. At the bottom of the intaQt Studio window, you will see a list of executed steps, step details and their status.

Click on the step phones A and B:

alt text

The output from ids shows a list of two elements: [A, B]. By using the phoneTags keyword, A and B are implicitly created as Virtual Phones. We can now make use of these phones by adapting our Stepdef.

6. Access the Phone

Revise your Stepdef to the following:

1
2
3
4
5
6
7
8
stepdef "a? ?phones? as {phoneTags}:" / ids, details /
    println("ids= " + ids)
    println("details= " + details)
    for phone in ids
        p := getContextObject(phone)
        println("p is " + p)
    end
end

After printing the parameters, a for loop runs through the elements in ids and prints them out. Accessing the Scenario execution context is done using getContextObject(phone). Here that phone is the loop variable.

Note
A and B are part of the context since they were implicitly created when the stepdef started. This is why we can retrieve them and print them.

In the next section, we will create a virtual phone call. Read the information below for further details about phoneTags.

Additional Notes

Using the phoneTags Keyword: Single Phone

When using the phoneTags keyword for a stepdef parameter, the phones are always passed as a list.

Use the command below for a single phone like this:

1
2
Given phones as A:
    * of type Android

In this case, the parameter is still passed as a list of VirtualPhones, but with a single element.

Using the phoneTags Keyword: Multiple Phones

Use commas , and the word and as separators in lists when specifying multiple phones:

1
2
    Given phones as A and B, C and D:
          * of type Android

If you need a single VirtualPhone element, the phoneTag keyword may be used. The parameter is passed as a single phone (as opposed to a single element list). Details can be found in the intaQt manual.