URL Builder | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Building a Framework
Instructor: Artem Bondar
Lesson Summary
In this lesson, we developed a new method within our request handler to build API request URLs. The request URL consists of three components: URL , path , and parameters . Depending on the API request, parameters may or may not be required. Key Concepts The base URL can be read from an environment variable . We will create a method called getUrl to handle URL construction. Parameters will be formatted as key=value pairs in the URL. Method Implementation The getUrl method will: Create a constant URL using the built-in URL object in Node.js. Utilize string interpolation to combine the base URL and API path. Convert the URL object to a string for output. Handling Default Values We introduced a field called defaultBaseURL to manage default values for the base URL. The method will check if a custom base URL is provided; if not, it will use the default: this.defaultBaseURL ?? baseURL Appending Query Parameters To append query parameters, we will: Loop through the key-value pairs of the parameters. Use URLSearchParams.append() to add them to the URL. Finally, we converted getUrl to a private method that returns the constructed URL, ensuring it can be used internally within the request handler. In summary, we created a robust method for generating API request URLs, accommodating both default values and dynamic parameters.