如何通过API复用UI测试结果页的“Create Bug”功能?
Great question! I’ve run into this exact scenario before—when you use the basic Bug creation API, the end result feels sparse compared to what the UI generates, which automatically ties in test run context, failure details, and pre-filled templates. Let’s walk through how to replicate that UI-quality Bug creation programmatically.
Can you directly "reuse" the UI button’s functionality?
Sort of, but not by triggering the button itself. The UI’s "Create Bug" button is just a frontend wrapper that calls an internal API endpoint with a full set of parameters you’re probably missing in your current API calls. Instead of relying on brittle UI automation (like Selenium) to click the button, you can reverse-engineer what the UI does and mimic it with your own API requests.
Step-by-step solution
- Capture the UI’s API traffic: Open your browser’s DevTools (F12), navigate to the Network tab, then click the UI’s "Create Bug" button. Filter for API requests (look for calls to endpoints like
/_apis/wit/workitems/$Bug). Inspect the request details closely:- Request method: It will almost certainly be a
POSTrequest. - Headers: Pay attention to authentication headers (like
Authorizationwith your session token or PAT) and any platform-specific headers (e.g.,X-TFS-FedAuthfor Azure DevOps). - Request body: This is the key. The UI sends a JSON payload packed with context: test run ID, result ID, linked test case details, pre-filled failure descriptions, environment info, and project-specific field defaults. You’ll see fields like
System.Title(auto-generated with the failed test name),System.Description(populated with error logs or test step outcomes), and relations that link the Bug directly to the test result.
- Request method: It will almost certainly be a
- Replicate the payload in your automation: Take the JSON payload you captured, swap out dynamic values (like
runIdorresultIdwith your automation’s variables), and use it in your existing API client. Make sure to include all the same headers (especially authentication) to match the UI’s request context. - Prioritize official APIs if available: Check if your test management platform’s official API has a dedicated endpoint for creating bugs linked to test results. These endpoints are documented, supported, and more maintainable than reverse-engineered internal calls.
- Use stable authentication: Instead of relying on short-lived session cookies, use a Personal Access Token (PAT) with the correct scopes (e.g.,
Work Itemswrite access andTest Managementread access) to authenticate your API requests. This keeps your automation reliable long-term.
Why this works better than basic API creation
The UI’s "Create Bug" doesn’t just make a blank Bug—it pulls in all critical context from the failed test:
- Automatic links to the specific test case, run, and result
- Pre-filled failure details (stack traces, error messages, test step outcomes)
- Application of project-specific templates for fields like severity, priority, or area path
By mimicking that full payload, your automated Bugs will have the same level of detail and utility as those created manually via the UI.
内容的提问来源于stack exchange,提问作者Bas Hamer

