Skip to content

Commit

Permalink
splash:click() tests + sample (failing) implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelmhm committed Apr 5, 2016
1 parent 2796a4d commit 3dbf1e4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
29 changes: 28 additions & 1 deletion splash/browser_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import weakref
import uuid

from PyQt5.QtCore import QObject, QSize, Qt, QTimer, pyqtSlot
from PyQt5.QtCore import QObject, QSize, Qt, QTimer, pyqtSlot, QEvent, QPointF
from PyQt5.QtGui import QMouseEvent
from PyQt5.QtNetwork import QNetworkRequest
from PyQt5.QtWebKitWidgets import QWebPage
from PyQt5.QtWebKit import QWebSettings
Expand All @@ -32,6 +33,7 @@
)



def skip_if_closing(meth):
@functools.wraps(meth)
def wrapped(self, *args, **kwargs):
Expand Down Expand Up @@ -833,6 +835,31 @@ def _frame_to_dict(self, frame, children=True, html=True):

return res

def click(self, element):
from splash.qtutils import _qtapp
from PyQt5.QtCore import Qt
frame = self.web_page.mainFrame()
# TODO make this new method
element = frame.findFirstElement(element)
# TODO error handling
assert(not element.isNull())
x, y = element.geometry().x(), element.geometry().y()
event_type = QEvent.MouseButtonPress
point = QPointF()
point.setX(x)
point.setY(y)
buttons = _qtapp.mouseButtons()
modifiers = _qtapp.keyboardModifiers()
# there are couple of signatures for QMouseEvent, this one adds more info about
# relative event position
event = QMouseEvent(event_type, point, point, point, Qt.LeftButton, buttons, modifiers)

view = self.web_page.view()
# TODO this posts event but doesn't update html, why?
_qtapp.postEvent(view, event)




class _SplashHttpClient(QObject):
""" Wrapper class for making HTTP requests on behalf of a SplashQWebPage """
Expand Down
4 changes: 4 additions & 0 deletions splash/qtrender_lua.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,10 @@ def callback(reply):
def autoload_reset(self):
self.tab.autoload_reset()

@command()
def click(self, element):
self.tab.click(element)

@command(async=True)
def set_content(self, data, mime_type=None, baseurl=None):
if isinstance(data, six.text_type):
Expand Down
6 changes: 6 additions & 0 deletions splash/qwebpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def closeEvent(self, event):
else:
event.accept()

def mousePressEvent(self, e):
print("*" * 99)
print("\n\n\n mouse is pressed \n\n")
print("*" * 99)



class SplashQWebPage(QWebPage):
"""
Expand Down
29 changes: 29 additions & 0 deletions splash/tests/mockserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,34 @@ def render_GET(self, request):
""" % next_url).encode('utf-8')


class JsClickResource(Resource):
isLeaf = True

def render_GET(self, request):
js_code = """
function modify_h1() {
console.log("modifying h1");
var h1 = document.getElementById("h1")
h1.remove()
}
var element = document.getElementById("button");
document.addEventListener("click", modify_h1, false);
"""
html_with_js = """
<html>
<head></head>
<body>
<h1 id="h1"> this must be removed after click</h1>
<button id="button">press this</button>
<script>
%s
</script>
</body>
</html>
""" % js_code
return html_with_js.encode("utf8")


class CP1251Resource(Resource):

@use_chunked_encoding
Expand Down Expand Up @@ -821,6 +849,7 @@ def __init__(self, http_port, https_port, proxy_port):
self.putChild(b"echourl", EchoUrl())
self.putChild(b"bad-content-type", InvalidContentTypeResource())
self.putChild(b"bad-content-type2", InvalidContentTypeResource2())
self.putChild(b"jsclick", JsClickResource())

self.putChild(b"jsredirect", JsRedirect())
self.putChild(b"jsredirect-to", JsRedirectTo())
Expand Down
20 changes: 20 additions & 0 deletions splash/tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3456,3 +3456,23 @@ def test_enable_and_disable_private_mode(self):
data = resp.json()
self.assertIn(u'world of splash', data["html1"])
self.assertNotIn(u"world of splash", data["html2"])


class MouseEventsTest(BaseLuaRenderTest):
def test_click(self):
resp = self.request_lua("""
function main(splash)
assert(splash:go(splash.args.url))
-- make sure we deal with page that hides elem on click
splash:evaljs("document.getElementById('button').click()")
html = splash:html()
assert(string.find(html, 'must be removed') == nil)
assert(splash:go(splash.args.url))
splash:click('button')
splash:wait(0.5)
return splash:html()
end
""", {"url": self.mockurl("jsclick")})
self.assertStatusCode(resp, 200)
self.assertNotIn('this must be removed after click', resp.text)

0 comments on commit 3dbf1e4

Please sign in to comment.