> For the complete documentation index, see [llms.txt](https://ultimatewebsolutions.gitbook.io/qa-knowledge-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ultimatewebsolutions.gitbook.io/qa-knowledge-base/api-testing-with-python/using-playwright.md).

# Using Playwright

In **Playwright with Python**, you can test APIs without opening a browser. Playwright provides an `APIRequestContext` for this.

* `new_context()` → **open/create** an API session
* `get()` → **send the request**
* `assert` → **check the result**
* `dispose()` → **close/clean up** the API session

Let's test a `GET` endpoint:

```python
from playwright.sync_api import Playwright


def test_get_user(playwright: Playwright):
    request = playwright.request.new_context()

    response = request.get(
        "https://jsonplaceholder.typicode.com/users/1"
    )

    assert response.status == 200, \
        f"Expected status 200, but got {response.status}"

    data = response.json()

    assert data["name"] == "Lui Bartol", \
        f"Expected name 'Lui Bartol', but got '{data['name']}'"

    request.dispose()
```

```py
python -m pytest tests/test_api_get.py -v
```

<div align="left"><figure><img src="https://68936076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKa5W2q6HA7EDVl7m4OHV%2Fuploads%2FgiwcYvj8mjuhcYRtCPwS%2Fplaywright.png?alt=media&amp;token=0938ed1a-1753-4bbd-8ca2-d51890e4e2ab" alt=""><figcaption></figcaption></figure></div>

`POST` example:

```python
def test_create_user(playwright: Playwright):
    request = playwright.request.new_context()

    response = request.post(
        "https://jsonplaceholder.typicode.com/users",
        data={
            "name": "John",
            "email": "john@test.com"
        }
    )

    assert response.status == 201

    data = response.json()

    assert data["name"] == "John"
    assert data["email"] == "john@test.com"

    request.dispose()
```
