Skip to content
QiTASC.com / General Concepts /
Before and After Scenario Hooks
/ .. /
Before and After Scenario Hooks





Before and After Scenario Hooks

Before and After Scenario Hooks (or Scenario Hooks), which are annotated with @Before and @After, are invoked either at the beginning or end of a Scenario. They are always automatically invoked when executing a test case, and can not be explicitly called. This results in passing one of the following:

  • Before Hooks - Before Hook functions have no parameters and are executed before every Scenario execution.

  • After Hooks - After Hook functions accept a single Scenario Result Object argument. They are executed following the execution of all steps, including deferred steps.

A project may contain multiple Scenario Hooks. If a project contains multiple Scenario Hooks, their order of execution will be random.

Note: Scenario Hooks are executed as part of every scenario run within the project they belong to. If a project contains more than one Scenario Hook, each Scenario Hook will be executed before the Scenario run (Before Hooks) or following each scenario run (After Hooks). Therefore, it is highly recommended to have a project organized in a way that prevents the unintended execution of Scenario Hooks.

Before Hooks

Before Hook functions have no parameters and are executed before every Scenario execution.

Syntax

1
@Before func <function Name>()

Parameter

  • function - The name assigned to the function called by the Before Hook

Example

1
2
3
4
5
model Mine
    @Before someFun()
        println("this will be executed before the scenario starts executing")
    end
en

After Hooks

After Hook functions accept a single Scenario Result Object argument. They are executed following the execution of all steps, including deferred steps.

Syntax

1
@After func <function Name>(<scenarioResult Object>)

Parameters

  • function - The name assigned to the function called by the After Hook

  • scenarioResult (optional) - The result object

    • In cases where the scenario result's information is not needed for the function, the parameter may be omitted

After Hooks Result Methods

The Scenario Result Object, stepResults makes Step execution information available including the start time, end time and result of a step. Each of these values are contained in the stepResults object of a given result object and can be accessed using startTime, endTime and resultString.

Syntax

1
<result Object>.stepResults

Parameters

  • result - The result object created by the After Hook

Available Methods

  • isPassed() - Returns true or false

  • isFailed() - Returns true or false

  • isSkipped() - Returns true or false

  • getResultAsString() - Returns passed, failed or skipped

  • getFeature() - Returns a string containing the feature name the scenario belongs to

  • getScenario() - Returns a string containing the scenario's name

  • getAttachments() - Returns a list of strings containing the paths to the attachments

    • If there are no attachments, this function returns an empty list
  • failedStep - Returns the step on which the scenario failed

    • Not returned if scenario passes
  • failedStepIndex - Returns the step index position (starting from 1) on which the scenario failed

    • Not returned if scenario passes
  • failureMessage - Returns the message accompanying the failed scenario

    • Not returned if scenario passes
  • stepText - Returns the text of the current step

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
model AfterScenarioHookModel
    @After func after(result)
        println("Scenario passed:" + result.isPassed())
        println("Scenario result: " + result.getResultAsString())
        println("Feature name: " + result.getFeature())
        println("Scenario name:" + result.getScenario())
        for j in result.stepResults
            println("Step: " + j.stepText)
            println("Step start: " + j.startTime)
            println("Step end: " + j.endTime)
            println("Step result: " + j.resultString)
            println("")
        end
        for i, f in result.attachments
            println("Attachment " + i + ": " + f)
        end
    end
end

Example 2

1
2
3
4
5
6
7
model AfterModel
   @After func afterScenario(result)
       println(result.failedStep)
       println(result.failedStepIndex)
       println(result.failureMessage)
   end
end