Skip to main content

<context>

Description

The context tag overrides the $client and  $session variables’ values in test cases.

Attributes

var defines the variable for which the new value is set. Possible values for the attribute: client and session.

Body

A JSON object.

How field values are overridden within a session

  • If you have not set field values, the ones you specify will be used.
  • If you have already set values for some fields, they will be overridden.

The values specified in the script can also be overridden.

For example, if you specify in the script:

state: Password
a: Enter the password, so that the main bot features become available.

state: GetPassword
q: qwerty
script:
$client.account = $client.account || {};
$client.account.password = "qwerty";
a: This password is insecure.

And in the test-case, you set:

<context var="client">
{
"name": "Arnie",
"account": {"username": "arnie2000"}
}
</context>

And then:

<context var="client">
{
"name": "Arnold"
}
</context>

As a result, the value of the $client object will be as follows:

{
"name": "Arnold",
"account": {"username": "arnie2000", "password": "qwerty"}
}

Example

Let’s consider a script:

theme: /

state: Start
q!: $regex</start>
a: Let’s play a game. I’ll write a quote and you’ll try to guess the author. Ok?

state: Quote
q!: (ok | sure | great)
script:
# An HTTP request to API is executed.
var response = $http.get("https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en");
# The response data is saved to the $session object.
$session.quoteText = response.data.quoteText;
$session.quoteAuthor = response.data.quoteAuthor;
# The bot checks if the response contains information about the author.
if: $session.quoteAuthor !== ""
go!: /Quote/Who
else:
a: I have a great quote, but I don’t know the author. This is the quote: {{$session.quoteText}}
a: Let’s try again?

state: Who
a: Who is the author for the quote: {{$session.quoteText}}?

state: GetAuthor
q!: *
# The bot checks if the user response matches the author from $session.quoteAuthor.
if: $session.quoteAuthor === $parseTree.text
a: Great! You’re right!
else:
a: Please, try again.

The case for testing the user answer may be as follows:

<test>
<test-case id="AnswerCheck">
<!-- The test case checks if the user response matches the author of the quote -->
<context var="session">
{
"quoteAuthor": "Galileo Galilei"
}
</context>
<q>Nicolaus Copernicus</q>
<a>Please, try again.</a>
<q>Galileo Galilei</q>
<a>Great! You’re right!</a>
</test-case>
</test>

Refer to Testing a script with variables for a detailed example of the <context> tag usage.