...
/Playwright Essentials: API Testing & Network Mocking
Playwright Essentials: API Testing & Network Mocking
Learn about Playwright's API testing support, built-in assertion capabilities, network mocking abilities, and support of the POM design pattern.
We'll cover the following...
Playwright’s API testing
Playwright supports API testing activities. API testing is important within the test pyramid as well as the additional layer of coverage that such a testing type adds to the overall testing activities. With Playwright, we can develop API testing for our web applications. We can use the GET
, POST
, DELETE
, and other API methods to send API requests and validate their responses.
Developing API tests with Playwright can be done through the specified methods of request.get()
, request.post
()
, request.delete()
, and so on, or by means of a request context that we create through the following code.
When using a request context, the newly created context const
is the one that drives all of the API methods through context.get()
, context.post
()
, and so on:
const context = await request.newContext({baseURL: 'https://api.github.com',});
Based on the context created above, which will trigger the HTTP requests, we can create a GitHub API POST
test scenario:
await context.post('/user/repos', {headers: {'Accept': 'application/vnd.github.v3+json',// Add GitHub personal access token.'Authorization': `token ${process.env.API_TOKEN}`},data: {name: REPO}});
Playwright provides more code ...