mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
95 lines
2.0 KiB
Markdown
95 lines
2.0 KiB
Markdown
![]() |
# class: APIResponseAssertions
|
||
|
* langs: js
|
||
|
|
||
|
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');
|
||
|
expect(response).toBeOK();
|
||
|
});
|
||
|
```
|
||
|
|
||
|
```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
|
||
|
import re
|
||
|
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')
|
||
|
expect(response).to_be_ok()
|
||
|
```
|
||
|
|
||
|
```python sync
|
||
|
import re
|
||
|
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()
|
||
|
```
|
||
|
|
||
|
|
||
|
## method: APIResponseAssertions.not
|
||
|
* langs: java, js
|
||
|
- returns: <[APIResponseAssertions]>
|
||
|
|
||
|
Makes the assertion check for the opposite condition. For example, this code tests that the response status is not successfull:
|
||
|
|
||
|
```js
|
||
|
expect(response).not.toBeOK();
|
||
|
```
|
||
|
|
||
|
```java
|
||
|
assertThat(response).not().isOK();
|
||
|
```
|
||
|
|
||
|
## method: APIResponseAssertions.toBeOK
|
||
|
* langs:
|
||
|
- alias-java: isOK
|
||
|
|
||
|
Ensures the response status code is within [200..299) range.
|
||
|
|
||
|
```js
|
||
|
expect(response).toBeOK();
|
||
|
```
|
||
|
|
||
|
```java
|
||
|
assertThat(response).isOK();
|
||
|
```
|
||
|
|
||
|
```python async
|
||
|
import re
|
||
|
from playwright.async_api import expect
|
||
|
|
||
|
# ...
|
||
|
expect(response).to_be_ok()
|
||
|
```
|
||
|
|
||
|
```python sync
|
||
|
import re
|
||
|
from playwright.sync_api import expect
|
||
|
|
||
|
# ...
|
||
|
expect(response).to_be_ok()
|
||
|
```
|