Automation QA Testing Course Content

Understanding Playwright API and how to define a framework in typescript

 





Unified API and UI Testing

  • Playwright uses a single framework and runner (@playwright/test) for both API tests and End-to-End (UI) tests. This allows for easily mixing them together.

Playwright Provides Built-in APIRequestContext fixture

  • Playwright provides a dedicated request fixture that abstracts the HTTP client logic.
  • It supports all the HTTP methods.
  • The APIRequestContext can be created with cookies, headers, and authentication tokens that are automatically attached to all requests made within that context.
  • The API methods allow fine-grained control over request parameters, including headers, JSON/form data, and multipart file uploads, all through structured configuration objects.
static async login(request: APIRequestContext, body: LoginRequest) {
    const response = await request.post(ENV.baseURL +'/login', {
    headers: {
      'x-api-key': ENV.apiKey,
      'Content-Type': 'application/json'
    },
      data: body,
    });

    const parsed: LoginResponse = await response.json();
    return { response, parsed };
  } 
//above LoginRequest defines the body of the request
export interface LoginRequest {
  email: string;
  password: string;
}
await api.post('/upload', {
  multipart: { file: fs.createReadStream('./image.png') }
});

JSON Body Handling is Extremely Easy

  • No need for external libraries (e.g., Gson, Jackson like in Rest Assured).
  • Playwright automatically convert the response into data object



A basic flow of Request and Response in Playwright API




Assertions in Playwright API:




Schema Validators:

  • Defines the rules for the structure, data types, and constraints of API responses, ensuring the API contract is strictly followed
  • Schema Validator verifies that the actual data returned by the API matches the expected format
  • Services often call schema validation immediately after receiving and parsing a successful API response.



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.