Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

docs(expect): APIResponse assertions (#10957)

Yury Semikhatsky0eaa19d5e77f
diff --git a/docs/src/api/class-apiresponseassertions.md b/docs/src/api/class-apiresponseassertions.md
new file mode 100644
index 0000000000..e83076048c
--- /dev/null
+++ b/docs/src/api/class-apiresponseassertions.md
@@ -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()
+```
diff --git a/docs/src/api/class-pageassertions.md b/docs/src/api/class-pageassertions.md
index 65bd078150..e90598a69e 100644
--- a/docs/src/api/class-pageassertions.md
+++ b/docs/src/api/class-pageassertions.md
@@ -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';
diff --git a/docs/src/api/class-playwrightassertions.md b/docs/src/api/class-playwrightassertions.md
index 1e375bb227..3c9c226bc7 100644
--- a/docs/src/api/class-playwrightassertions.md
+++ b/docs/src/api/class-playwrightassertions.md
@@ -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
diff --git a/utils/doclint/missingDocs.js b/utils/doclint/missingDocs.js
index 79bcb0c975..c4e1cd0930 100644
--- a/utils/doclint/missingDocs.js
+++ b/utils/doclint/missingDocs.js
@@ -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 = [];
diff --git a/utils/generate_types/index.js b/utils/generate_types/index.js
index f29bc3f01c..23eb9c36ae 100644
--- a/utils/generate_types/index.js
+++ b/utils/generate_types/index.js
@@ -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');