HMaker / python-cdp

Python client and types generator for the Chrome DevTools Protocol (CDP)
MIT License
55 stars 18 forks source link

Add to_json method to CDP events #9

Closed RazorBest closed 3 months ago

RazorBest commented 10 months ago

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

HMaker commented 9 months ago

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.

RazorBest commented 9 months ago

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.

HMaker commented 9 months ago

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.