---
title: "Generic automation of Testing processes"
canonical: "https://docs.getxray.app/space/XRAYCLOUD/44565672/Generic%20automation%20of%20Testing%20processes"
format: markdown
---
> Macro (toc)

# <span style="color: #172b4d">Add custom steps to a manual test</span>

<span style="color: #172b4d">In this simple scenario we are adding manual steps to a Test previously created. We'll use Xray's GraphQL API to achieve this goal. Even though this use case is mostly for demonstration purposes only, it's a good example of how to take advantage of Xray API to query/modify information in Xray side.</span>

## <span style="color: #172b4d">Automation Configuration</span>

1. <span style="color: #172b4d">create a new rule and define the "</span><span style="color: #172b4d">*When*</span><span style="color: #172b4d">" (i.e. when it should be triggered ), to be "</span><span style="color: #172b4d">*Manually triggered*</span><span style="color: #172b4d">".</span>
2. <span style="color: #172b4d">for this example, we have added a condition so that this automation can only be triggered by Jira issues of the type: </span><span style="color: #172b4d">*Test.*</span>
3. <span style="color: #172b4d">The next component to be added is a of type: "</span><span style="color: #172b4d">*Send web request*</span><span style="color: #172b4d">" where we will perform the authentication with the Xray API. </span>
  2. <span style="color: #172b4d">Make sure to fill the fields with correct information:</span>
    1. <span style="color: #172b4d">*Web request URL*</span><span style="color: #172b4d">: Authentication URL of Xray API.</span>
    2. <span style="color: #172b4d">*Headers*</span><span style="color: #172b4d">: Add the "</span><span style="color: #172b4d">*Content-Type*</span><span style="color: #172b4d">" header with the value: "</span><span style="color: #172b4d">*application/json*</span><span style="color: #172b4d">".</span>
    3. <span style="color: #172b4d">Define the </span><span style="color: #172b4d">*HTTP method*</span><span style="color: #172b4d"> as </span><span style="color: #172b4d">*POST.*</span>
    4. <span style="color: #172b4d">Choose the "</span><span style="color: #172b4d">*Custom data*</span><span style="color: #172b4d">" as the </span><span style="color: #172b4d">*Web request body.*</span>
    5. <span style="color: #172b4d">Add the Json request as seen in the picture with a valid </span><span style="color: #172b4d">*client_id*</span><span style="color: #172b4d"> and </span><span style="color: #172b4d">*client_secret *</span><span style="color: #172b4d">(obtained in your Jira instance).</span>
    6. <span style="color: #172b4d"> Tick the "</span><span style="color: #172b4d">*delay execution...*</span><span style="color: #172b4d">" option at the bottom as we want to use the token generated in this request in the next ones.</span>
4. <span style="color: #172b4d">Next add another "Send web request" component to make the GraphQL request.</span>
  2. <span style="color: #172b4d">Make sure to fill the fields with correct information:</span>
    1. <span style="color: #172b4d">*Web request URL*</span><span style="color: #172b4d"> with the endpoint of the Xray graphQL.</span>
    2. <span style="color: #172b4d">Add two headers:</span>
      1. <span style="color: #172b4d">"</span><span style="color: #172b4d">*Content-Type*</span><span style="color: #172b4d">" with "application/json".</span>
      2. <span style="color: #172b4d">"</span><span style="color: #172b4d">*Authorization*</span><span style="color: #172b4d">" with "</span><span style="color: #172b4d">*Bearer <token>*</span><span style="color: #172b4d">". Notice that we are using a special way to get the token </span><span style="color: #172b4d">*{{webResponse.body}}*</span><span style="color: #172b4d"> provided by Jira automation, this will get the value from the last web request made and fetch the body of the answer.</span>
    3. <span style="color: #172b4d">Define the </span><span style="color: #172b4d">*HTTP method*</span><span style="color: #172b4d"> as </span><span style="color: #172b4d">*POST.*</span>
    4. <span style="color: #172b4d">Choose the </span><span style="color: #172b4d">*Web request body*</span><span style="color: #172b4d"> to be </span><span style="color: #172b4d">*Custom data.*</span>
    5. <span style="color: #172b4d">Fill the </span><span style="color: #172b4d">*Custom data*</span><span style="color: #172b4d"> with a proper formatted graphQL request as seen in the picture (make sure the format is correct or the request will fail).</span>

### <span style="color: #172b4d">Usage</span>

<span style="color: #172b4d">Once the automation is defined we can access it in the test detail view of a Test in Jira. On the right side you have an entry called "</span><span style="color: #172b4d">*Automation*</span><span style="color: #172b4d">".</span>

![image](media://a78a1b1a-0a9f-4a39-a7c9-a826ecd40f29)

<span style="color: #172b4d">After clicking on it another screen will load with all the automation rules defined and the possibility to run each of them. Once you choose the correct rule and press "Run" the rule will be executed.</span>

![image](media://4087a704-05d9-491f-abd0-d518b1d28bae)


<span style="color: #172b4d">The status of the execution is showed in the section "</span><span style="color: #172b4d">*Recent rule executions*</span><span style="color: #172b4d">".</span>

![image](media://57cebab7-d3bf-4160-bfa2-29d819e74a68)


<span style="color: #172b4d">When the automation rule is executed with success a new test step will be added to the Test as we can see below.</span>

![image](media://35283dbd-a2e1-4dc2-8287-f8af92a643db)

# <span style="color: #172b4d">Copy fields from requirement/Story to Test whenever creating a Test or linking it to a story</span>

<span style="color: #172b4d">Sometimes it may be useful to copy some fields from the requirement/Story to the Tests that cover it.</span>


## <span style="color: #172b4d">Automation configuration</span>

<span style="color: #172b4d">On the Jira side we will use the Automation capabilities that it provides out of the box, so within the administration area go to the automation entry in the system settings and: </span>

1. <span style="color: #172b4d">create a new rule and define the "When" (i.e. when it should be triggered) to be "Issue linked". Since Xray, by default, uses the issue link type "Test" to establish the coverage relation between a Test and the requirement, we can take advantage of that to trigger the rule whenever such issue link is created.</span>
2. <span style="color: #172b4d">create a condition to ensure that the rule only runs for Test issues</span>
3. <span style="color: #172b4d">use an "Edit issue" action to set the fields on the Test based on the fields of the linked requirement/Story (i.e., the </span><span style="color: #172b4d">*destination*</span><span style="color: #172b4d"> issue of the linking event). In this example, we'll copy the values of Labels and Priority custom fields.  </span>


<span style="color: #172b4d">This rule will run:</span>

- <span style="color: #172b4d">whenever a Test is created from the requirement/Story issue screen</span>
- <span style="color: #172b4d">whenever a Test has been initially created and later on linked to the requirement</span>

# <span style="color: #172b4d">Reopen/transitionTests linked to a requirement whenever the requirement is transitioned or changed</span>

<span style="color: #172b4d">Whenever you change the specification of a requirement/story, you most probably will need to review the Tests that you have already specified.</span>

<span style="color: #172b4d">The following rule tries to perform a transition of </span><span style="color: #172b4d">**all**</span><span style="color: #172b4d"> Tests linked to a requirement.</span>

## <span style="color: #172b4d">Automation configuration</span>

<span style="color: #172b4d">On the Jira side we will use the Automation capabilities that it provides out of the box, so within the administration area go to the automation entry in the system settings and: </span>

1. <span style="color: #172b4d">create a new rule and define the "When" (i.e. when it should be triggered) to be "Field value changed"</span>
  2. <span style="color: #172b4d">Note: we could also define the trigger to be based on the transition of the requirement issue to a certain workflow status; in that case we would define it, for example, as shown below.</span>
2. <span style="color: #172b4d">create a condition to ensure that the rule only runs for Story and Epic issues; adjust these to include all the "requirement" issue types (i.e., the ones that you can cover with Tests)</span>
3. <span style="color: #172b4d">create a "branch rule / related issues" to obtain related Tests using JQL and the </span>`requirementTests()`<span style="color: #172b4d"> JQL function and run one, or more, action(s) on them</span>
4. <span style="color: #172b4d">under the "For JQL" block, create a action "Transition the issue to" in order to reopen the related Test issues</span>

# <span style="color: #172b4d">Create a new Test Execution for the Tests that failed on a previous Test Execution</span>

<span style="color: #172b4d">Whether you're using Test Plans or not, you may want to rerun the failed Tests from a given Test Execution by scheduling a new Test Execution containing </span><span style="color: #172b4d">**just those Tests**</span><span style="color: #172b4d"> (e.g., like rerunning the failed tests, but on a new Test Execution).</span>

<span style="color: #172b4d">In this example, we'll associate the new Test Execution to the same Test Environments and the same Test Plan of the Test Execution where we triggered the rule from (i.e., the one that contains some tests that failed).</span>

## <span style="color: #172b4d">Automation configuration</span>

<span style="color: #172b4d">On the Jira side we will use the Automation capabilities that it provides out of the box.</span>

<span style="color: #172b4d">This use case requires a slightly more complex flow to set up due to some limitations of Jira Automation.</span>

<span style="color: #172b4d">We need to create 2 rules:</span>

1. <span style="color: #172b4d">an automation rule to associate the new Test Execution (created on the next rule) to the same Test Plan, if required</span>
2. <span style="color: #172b4d">an automation rule to create the Test Execution with only the Tests that failed on the original Test Execution</span>

### <span style="color: #172b4d">Automation rule 1</span>

1. <span style="color: #172b4d">we start by creating the rule to associate the Test Execution with a Test Plan; we need to make sure this rule can be triggered by another rule</span>
2. <span style="color: #172b4d">in this rule, the "When" trigger should be "Incoming webhook"</span>
3. <span style="color: #172b4d">use an "IF" block to only proceed if there are Test Plan AND Test Execution issue ids </span>
4. <span style="color: #172b4d">make a GraphQL API call to associate the Test Execution that will be created with the Test Plan</span>
  2. 
    **sample GraphQL query**
    

### <span style="color: #172b4d">Automation rule 2</span>

1. <span style="color: #172b4d">create a new rule (i.e., the one that will actually create a new Test Execution and trigger the previous rule) and define the "When" (i.e. when it should be triggered) to be "Manual Trigger"</span>
2. <span style="color: #172b4d">create an action using the "Send web request" template to make the </span><span style="color: #172b4d">[authentication request ](https://getxraydocs.atlassian.net/wiki/spaces/XRAYCLOUD/pages/44577089)</span><span style="color: #172b4d">and obtain a token to be used on the GraphQL operations; we need a </span><span style="color: #172b4d">*client_id*</span><span style="color: #172b4d"> and a </span><span style="color: #172b4d">*client_secret *</span><span style="color: #172b4d">(please see </span><span style="color: #172b4d">[how to create API keys](https://getxraydocs.atlassian.net/wiki/spaces/XRAYCLOUDDRAFT/pages/44838245)</span><span style="color: #172b4d">).</span>
3. <span style="color: #172b4d">save the response in a variable (e.g., "token")</span>
4. <span style="color: #172b4d">make a GraphQL query to obtain the details of the Test Execution and its results</span>
  1. <span style="color: #0e101a">Use the GraphQL API, namely the </span><span style="color: #4a6ee0">[getTestExecution function](https://xray.cloud.getxray.app/doc/graphql/gettestexecution.doc.html)</span><span style="color: #0e101a">. We'll need the issue id of the Test Execution that was already completed; we can use the smart values feature from Jira automation to obtain it. We need to also obtain the custom field id of the "Revision" custom field, if we want to set it later on, under the "Issue Fields" section of your Jira administration. </span>
  2. <span style="color: #172b4d">Escape the GraphQL query (e.g., using the online </span><span style="color: #172b4d">[GraphQL to JSON Body Converter](https://datafetcher.com/graphql-json-body-converter)</span><span style="color: #172b4d"> tool)</span>
    
    **sample GraphQL query**
    
5. <span style="color: #172b4d">get the linked Test Plan issue id and store it in a variable</span>
  2. 
    
6. <span style="color: #172b4d">get the associated Test Environments and store it in a variable</span>
  2. 
    
7. <span style="color: #172b4d">post-process it and store it in another variable, as we need to escape some characters to embed the Test Environments on the GraphQL request later on</span>
  2. 
    
8. <span style="color: #172b4d">get all the failed tests and store them in a variable (note that this is an approximate value as GraphQL results can be limited and paginated)</span>
  2. 
    
9. <span style="color: #172b4d">store the issue ids of the failed tests in a variable (note that this is an approximate value as GraphQL results can be limited and paginated) </span>
  2. 
    
10. <span style="color: #172b4d">post-process it and store it in another variable, as we need to escape some characters to embed the Test issue ids on the GraphQL request later on</span>
  2. 
    
  4. 
    
11. <span style="color: #172b4d">use an "IF" block to only proceed if there are failed tests</span>
12. <span style="color: #172b4d">make a GraphQL API request to create a new Test Execution containing just the Tests that failed</span>
  2. 
    
13. <span style="color: #172b4d">store the issue id of the new Test Execution in a variable</span>
  2. 
    
14. <span style="color: #172b4d">use the "Send web request" action to trigger the incoming webhook we defined in the "Automation rule 1" mentioned at start  </span>
  2. 
    

# <span style="color: #172b4d">References</span>

- <span style="color: #172b4d">[GraphQL to JSON Body Converter](https://datafetcher.com/graphql-json-body-converter)</span>
- <span style="color: #172b4d">[Jira Automation in Jira Cloud](https://support.atlassian.com/cloud-automation/docs/jira-cloud-automation/)</span>
  - <span style="color: #172b4d">[Jira smart values - issues](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-issues/)</span>
  - <span style="color: #172b4d">[Jira smart values - lists](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/)</span>
  - <span style="color: #172b4d">[Jira smart values - text fields](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/)</span>
  - <span style="color: #172b4d">[Jira smart values - users](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-users/)</span>
  - <span style="color: #172b4d">[Jira smart values - conditional logic](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-conditional-logic/)</span>
  - <span style="color: #172b4d">[Jira smart values - JSON functions](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-json-functions/)</span>