Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Network Monitoring and API Testing

Playwright Network Monitoring and API Testing

This chapter introduces how to monitor browser network traffic and use Playwright for independent API testing.


Listen to network requests and responses

page.on('request') listen to requests

Example

test

(

'Monitor network requests'

,

async

(

{

page

}

)

=>

{

 

const

requests

=

[

]

;

 

// Listen to all requests

  page.

on

(

'request'

,

request

=>

{

    requests.

push

(

{

      url

:

request.

url

(

)

,

      method

:

request.

method

(

)

,

      postData

:

request.

postData

(

)

,

   

}

)

;

 

}

)

;

  await page.

goto

(

'https://www.runoob.com/'

)

;

 

// Print all requests

  console.

log

(

'The page made a total of'

,

requests.

length

,

'requests'

)

;

  requests.

forEach

(

r

=>

console.

log

(

r.

method

,

r.

url

)

)

;

}

)

;

page.on('response') listen to responses

Example

test

(

'Monitor network responses'

,

async

(

{

page

}

)

=>

{

  page.

on

(

'response'

,

async response

=>

{

   

// Only focus on API responses

   

if

(

response.

url

(

)

.

includes

(

'/api/'

)

)

{

      console.

log

(

'API status code:'

,

response.

status

(

)

)

;

      console.

log

(

'API URL:'

,

response.

url

(

)

)

;

   

}

 

}

)

;

  await page.

goto

(

'https://www.runoob.com/'

)

;

}

)

;


page.waitForResponse() wait for a specific response

Example

test

(

'Wait for API response to complete'

,

async

(

{

page

}

)

=>

{

 

// Prepare to listen for the API response before clicking the button

 

const

responsePromise

=

page.

waitForResponse

(

    response

=>

response.

url

(

)

.

includes

(

'/api/search'

)

&&

response.

status

(

)

===

200

 

)

;

 

// Trigger search

  await page.

getByPlaceholder

(

'Search RUNOOB Tutorial'

)

.

fill

(

'Playwright'

)

;

  await page.

keyboard

.

press

(

'Enter'

)

;

 

// Wait for API response

 

const

response

=

await responsePromise

;

 

const

data

=

await response.

json

(

)

;

 

// Verify data returned by the API

  expect

(

data.

results

.

length

)

.

toBeGreaterThan

(

0

)

;

}

)

;


Console message listening

Example

test

(

'Listen for console errors'

,

async

(

{

page

}

)

=>

{

 

const

consoleErrors

=

[

]

;

 

// Listen to console messages

  page.

on

(

'console'

,

msg

=>

{

   

if

(

msg.

type

(

)

===

'error'

)

{

      consoleErrors.

push

(

msg.

text

(

)

)

;

   

}

 

}

)

;

  await page.

goto

(

'https://www.runoob.com/'

)

;

 

// Assert the page has no JavaScript errors

  expect

(

consoleErrors

)

.

toHaveLength

(

0

)

;

}

)

;

ConsoleMessage types

Type Corresponding methods
'log' console.log()
'error' console.error()
'warning' console.warn()
'info' console.info()
'debug' console.debug()

API testing (request fixture)

Playwright's request The fixture allows you to send HTTP requests directly without going through the browser.

This is ideal for preparing or cleaning up server-side state before and after tests.

Send a GET request

Example

test

(

'API GET request'

,

async

(

{

request

}

)

=>

{

 

const

response

=

await request.

get

(

'https://jsonplaceholder.typicode.com/posts/1'

)

;

 

// Verify response status

  expect

(

response.

status

(

)

)

.

toBe

(

200

)

;

 

// Get JSON data

 

const

data

=

await response.

json

(

)

;

  expect

(

data.

id

)

.

toBe

(

1

)

;

  expect

(

data

)

.

toHaveProperty

(

'title'

)

;

}

)

;

Send a POST request

Example

test

(

'API POST request'

,

async

(

{

request

}

)

=>

{

 

const

response

=

await request.

post

(

'https://jsonplaceholder.typicode.com/posts'

,

{

    data

:

{

      title

:

'RUNOOB Tutorial'

,

      body

:

'Playwright API test example'

,

      userId

:

1

,

   

}

,

 

}

)

;

  expect

(

response.

status

(

)

)

.

toBe

(

201

)

;

 

const

data

=

await response.

json

(

)

;

  expect

(

data.

title

)

.

toBe

(

'RUNOOB Tutorial'

)

;

}

)

;

Configure baseURL and authentication headers

Example

// File path: playwright.config.ts

export

default

defineConfig

(

{

  use

:

{

   

// Base URL for API requests

    baseURL

:

'https://api.runoob.com'

,

   

// Default request headers

    extraHTTPHeaders

:

{

     

'Authorization'

:

'Bearer YOUR_API_TOKEN'

,

     

'Accept'

:

'application/json'

,

   

}

,

 

}

,

}

)

;


API + browser combined testing

In real projects, APIs are often used for state management before and after browser operations.

Example

test

(

'API data preparation + browser verification'

,

async

(

{

request

,

page

}

)

=>

{

 

// Step 1: Create a test record via API

 

const

response

=

await request.

post

(

'/api/todos'

,

{

    data

:

{

title

:

'RUNOOB test task'

,

completed

:

false

}

,

 

}

)

;

 

const

todo

=

await response.

json

(

)

;

 

// Step 2: Verify in browser that the data is displayed

  await page.

goto

(

'/todos'

)

;

  await expect

(

page.

getByText

(

'RUNOOB test task'

)

)

.

toBeVisible

(

)

;

 

// Step 3: Clean up data via API

  await request.

delete

(

`

/

api

/

todos

/

$

{

todo.

id

}

`

)

;

}

)

;

API testing is much faster than browser operations, making it suitable for data preparation and cleanup. Combining API and browser operations can greatly improve test efficiency and reliability.