# Assertion

**Assertion** nodes are used in writing tests. Each **Assertion** node consists of several assertions - statements that allow you to test certain assumptions. When you run an **Assertion** node, you run all assertions. If any test raises an error, the entire **Assertion** node run is unsuccessful.

An **Assertion** node can only be created as a child of a [RequestStep](https://docs.testmace.com/master/node-types/request-step) node. At the same time, a [RequestStep](https://docs.testmace.com/master/node-types/request-step) node can have only one **Assertion** node as a child.

There are two ways to create an **Assertion** node. First, choose **Add node -> Assertion** from a [RequestStep](https://docs.testmace.com/master/node-types/request-step) node context menu. Second, click **+ CREATE NEW ASSERTION NODE** on the Assertion tab of the [RequestStep](https://docs.testmace.com/master/node-types/request-step) node response area.

![Creating an Assertion node in RequestStep node response area](https://1914040845-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYfhIaTTw23Sf3L5jfe%2F-LglKEp1bR5zbag8DHT5%2F-LglNkc9IYC5Z5npsofj%2F1.png?alt=media\&token=3708e65c-0629-4a4d-be19-d6e1f7e25f00)

This is how an **Assertion** node looks like in the project tree:

![](https://1914040845-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYfhIaTTw23Sf3L5jfe%2F-LglKEp1bR5zbag8DHT5%2F-LglWSOff34C1u6ONDNC%2F2.png?alt=media\&token=fbe8e5ff-8fb5-4d97-b3fd-22dc984fe32c)

If an **Assertion** node was run successfully, its icon in the project tree looks like that:

![](https://1914040845-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYfhIaTTw23Sf3L5jfe%2F-LglKEp1bR5zbag8DHT5%2F-LglWqJsXhc_nr4PDNhL%2F3.png?alt=media\&token=e2e3a2ab-01b0-466d-b5af-a74031348ce9)

If an **Assertion** node was run unsuccessfully, its icon in the project tree changes to this:

![](https://1914040845-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYfhIaTTw23Sf3L5jfe%2F-LglKEp1bR5zbag8DHT5%2F-LglXTbrsgXUyMQNolOG%2F4.png?alt=media\&token=cb5a1ebf-2504-49b9-b0a3-fba5e7908d13)

There are several actions you can do with this node type:

![](https://1914040845-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYfhIaTTw23Sf3L5jfe%2F-LgrQs60raNlT-LTs16T%2F-LgrR_jGcOtaB-mjPOUu%2F11.png?alt=media\&token=cc66afd5-b77b-4e50-b6b5-84a0bdd996bd)

* **Remove node.** Use it to delete the node.
* **Run.** Run the node.
* **Show in explorer.** Open the folder with the node in the file manager.

The **Assertion** node tab has the following interface:

![The Assertion node interface](https://1914040845-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYfhIaTTw23Sf3L5jfe%2F-LglKEp1bR5zbag8DHT5%2F-Lgld-aq-pWwX-HKCfKO%2F5.png?alt=media\&token=dad6bee7-5d42-46b7-9c38-d1bd4bfbccd0)

In the screenshot you can see the following parts of the interface:

1. The control panel
2. The settings panel for the chosen assertion
3. The list of all assertions

You can see the following buttons on the control panel:

* **RUN** - runs all assertions in the list.
* **FIX ERRORS** - fixes assertions errors if possible The button is available if there are some errors in the assertions. The **error fixing** algorithms are described for each assertion type separately.
* **DISABLE ERRORS** - disables the failed assertions. They won't be included in further runs. The button is available if there are some errors in the assertions.
* **+ ADD ASSERTION** - adds an assertion to the list.

The assertions list is right under the control panel. Each element of the list has the looks like this:

![](https://1914040845-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYfhIaTTw23Sf3L5jfe%2F-LgqpgXPdLrM2Ecs9J0d%2F-Lgr-8n8zOAAdDhDpJ9O%2F6.png?alt=media\&token=87cf4813-dc69-43f9-a1b7-7cbfa698d7db)

In the screenshot you can see the following parts of the interface:

1. Status highlighting. Its status is gray, if the assertion hasn't been run yet, red - if the run failed, green - if it was successful.
2. The assertion type icon.
3. The assertion type.
4. Delete the assertion.
5. Disable the assertion. It won't be included in the further runs.
6. Run the assertion.
7. Fix the assertion.

Note that the controls 4, 5, 6, and 7 appear when you hover the mouse over the assertion.

The interface of the settings panel depends on the chosen assertion type. We'll discuss all of them in the next sections.

### File Representation

An **Assertion** node is stored in the \<nodename>.yml file, where \<nodename> is the **Assertion** node name. The file has the following format:

```javascript
{
  "type": "object",
  "properties": {
    "type": {
      "description": "Type of Assertion node",
      "const": "Assertion",
      "type": "string"
    },
    "assertions": {
      "description": "List of assertions",
      "type": "array",
      "items": {
        "$ref": "#/definitions/AbstractAssertion"
      },
      "default": []
    },
    "children": {
      "description": "List of children names",
      "type": "array",
      "items": {
        "type": "string"
      },
      "default": []
    },
    "variables": {
      "$ref": "#/definitions/NodeVariables",
      "description": "Node variables dictionary"
    },
    "name": {
      "description": "Node name",
      "type": "string"
    }
  },
  "required": [
    "assertions",
    "children",
    "name",
    "type",
    "variables"
  ],
  "definitions": {
    "AbstractAssertion": {
      "oneOf": [
        {
          "$ref": "#/definitions/CompareAssertion"
        },
        {
          "$ref": "#/definitions/ContainsAssertion"
        },
        {
          "$ref": "#/definitions/XPathAssertion"
        },
        {
          "$ref": "#/definitions/ScriptAssertion"
        }
      ]
    },
    "CompareAssertion": {
      "type": "object",
      "properties": {
        "type": {
          "description": "Type of Compare assertion",
          "const": "compare",
          "type": "string"
        },
        "actualValue": {
          "description": "Actual value",
          "type": "string",
          "default": "${$response.body}"
        },
        "operator": {
          "$ref": "#/definitions/CompareOperator",
          "description": "Operator",
          "default": "equal"
        },
        "expectedValue": {
          "description": "Expected value",
          "type": "string"
        },
        "disabled": {
          "type": "boolean",
          "default": false
        }
      },
      "required": [
        "actualValue",
        "disabled",
        "expectedValue",
        "operator",
        "type"
      ]
    },
    "CompareOperator": {
      "enum": [
        "equal",
        "greater",
        "greater or equal",
        "less",
        "less or equal",
        "not equal"
      ],
      "type": "string"
    },
    "ContainsAssertion": {
      "type": "object",
      "properties": {
        "type": {
          "description": "Type of Contains assertion",
          "const": "contains",
          "type": "string"
        },
        "text": {
          "description": "Text to be searched",
          "type": "string",
          "default": "${$response.body}"
        },
        "value": {
          "description": "Value for search in text",
          "type": "string"
        },
        "disabled": {
          "type": "boolean",
          "default": false
        }
      },
      "required": [
        "disabled",
        "text",
        "type",
        "value"
      ]
    },
    "XPathAssertion": {
      "type": "object",
      "properties": {
        "type": {
          "description": "Type of Xpath assertion",
          "const": "xpath",
          "type": "string"
        },
        "text": {
          "description": "Text to be searched",
          "type": "string",
          "default": "${$response.body}"
        },
        "path": {
          "description": "XPath selector",
          "type": "string"
        },
        "expectedValue": {
          "description": "Expected value",
          "type": "string"
        },
        "disabled": {
          "type": "boolean",
          "default": false
        }
      },
      "required": [
        "disabled",
        "expectedValue",
        "path",
        "text",
        "type"
      ]
    },
    "ScriptAssertion": {
      "type": "object",
      "properties": {
        "type": {
          "description": "Type of Script assertion",
          "const": "script",
          "type": "string"
        },
        "script": {
          "description": "Assertion script",
          "type": "string",
          "default": "`function test(assertion, variables) {\n  // It should return true if test is passed\n  // return true;\n}`"
        },
        "disabled": {
          "type": "boolean",
          "default": false
        }
      },
      "required": [
        "disabled",
        "script",
        "type"
      ]
    },
    "NodeVariables": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      }
    }
  },
  "$schema": "http://json-schema.org/draft-07/schema#"
}
```
