You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Katalon中setBodyContent(HttpBodyContent)用法?测试后更新JIRA Adaptavist测试管理API

Solution: Use Katalon Test Listeners + HTTP Request with setBodyContent

To update your Adaptavist Test Management API after Katalon test execution, you’ll want to use a Test Listener (to trigger code post-test) and build a REST request with the setBodyContent method to send the required JSON payload. Here’s a complete, actionable implementation:

Step 1: Create a Test Listener

Test Listeners let you run code automatically after each test case finishes.

  1. Right-click on Test Listeners in the Katalon Project Explorer → New → Test Listener.
  2. Choose the After Test Case template to generate the basic structure.

Step 2: Implement the API Call with setBodyContent

Replace the generated listener code with the following, adjusting values to match your Adaptavist setup:

import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.RestRequestObjectBuilder
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.exception.StepFailedException
import com.kms.katalon.core.annotation.AfterTestCase
import com.kms.katalon.core.context.TestCaseContext

class UpdateAdaptavistListener {

    @AfterTestCase
    def afterTestCase(TestCaseContext testCaseContext) {
        // 1. Define your Adaptavist API and project details
        def apiEndpoint = "https://your-adaptavist-instance/rest/atm/1.0/testresult" // Replace with actual endpoint
        def projectKey = "FVS" // Your JIRA project key
        def environment = "DEV" // Target test environment

        // 2. Fetch dynamic values from test execution
        def katalonTestStatus = testCaseContext.getTestCaseStatus()
        // Map Katalon's status to Adaptavist's required status strings
        def adaptavistStatus = katalonTestStatus == "PASSED" ? "Pass" : "Fail"
        
        // Get test case key (replace with dynamic logic if needed)
        // Example: Pull from a test case variable: testCaseContext.getTestCaseVariables().get("jiraTestCaseKey")
        def testCaseKey = "FVS-T1"

        // 3. Build the JSON payload string
        def jsonPayload = """
        {
            "projectKey": "${projectKey}",
            "testCaseKey": "${testCaseKey}",
            "status": "${adaptavistStatus}",
            "environment": "${environment}"
        }
        """.trim()

        try {
            // 4. Construct the POST request and set body with setBodyContent
            def requestBuilder = new RestRequestObjectBuilder()
            def requestObject = requestBuilder
                .withRestRequestMethod("POST")
                .withRestUrl(apiEndpoint)
                .addHeader("Content-Type", "application/json")
                // Add authentication headers if required (e.g., API key or Basic Auth)
                // .addHeader("Authorization", "Bearer YOUR_ADAPTAVIST_API_KEY")
                .setBodyContent(new HttpBodyContent(jsonPayload, "application/json"))
                .build()

            // 5. Send the request and handle response
            ResponseObject response = com.kms.katalon.core.testobject.RestKeywordUtil.sendRequest(requestObject)
            
            if (response.getStatusCode() in 200..299) {
                println("Successfully updated Adaptavist test result. Response: " + response.getResponseBodyContent())
            } else {
                println("Failed to update Adaptavist. Status code: ${response.getStatusCode()}, Response: ${response.getResponseBodyContent()}")
            }
        } catch (StepFailedException e) {
            println("Error sending request to Adaptavist API: " + e.getMessage())
        }
    }
}

Key Adjustments for Your Setup:

  • API Endpoint: Replace the placeholder URL with your actual Adaptavist Test Management API endpoint (check Adaptavist’s docs for the correct path).
  • Authentication: Add required headers (like Authorization with an API key) if your API instance is secured.
  • Dynamic Test Case Key: Instead of hardcoding FVS-T1, pull this from a test case variable or map Katalon’s test case ID to your JIRA test case key for full automation.
  • Status Mapping: Confirm the status strings match what your Adaptavist instance expects (some use "PASSED" instead of "Pass"—verify via API docs).

How It Works:

  • The @AfterTestCase annotation ensures this code runs immediately after every test case completes.
  • We use RestRequestObjectBuilder to construct the POST request, then setBodyContent(new HttpBodyContent(jsonPayload, "application/json")) to attach your formatted JSON payload.
  • Basic error handling and response logging help debug any issues with the API call without breaking your test execution flow.

内容的提问来源于stack exchange,提问作者Doomster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:57:02