2021-12-16 11:27:30 -08:00
|
|
|
# class: APIResponseAssertions
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2021-12-16 11:27:30 -08:00
|
|
|
|
2023-02-16 11:48:38 -08:00
|
|
|
The [APIResponseAssertions] class provides assertion methods that can be used to make assertions about the [APIResponse] in the tests.
|
2021-12-16 11:27:30 -08:00
|
|
|
|
|
|
|
```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
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.20
|
2022-08-24 17:30:47 +02:00
|
|
|
* langs: java, js, csharp
|
2021-12-16 11:27:30 -08:00
|
|
|
- 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
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.19
|
2022-01-25 18:53:49 +01:00
|
|
|
* langs: python
|
|
|
|
|
|
|
|
The opposite of [`method: APIResponseAssertions.toBeOK`].
|
|
|
|
|
2022-01-06 14:40:44 -08:00
|
|
|
## async method: APIResponseAssertions.toBeOK
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.18
|
2021-12-16 11:27:30 -08:00
|
|
|
* langs:
|
|
|
|
- alias-java: isOK
|
|
|
|
|
2022-08-24 17:30:47 +02:00
|
|
|
Ensures the response status code is within `200..299` range.
|
2021-12-16 11:27:30 -08:00
|
|
|
|
2022-11-21 10:40:21 -08:00
|
|
|
**Usage**
|
|
|
|
|
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()
|
|
|
|
```
|