Skip to content

Commit ad08ab8

Browse files
authored
Merge pull request #15 from scivisum/add_specialised_methods
Added some specialised functions.
2 parents 4f63ed1 + a18799e commit ad08ab8

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# browser-debugger-tools
22
[![Build Status](https://img.shields.io/travis/scivisum/browser-debugger-tools/master.svg?style=flat-square)](https://travis-ci.org/scivisum/browser-debugger-tools)
33
[![PyPI](https://img.shields.io/pypi/v/browserdebuggertools.svg?style=flat-square)](https://pypi.python.org/pypi/browserdebuggertools)
4+
![Python](https://img.shields.io/pypi/pyversions/browserdebuggertools.svg?style=flat-square)
45
![License](https://img.shields.io/pypi/l/browserdebuggertools.svg?style=flat-square)
56
## Overview
67
The purpose is to provide a python client to connect to the debugger tools of a web-browser.

browserdebuggertools/chrome/interface.py

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from base64 import b64decode
55

66
from browserdebuggertools.sockethandler import SocketHandler
7-
from browserdebuggertools.exceptions import DevToolsTimeoutException, ResultNotFoundError, \
8-
DomainNotFoundError
7+
from browserdebuggertools.exceptions import (
8+
DevToolsTimeoutException, ResultNotFoundError, DomainNotFoundError
9+
)
910

1011
logging.basicConfig(format='%(levelname)s:%(message)s')
1112

@@ -160,8 +161,63 @@ def take_screenshot(self, filepath):
160161
with open(filepath, "wb") as f:
161162
f.write(b64decode(image_data))
162163

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+
163178
def get_document_readystate(self):
164179
""" Gets the document.readyState of the page.
165180
"""
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)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setup(
1313
name="browserdebuggertools",
14-
version="2.0.0",
14+
version="2.1.0",
1515
packages=PACKAGES,
1616
install_requires=requires,
1717
license="GNU General Public License v3",

tests/unittests/chrome/test_interface.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,19 @@ def test_timed_out(self, time):
5757
class Test_ChromeInterface_enable_domain(ChromeInterfaceTest):
5858

5959
def test_invalid_domain(self):
60-
self.interface.execute.return_value = {"id": 1, "error": MagicMock}
60+
self.interface.execute.return_value = {"id": 1, "error": MagicMock()}
6161

6262
with self.assertRaises(DomainNotFoundError):
6363
self.interface.enable_domain("InvalidDomain")
64+
65+
66+
@patch(MODULE_PATH + "ChromeInterface.execute", MagicMock())
67+
class Test_ChromeInterface_execute_javascript(ChromeInterfaceTest):
68+
69+
def test(self):
70+
mock_result = MagicMock()
71+
self.interface.execute.return_value = {"id": 1, "result": {"value": mock_result}}
72+
73+
result = self.interface.execute_javascript("document.readyState")
74+
75+
self.assertEqual(mock_result, result)

0 commit comments

Comments
 (0)