|
4 | 4 | from base64 import b64decode |
5 | 5 |
|
6 | 6 | from browserdebuggertools.sockethandler import SocketHandler |
7 | | -from browserdebuggertools.exceptions import DevToolsTimeoutException, ResultNotFoundError, \ |
8 | | - DomainNotFoundError |
| 7 | +from browserdebuggertools.exceptions import ( |
| 8 | + DevToolsTimeoutException, ResultNotFoundError, DomainNotFoundError |
| 9 | +) |
9 | 10 |
|
10 | 11 | logging.basicConfig(format='%(levelname)s:%(message)s') |
11 | 12 |
|
@@ -160,8 +161,63 @@ def take_screenshot(self, filepath): |
160 | 161 | with open(filepath, "wb") as f: |
161 | 162 | f.write(b64decode(image_data)) |
162 | 163 |
|
| 164 | + def stop_page_load(self): |
| 165 | + return self.execute("Page", "stopLoading") |
| 166 | + |
| 167 | + def execute_javascript(self, script): |
| 168 | + result = self.execute("Runtime", "evaluate", { |
| 169 | + "expression": script, |
| 170 | + "returnByValue": True |
| 171 | + })["result"] |
| 172 | + |
| 173 | + return result.get("value") |
| 174 | + |
| 175 | + def get_url(self): |
| 176 | + return self.execute_javascript("document.URL") |
| 177 | + |
163 | 178 | def get_document_readystate(self): |
164 | 179 | """ Gets the document.readyState of the page. |
165 | 180 | """ |
166 | | - response = self.execute("Runtime", "evaluate", {"expression": "document.readyState"}) |
167 | | - return response["result"]["value"] |
| 181 | + return self.execute_javascript("document.readyState") |
| 182 | + |
| 183 | + def get_page_source(self): |
| 184 | + """ Returns a string serialization of the active document's DOM |
| 185 | + """ |
| 186 | + return self.execute_javascript("document.documentElement.innerHTML") |
| 187 | + |
| 188 | + def set_user_agent_override(self, user_agent): |
| 189 | + """ Overriding user agent with the given string. |
| 190 | + :param user_agent: |
| 191 | + :return: |
| 192 | + """ |
| 193 | + return self.execute("Network", "setUserAgentOverride", { |
| 194 | + "userAgent": user_agent |
| 195 | + }) |
| 196 | + |
| 197 | + def emulate_network_conditions( |
| 198 | + self, offline=False, latency=-1, download_throughput=-1, upload_throughput=-1, |
| 199 | + connection_type=None |
| 200 | + ): |
| 201 | + """ |
| 202 | +
|
| 203 | + :param offline: Whether to emulate network disconnection |
| 204 | + :param latency: Minimum latency from request sent to response headers (ms). |
| 205 | + :param download_throughput: Maximal aggregated download throughput (bytes/sec). |
| 206 | + -1 disables download throttling. |
| 207 | + :param upload_throughput: Maximal aggregated upload throughput (bytes/sec). |
| 208 | + -1 disables upload throttling. |
| 209 | + :param connection_type: The underlying connection technology |
| 210 | + that the browser is supposedly using |
| 211 | + example values: "cellular2g", "cellular3g", "cellular4g", |
| 212 | + "bluetooth", "ethernet", "wifi", "wimax" |
| 213 | + """ |
| 214 | + network_conditions = { |
| 215 | + "offline": offline, |
| 216 | + "latency": latency, |
| 217 | + "downloadThroughput": download_throughput, |
| 218 | + "uploadThroughput": upload_throughput, |
| 219 | + } |
| 220 | + if connection_type: |
| 221 | + network_conditions.update({"connectionType": connection_type}) |
| 222 | + |
| 223 | + return self.execute("Network", "emulateNetworkConditions", network_conditions) |
0 commit comments