Karcel
Karcel

Reputation: 45

How to wait for Requests to finish using Selenium CDP

I want to use CDP (or any other way) in java to dynamically wait in Selenium until specific requests are fully served. Normally I am waiting using FluentWait for specific elements to be seen on page but these requests have no immidiate effect on the UI itself. Instead of that, they are being used when clicking in the UI. Problem is that often in automation selenium proceeds with clicking once it finds element (e.g. logo) and since these requests are not fully loaded then no data are being shown in it as a consequence of not finished request.

I wanted to use CDP and its addListener but dont know how to filter properly there on which requests to wait and to give it conditional wait for status e.g. 200. Any help on that? Thank you :)

Upvotes: -1

Views: 1197

Answers (1)

Robbie Wareham
Robbie Wareham

Reputation: 3448

I would avoid trying to use Selenium 4 methods to access the CDP as I have seen performance issues.

Instead I would suggesting using https://mvnrepository.com/artifact/com.github.kklisura.cdt/cdt-java-client.

You will initially need to get the CDP port by using getCapabilities of the instantiated WebDriver. There is a capability called "goog:chromeOptions" and within this there is a property called "debuggerAddress".

https://github.com/kklisura/chrome-devtools-java-client will explain how to now use this address to connect to the CDP.

With this, you should be able to use the Network domain (https://chromedevtools.github.io/devtools-protocol/tot/Network/) to listen to 3 events:

  • onRequestWillBeSent
  • onResponseReceived
  • onResponseFailed

Each of the events will provide a unique requestId and you can use that to create some kind of mechanism to track requests when they are made and when they are completed.

Such a mechanism could be a Set<String> that you could consider as "all requests finished" when that Set is empty.

All these events are "fire and forget" from the Chrome side, so shouldn't impact the perceived performance of your application under test.

How this is implemented would be dependent on your current framework and your AUT.

For example, onRequestWillBeSent provides access to the URL, so you could filter based on that and ignore requests you don't care about.

Also, dependent on your AUT, you may experience "hanging" requests if navigation occurs while requests are still in progress. To combat this, you can also subscribe to onFrameNavigated from the Page domain, and use this event to clear the Set

Upvotes: 1

Related Questions