Skip to content

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

chore: make maxRedirects available in java, improve docs. tests (#17227)

Yury Semikhatsky24ee456d434e
diff --git a/docs/src/api/class-apirequestcontext.md b/docs/src/api/class-apirequestcontext.md
index 93e1d44e6e..033189c8a5 100644
--- a/docs/src/api/class-apirequestcontext.md
+++ b/docs/src/api/class-apirequestcontext.md
@@ -143,9 +143,9 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.delete.params = %%-csharp-fetch-option-params-%%
* since: v1.16
-### option: APIRequestContext.delete.headers = %%-js-python-fetch-option-headers-%%
+### option: APIRequestContext.delete.headers = %%-js-python-csharp-fetch-option-headers-%%
* since: v1.16
-### option: APIRequestContext.delete.data = %%-js-python-fetch-option-data-%%
+### option: APIRequestContext.delete.data = %%-js-python-csharp-fetch-option-data-%%
* since: v1.17
### option: APIRequestContext.delete.form = %%-js-python-fetch-option-form-%%
* since: v1.17
@@ -155,13 +155,13 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.17
### option: APIRequestContext.delete.multipart = %%-csharp-fetch-option-multipart-%%
* since: v1.17
-### option: APIRequestContext.delete.timeout = %%-js-python-fetch-option-timeout-%%
+### option: APIRequestContext.delete.timeout = %%-js-python-csharp-fetch-option-timeout-%%
* since: v1.16
-### option: APIRequestContext.delete.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
+### option: APIRequestContext.delete.failOnStatusCode = %%-js-python-csharp-fetch-option-failonstatuscode-%%
* since: v1.16
-### option: APIRequestContext.delete.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
+### option: APIRequestContext.delete.ignoreHTTPSErrors = %%-js-python-csharp-fetch-option-ignorehttpserrors-%%
* since: v1.16
-### option: APIRequestContext.delete.maxRedirects = %%-js-python-fetch-option-maxredirects-%%
+### option: APIRequestContext.delete.maxRedirects = %%-js-python-csharp-fetch-option-maxredirects-%%
* since: v1.26
## async method: APIRequestContext.dispose
@@ -198,9 +198,9 @@ Target URL or Request to get all parameters from.
If set changes the fetch method (e.g. [PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) or
[POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)). If not specified, GET method is used.
-### option: APIRequestContext.fetch.headers = %%-js-python-fetch-option-headers-%%
+### option: APIRequestContext.fetch.headers = %%-js-python-csharp-fetch-option-headers-%%
* since: v1.16
-### option: APIRequestContext.fetch.data = %%-js-python-fetch-option-data-%%
+### option: APIRequestContext.fetch.data = %%-js-python-csharp-fetch-option-data-%%
* since: v1.16
### option: APIRequestContext.fetch.form = %%-js-python-fetch-option-form-%%
* since: v1.16
@@ -210,13 +210,13 @@ If set changes the fetch method (e.g. [PUT](https://developer.mozilla.org/en-US/
* since: v1.16
### option: APIRequestContext.fetch.multipart = %%-csharp-fetch-option-multipart-%%
* since: v1.16
-### option: APIRequestContext.fetch.timeout = %%-js-python-fetch-option-timeout-%%
+### option: APIRequestContext.fetch.timeout = %%-js-python-csharp-fetch-option-timeout-%%
* since: v1.16
-### option: APIRequestContext.fetch.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
+### option: APIRequestContext.fetch.failOnStatusCode = %%-js-python-csharp-fetch-option-failonstatuscode-%%
* since: v1.16
-### option: APIRequestContext.fetch.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
+### option: APIRequestContext.fetch.ignoreHTTPSErrors = %%-js-python-csharp-fetch-option-ignorehttpserrors-%%
* since: v1.16
-### option: APIRequestContext.fetch.maxRedirects = %%-js-python-fetch-option-maxredirects-%%
+### option: APIRequestContext.fetch.maxRedirects = %%-js-python-csharp-fetch-option-maxredirects-%%
* since: v1.26
## async method: APIRequestContext.get
@@ -235,15 +235,15 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.get.params = %%-csharp-fetch-option-params-%%
* since: v1.16
-### option: APIRequestContext.get.headers = %%-js-python-fetch-option-headers-%%
+### option: APIRequestContext.get.headers = %%-js-python-csharp-fetch-option-headers-%%
* since: v1.16
-### option: APIRequestContext.get.timeout = %%-js-python-fetch-option-timeout-%%
+### option: APIRequestContext.get.timeout = %%-js-python-csharp-fetch-option-timeout-%%
* since: v1.16
-### option: APIRequestContext.get.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
+### option: APIRequestContext.get.failOnStatusCode = %%-js-python-csharp-fetch-option-failonstatuscode-%%
* since: v1.16
-### option: APIRequestContext.get.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
+### option: APIRequestContext.get.ignoreHTTPSErrors = %%-js-python-csharp-fetch-option-ignorehttpserrors-%%
* since: v1.16
-### option: APIRequestContext.get.maxRedirects = %%-js-python-fetch-option-maxredirects-%%
+### option: APIRequestContext.get.maxRedirects = %%-js-python-csharp-fetch-option-maxredirects-%%
* since: v1.26
## async method: APIRequestContext.head
@@ -262,15 +262,15 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.head.params = %%-csharp-fetch-option-params-%%
* since: v1.16
-### option: APIRequestContext.head.headers = %%-js-python-fetch-option-headers-%%
+### option: APIRequestContext.head.headers = %%-js-python-csharp-fetch-option-headers-%%
* since: v1.16
-### option: APIRequestContext.head.timeout = %%-js-python-fetch-option-timeout-%%
+### option: APIRequestContext.head.timeout = %%-js-python-csharp-fetch-option-timeout-%%
* since: v1.16
-### option: APIRequestContext.head.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
+### option: APIRequestContext.head.failOnStatusCode = %%-js-python-csharp-fetch-option-failonstatuscode-%%
* since: v1.16
-### option: APIRequestContext.head.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
+### option: APIRequestContext.head.ignoreHTTPSErrors = %%-js-python-csharp-fetch-option-ignorehttpserrors-%%
* since: v1.16
-### option: APIRequestContext.head.maxRedirects = %%-js-python-fetch-option-maxredirects-%%
+### option: APIRequestContext.head.maxRedirects = %%-js-python-csharp-fetch-option-maxredirects-%%
* since: v1.26
## async method: APIRequestContext.patch
@@ -289,9 +289,9 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.patch.params = %%-csharp-fetch-option-params-%%
* since: v1.16
-### option: APIRequestContext.patch.headers = %%-js-python-fetch-option-headers-%%
+### option: APIRequestContext.patch.headers = %%-js-python-csharp-fetch-option-headers-%%
* since: v1.16
-### option: APIRequestContext.patch.data = %%-js-python-fetch-option-data-%%
+### option: APIRequestContext.patch.data = %%-js-python-csharp-fetch-option-data-%%
* since: v1.16
### option: APIRequestContext.patch.form = %%-js-python-fetch-option-form-%%
* since: v1.16
@@ -301,13 +301,13 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.patch.multipart = %%-csharp-fetch-option-multipart-%%
* since: v1.16
-### option: APIRequestContext.patch.timeout = %%-js-python-fetch-option-timeout-%%
+### option: APIRequestContext.patch.timeout = %%-js-python-csharp-fetch-option-timeout-%%
* since: v1.16
-### option: APIRequestContext.patch.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
+### option: APIRequestContext.patch.failOnStatusCode = %%-js-python-csharp-fetch-option-failonstatuscode-%%
* since: v1.16
-### option: APIRequestContext.patch.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
+### option: APIRequestContext.patch.ignoreHTTPSErrors = %%-js-python-csharp-fetch-option-ignorehttpserrors-%%
* since: v1.16
-### option: APIRequestContext.patch.maxRedirects = %%-js-python-fetch-option-maxredirects-%%
+### option: APIRequestContext.patch.maxRedirects = %%-js-python-csharp-fetch-option-maxredirects-%%
* since: v1.26
## async method: APIRequestContext.post
@@ -326,9 +326,9 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.post.params = %%-csharp-fetch-option-params-%%
* since: v1.16
-### option: APIRequestContext.post.headers = %%-js-python-fetch-option-headers-%%
+### option: APIRequestContext.post.headers = %%-js-python-csharp-fetch-option-headers-%%
* since: v1.16
-### option: APIRequestContext.post.data = %%-js-python-fetch-option-data-%%
+### option: APIRequestContext.post.data = %%-js-python-csharp-fetch-option-data-%%
* since: v1.16
### option: APIRequestContext.post.form = %%-js-python-fetch-option-form-%%
* since: v1.16
@@ -338,13 +338,13 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.post.multipart = %%-csharp-fetch-option-multipart-%%
* since: v1.16
-### option: APIRequestContext.post.timeout = %%-js-python-fetch-option-timeout-%%
+### option: APIRequestContext.post.timeout = %%-js-python-csharp-fetch-option-timeout-%%
* since: v1.16
-### option: APIRequestContext.post.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
+### option: APIRequestContext.post.failOnStatusCode = %%-js-python-csharp-fetch-option-failonstatuscode-%%
* since: v1.16
-### option: APIRequestContext.post.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
+### option: APIRequestContext.post.ignoreHTTPSErrors = %%-js-python-csharp-fetch-option-ignorehttpserrors-%%
* since: v1.16
-### option: APIRequestContext.post.maxRedirects = %%-js-python-fetch-option-maxredirects-%%
+### option: APIRequestContext.post.maxRedirects = %%-js-python-csharp-fetch-option-maxredirects-%%
* since: v1.26
## async method: APIRequestContext.put
@@ -363,9 +363,9 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.put.params = %%-csharp-fetch-option-params-%%
* since: v1.16
-### option: APIRequestContext.put.headers = %%-js-python-fetch-option-headers-%%
+### option: APIRequestContext.put.headers = %%-js-python-csharp-fetch-option-headers-%%
* since: v1.16
-### option: APIRequestContext.put.data = %%-js-python-fetch-option-data-%%
+### option: APIRequestContext.put.data = %%-js-python-csharp-fetch-option-data-%%
* since: v1.16
### option: APIRequestContext.put.form = %%-js-python-fetch-option-form-%%
* since: v1.16
@@ -375,13 +375,13 @@ context cookies from the response. The method will automatically follow redirect
* since: v1.16
### option: APIRequestContext.put.multipart = %%-csharp-fetch-option-multipart-%%
* since: v1.16
-### option: APIRequestContext.put.timeout = %%-js-python-fetch-option-timeout-%%
+### option: APIRequestContext.put.timeout = %%-js-python-csharp-fetch-option-timeout-%%
* since: v1.16
-### option: APIRequestContext.put.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
+### option: APIRequestContext.put.failOnStatusCode = %%-js-python-csharp-fetch-option-failonstatuscode-%%
* since: v1.16
-### option: APIRequestContext.put.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
+### option: APIRequestContext.put.ignoreHTTPSErrors = %%-js-python-csharp-fetch-option-ignorehttpserrors-%%
* since: v1.16
-### option: APIRequestContext.put.maxRedirects = %%-js-python-fetch-option-maxredirects-%%
+### option: APIRequestContext.put.maxRedirects = %%-js-python-csharp-fetch-option-maxredirects-%%
* since: v1.26
## async method: APIRequestContext.storageState
diff --git a/docs/src/api/class-requestoptions.md b/docs/src/api/class-requestoptions.md
index c9ac9438ce..65130ee494 100644
--- a/docs/src/api/class-requestoptions.md
+++ b/docs/src/api/class-requestoptions.md
@@ -114,6 +114,17 @@ Header value.
Whether to ignore HTTPS errors when sending network requests.
+## method: RequestOptions.setMaxRedirects
+* since: v1.26
+- returns: <[RequestOptions]>
+
+### param: RequestOptions.setMaxRedirects.maxRedirects
+* since: v1.26
+- `maxRedirects` <[int]>
+
+Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded.
+Defaults to `20`. Pass `0` to not follow redirects.
+
## method: RequestOptions.setMethod
* since: v1.18
- returns: <[RequestOptions]>
diff --git a/docs/src/api/params.md b/docs/src/api/params.md
index 100f13ad17..4dc1969c56 100644
--- a/docs/src/api/params.md
+++ b/docs/src/api/params.md
@@ -324,19 +324,19 @@ Query parameters to be sent with the URL.
Optional request parameters.
-## js-python-fetch-option-headers
+## js-python-csharp-fetch-option-headers
* langs: js, python, csharp
- `headers` <[Object]<[string], [string]>>
Allows to set HTTP headers.
-## js-python-fetch-option-timeout
+## js-python-csharp-fetch-option-timeout
* langs: js, python, csharp
- `timeout` <[float]>
Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
-## js-python-fetch-option-failonstatuscode
+## js-python-csharp-fetch-option-failonstatuscode
* langs: js, python, csharp
- `failOnStatusCode` <[boolean]>
@@ -384,7 +384,7 @@ or as file-like object containing file name, mime-type and its content.
An instance of [FormData] can be created via [`method: APIRequestContext.createFormData`].
-## js-python-fetch-option-data
+## js-python-csharp-fetch-option-data
* langs: js, python, csharp
- `data` <[string]|[Buffer]|[Serializable]>
@@ -392,17 +392,18 @@ Allows to set post data of the request. If the data parameter is an object, it w
and `content-type` header will be set to `application/json` if not explicitly set. Otherwise the `content-type` header will be
set to `application/octet-stream` if not explicitly set.
-## js-python-fetch-option-ignorehttpserrors
+## js-python-csharp-fetch-option-ignorehttpserrors
* langs: js, python, csharp
- `ignoreHTTPSErrors` <[boolean]>
Whether to ignore HTTPS errors when sending network requests. Defaults to `false`.
-## js-python-fetch-option-maxredirects
-* langs: js
+## js-python-csharp-fetch-option-maxredirects
+* langs: js, python, csharp
- `maxRedirects` <[int]>
-Maximum number of request allowed redirects. Defaults to `20`. Pass `0` to disable automatic follow.
+Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded.
+Defaults to `20`. Pass `0` to not follow redirects.
## evaluate-expression
- `expression` <[string]>
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 6b76347d5e..06013c0769 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -12668,7 +12668,8 @@ export interface APIRequestContext {
ignoreHTTPSErrors?: boolean;
/**
- * Maximum number of request allowed redirects. Defaults to `20`. Pass `0` to disable automatic follow.
+ * Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is
+ * exceeded. Defaults to `20`. Pass `0` to not follow redirects.
*/
maxRedirects?: number;
@@ -12753,7 +12754,8 @@ export interface APIRequestContext {
ignoreHTTPSErrors?: boolean;
/**
- * Maximum number of request allowed redirects. Defaults to `20`. Pass `0` to disable automatic follow.
+ * Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is
+ * exceeded. Defaults to `20`. Pass `0` to not follow redirects.
*/
maxRedirects?: number;
@@ -12821,7 +12823,8 @@ export interface APIRequestContext {
ignoreHTTPSErrors?: boolean;
/**
- * Maximum number of request allowed redirects. Defaults to `20`. Pass `0` to disable automatic follow.
+ * Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is
+ * exceeded. Defaults to `20`. Pass `0` to not follow redirects.
*/
maxRedirects?: number;
@@ -12860,7 +12863,8 @@ export interface APIRequestContext {
ignoreHTTPSErrors?: boolean;
/**
- * Maximum number of request allowed redirects. Defaults to `20`. Pass `0` to disable automatic follow.
+ * Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is
+ * exceeded. Defaults to `20`. Pass `0` to not follow redirects.
*/
maxRedirects?: number;
@@ -12913,7 +12917,8 @@ export interface APIRequestContext {
ignoreHTTPSErrors?: boolean;
/**
- * Maximum number of request allowed redirects. Defaults to `20`. Pass `0` to disable automatic follow.
+ * Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is
+ * exceeded. Defaults to `20`. Pass `0` to not follow redirects.
*/
maxRedirects?: number;
@@ -12989,7 +12994,8 @@ export interface APIRequestContext {
ignoreHTTPSErrors?: boolean;
/**
- * Maximum number of request allowed redirects. Defaults to `20`. Pass `0` to disable automatic follow.
+ * Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is
+ * exceeded. Defaults to `20`. Pass `0` to not follow redirects.
*/
maxRedirects?: number;
@@ -13065,7 +13071,8 @@ export interface APIRequestContext {
ignoreHTTPSErrors?: boolean;
/**
- * Maximum number of request allowed redirects. Defaults to `20`. Pass `0` to disable automatic follow.
+ * Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is
+ * exceeded. Defaults to `20`. Pass `0` to not follow redirects.
*/
maxRedirects?: number;
diff --git a/tests/library/global-fetch.spec.ts b/tests/library/global-fetch.spec.ts
index 6f93084a7d..6e20f171ae 100644
--- a/tests/library/global-fetch.spec.ts
+++ b/tests/library/global-fetch.spec.ts
@@ -391,8 +391,11 @@ it('should throw an error when maxRedirects is exceeded', async ({ playwright, s
server.setRedirect('/b/c/redirect4', '/simple.json');
const request = await playwright.request.newContext();
- for (const method of ['GET', 'PUT', 'POST', 'OPTIONS', 'HEAD', 'PATCH'])
- for (const maxRedirects of [1, 2, 3]) await expect(async () => request.fetch(`${server.PREFIX}/a/redirect1`, { method: method, maxRedirects: maxRedirects })).rejects.toThrow('Max redirect count exceeded');
+ for (const method of ['GET', 'PUT', 'POST', 'OPTIONS', 'HEAD', 'PATCH']) {
+ for (const maxRedirects of [1, 2, 3])
+ await expect(async () => request.fetch(`${server.PREFIX}/a/redirect1`, { method: method, maxRedirects: maxRedirects })).rejects.toThrow('Max redirect count exceeded');
+ }
+ await request.dispose();
});
it('should not follow redirects when maxRedirects is set to 0', async ({ playwright, server }) => {
@@ -401,10 +404,11 @@ it('should not follow redirects when maxRedirects is set to 0', async ({ playwri
const request = await playwright.request.newContext();
for (const method of ['GET', 'PUT', 'POST', 'OPTIONS', 'HEAD', 'PATCH']){
- const response = await request.fetch(`${server.PREFIX}/a/redirect1`, { method: method, maxRedirects: 0 });
+ const response = await request.fetch(`${server.PREFIX}/a/redirect1`, { method, maxRedirects: 0 });
expect(response.headers()['location']).toBe('/b/c/redirect2');
expect(response.status()).toBe(302);
}
+ await request.dispose();
});
it('should throw an error when maxRedirects is less than 0', async ({ playwright, server }) => {
@@ -412,5 +416,7 @@ it('should throw an error when maxRedirects is less than 0', async ({ playwright
server.setRedirect('/b/c/redirect2', '/simple.json');
const request = await playwright.request.newContext();
- for (const method of ['GET', 'PUT', 'POST', 'OPTIONS', 'HEAD', 'PATCH']) await expect(async () => request.fetch(`${server.PREFIX}/a/redirect1`, { method: method, maxRedirects: -1 })).rejects.toThrow(`'maxRedirects' should be greater than or equal to '0'`);
+ for (const method of ['GET', 'PUT', 'POST', 'OPTIONS', 'HEAD', 'PATCH'])
+ await expect(async () => request.fetch(`${server.PREFIX}/a/redirect1`, { method, maxRedirects: -1 })).rejects.toThrow(`'maxRedirects' should be greater than or equal to '0'`);
+ await request.dispose();
});
\ No newline at end of file