Skip to content

Commit

Permalink
Merge pull request #15 from scivisum/add_specialised_methods
Browse files Browse the repository at this point in the history
Added some specialised functions.
  • Loading branch information
earendil authored Jul 16, 2019
2 parents 4f63ed1 + a18799e commit ad08ab8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# browser-debugger-tools
[![Build Status](https://img.shields.io/travis/scivisum/browser-debugger-tools/master.svg?style=flat-square)](https://travis-ci.org/scivisum/browser-debugger-tools)
[![PyPI](https://img.shields.io/pypi/v/browserdebuggertools.svg?style=flat-square)](https://pypi.python.org/pypi/browserdebuggertools)
![Python](https://img.shields.io/pypi/pyversions/browserdebuggertools.svg?style=flat-square)
![License](https://img.shields.io/pypi/l/browserdebuggertools.svg?style=flat-square)
## Overview
The purpose is to provide a python client to connect to the debugger tools of a web-browser.
Expand Down
64 changes: 60 additions & 4 deletions browserdebuggertools/chrome/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from base64 import b64decode

from browserdebuggertools.sockethandler import SocketHandler
from browserdebuggertools.exceptions import DevToolsTimeoutException, ResultNotFoundError, \
DomainNotFoundError
from browserdebuggertools.exceptions import (
DevToolsTimeoutException, ResultNotFoundError, DomainNotFoundError
)

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

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

def stop_page_load(self):
return self.execute("Page", "stopLoading")

def execute_javascript(self, script):
result = self.execute("Runtime", "evaluate", {
"expression": script,
"returnByValue": True
})["result"]

return result.get("value")

def get_url(self):
return self.execute_javascript("document.URL")

def get_document_readystate(self):
""" Gets the document.readyState of the page.
"""
response = self.execute("Runtime", "evaluate", {"expression": "document.readyState"})
return response["result"]["value"]
return self.execute_javascript("document.readyState")

def get_page_source(self):
""" Returns a string serialization of the active document's DOM
"""
return self.execute_javascript("document.documentElement.innerHTML")

def set_user_agent_override(self, user_agent):
""" Overriding user agent with the given string.
:param user_agent:
:return:
"""
return self.execute("Network", "setUserAgentOverride", {
"userAgent": user_agent
})

def emulate_network_conditions(
self, offline=False, latency=-1, download_throughput=-1, upload_throughput=-1,
connection_type=None
):
"""
:param offline: Whether to emulate network disconnection
:param latency: Minimum latency from request sent to response headers (ms).
:param download_throughput: Maximal aggregated download throughput (bytes/sec).
-1 disables download throttling.
:param upload_throughput: Maximal aggregated upload throughput (bytes/sec).
-1 disables upload throttling.
:param connection_type: The underlying connection technology
that the browser is supposedly using
example values: "cellular2g", "cellular3g", "cellular4g",
"bluetooth", "ethernet", "wifi", "wimax"
"""
network_conditions = {
"offline": offline,
"latency": latency,
"downloadThroughput": download_throughput,
"uploadThroughput": upload_throughput,
}
if connection_type:
network_conditions.update({"connectionType": connection_type})

return self.execute("Network", "emulateNetworkConditions", network_conditions)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="browserdebuggertools",
version="2.0.0",
version="2.1.0",
packages=PACKAGES,
install_requires=requires,
license="GNU General Public License v3",
Expand Down
14 changes: 13 additions & 1 deletion tests/unittests/chrome/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,19 @@ def test_timed_out(self, time):
class Test_ChromeInterface_enable_domain(ChromeInterfaceTest):

def test_invalid_domain(self):
self.interface.execute.return_value = {"id": 1, "error": MagicMock}
self.interface.execute.return_value = {"id": 1, "error": MagicMock()}

with self.assertRaises(DomainNotFoundError):
self.interface.enable_domain("InvalidDomain")


@patch(MODULE_PATH + "ChromeInterface.execute", MagicMock())
class Test_ChromeInterface_execute_javascript(ChromeInterfaceTest):

def test(self):
mock_result = MagicMock()
self.interface.execute.return_value = {"id": 1, "result": {"value": mock_result}}

result = self.interface.execute_javascript("document.readyState")

self.assertEqual(mock_result, result)

0 comments on commit ad08ab8

Please sign in to comment.