class TestEvents: def setup(self): self.cp = CProto() self.Page = self.cp.Page self.Page.enable() def teardown(self): self.cp.close() def test_events(self): def callback(_): self.events_fired += 1 self.events_fired = 0 self.Page.frameStartedLoading = callback self.Page.frameNavigated = callback self.Page.domContentEventFired = callback self.Page.loadEventFired = callback self.Page.frameStoppedLoading = callback self.Page.navigate(url='about:blank') sleep(0.3) assert self.events_fired == 5, 'Should receive all events'
class TestMethods: def setup(self): self.cp = CProto() def teardown(self): self.cp.close() def test_navigate_reply(self): assert self.cp.Page.navigate(url='about:blank')['id'] == 1, 'Should have initial request id' assert self.cp.Page.navigate(url='about:blank')['id'] == 2, 'Should have (initial + 1) request id'
def do_capture(): cp = CProto() payload = json.loads(cp.Page.captureScreenshot()) data = base64.b64decode(payload['result']['data']) with open('output.jpeg', 'wb') as f: f.write(data)
def test_websocket_connection(): cp = CProto() assert cp.ws.connected is True, 'Should be connected to CDP' cp.close() assert cp.ws.connected is False, 'Should be disconnected from CDP'
def setup(self): self.cp = CProto()
def setup(self): self.cp = CProto() self.Page = self.cp.Page self.Page.enable()
from cproto import CProto def capture_screen(_): payload = cp.Page.captureScreenshot() data = payload['result']['data'].decode('base64') with open('output.jpeg', 'wb') as f: f.write(data) cp.close() if __name__ == '__main__': cp = CProto() cp.Page.enable() cp.Page.loadEventFired = capture_screen cp.Page.navigate(url='https://github.com')