Class: Playwright::APIRequestContext

Inherits:
PlaywrightApi show all
Defined in:
lib/playwright_api/api_request_context.rb,
sig/playwright.rbs

Overview

This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare environment or the service to your e2e test.

Each Playwright browser context has an associated APIRequestContext, accessible via [property: BrowserContext.request] or [property: Page.request] (these return the

same instancepage.request is a shortcut for page.context().request). You can also create a standalone, isolated instance with [method: APIRequest.newContext].

Cookie management

The APIRequestContext returned by [property: BrowserContext.request] and

[property: Page.request] uses the same cookie jar as its BrowserContext:

If you want API requests that do not share cookies with the browser, create an isolated context via [method: APIRequest.newContext]. Such APIRequestContext object will have its own isolated cookie storage.

import os
from playwright.sync_api import sync_playwright

REPO = "test-repo-1"
USER = "github-username"
API_TOKEN = os.getenv("GITHUB_API_TOKEN")

with sync_playwright() as p:
    # This will launch a new browser, create a context and page. When making HTTP
    # requests with the internal APIRequestContext (e.g. `context.request` or `page.request`)
    # it will automatically set the cookies to the browser page and vice versa.
    browser = p.chromium.launch()
    context = browser.new_context(base_url="https://api.github.com")
    api_request_context = context.request
    page = context.new_page()

    # Alternatively you can create a APIRequestContext manually without having a browser context attached:
    # api_request_context = p.request.new_context(base_url="https://api.github.com")


    # Create a repository.
    response = api_request_context.post(
        "/user/repos",
        headers={
            "Accept": "application/vnd.github.v3+json",
            # Add GitHub personal access token.
            "Authorization": f"token {API_TOKEN}",
        },
        data={"name": REPO},
    )
    assert response.ok
    assert response.json()["name"] == REPO

    # Delete a repository.
    response = api_request_context.delete(
        f"/repos/{USER}/{REPO}",
        headers={
            "Accept": "application/vnd.github.v3+json",
            # Add GitHub personal access token.
            "Authorization": f"token {API_TOKEN}",
        },
    )
    assert response.ok
    assert await response.body() == '{"status": "ok"}'

Direct Known Subclasses

ChannelOwners::FetchRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PlaywrightApi

#initialize, unwrap, wrap

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Attribute Details

#tracingTracing (readonly)

property

Returns:



70
71
72
# File 'lib/playwright_api/api_request_context.rb', line 70

def tracing # property
  wrap_impl(@impl.tracing)
end

Instance Method Details

#delete(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) ⇒ APIResponse

Sends HTTP(S) DELETE request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

Parameters:

  • url (String)
  • data: (String, String, untyped) (defaults to: nil)
  • failOnStatusCode: (Boolean) (defaults to: nil)
  • form: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • headers: (Hash[untyped, untyped]) (defaults to: nil)
  • ignoreHTTPSErrors: (Boolean) (defaults to: nil)
  • maxRedirects: (Integer) (defaults to: nil)
  • maxRetries: (Integer) (defaults to: nil)
  • multipart: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • params: (Hash[untyped, untyped], String) (defaults to: nil)
  • timeout: (Float) (defaults to: nil)

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/playwright_api/api_request_context.rb', line 78

def delete(
      url,
      data: nil,
      failOnStatusCode: nil,
      form: nil,
      headers: nil,
      ignoreHTTPSErrors: nil,
      maxRedirects: nil,
      maxRetries: nil,
      multipart: nil,
      params: nil,
      timeout: nil)
  wrap_impl(@impl.delete(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout)))
end

#dispose(reason: nil) ⇒ void

This method returns an undefined value.

All responses returned by [method: APIRequestContext.get] and similar methods are stored in the memory, so that you can later call [method: APIResponse.body].This method discards all its resources, calling any method on disposed APIRequestContext will throw an exception.

Parameters:

  • reason: (String) (defaults to: nil)


95
96
97
# File 'lib/playwright_api/api_request_context.rb', line 95

def dispose(reason: nil)
  wrap_impl(@impl.dispose(reason: unwrap_impl(reason)))
end

#fetch(urlOrRequest, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, method: nil, multipart: nil, params: nil, timeout: nil) ⇒ APIResponse

Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

Usage

JSON objects can be passed directly to the request:

data = {
    "title": "Book Title",
    "body": "John Doe",
}
api_request_context.fetch("https://example.com/api/createBook", method="post", data=data)

The common way to send file(s) in the body of a request is to upload them as form fields with multipart/form-data encoding, by specifiying the multipart parameter:

api_request_context.fetch(
  "https://example.com/api/uploadScript",  method="post",
  multipart={
    "fileField": {
      "name": "f.js",
      "mimeType": "text/javascript",
      "buffer": b"console.log(2022);",
    },
  })

Parameters:

  • urlOrRequest (String, Request)
  • data: (String, String, untyped) (defaults to: nil)
  • failOnStatusCode: (Boolean) (defaults to: nil)
  • form: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • headers: (Hash[untyped, untyped]) (defaults to: nil)
  • ignoreHTTPSErrors: (Boolean) (defaults to: nil)
  • maxRedirects: (Integer) (defaults to: nil)
  • maxRetries: (Integer) (defaults to: nil)
  • method: (String) (defaults to: nil)
  • multipart: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • params: (Hash[untyped, untyped], String) (defaults to: nil)
  • timeout: (Float) (defaults to: nil)

Returns:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/playwright_api/api_request_context.rb', line 128

def fetch(
      urlOrRequest,
      data: nil,
      failOnStatusCode: nil,
      form: nil,
      headers: nil,
      ignoreHTTPSErrors: nil,
      maxRedirects: nil,
      maxRetries: nil,
      method: nil,
      multipart: nil,
      params: nil,
      timeout: nil)
  wrap_impl(@impl.fetch(unwrap_impl(urlOrRequest), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), method: unwrap_impl(method), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout)))
end

#get(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) ⇒ APIResponse

Sends HTTP(S) GET request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

Usage

Request parameters can be configured with params option, they will be serialized into the URL search parameters:

query_params = {
  "isbn": "1234",
  "page": "23"
}
api_request_context.get("https://example.com/api/getText", params=query_params)

Parameters:

  • url (String)
  • data: (String, String, untyped) (defaults to: nil)
  • failOnStatusCode: (Boolean) (defaults to: nil)
  • form: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • headers: (Hash[untyped, untyped]) (defaults to: nil)
  • ignoreHTTPSErrors: (Boolean) (defaults to: nil)
  • maxRedirects: (Integer) (defaults to: nil)
  • maxRetries: (Integer) (defaults to: nil)
  • multipart: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • params: (Hash[untyped, untyped], String) (defaults to: nil)
  • timeout: (Float) (defaults to: nil)

Returns:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/playwright_api/api_request_context.rb', line 160

def get(
      url,
      data: nil,
      failOnStatusCode: nil,
      form: nil,
      headers: nil,
      ignoreHTTPSErrors: nil,
      maxRedirects: nil,
      maxRetries: nil,
      multipart: nil,
      params: nil,
      timeout: nil)
  wrap_impl(@impl.get(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout)))
end

#head(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) ⇒ APIResponse

Sends HTTP(S) HEAD request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

Parameters:

  • url (String)
  • data: (String, String, untyped) (defaults to: nil)
  • failOnStatusCode: (Boolean) (defaults to: nil)
  • form: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • headers: (Hash[untyped, untyped]) (defaults to: nil)
  • ignoreHTTPSErrors: (Boolean) (defaults to: nil)
  • maxRedirects: (Integer) (defaults to: nil)
  • maxRetries: (Integer) (defaults to: nil)
  • multipart: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • params: (Hash[untyped, untyped], String) (defaults to: nil)
  • timeout: (Float) (defaults to: nil)

Returns:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/playwright_api/api_request_context.rb', line 179

def head(
      url,
      data: nil,
      failOnStatusCode: nil,
      form: nil,
      headers: nil,
      ignoreHTTPSErrors: nil,
      maxRedirects: nil,
      maxRetries: nil,
      multipart: nil,
      params: nil,
      timeout: nil)
  wrap_impl(@impl.head(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout)))
end

#off(event, callback) ⇒ Object

-- inherited from EventEmitter --



295
296
297
# File 'lib/playwright_api/api_request_context.rb', line 295

def off(event, callback)
  event_emitter_proxy.off(event, callback)
end

#on(event, callback) ⇒ Object

-- inherited from EventEmitter --



307
308
309
# File 'lib/playwright_api/api_request_context.rb', line 307

def on(event, callback)
  event_emitter_proxy.on(event, callback)
end

#once(event, callback) ⇒ Object

-- inherited from EventEmitter --



301
302
303
# File 'lib/playwright_api/api_request_context.rb', line 301

def once(event, callback)
  event_emitter_proxy.once(event, callback)
end

#patch(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) ⇒ APIResponse

Sends HTTP(S) PATCH request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

Parameters:

  • url (String)
  • data: (String, String, untyped) (defaults to: nil)
  • failOnStatusCode: (Boolean) (defaults to: nil)
  • form: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • headers: (Hash[untyped, untyped]) (defaults to: nil)
  • ignoreHTTPSErrors: (Boolean) (defaults to: nil)
  • maxRedirects: (Integer) (defaults to: nil)
  • maxRetries: (Integer) (defaults to: nil)
  • multipart: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • params: (Hash[untyped, untyped], String) (defaults to: nil)
  • timeout: (Float) (defaults to: nil)

Returns:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/playwright_api/api_request_context.rb', line 198

def patch(
      url,
      data: nil,
      failOnStatusCode: nil,
      form: nil,
      headers: nil,
      ignoreHTTPSErrors: nil,
      maxRedirects: nil,
      maxRetries: nil,
      multipart: nil,
      params: nil,
      timeout: nil)
  wrap_impl(@impl.patch(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout)))
end

#post(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) ⇒ APIResponse

Sends HTTP(S) POST request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

Usage

JSON objects can be passed directly to the request:

data = {
    "title": "Book Title",
    "body": "John Doe",
}
api_request_context.post("https://example.com/api/createBook", data=data)

To send form data to the server use form option. Its value will be encoded into the request body with application/x-www-form-urlencoded encoding (see below how to use multipart/form-data form encoding to send files):

formData = {
    "title": "Book Title",
    "body": "John Doe",
}
api_request_context.post("https://example.com/api/findBook", form=formData)

The common way to send file(s) in the body of a request is to upload them as form fields with multipart/form-data encoding. Use FormData to construct request body and pass it to the request as multipart parameter:

api_request_context.post(
  "https://example.com/api/uploadScript'",
  multipart={
    "fileField": {
      "name": "f.js",
      "mimeType": "text/javascript",
      "buffer": b"console.log(2022);",
    },
  })

Parameters:

  • url (String)
  • data: (String, String, untyped) (defaults to: nil)
  • failOnStatusCode: (Boolean) (defaults to: nil)
  • form: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • headers: (Hash[untyped, untyped]) (defaults to: nil)
  • ignoreHTTPSErrors: (Boolean) (defaults to: nil)
  • maxRedirects: (Integer) (defaults to: nil)
  • maxRetries: (Integer) (defaults to: nil)
  • multipart: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • params: (Hash[untyped, untyped], String) (defaults to: nil)
  • timeout: (Float) (defaults to: nil)

Returns:



253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/playwright_api/api_request_context.rb', line 253

def post(
      url,
      data: nil,
      failOnStatusCode: nil,
      form: nil,
      headers: nil,
      ignoreHTTPSErrors: nil,
      maxRedirects: nil,
      maxRetries: nil,
      multipart: nil,
      params: nil,
      timeout: nil)
  wrap_impl(@impl.post(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout)))
end

#put(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) ⇒ APIResponse

Sends HTTP(S) PUT request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

Parameters:

  • url (String)
  • data: (String, String, untyped) (defaults to: nil)
  • failOnStatusCode: (Boolean) (defaults to: nil)
  • form: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • headers: (Hash[untyped, untyped]) (defaults to: nil)
  • ignoreHTTPSErrors: (Boolean) (defaults to: nil)
  • maxRedirects: (Integer) (defaults to: nil)
  • maxRetries: (Integer) (defaults to: nil)
  • multipart: (Hash[untyped, untyped], untyped) (defaults to: nil)
  • params: (Hash[untyped, untyped], String) (defaults to: nil)
  • timeout: (Float) (defaults to: nil)

Returns:



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/playwright_api/api_request_context.rb', line 272

def put(
      url,
      data: nil,
      failOnStatusCode: nil,
      form: nil,
      headers: nil,
      ignoreHTTPSErrors: nil,
      maxRedirects: nil,
      maxRetries: nil,
      multipart: nil,
      params: nil,
      timeout: nil)
  wrap_impl(@impl.put(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout)))
end

#storage_state(indexedDB: nil, path: nil) ⇒ Object

Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.

Raises:

  • (NotImplementedError)


289
290
291
# File 'lib/playwright_api/api_request_context.rb', line 289

def storage_state(indexedDB: nil, path: nil)
  raise NotImplementedError.new('storage_state is not implemented yet.')
end