{"flag":true,"single":true,"pageTitle":"Playwrite using API","post":{"id":293,"user_id":"1","slug":"playwrite-using-api-vymj","title":"Playwrite using API","body":"<p><a href=\"https:\/\/playwright.dev\/python\/docs\/api-testing\">https:\/\/playwright.dev\/python\/docs\/api-testing<\/a><br>Playwright can be used to get access to the REST API of your application.<\/p>\r\n<p><strong>test_api_testing.py<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>from enum import auto\r\nimport os\r\nfrom typing import Generator\r\n\r\nimport pytest\r\nfrom playwright.sync_api import Playwright, Page, APIRequestContext, expect\r\n\r\nGITHUB_API_TOKEN = \"TOKEN HERE\" #https:\/\/github.com\/settings\/tokens \r\nassert GITHUB_API_TOKEN, \"GITHUB_API_TOKEN is not set\"\r\n\r\nGITHUB_USER = \"RizwanKMW\"\r\nassert GITHUB_USER, \"GITHUB_USER is not set\"\r\n\r\nGITHUB_REPO = \"test\"\r\n\r\n\r\n@pytest.fixture(scope=\"session\")\r\ndef api_request_context(\r\n    playwright: Playwright,\r\n) -&gt; Generator[APIRequestContext, None, None]:\r\n    headers = {\r\n        \"Accept\": \"application\/vnd.github.v3+json\",\r\n        \"Authorization\": f\"token {GITHUB_API_TOKEN}\",\r\n    }\r\n    request_context = playwright.request.new_context(\r\n        base_url=\"https:\/\/api.github.com\", extra_http_headers=headers\r\n    )\r\n    yield request_context\r\n    request_context.dispose()\r\n\r\n\r\n@pytest.fixture(scope=\"session\", autouse=True)\r\ndef create_test_repository(\r\n    api_request_context: APIRequestContext,\r\n) -&gt; Generator[None, None, None]:\r\n    #Before all\r\n    new_repo = api_request_context.post(\"\/user\/repos\", data={\"name\": GITHUB_REPO})\r\n    if not new_repo.ok:\r\n        print(f\"Failed to create repo: {new_repo.status} - {new_repo.text()}\")\r\n    assert new_repo.ok\r\n    yield\r\n    #After all\r\n    deleted_repo = api_request_context.delete(f\"\/repos\/{GITHUB_USER}\/{GITHUB_REPO}\")\r\n    if not deleted_repo.ok:\r\n        print(f\"Failed to delete repo: {deleted_repo.status} - {deleted_repo.text()}\")\r\n    assert deleted_repo.ok, f\"Repository deletion failed: {deleted_repo.text()}\"\r\n\r\ndef test_should_create_bug_report(api_request_context: APIRequestContext) -&gt; None:\r\n    data = {\r\n        \"title\": \"[Bug] report 1\",\r\n        \"body\": \"Bug description\",\r\n    }\r\n    new_issue = api_request_context.post(\r\n        f\"\/repos\/{GITHUB_USER}\/{GITHUB_REPO}\/issues\", data=data\r\n    )\r\n    assert new_issue.ok\r\n\r\n    issues = api_request_context.get(f\"\/repos\/{GITHUB_USER}\/{GITHUB_REPO}\/issues\")\r\n    assert issues.ok\r\n    issues_response = issues.json()\r\n    issue = list(\r\n        filter(lambda issue: issue[\"title\"] == \"[Bug] report 1\", issues_response)\r\n    )[0]\r\n    assert issue\r\n    assert issue[\"body\"] == \"Bug description\"\r\n\r\n\r\ndef test_should_create_feature_request(api_request_context: APIRequestContext) -&gt; None:\r\n    data = {\r\n        \"title\": \"[Feature] request 1\",\r\n        \"body\": \"Feature description\",\r\n    }\r\n    new_issue = api_request_context.post(\r\n        f\"\/repos\/{GITHUB_USER}\/{GITHUB_REPO}\/issues\", data=data\r\n    )\r\n    assert new_issue.ok\r\n\r\n    issues = api_request_context.get(f\"\/repos\/{GITHUB_USER}\/{GITHUB_REPO}\/issues\")\r\n    assert issues.ok\r\n    issues_response = issues.json()\r\n    issue = list(\r\n        filter(lambda issue: issue[\"title\"] == \"[Feature] request 1\", issues_response)\r\n    )[0]\r\n    assert issue\r\n    assert issue[\"body\"] == \"Feature description\"<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p><strong>One more testing: test_api_json_place<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>import pytest\r\nfrom playwright.sync_api import Playwright, APIRequestContext, expect\r\nfrom typing import Generator\r\n\r\n# API base URL (assuming a mock API is running locally)\r\nAPI_BASE_URL = \"https:\/\/jsonplaceholder.typicode.com\/\"\r\n\r\n# Fixture to set up API request context\r\n@pytest.fixture(scope=\"session\")\r\ndef api_request_context(\r\n    playwright: Playwright,\r\n) -&gt; Generator[APIRequestContext, None, None]:\r\n    \"\"\"Sets up a Playwright API request context for the session.\"\"\"\r\n    request_context = playwright.request.new_context(\r\n        base_url=API_BASE_URL,\r\n        extra_http_headers={\"Accept\": \"application\/json\"}\r\n    )\r\n    yield request_context\r\n    request_context.dispose()\r\n\r\n# Test for \/users endpoint\r\ndef test_users_endpoint(api_request_context: APIRequestContext) -&gt; None:\r\n    \"\"\"Tests that the \/users endpoint returns 10 users.\"\"\"\r\n    response = api_request_context.get(\"\/users\")\r\n    assert response.ok, f\"Request failed with status {response.status}\"\r\n    \r\n    users = response.json()\r\n    assert len(users) == 10, f\"Expected 10 users, got {len(users)}\"\r\n    \r\n    # Check basic structure of a user\r\n    first_user = users[0]\r\n    assert \"id\" in first_user\r\n    assert \"name\" in first_user\r\n\r\n# Test for \/posts endpoint\r\ndef test_posts_endpoint(api_request_context: APIRequestContext) -&gt; None:\r\n    \"\"\"Tests that the \/posts endpoint returns 100 posts.\"\"\"\r\n    response = api_request_context.get(\"\/posts\")\r\n    assert response.ok, f\"Request failed with status {response.status}\"\r\n    \r\n    posts = response.json()\r\n    assert len(posts) == 100, f\"Expected 100 posts, got {len(posts)}\"\r\n    \r\n    # Check basic structure of a post\r\n    first_post = posts[0]\r\n    assert \"id\" in first_post\r\n    assert \"title\" in first_post\r\n    print(first_post[\"title\"])\r\n\r\n# Test for \/comments endpoint\r\ndef test_comments_endpoint(api_request_context: APIRequestContext) -&gt; None:\r\n    \"\"\"Tests that the \/comments endpoint returns 500 comments.\"\"\"\r\n    response = api_request_context.get(\"\/comments\")\r\n    assert response.ok, f\"Request failed with status {response.status}\"\r\n    \r\n    comments = response.json()\r\n    assert len(comments) == 500, f\"Expected 500 comments, got {len(comments)}\"\r\n    \r\n    # Check basic structure of a comment\r\n    first_comment = comments[0]\r\n    assert \"id\" in first_comment\r\n    assert \"email\" in first_comment\r\n    print(first_comment[\"email\"])\r\n    \r\n\r\n# Optional: Test relationships between endpoints\r\ndef test_data_relationships(api_request_context: APIRequestContext) -&gt; None:\r\n    \"\"\"Tests that user_ids and post_ids in comments are valid.\"\"\"\r\n    users_response = api_request_context.get(\"\/users\")\r\n    posts_response = api_request_context.get(\"\/posts\")\r\n    comments_response = api_request_context.get(\"\/comments\")\r\n    \r\n    assert users_response.ok and posts_response.ok and comments_response.ok\r\n    \r\n    users = users_response.json()\r\n    posts = posts_response.json()\r\n    comments = comments_response.json()\r\n    \r\n    user_ids = {user[\"id\"] for user in users}  # Set of valid user IDs\r\n    post_ids = {post[\"id\"] for post in posts}  # Set of valid post IDs\r\n   <\/code><\/pre>","category_id":"37","is_private":"0","created_at":"2025-03-05T22:59:27.000000Z","updated_at":"2025-03-05T22:59:27.000000Z","category":{"id":37,"user_id":"1","name":"Playwright","slug":"playwright-c2zf","parent_id":null,"created_at":"2025-03-05T22:35:12.000000Z","updated_at":"2025-03-05T22:35:12.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"https:\/\/playwright.dev\/python\/docs\/api-testingPlaywright can be used to get access to the REST API of your application. test_api_testing.py  - Playwrite using API (Updated: March 5, 2025) - Read more about Playwrite using API at my programming site [SITE]","categories":[]}