Status Code Validator | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Building a Framework
Instructor: Artem Bondar
Lesson Summary
In this lesson, we enhance our testing framework by integrating a custom logger to capture logs when tests fail, improving the information presented in the report. Key Modifications We aim to improve the stack trace displayed in the report, which currently shows misleading line numbers from the utility functions instead of the actual test steps. We will modify the request handler to log request and response details for each API call. Implementation Steps Remove the dummy logger and integrate the new logger in the request handler. Create a new instance of the logger in the API fixture for each test run to ensure isolation. Modify the request handler constructor to accept the logger instance and store it in a private field. Log request details before making API calls and log response details after receiving the response, ensuring logs are captured before any assertions are made. Custom Status Code Validator We implemented a custom status code validator method that compares the actual and expected status codes. If they differ, it throws an error with detailed logs: if (actualStatus !== expectedStatus) { const logs = this.logger.getRecentLogs(); throw new Error(`Expected status ${expectedStatus} but got ${actualStatus}\nRecent API activity:\n${logs}`); } Results After implementing these changes, the test reports now correctly indicate the line number where the failure occurred, along with detailed logs of the API requests and responses. This significantly enhances debugging and understanding of test failures. In summary, we have successfully integrated a custom logger into our testing framework, improving the clarity and usefulness of our test reports.