docs(expect): APIResponse assertions (#10957)

This commit is contained in:
Yury Semikhatsky 2021-12-16 11:27:30 -08:00 committed by GitHub
parent b606b5b8a0
commit 0eaa19d5e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 115 additions and 3 deletions

View file

@ -0,0 +1,94 @@
# 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()
```

View file

@ -1,7 +1,7 @@
# class: PageAssertions
* langs: java, python, js
The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests. A new instance of [LocatorAssertions] is created by calling [`method: PlaywrightAssertions.expectPage`]:
The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests. A new instance of [PageAssertions] is created by calling [`method: PlaywrightAssertions.expectPage`]:
```js
import { test, expect } from '@playwright/test';

View file

@ -64,6 +64,24 @@ By default, the timeout for assertions is set to 5 seconds.
</dependency>
```
## method: PlaywrightAssertions.expectAPIResponse
* langs: js
- alias-java: assertThat
- alias-python: expect
- alias-js: expect
- returns: <[APIResponseAssertions]>
Creates a [APIResponseAssertions] object for the given [APIResponse].
```java
PlaywrightAssertions.assertThat(response).isOK();
```
### param: PlaywrightAssertions.expectAPIResponse.response
- `response` <[APIResponse]>
[APIResponse] object to use for assertions.
## method: PlaywrightAssertions.expectLocator
* langs: java, python, js
- alias-java: assertThat

View file

@ -22,7 +22,7 @@ const path = require('path');
/** @typedef {import('../../markdown').MarkdownNode} MarkdownNode */
const IGNORE_CLASSES = ['PlaywrightAssertions', 'LocatorAssertions', 'PageAssertions'];
const IGNORE_CLASSES = ['PlaywrightAssertions', 'LocatorAssertions', 'PageAssertions', 'APIResponseAssertions'];
module.exports = function lint(documentation, jsSources, apiFileName) {
const errors = [];

View file

@ -109,7 +109,7 @@ class TypesGenerator {
return (!docsOnlyClassMapping && docClass) ? this.classBody(docClass) : '';
});
const IGNORED_CLASSES = ['PlaywrightAssertions', 'LocatorAssertions', 'PageAssertions'];
const IGNORED_CLASSES = ['PlaywrightAssertions', 'LocatorAssertions', 'PageAssertions', 'APIResponseAssertions'];
const classes = this.documentation.classesArray.filter(cls => !IGNORED_CLASSES.includes(cls.name)).filter(cls => !handledClasses.has(cls.name));
{
const playwright = this.documentation.classesArray.find(c => c.name === 'Playwright');