2019-06-05 22:57:55 +03:00
|
|
|
# Copyright 2017-2019 Daniel Silverstone <dsilvers@digital-scurf.org>
|
2017-06-10 14:23:55 +03:00
|
|
|
#
|
|
|
|
# This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
#
|
|
|
|
# NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; version 2 of the License.
|
|
|
|
#
|
|
|
|
# NetSurf is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Monkey Farmer
|
|
|
|
|
|
|
|
The monkey farmer is a wrapper around `nsmonkey` which can be used to simplify
|
|
|
|
access to the monkey behaviours and ultimately to write useful tests in an
|
|
|
|
expressive but not overcomplicated DSLish way. Tests are, ultimately, still
|
|
|
|
Python code.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
# pylint: disable=locally-disabled, missing-docstring
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
import asyncore
|
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import subprocess
|
|
|
|
import time
|
2019-07-06 12:08:10 +03:00
|
|
|
import errno
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
class MonkeyFarmer(asyncore.dispatcher):
|
2019-07-05 00:44:58 +03:00
|
|
|
|
|
|
|
# pylint: disable=locally-disabled, too-many-instance-attributes
|
|
|
|
|
2019-06-15 22:01:08 +03:00
|
|
|
def __init__(self, monkey_cmd, online, quiet=False, *, wrapper=None):
|
2017-06-10 14:23:55 +03:00
|
|
|
(mine, monkeys) = socket.socketpair()
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
asyncore.dispatcher.__init__(self, sock=mine)
|
|
|
|
|
2019-06-15 22:01:08 +03:00
|
|
|
if wrapper is not None:
|
|
|
|
new_cmd = list(wrapper)
|
|
|
|
new_cmd.extend(monkey_cmd)
|
|
|
|
monkey_cmd = new_cmd
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
self.monkey = subprocess.Popen(
|
|
|
|
monkey_cmd,
|
|
|
|
stdin=monkeys,
|
|
|
|
stdout=monkeys,
|
|
|
|
close_fds=[mine])
|
|
|
|
|
|
|
|
monkeys.close()
|
|
|
|
|
2018-11-03 14:42:44 +03:00
|
|
|
self.buffer = b""
|
|
|
|
self.incoming = b""
|
2017-06-10 14:23:55 +03:00
|
|
|
self.lines = []
|
|
|
|
self.scheduled = []
|
|
|
|
self.deadmonkey = False
|
|
|
|
self.online = online
|
|
|
|
self.quiet = quiet
|
2018-11-03 18:28:23 +03:00
|
|
|
self.discussion = []
|
2019-06-15 22:01:08 +03:00
|
|
|
self.maybe_slower = wrapper is not None
|
2017-06-10 14:23:55 +03:00
|
|
|
|
|
|
|
def handle_connect(self):
|
|
|
|
pass
|
2019-06-06 17:07:52 +03:00
|
|
|
|
|
|
|
def handle_close(self):
|
|
|
|
# the pipe to the monkey process has closed
|
|
|
|
self.close()
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def handle_read(self):
|
2019-07-06 12:08:10 +03:00
|
|
|
try:
|
|
|
|
got = self.recv(8192)
|
|
|
|
if not got:
|
|
|
|
self.deadmonkey = True
|
|
|
|
# ensure the child process is finished and report the exit
|
|
|
|
if self.monkey.poll() is None:
|
|
|
|
self.monkey.terminate()
|
|
|
|
self.monkey.wait()
|
2019-08-02 19:40:15 +03:00
|
|
|
print("Handling an exit {}".format(self.monkey.returncode))
|
|
|
|
print("The following are present in the queue: {}".format(self.lines))
|
2019-08-03 14:31:22 +03:00
|
|
|
self.lines.append("GENERIC EXIT {}".format(
|
2019-07-06 12:08:10 +03:00
|
|
|
self.monkey.returncode).encode('utf-8'))
|
2019-08-02 19:40:15 +03:00
|
|
|
print("The queue is now: {}".format(self.lines))
|
2019-07-06 12:08:10 +03:00
|
|
|
return
|
|
|
|
except socket.error as error:
|
|
|
|
if error.errno == errno.EAGAIN or error.errno == errno.EWOULDBLOCK:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
self.incoming += got
|
2018-11-03 14:42:44 +03:00
|
|
|
if b"\n" in self.incoming:
|
|
|
|
lines = self.incoming.split(b"\n")
|
2017-06-10 14:23:55 +03:00
|
|
|
self.incoming = lines.pop()
|
2019-08-03 14:31:22 +03:00
|
|
|
self.lines.extend(lines)
|
2017-06-10 14:23:55 +03:00
|
|
|
|
|
|
|
def writable(self):
|
2019-07-05 00:44:58 +03:00
|
|
|
return len(self.buffer) > 0
|
2017-06-10 14:23:55 +03:00
|
|
|
|
|
|
|
def handle_write(self):
|
|
|
|
sent = self.send(self.buffer)
|
|
|
|
self.buffer = self.buffer[sent:]
|
|
|
|
|
|
|
|
def tell_monkey(self, *args):
|
|
|
|
cmd = (" ".join(args))
|
|
|
|
if not self.quiet:
|
2018-11-03 14:42:44 +03:00
|
|
|
print(">>> {}".format(cmd))
|
2019-07-05 00:44:58 +03:00
|
|
|
self.discussion.append((">", cmd))
|
2018-11-03 14:42:44 +03:00
|
|
|
cmd = cmd + "\n"
|
|
|
|
self.buffer += cmd.encode('utf-8')
|
2017-06-10 14:23:55 +03:00
|
|
|
|
|
|
|
def monkey_says(self, line):
|
2018-11-03 14:42:44 +03:00
|
|
|
line = line.decode('utf-8')
|
2017-06-10 14:23:55 +03:00
|
|
|
if not self.quiet:
|
2018-11-03 14:42:44 +03:00
|
|
|
print("<<< {}".format(line))
|
2018-11-03 18:28:23 +03:00
|
|
|
self.discussion.append(("<", line))
|
2017-06-10 14:23:55 +03:00
|
|
|
self.online(line)
|
|
|
|
|
|
|
|
def schedule_event(self, event, secs=None, when=None):
|
2019-07-05 00:44:58 +03:00
|
|
|
assert secs is not None or when is not None
|
2017-06-10 14:23:55 +03:00
|
|
|
if when is None:
|
|
|
|
when = time.time() + secs
|
|
|
|
self.scheduled.append((when, event))
|
2019-07-05 00:44:58 +03:00
|
|
|
self.scheduled.sort()
|
2017-06-10 14:23:55 +03:00
|
|
|
|
|
|
|
def unschedule_event(self, event):
|
|
|
|
self.scheduled = [x for x in self.scheduled if x[1] != event]
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def loop(self, once=False):
|
|
|
|
if len(self.lines) > 0:
|
|
|
|
self.monkey_says(self.lines.pop(0))
|
|
|
|
if once:
|
|
|
|
return
|
|
|
|
while not self.deadmonkey:
|
|
|
|
now = time.time()
|
|
|
|
while len(self.scheduled) > 0 and now >= self.scheduled[0][0]:
|
|
|
|
func = self.scheduled[0][1]
|
|
|
|
self.scheduled.pop(0)
|
|
|
|
func(self)
|
|
|
|
now = time.time()
|
|
|
|
if len(self.scheduled) > 0:
|
2019-07-05 00:44:58 +03:00
|
|
|
next_event = self.scheduled[0][0]
|
|
|
|
asyncore.loop(timeout=next_event - now, count=1)
|
2017-06-10 14:23:55 +03:00
|
|
|
else:
|
|
|
|
asyncore.loop(count=1)
|
|
|
|
if len(self.lines) > 0:
|
|
|
|
self.monkey_says(self.lines.pop(0))
|
|
|
|
if once:
|
|
|
|
break
|
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
class Browser:
|
2019-07-05 00:44:58 +03:00
|
|
|
|
|
|
|
# pylint: disable=locally-disabled, too-many-instance-attributes, dangerous-default-value, invalid-name
|
|
|
|
|
2019-06-15 22:01:08 +03:00
|
|
|
def __init__(self, monkey_cmd=["./nsmonkey"], quiet=False, *, wrapper=None):
|
2019-07-05 00:44:58 +03:00
|
|
|
self.farmer = MonkeyFarmer(
|
|
|
|
monkey_cmd=monkey_cmd,
|
|
|
|
online=self.on_monkey_line,
|
|
|
|
quiet=quiet,
|
|
|
|
wrapper=wrapper)
|
2017-06-10 14:23:55 +03:00
|
|
|
self.windows = {}
|
2018-11-04 13:35:22 +03:00
|
|
|
self.logins = {}
|
2019-06-05 22:57:55 +03:00
|
|
|
self.sslcerts = {}
|
2017-06-10 14:23:55 +03:00
|
|
|
self.current_draw_target = None
|
2018-11-03 18:28:23 +03:00
|
|
|
self.started = False
|
|
|
|
self.stopped = False
|
|
|
|
self.launchurl = None
|
2018-11-03 20:14:43 +03:00
|
|
|
now = time.time()
|
2019-06-15 22:01:08 +03:00
|
|
|
timeout = now + 1
|
|
|
|
|
|
|
|
if wrapper is not None:
|
|
|
|
timeout = now + 10
|
|
|
|
|
2018-11-03 18:28:23 +03:00
|
|
|
while not self.started:
|
|
|
|
self.farmer.loop(once=True)
|
2019-06-15 22:01:08 +03:00
|
|
|
if time.time() > timeout:
|
2018-11-03 20:14:43 +03:00
|
|
|
break
|
2017-06-10 14:23:55 +03:00
|
|
|
|
|
|
|
def pass_options(self, *opts):
|
|
|
|
if len(opts) > 0:
|
2019-07-02 01:27:45 +03:00
|
|
|
self.farmer.tell_monkey("OPTIONS " + (" ".join(['--' + opt for opt in opts])))
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def on_monkey_line(self, line):
|
|
|
|
parts = line.split(" ")
|
|
|
|
handler = getattr(self, "handle_" + parts[0], None)
|
|
|
|
if handler is not None:
|
|
|
|
handler(*parts[1:])
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
self.farmer.tell_monkey("QUIT")
|
|
|
|
|
|
|
|
def quit_and_wait(self):
|
|
|
|
self.quit()
|
|
|
|
self.farmer.loop()
|
2018-11-03 18:28:23 +03:00
|
|
|
return self.stopped
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def handle_GENERIC(self, what, *args):
|
2018-11-03 18:28:23 +03:00
|
|
|
if what == 'STARTED':
|
|
|
|
self.started = True
|
|
|
|
elif what == 'FINISHED':
|
|
|
|
self.stopped = True
|
|
|
|
elif what == 'LAUNCH':
|
|
|
|
self.launchurl = args[1]
|
2019-06-06 17:07:52 +03:00
|
|
|
elif what == 'EXIT':
|
|
|
|
if not self.stopped:
|
|
|
|
print("Unexpected exit of monkey process with code {}".format(args[0]))
|
2019-07-05 00:44:58 +03:00
|
|
|
assert self.stopped
|
2018-11-03 18:28:23 +03:00
|
|
|
else:
|
|
|
|
pass
|
2017-06-10 14:23:55 +03:00
|
|
|
|
|
|
|
def handle_WINDOW(self, action, _win, winid, *args):
|
|
|
|
if action == "NEW":
|
|
|
|
new_win = BrowserWindow(self, winid, *args)
|
|
|
|
self.windows[winid] = new_win
|
|
|
|
else:
|
|
|
|
win = self.windows.get(winid, None)
|
|
|
|
if win is None:
|
2018-11-03 14:42:44 +03:00
|
|
|
print(" Unknown window id {}".format(winid))
|
2017-06-10 14:23:55 +03:00
|
|
|
else:
|
|
|
|
win.handle(action, *args)
|
|
|
|
|
2018-11-04 13:35:22 +03:00
|
|
|
def handle_LOGIN(self, action, _lwin, winid, *args):
|
|
|
|
if action == "OPEN":
|
|
|
|
new_win = LoginWindow(self, winid, *args)
|
|
|
|
self.logins[winid] = new_win
|
|
|
|
else:
|
|
|
|
win = self.logins.get(winid, None)
|
|
|
|
if win is None:
|
|
|
|
print(" Unknown login window id {}".format(winid))
|
|
|
|
else:
|
|
|
|
win.handle(action, *args)
|
|
|
|
if win.alive and win.ready:
|
|
|
|
self.handle_ready_login(win)
|
|
|
|
|
2019-06-05 22:57:55 +03:00
|
|
|
def handle_SSLCERT(self, action, _lwin, winid, *args):
|
|
|
|
if action == "VERIFY":
|
|
|
|
new_win = SSLCertWindow(self, winid, *args)
|
|
|
|
self.sslcerts[winid] = new_win
|
|
|
|
self.handle_ready_sslcert(new_win)
|
|
|
|
else:
|
|
|
|
win = self.sslcerts.get(winid, None)
|
|
|
|
if win is None:
|
|
|
|
print(" Unknown ssl cert window id {}".format(winid))
|
|
|
|
else:
|
|
|
|
win.handle(action, *args)
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def handle_PLOT(self, *args):
|
|
|
|
if self.current_draw_target is not None:
|
|
|
|
self.current_draw_target.handle_plot(*args)
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def new_window(self, url=None):
|
|
|
|
if url is None:
|
|
|
|
self.farmer.tell_monkey("WINDOW NEW")
|
|
|
|
else:
|
|
|
|
self.farmer.tell_monkey("WINDOW NEW %s" % url)
|
|
|
|
wins_known = set(self.windows.keys())
|
|
|
|
while len(set(self.windows.keys()).difference(wins_known)) == 0:
|
|
|
|
self.farmer.loop(once=True)
|
|
|
|
poss_wins = set(self.windows.keys()).difference(wins_known)
|
|
|
|
return self.windows[poss_wins.pop()]
|
2018-11-04 13:35:22 +03:00
|
|
|
|
|
|
|
def handle_ready_login(self, lwin):
|
2019-07-05 00:44:58 +03:00
|
|
|
|
|
|
|
# pylint: disable=locally-disabled, no-self-use
|
|
|
|
|
2018-11-04 13:35:22 +03:00
|
|
|
# Override this method to do useful stuff
|
|
|
|
lwin.destroy()
|
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
def handle_ready_sslcert(self, cwin):
|
|
|
|
|
|
|
|
# pylint: disable=locally-disabled, no-self-use
|
|
|
|
|
|
|
|
# Override this method to do useful stuff
|
|
|
|
cwin.destroy()
|
|
|
|
|
|
|
|
|
2019-06-05 22:57:55 +03:00
|
|
|
class SSLCertWindow:
|
2019-07-05 00:44:58 +03:00
|
|
|
|
|
|
|
# pylint: disable=locally-disabled, invalid-name
|
|
|
|
|
2019-06-05 22:57:55 +03:00
|
|
|
def __init__(self, browser, winid, _url, *url):
|
|
|
|
self.alive = True
|
|
|
|
self.browser = browser
|
|
|
|
self.winid = winid
|
|
|
|
self.url = " ".join(url)
|
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
def handle(self, action, _str="STR"):
|
2019-06-05 22:57:55 +03:00
|
|
|
if action == "DESTROY":
|
|
|
|
self.alive = False
|
|
|
|
else:
|
|
|
|
raise AssertionError("Unknown action {} for sslcert window".format(action))
|
|
|
|
|
|
|
|
def _wait_dead(self):
|
|
|
|
while self.alive:
|
|
|
|
self.browser.farmer.loop(once=True)
|
|
|
|
|
|
|
|
def go(self):
|
2019-07-05 00:44:58 +03:00
|
|
|
assert self.alive
|
2019-06-05 22:57:55 +03:00
|
|
|
self.browser.farmer.tell_monkey("SSLCERT GO {}".format(self.winid))
|
|
|
|
self._wait_dead()
|
|
|
|
|
|
|
|
def destroy(self):
|
2019-07-05 00:44:58 +03:00
|
|
|
assert self.alive
|
2019-06-05 22:57:55 +03:00
|
|
|
self.browser.farmer.tell_monkey("SSLCERT DESTROY {}".format(self.winid))
|
|
|
|
self._wait_dead()
|
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2018-11-04 13:35:22 +03:00
|
|
|
class LoginWindow:
|
2019-07-05 00:44:58 +03:00
|
|
|
|
|
|
|
# pylint: disable=locally-disabled, too-many-instance-attributes, invalid-name
|
|
|
|
|
2018-11-04 13:35:22 +03:00
|
|
|
def __init__(self, browser, winid, _url, *url):
|
|
|
|
self.alive = True
|
|
|
|
self.ready = False
|
|
|
|
self.browser = browser
|
|
|
|
self.winid = winid
|
|
|
|
self.url = " ".join(url)
|
|
|
|
self.username = None
|
|
|
|
self.password = None
|
|
|
|
self.realm = None
|
|
|
|
|
|
|
|
def handle(self, action, _str="STR", *rest):
|
|
|
|
content = " ".join(rest)
|
|
|
|
if action == "USER":
|
|
|
|
self.username = content
|
|
|
|
elif action == "PASS":
|
|
|
|
self.password = content
|
|
|
|
elif action == "REALM":
|
|
|
|
self.realm = content
|
|
|
|
elif action == "DESTROY":
|
|
|
|
self.alive = False
|
|
|
|
else:
|
|
|
|
raise AssertionError("Unknown action {} for login window".format(action))
|
|
|
|
if not (self.username is None or self.password is None or self.realm is None):
|
|
|
|
self.ready = True
|
|
|
|
|
|
|
|
def send_username(self, username=None):
|
2019-07-05 00:44:58 +03:00
|
|
|
assert self.alive
|
2018-11-04 13:35:22 +03:00
|
|
|
if username is None:
|
|
|
|
username = self.username
|
|
|
|
self.browser.farmer.tell_monkey("LOGIN USERNAME {} {}".format(self.winid, username))
|
|
|
|
|
|
|
|
def send_password(self, password=None):
|
2019-07-05 00:44:58 +03:00
|
|
|
assert self.alive
|
2018-11-04 13:35:22 +03:00
|
|
|
if password is None:
|
|
|
|
password = self.password
|
|
|
|
self.browser.farmer.tell_monkey("LOGIN PASSWORD {} {}".format(self.winid, password))
|
|
|
|
|
|
|
|
def _wait_dead(self):
|
|
|
|
while self.alive:
|
|
|
|
self.browser.farmer.loop(once=True)
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2018-11-04 13:35:22 +03:00
|
|
|
def go(self):
|
2019-07-05 00:44:58 +03:00
|
|
|
assert self.alive
|
2018-11-04 13:35:22 +03:00
|
|
|
self.browser.farmer.tell_monkey("LOGIN GO {}".format(self.winid))
|
|
|
|
self._wait_dead()
|
|
|
|
|
|
|
|
def destroy(self):
|
2019-07-05 00:44:58 +03:00
|
|
|
assert self.alive
|
2018-11-04 13:35:22 +03:00
|
|
|
self.browser.farmer.tell_monkey("LOGIN DESTROY {}".format(self.winid))
|
|
|
|
self._wait_dead()
|
2019-07-05 00:44:58 +03:00
|
|
|
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
class BrowserWindow:
|
2019-07-05 00:44:58 +03:00
|
|
|
|
|
|
|
# pylint: disable=locally-disabled, too-many-instance-attributes, too-many-public-methods, invalid-name
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
browser,
|
|
|
|
winid,
|
|
|
|
_for,
|
|
|
|
coreid,
|
|
|
|
_existing,
|
|
|
|
otherid,
|
|
|
|
_newtab,
|
|
|
|
newtab,
|
|
|
|
_clone,
|
|
|
|
clone):
|
|
|
|
# pylint: disable=locally-disabled, too-many-arguments
|
2017-06-10 14:23:55 +03:00
|
|
|
self.alive = True
|
|
|
|
self.browser = browser
|
|
|
|
self.winid = winid
|
|
|
|
self.coreid = coreid
|
|
|
|
self.existing = browser.windows.get(otherid, None)
|
|
|
|
self.newtab = newtab == "TRUE"
|
|
|
|
self.clone = clone == "TRUE"
|
|
|
|
self.width = 0
|
|
|
|
self.height = 0
|
|
|
|
self.title = ""
|
|
|
|
self.throbbing = False
|
|
|
|
self.scrollx = 0
|
|
|
|
self.scrolly = 0
|
|
|
|
self.content_width = 0
|
|
|
|
self.content_height = 0
|
|
|
|
self.status = ""
|
|
|
|
self.pointer = ""
|
|
|
|
self.scale = 1.0
|
|
|
|
self.url = ""
|
|
|
|
self.plotted = []
|
|
|
|
self.plotting = False
|
2019-05-06 12:40:45 +03:00
|
|
|
self.log_entries = []
|
2017-06-10 14:23:55 +03:00
|
|
|
|
|
|
|
def kill(self):
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW DESTROY %s" % self.winid)
|
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
def wait_until_dead(self, timeout=1):
|
|
|
|
now = time.time()
|
|
|
|
while self.alive:
|
|
|
|
self.browser.farmer.loop(once=True)
|
|
|
|
if (time.time() - now) > timeout:
|
|
|
|
break
|
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
def go(self, url, referer=None):
|
2017-06-10 14:23:55 +03:00
|
|
|
if referer is None:
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW GO %s %s" % (
|
|
|
|
self.winid, url))
|
|
|
|
else:
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW GO %s %s %s" % (
|
|
|
|
self.winid, url, referer))
|
2018-11-03 18:28:23 +03:00
|
|
|
self.wait_start_loading()
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2019-06-06 19:16:23 +03:00
|
|
|
def stop(self):
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW STOP %s" % (self.winid))
|
|
|
|
|
2019-08-02 14:24:14 +03:00
|
|
|
def reload(self, all=False):
|
|
|
|
all = " ALL" if all else ""
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW RELOAD %s%s" % (self.winid, all))
|
|
|
|
self.wait_start_loading()
|
2019-05-06 21:20:15 +03:00
|
|
|
|
2019-08-03 14:31:43 +03:00
|
|
|
def click(self, x, y, button="LEFT", kind="SINGLE"):
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW CLICK WIN %s X %s Y %s BUTTON %s KIND %s" % (self.winid, x, y, button, kind))
|
|
|
|
|
2019-05-06 21:20:15 +03:00
|
|
|
def js_exec(self, src):
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW EXEC WIN %s %s" % (self.winid, src))
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def handle(self, action, *args):
|
|
|
|
handler = getattr(self, "handle_window_" + action, None)
|
|
|
|
if handler is not None:
|
|
|
|
handler(*args)
|
|
|
|
|
|
|
|
def handle_window_SIZE(self, _width, width, _height, height):
|
|
|
|
self.width = int(width)
|
|
|
|
self.height = int(height)
|
2019-07-05 00:44:58 +03:00
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def handle_window_DESTROY(self):
|
|
|
|
self.alive = False
|
|
|
|
|
|
|
|
def handle_window_TITLE(self, _str, *title):
|
|
|
|
self.title = " ".join(title)
|
|
|
|
|
|
|
|
def handle_window_GET_DIMENSIONS(self, _width, width, _height, height):
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
|
|
|
|
def handle_window_NEW_CONTENT(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle_window_NEW_ICON(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle_window_START_THROBBER(self):
|
|
|
|
self.throbbing = True
|
|
|
|
|
|
|
|
def handle_window_STOP_THROBBER(self):
|
|
|
|
self.throbbing = False
|
|
|
|
|
|
|
|
def handle_window_SET_SCROLL(self, _x, x, _y, y):
|
|
|
|
self.scrollx = int(x)
|
|
|
|
self.scrolly = int(y)
|
|
|
|
|
|
|
|
def handle_window_UPDATE_BOX(self, _x, x, _y, y, _width, width, _height, height):
|
2019-07-05 00:44:58 +03:00
|
|
|
# pylint: disable=locally-disabled, no-self-use
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
x = int(x)
|
|
|
|
y = int(y)
|
|
|
|
width = int(width)
|
|
|
|
height = int(height)
|
|
|
|
|
|
|
|
def handle_window_UPDATE_EXTENT(self, _width, width, _height, height):
|
|
|
|
self.content_width = int(width)
|
|
|
|
self.content_height = int(height)
|
|
|
|
|
|
|
|
def handle_window_SET_STATUS(self, _str, *status):
|
|
|
|
self.status = (" ".join(status))
|
|
|
|
|
|
|
|
def handle_window_SET_POINTER(self, _ptr, ptr):
|
|
|
|
self.pointer = ptr
|
|
|
|
|
|
|
|
def handle_window_SET_SCALE(self, _scale, scale):
|
|
|
|
self.scale = float(scale)
|
|
|
|
|
|
|
|
def handle_window_SET_URL(self, _url, url):
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
def handle_window_GET_SCROLL(self, _x, x, _y, y):
|
|
|
|
self.scrollx = int(x)
|
|
|
|
self.scrolly = int(y)
|
|
|
|
|
|
|
|
def handle_window_SCROLL_START(self):
|
|
|
|
self.scrollx = 0
|
|
|
|
self.scrolly = 0
|
|
|
|
|
|
|
|
def handle_window_REDRAW(self, act):
|
|
|
|
if act == "START":
|
|
|
|
self.browser.current_draw_target = self
|
|
|
|
self.plotted = []
|
|
|
|
self.plotting = True
|
|
|
|
else:
|
|
|
|
self.browser.current_draw_target = None
|
|
|
|
self.plotting = False
|
|
|
|
|
2019-05-06 12:40:45 +03:00
|
|
|
def handle_window_CONSOLE_LOG(self, _src, src, folding, level, *msg):
|
|
|
|
self.log_entries.append((src, folding == "FOLDABLE", level, " ".join(msg)))
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
def load_page(self, url=None, referer=None):
|
|
|
|
if url is not None:
|
|
|
|
self.go(url, referer)
|
|
|
|
self.wait_loaded()
|
|
|
|
|
2018-11-03 18:28:23 +03:00
|
|
|
def wait_start_loading(self):
|
2017-06-10 14:23:55 +03:00
|
|
|
while not self.throbbing:
|
|
|
|
self.browser.farmer.loop(once=True)
|
2018-11-03 18:28:23 +03:00
|
|
|
|
|
|
|
def wait_loaded(self):
|
|
|
|
self.wait_start_loading()
|
2017-06-10 14:23:55 +03:00
|
|
|
while self.throbbing:
|
|
|
|
self.browser.farmer.loop(once=True)
|
|
|
|
|
|
|
|
def handle_plot(self, *args):
|
|
|
|
self.plotted.append(args)
|
|
|
|
|
|
|
|
def redraw(self, coords=None):
|
|
|
|
if coords is None:
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW REDRAW %s" % self.winid)
|
|
|
|
else:
|
|
|
|
self.browser.farmer.tell_monkey("WINDOW REDRAW %s %s" % (
|
|
|
|
self.winid, (" ".join(coords))))
|
|
|
|
while not self.plotting:
|
|
|
|
self.browser.farmer.loop(once=True)
|
|
|
|
while self.plotting:
|
|
|
|
self.browser.farmer.loop(once=True)
|
|
|
|
return self.plotted
|
2019-05-06 12:40:45 +03:00
|
|
|
|
|
|
|
def clear_log(self):
|
|
|
|
self.log_entries = []
|
|
|
|
|
|
|
|
def log_contains(self, source=None, foldable=None, level=None, substr=None):
|
|
|
|
if (source is None) and (foldable is None) and (level is None) and (substr is None):
|
|
|
|
assert False, "Unable to run log_contains, no predicate given"
|
|
|
|
|
|
|
|
for (source_, foldable_, level_, msg_) in self.log_entries:
|
|
|
|
ok = True
|
|
|
|
if (source is not None) and (source != source_):
|
|
|
|
ok = False
|
|
|
|
if (foldable is not None) and (foldable != foldable_):
|
|
|
|
ok = False
|
|
|
|
if (level is not None) and (level != level_):
|
|
|
|
ok = False
|
|
|
|
if (substr is not None) and (substr not in msg_):
|
|
|
|
ok = False
|
|
|
|
if ok:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def wait_for_log(self, source=None, foldable=None, level=None, substr=None):
|
|
|
|
while not self.log_contains(source=source, foldable=foldable, level=level, substr=substr):
|
|
|
|
self.browser.farmer.loop(once=True)
|
|
|
|
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
def farmer_test():
|
|
|
|
'''
|
|
|
|
Simple farmer test
|
|
|
|
'''
|
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
browser = Browser(quiet=True)
|
|
|
|
win = browser.new_window()
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
fname = "test/js/inline-doc-write-simple.html"
|
|
|
|
full_fname = os.path.join(os.getcwd(), fname)
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
browser.pass_options("--enable_javascript=0")
|
|
|
|
win.load_page("file://" + full_fname)
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
print("Loaded, URL is {}".format(win.url))
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
cmds = win.redraw()
|
|
|
|
print("Received {} plot commands".format(len(cmds)))
|
|
|
|
for cmd in cmds:
|
|
|
|
if cmd[0] == "TEXT":
|
2019-07-05 00:44:58 +03:00
|
|
|
text_x = cmd[2]
|
|
|
|
text_y = cmd[4]
|
2018-11-03 20:14:43 +03:00
|
|
|
rest = " ".join(cmd[6:])
|
2019-07-05 00:44:58 +03:00
|
|
|
print("{} {} -> {}".format(text_x, text_y, rest))
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
browser.pass_options("--enable_javascript=1")
|
|
|
|
win.load_page("file://" + full_fname)
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
print("Loaded, URL is {}".format(win.url))
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
cmds = win.redraw()
|
|
|
|
print("Received {} plot commands".format(len(cmds)))
|
|
|
|
for cmd in cmds:
|
|
|
|
if cmd[0] == "TEXT":
|
2019-07-05 00:44:58 +03:00
|
|
|
text_x = cmd[2]
|
|
|
|
text_y = cmd[4]
|
2018-11-03 20:14:43 +03:00
|
|
|
rest = " ".join(cmd[6:])
|
2019-07-05 00:44:58 +03:00
|
|
|
print("{} {} -> {}".format(text_x, text_y, rest))
|
2017-06-10 14:23:55 +03:00
|
|
|
|
2018-11-03 20:14:43 +03:00
|
|
|
browser.quit_and_wait()
|
2018-11-03 18:28:23 +03:00
|
|
|
|
2018-11-04 13:35:22 +03:00
|
|
|
class FooBarLogin(Browser):
|
|
|
|
def handle_ready_login(self, lwin):
|
|
|
|
lwin.send_username("foo")
|
|
|
|
lwin.send_password("bar")
|
|
|
|
lwin.go()
|
|
|
|
|
2019-06-05 22:57:55 +03:00
|
|
|
def handle_ready_sslcert(self, cwin):
|
|
|
|
cwin.destroy()
|
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
fbbrowser = FooBarLogin(quiet=True)
|
|
|
|
win = fbbrowser.new_window()
|
2018-11-04 13:35:22 +03:00
|
|
|
win.load_page("https://httpbin.org/basic-auth/foo/bar")
|
|
|
|
cmds = win.redraw()
|
2019-02-16 13:23:17 +03:00
|
|
|
print("Received {} plot commands for auth test".format(len(cmds)))
|
2018-11-04 13:35:22 +03:00
|
|
|
for cmd in cmds:
|
|
|
|
if cmd[0] == "TEXT":
|
2019-07-05 00:44:58 +03:00
|
|
|
text_x = cmd[2]
|
|
|
|
text_y = cmd[4]
|
2018-11-04 13:35:22 +03:00
|
|
|
rest = " ".join(cmd[6:])
|
2019-07-05 00:44:58 +03:00
|
|
|
print("{} {} -> {}".format(text_x, text_y, rest))
|
2018-11-04 13:35:22 +03:00
|
|
|
|
2019-05-06 12:40:45 +03:00
|
|
|
fname = "test/js/inserted-script.html"
|
|
|
|
full_fname = os.path.join(os.getcwd(), fname)
|
|
|
|
|
|
|
|
browser = Browser(quiet=True)
|
|
|
|
browser.pass_options("--enable_javascript=1")
|
|
|
|
win = browser.new_window()
|
|
|
|
win.load_page("file://" + full_fname)
|
|
|
|
print("Loaded, URL is {}".format(win.url))
|
|
|
|
|
|
|
|
win.wait_for_log(substr="deferred")
|
|
|
|
|
2019-07-05 00:44:58 +03:00
|
|
|
# print("Discussion was:")
|
|
|
|
# for line in browser.farmer.discussion:
|
2018-11-03 20:14:43 +03:00
|
|
|
# print("{} {}".format(line[0], line[1]))
|
2019-07-05 00:44:58 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
farmer_test()
|