| |
- builtins.object
-
- ChromeExecutionManager
class ChromeExecutionManager(builtins.object) |
|
ChromeExecutionManager(binary, base_tab_key, host='localhost', port=None, websocket_timeout=10, enable_gpu=False, headless=True, xvfb=False, additional_options=[])
Class for managing talking to a chromium instance, as well as
managing the chromium instance's execution lifetime. |
|
Methods defined here:
- __del__(self)
- __init__(self, binary, base_tab_key, host='localhost', port=None, websocket_timeout=10, enable_gpu=False, headless=True, xvfb=False, additional_options=[])
- Start up a chromium instance binary `binary`, with the base debug port `port`,
on host `localhost`.
Note: If the port is set to None (the default), the port will be the next availble port accounting for other
chrome instances in the current python process.
If port is not none, and the port is already in use, you will get a ReusedPortError.
base_tab_key is any hashable python object that is used for multi-tab interfacing.
- __repr__(self)
- asynchronous_command(self, command, tab_key, **params)
- Asynchronously send command `command` with params `params` in the
remote chrome instance, returning the send_id for the sent command.
- close_all(self)
- close_chromium(self)
- Close the remote chromium instance.
This command is normally executed as part of the class destructor.
It can be called early without issue, but calling ANY class functions
after the remote chromium instance is shut down will have unknown effects.
Note that if you are rapidly creating and destroying ChromeController instances,
you may need to *explicitly* call this before destruction.
- close_tab(self, tab_key)
- close_websockets(self)
- Close websocket connection to remote browser.
- connect_to_chromium(self, tab_key)
- Open a websocket connection to remote browser, determined by
self.host and self.port. Each tab has it's own websocket
endpoint.
Note that the manager maintains a connection to a blank tab to
prevent closing all remote tabs from causing the browser to exit.
This tab is not used by anything.
- drain(self, tab_key)
- Return all messages in waiting for the websocket connection.
- fetch_tablist(self)
- Connect to host:port and request list of tabs
return list of dicts of data about open tabs.
- flush(self, tab_key)
- Flush the pending RX buffer for a specific tab key.
- install_message_handler_for_tab_key(self, tab_key, handler)
- Add handler `handler` to the list of message handlers that will be called
on all received messages for the tab with a key of `tab_key`.
The handler will be called with the tab context for each message the tab generates.
NOTE: Handler call order is not specified!
- pprint_tablist(self)
- process_available(self, tab_key, timeout=0.1)
- Process all messages in the socket rx queue.
Will block for at least 100 milliseconds.
- recv(self, tab_key, message_id=None, timeout=30)
- Recieve a message, optionally filtering for a specified message id.
If `message_id` is none, the first command in the receive queue is returned.
If `message_id` is not none, the command waits untill a message is received with
the specified id, or it times out.
Timeout is the number of seconds to wait for a response, or `None` if the timeout
has expired with no response.
- recv_all_filtered(self, keycheck, tab_key, timeout=0.5)
- Receive a all messages matching a filter, using the callable `keycheck` to filter received messages
for content.
This function will *ALWAY* block for at least `timeout` seconds.
If chromium is for some reason continuously streaming responses, it may block forever!
`keycheck` is expected to be a callable that takes a single parameter (the decoded response
from chromium), and returns a boolean (true, if the command is the one filtered for, or false
if the command is not the one filtered for).
```
def check_func(message):
if message_id is None:
return True
if "id" in message:
return message['id'] == message_id
return False
return self.recv_filtered(check_func, timeout)
```
Note that the function is defined dynamically, and `message_id` is captured via closure.
- recv_filtered(self, keycheck, tab_key, timeout=30, message=None)
- Receive a filtered message, using the callable `keycheck` to filter received messages
for content.
`keycheck` is expected to be a callable that takes a single parameter (the decoded response
from chromium), and returns a boolean (true, if the command is the one filtered for, or false
if the command is not the one filtered for).
This is used internally, for example, by `recv()`, to filter the response for a specific ID:
```
def check_func(message):
if message_id is None:
return True
if "id" in message:
return message['id'] == message_id
return False
return self.recv_filtered(check_func, timeout)
```
Note that the function is defined dynamically, and `message_id` is captured via closure.
- remove_all_handlers_for_tab_key(self, tab_key)
- Add handler `handler` to the list of message handlers that will be called on all received messages.
If the tab key is not present, no error will be raised.
- remove_handlers_for_tab_key(self, tab_key, handler)
- Remove handler `handler` from the list of message handlers that will be called
on all received messages for the tab with a key of `tab_key`.
If the tab_key is not present, or the handler is not present in the tab, a
KeyError will be raised.
This is potentially of little use, since the tab layer uses a dynamically
defined closure to capture the calling tab context, so attempting to remove
a defined funtion will fail since the function is no longer available.
- send(self, command, tab_key, params=None)
- Send command `command` with optional parameters `params` to the
remote chrome instance.
The command `id` is automatically added to the outgoing message.
return value is the command id, which can be used to match a command
to it's associated response.
- sync_tablist(self)
- Sync tablist with browser, adding any tabs created by the browser itself to the available tab listing.
This is mostly useful to maually call if you are trying to interact with things like extensions, that
internally change tab IDs.
This also gets called before each tab creation, because we need to be able to determine which
tab is actually newly created after interacting with the browser.
- synchronous_command(self, command, tab_key, **params)
- Synchronously execute command `command` with params `params` in the
remote chrome instance, returning the response from the chrome instance.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
| |