microsoft / playwright-java

Java version of the Playwright testing and automation library
https://playwright.dev/java/
Apache License 2.0
1.13k stars 205 forks source link

[Feature]: Allow to configure connection reuse policy. #1597

Closed LucaLis closed 3 months ago

LucaLis commented 3 months ago

🚀 Feature Request

Hello!

This may be more of a question rather than a feature request. I have failed to find the answers on discord so maybe here I will find someone who can advise how to deal with the issue I'm facing... I'm using APIRequestContext to setup some test data for the E2E tests and/or do some API integration testing. I have noticed that every now and then we are getting a 'socket hangup' error - see attached screenshot.

Screenshot 2024-06-12 at 14 40 44

My understanding is that request context is trying to reuse connection that is already closed. Keep alive is enabled on the host but the host won't keep the connection open forever and afaik it will close it after specific numer of requests or specific period of time. Currently I'm just detecting this kind of error and simply retrying the request using the same request context object and it works in 90+% of cases... but this makes me wonder how Playwright handles connections... If connection is closed then the above would sugest that Playwright is reestablishing connection under the hood? I know that described problem can be eliminated by recreating a fresh request context but this is 'a bit' annoying and does not seem to be the way how it should be done... In addition - we were facing similar kind of problems in jMeter tests but in this case jMeter allows to adjust the http client configuration via parameters like httpclient4.idletimeout or httpclient4.validate_after_inactivity and this solves this problem... I can't find anything in the docs that explains this aspect of Playwright so I would be really grateful for your help and sugestions.

Example

No response

Motivation

It will allow to make better use of Playwright in tasks that are related purely with API calls.

yury-s commented 3 months ago

If connection is closed then the above would sugest that Playwright is reestablishing connection under the hood?

Playwright doesn't reuse a connection between requests. Every request will establish its own connection to the server, it might be that your server start throttling your client and closes the connection. If you think this is a bug in Playwright, please share a reproduction example which we could run locally and see the problem.