python-cdp icon indicating copy to clipboard operation
python-cdp copied to clipboard

Add to_json method to CDP events

Open RazorBest opened this issue 2 years ago • 3 comments

Hi,

All the python classes have the from_json method. The ones that represent CDP data types also have a to_json method, but the events don't have. I would like a complete coverage of json conversion for the python objects. I think the generation script can be modified to achieve this requirement.

At the moment, I don't see this to be a difficult task. I am happy to make the contribution myself.

GG for the project

RazorBest avatar Dec 30 '23 22:12 RazorBest

What would be the use case for that?

The CDP command types have the to_json() method to serialize it to be sent to the chrome browser (on-wire format). The JSON structure matches the defined ones by the CDP protocol specification.

The CDP events are just received by the client, they are never sent to the chrome browser (server). That's why there is no to_json() implemented for them.

HMaker avatar Jan 22 '24 21:01 HMaker

I understand that, but my use case is a little different. I have a program that uses python-cdp to listen to CDP events, and I want to add functional tests. So, I chose my tests to have as input, CDP events in JSON format, as close as possible to what chrome would give. I don't know a way to access the JSON objects directly, so I chose the following solution: get the CDP events converted to Python objects, as python-cdp does, then convert them back to JSON using the to_json methods.

RazorBest avatar Jan 23 '24 14:01 RazorBest

You want to inject fake CDP events and test your listener behavior? Can't you just inject plain JSON objects and let pycdp parse it?

Instead you can make the function which handles CDP events take them as argument, the Python wrapper types. Then you can call it directly from the test.

Anyway feel free to make a PR, I will review it. Make the to_json() follow the same syntax and semantic of CDP.

HMaker avatar Jan 24 '24 22:01 HMaker