Automatic API Test Generation from UI Test | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: AI-Assisted Scripting with Copilot
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore how to generate API tests based on UI tests using Playwright's CodeGen feature. This feature allows automatic generation of UI scripts by recording browser actions and capturing network activity, which can be saved in a HAR file. Key Steps in the Process Generate HAR File: Use the command npx playwright codegen --save-har networking.har to create a HAR file while performing actions in the browser. Record Actions: Sign in, create an article, and post a comment to capture the necessary API requests. Analyze HAR File: The generated HAR file contains extensive network data, including request details and responses. Clean Up HAR File: Filter out unnecessary data using a JavaScript function called HAR converter to retain only relevant API requests and responses. Prepare Instructions: Create a markdown file with processing instructions for the AI to generate the API tests based on the cleaned HAR file. Generate API Tests: Use the filtered HAR file and instructions to prompt the AI to create the API tests. Considerations While AI can assist in generating tests, it's important to evaluate whether crafting a perfect prompt is worth the time compared to manual test writing. AI is best utilized for repetitive tasks, while more complex scenarios may require manual intervention. In summary, this approach allows for efficient test generation by leveraging existing UI tests and automating the API testing process.