@@ -6,6 +6,106 @@ environment or the service to your e2e test. When used on [Page] or a [BrowserCo
66the cookies from the corresponding [ BrowserContext] . This means that if you log in using this API, your e2e test
77will 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
0 commit comments