Class: Playwright::APIRequestContext
- Inherits:
-
PlaywrightApi
- Object
- PlaywrightApi
- Playwright::APIRequestContext
- Defined in:
- lib/playwright_api/api_request_context.rb
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 associated with it ‘APIRequestContext` instance which shares cookie storage with the browser context and can be accessed via [`property: BrowserContext.request`] or [`property: Page.request`]. It is also possible to create a new APIRequestContext instance manually by calling [`method: APIRequest.newContext`].
**Cookie management**
‘APIRequestContext` returned by [`property: BrowserContext.request`] and [`property: Page.request`] shares cookie storage with the corresponding `BrowserContext`. Each API request will have `Cookie` header populated with the values from the browser context. If the API response contains `Set-Cookie` header it will automatically update `BrowserContext` cookies and requests made from the page will pick them up. This means that if you log in using this API, your e2e test will be logged in and vice versa.
If you want API requests to not interfere with the browser cookies you should create a new ‘APIRequestContext` by calling [`method: APIRequest.newContext`]. Such `APIRequestContext` object will have its own isolated cookie storage.
“‘python sync 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
Instance Method Summary collapse
-
#delete(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [DELETE](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE) request and returns its response.
-
#dispose ⇒ Object
All responses returned by [‘method: APIRequestContext.get`] and similar methods are stored in the memory, so that you can later call [`method: APIResponse.body`].
-
#fetch(urlOrRequest, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, method: nil, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) request and returns its response.
-
#get(url, failOnStatusCode: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [GET](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) request and returns its response.
-
#head(url, failOnStatusCode: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [HEAD](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) request and returns its response.
-
#off(event, callback) ⇒ Object
– inherited from EventEmitter –.
-
#on(event, callback) ⇒ Object
– inherited from EventEmitter –.
-
#once(event, callback) ⇒ Object
– inherited from EventEmitter –.
-
#patch(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [PATCH](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH) request and returns its response.
-
#post(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [POST](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request and returns its response.
-
#put(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [PUT](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) request and returns its response.
-
#storage_state(path: nil) ⇒ Object
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
Methods inherited from PlaywrightApi
Constructor Details
This class inherits a constructor from Playwright::PlaywrightApi
Instance Method Details
#delete(url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [DELETE](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/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.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/playwright_api/api_request_context.rb', line 71 def delete( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: 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), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end |
#dispose ⇒ Object
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 stored responses, and makes
- ‘method: APIResponse.body`
-
throw “Response disposed” error.
88 89 90 |
# File 'lib/playwright_api/api_request_context.rb', line 88 def dispose wrap_impl(@impl.dispose) end |
#fetch(urlOrRequest, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, method: nil, multipart: nil, params: nil, timeout: nil) ⇒ Object
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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/playwright_api/api_request_context.rb', line 94 def fetch( urlOrRequest, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: 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), method: unwrap_impl(method), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end |
#get(url, failOnStatusCode: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [GET](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/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.
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/playwright_api/api_request_context.rb', line 112 def get( url, failOnStatusCode: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, params: nil, timeout: nil) wrap_impl(@impl.get(unwrap_impl(url), failOnStatusCode: unwrap_impl(failOnStatusCode), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end |
#head(url, failOnStatusCode: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [HEAD](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/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.
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/playwright_api/api_request_context.rb', line 126 def head( url, failOnStatusCode: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, params: nil, timeout: nil) wrap_impl(@impl.head(unwrap_impl(url), failOnStatusCode: unwrap_impl(failOnStatusCode), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end |
#off(event, callback) ⇒ Object
– inherited from EventEmitter –
196 197 198 |
# File 'lib/playwright_api/api_request_context.rb', line 196 def off(event, callback) event_emitter_proxy.off(event, callback) end |
#on(event, callback) ⇒ Object
– inherited from EventEmitter –
208 209 210 |
# File 'lib/playwright_api/api_request_context.rb', line 208 def on(event, callback) event_emitter_proxy.on(event, callback) end |
#once(event, callback) ⇒ Object
– inherited from EventEmitter –
202 203 204 |
# File 'lib/playwright_api/api_request_context.rb', line 202 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, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [PATCH](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/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.
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/playwright_api/api_request_context.rb', line 140 def patch( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: 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), 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, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [POST](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/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.
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/playwright_api/api_request_context.rb', line 157 def post( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: 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), 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, multipart: nil, params: nil, timeout: nil) ⇒ Object
Sends HTTP(S) [PUT](developer.mozilla.org/en-US/docs/Web/HTTP/Methods/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.
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/playwright_api/api_request_context.rb', line 174 def put( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: 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), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end |
#storage_state(path: nil) ⇒ Object
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
190 191 192 |
# File 'lib/playwright_api/api_request_context.rb', line 190 def storage_state(path: nil) raise NotImplementedError.new('storage_state is not implemented yet.') end |