Skip to content

Commit af4a1c2

Browse files
authored
docs(python): add request API examples (microsoft#10512)
1 parent 8fa0a87 commit af4a1c2

File tree

5 files changed

+144
-3
lines changed

5 files changed

+144
-3
lines changed

docs/src/api/class-apirequest.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# class: APIRequest
22
* langs: js, java, python
33

4-
Exposes API that can be used for the Web API testing.
4+
Exposes API that can be used for the Web API testing. Each Playwright browser context
5+
has a APIRequestContext instance attached which shares cookies with the page context.
6+
Its also possible to create a new APIRequestContext instance manually. For more information
7+
see [here](./class-apirequestcontext).
58

69
## async method: APIRequest.newContext
710
* langs: js, java, python

docs/src/api/class-apirequestcontext.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,106 @@ environment or the service to your e2e test. When used on [Page] or a [BrowserCo
66
the cookies from the corresponding [BrowserContext]. This means that if you log in using this API, your e2e test
77
will be logged in and vice versa.
88

9+
```python async
10+
import os
11+
import asyncio
12+
from playwright.async_api import async_playwright, Playwright
13+
14+
REPO = "test-repo-1"
15+
USER = "github-username"
16+
API_TOKEN = os.getenv("GITHUB_API_TOKEN")
17+
18+
async def run(playwright: Playwright):
19+
# This will launch a new browser, create a context and page. When making HTTP
20+
# requests with the internal APIRequestContext (e.g. `context.request` or `page.request`)
21+
# it will automatically set the cookies to the browser page and vise versa.
22+
browser = await playwright.chromium.launch()
23+
context = await browser.new_context(base_url="https://api.github.com")
24+
api_request_context = context.request
25+
page = await context.new_page()
26+
27+
# Alternatively you can create a APIRequestContext manually without having a browser context attached:
28+
# api_request_context = await playwright.request.new_context(base_url="https://api.github.com")
29+
30+
# Create a repository.
31+
response = await api_request_context.post(
32+
"/user/repos",
33+
headers={
34+
"Accept": "application/vnd.github.v3+json",
35+
# Add GitHub personal access token.
36+
"Authorization": f"token {API_TOKEN}",
37+
},
38+
data={"name": REPO},
39+
)
40+
assert response.ok
41+
assert response.json()["name"] == REPO
42+
43+
# Delete a repository.
44+
response = await api_request_context.delete(
45+
f"/repos/{USER}/{REPO}",
46+
headers={
47+
"Accept": "application/vnd.github.v3+json",
48+
# Add GitHub personal access token.
49+
"Authorization": f"token {API_TOKEN}",
50+
},
51+
)
52+
assert response.ok
53+
assert await response.body() == '{"status": "ok"}'
54+
55+
async def main():
56+
async with async_playwright() as playwright:
57+
await run(playwright)
58+
59+
asyncio.run(main())
60+
```
61+
62+
```python sync
63+
import os
64+
from playwright.sync_api import sync_playwright
65+
66+
REPO = "test-repo-1"
67+
USER = "github-username"
68+
API_TOKEN = os.getenv("GITHUB_API_TOKEN")
69+
70+
with sync_playwright() as p:
71+
# This will launch a new browser, create a context and page. When making HTTP
72+
# requests with the internal APIRequestContext (e.g. `context.request` or `page.request`)
73+
# it will automatically set the cookies to the browser page and vise versa.
74+
browser = playwright.chromium.launch()
75+
context = browser.new_context(base_url="https://api.github.com")
76+
api_request_context = context.request
77+
page = context.new_page()
78+
79+
# Alternatively you can create a APIRequestContext manually without having a browser context attached:
80+
# api_request_context = playwright.request.new_context(base_url="https://api.github.com")
81+
82+
83+
# Create a repository.
84+
response = api_request_context.post(
85+
"/user/repos",
86+
headers={
87+
"Accept": "application/vnd.github.v3+json",
88+
# Add GitHub personal access token.
89+
"Authorization": f"token {API_TOKEN}",
90+
},
91+
data={"name": REPO},
92+
)
93+
assert response.ok
94+
assert response.json()["name"] == REPO
95+
96+
# Delete a repository.
97+
response = api_request_context.delete(
98+
f"/repos/{USER}/{REPO}",
99+
headers={
100+
"Accept": "application/vnd.github.v3+json",
101+
# Add GitHub personal access token.
102+
"Authorization": f"token {API_TOKEN}",
103+
},
104+
)
105+
assert response.ok
106+
assert await response.body() == '{"status": "ok"}'
107+
```
108+
9109
## async method: APIRequestContext.delete
10110
- returns: <[APIResponse]>
11111

docs/src/api/class-apiresponse.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,40 @@
33

44
[APIResponse] class represents responses returned by [`method: APIRequestContext.get`] and similar methods.
55

6+
```python async
7+
import asyncio
8+
from playwright.async_api import async_playwright, Playwright
9+
10+
async def run(playwright: Playwright):
11+
context = await playwright.request.new_context()
12+
response = await context.get("https://example.com/user/repos")
13+
assert response.ok
14+
assert response.status == 200
15+
assert response.headers["content-type"] == "application/json; charset=utf-8"
16+
assert response.json()["name"] == "foobar"
17+
assert await response.body() == '{"status": "ok"}'
18+
19+
20+
async def main():
21+
async with async_playwright() as playwright:
22+
await run(playwright)
23+
24+
asyncio.run(main())
25+
```
26+
27+
```python sync
28+
from playwright.sync_api import sync_playwright
29+
30+
with sync_playwright() as p:
31+
context = playwright.request.new_context()
32+
response = context.get("https://example.com/user/repos")
33+
assert response.ok
34+
assert response.status == 200
35+
assert response.headers["content-type"] == "application/json; charset=utf-8"
36+
assert response.json()["name"] == "foobar"
37+
assert response.body() == '{"status": "ok"}'
38+
```
39+
640
## async method: APIResponse.body
741
- returns: <[Buffer]>
842

docs/src/test-api-testing-js.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ const USER = 'github-username';
163163
});
164164

165165
// Delete a repository.
166-
await context.delete(`/repos/${USER}/${REPO}`{
166+
await context.delete(`/repos/${USER}/${REPO}`, {
167167
headers: {
168168
'Accept': 'application/vnd.github.v3+json',
169169
// Add GitHub personal access token.

packages/playwright-core/types/types.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11813,7 +11813,9 @@ export interface AndroidWebView {
1181311813
}
1181411814

1181511815
/**
11816-
* Exposes API that can be used for the Web API testing.
11816+
* Exposes API that can be used for the Web API testing. Each Playwright browser context has a APIRequestContext instance
11817+
* attached which shares cookies with the page context. Its also possible to create a new APIRequestContext instance
11818+
* manually. For more information see [here](https://playwright.dev/docs/class-apirequestcontext).
1181711819
*/
1181811820
export interface APIRequest {
1181911821
/**
@@ -11939,6 +11941,7 @@ export interface APIRequest {
1193911941
* environment or the service to your e2e test. When used on [Page] or a [BrowserContext], this API will automatically use
1194011942
* the cookies from the corresponding [BrowserContext]. This means that if you log in using this API, your e2e test will be
1194111943
* logged in and vice versa.
11944+
*
1194211945
*/
1194311946
export interface APIRequestContext {
1194411947
/**
@@ -12428,6 +12431,7 @@ export interface APIRequestContext {
1242812431
* [APIResponse] class represents responses returned by
1242912432
* [apiRequestContext.get(url[, options])](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get)
1243012433
* and similar methods.
12434+
*
1243112435
*/
1243212436
export interface APIResponse {
1243312437
/**

0 commit comments

Comments
 (0)