2021-12-16 11:27:30 -08:00
# class: APIResponseAssertions
2022-01-25 18:53:49 +01:00
* langs: js, java, python
2021-12-16 11:27:30 -08:00
The [APIResponseAssertions] class provides assertion methods that can be used to make assertions about the [APIResponse] in the tests. A new instance of [APIResponseAssertions] is created by calling [`method: PlaywrightAssertions.expectAPIResponse` ]:
```js
import { test, expect } from '@playwright/test ';
test('navigates to login', async ({ page }) => {
// ...
const response = await page.request.get('https://playwright.dev');
2022-01-06 14:40:44 -08:00
await expect(response).toBeOK();
2021-12-16 11:27:30 -08:00
});
```
```java
...
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class TestPage {
...
@Test
void navigatesToLoginPage() {
...
APIResponse response = page.request().get('https://playwright.dev');
assertThat(response).isOK();
}
}
```
```python async
from playwright.async_api import Page, expect
async def test_navigates_to_login_page(page: Page) -> None:
# ..
response = await page.request.get('https://playwright.dev')
2022-01-06 14:40:44 -08:00
await expect(response).to_be_ok()
2021-12-16 11:27:30 -08:00
```
```python sync
from playwright.sync_api import Page, expect
def test_navigates_to_login_page(page: Page) -> None:
# ..
response = page.request.get('https://playwright.dev')
expect(response).to_be_ok()
```
2022-03-02 15:03:33 -08:00
## property: APIResponseAssertions.not
2021-12-16 11:27:30 -08:00
* langs: java, js
- returns: < [APIResponseAssertions]>
2022-02-01 16:09:41 -03:00
Makes the assertion check for the opposite condition. For example, this code tests that the response status is not successful:
2021-12-16 11:27:30 -08:00
```js
2022-01-06 14:40:44 -08:00
await expect(response).not.toBeOK();
2021-12-16 11:27:30 -08:00
```
```java
assertThat(response).not().isOK();
```
2022-01-25 18:53:49 +01:00
## async method: APIResponseAssertions.NotToBeOK
* langs: python
The opposite of [`method: APIResponseAssertions.toBeOK` ].
2022-01-06 14:40:44 -08:00
## async method: APIResponseAssertions.toBeOK
2021-12-16 11:27:30 -08:00
* langs:
- alias-java: isOK
2022-01-06 15:46:08 -08:00
Ensures the response status code is within [200..299] range.
2021-12-16 11:27:30 -08:00
```js
2022-01-06 14:40:44 -08:00
await expect(response).toBeOK();
2021-12-16 11:27:30 -08:00
```
```java
assertThat(response).isOK();
```
```python async
from playwright.async_api import expect
# ...
2022-01-06 14:40:44 -08:00
await expect(response).to_be_ok()
2021-12-16 11:27:30 -08:00
```
```python sync
import re
from playwright.sync_api import expect
# ...
expect(response).to_be_ok()
```