Fluent Interface Design | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Building a Framework
Instructor: Artem Bondar
Lesson Summary
In this lesson, we focused on building a custom request handler for managing API requests in Playwright. The motivation behind this is to improve the readability and maintainability of API scripting, which can often appear bulky and hard to follow. Key Components of API Requests Every API request consists of five main components: URL Path Headers Parameters Body Unlike tools like Postman or frameworks like Rest Assured, which separate these components, Playwright combines them, making the code harder to read. Creating the Request Handler We started by creating a new folder called utils and a file named request-handler.ts . Inside this file, we defined a class RequestHandler with methods to handle each of the five components: URL - type: string Path - type: string Params - type: object (JSON) Headers - type: object Body - type: object Each method saves the provided values into private fields of the class, allowing for reuse later. Fluent Interface Design We implemented a fluent interface design by returning this at the end of each method, enabling method chaining. This allows us to call methods in a more readable format: API.url('example.com').path('/articles').params({ limit: 10, offset: 0 }); Conclusion By the end of the lesson, we successfully created a structured approach to building API requests, enhancing the clarity of our tests. Future lessons will focus on further developing the request handler.