Skip to content

Instantly share code, notes, and snippets.

@gsnedders
Created December 2, 2019 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsnedders/059f8d258653fae02dce2b11faf070f6 to your computer and use it in GitHub Desktop.
Save gsnedders/059f8d258653fae02dce2b11faf070f6 to your computer and use it in GitHub Desktop.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import csv
import socket
import sys
import threading
import time
import traceback
import urlparse
import uuid
import logging
logging.basicConfig(format='%(asctime)s %(message)s',level=logging.INFO)
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.remote.remote_connection import RemoteConnection
from tools.manifest import manifest
extra_timeout = 5
firefox_binary = "/home/gsnedders/Documents/other-projects/mozilla-central/obj-x86_64-pc-linux-gnu/dist/bin/firefox"
wait = """
var callback = arguments[arguments.length - 1];
function test(x) {
if (!root.classList.contains("reftest-wait")) {
observer.disconnect();
// As of 2017-04-05, the Chromium web browser exhibits a rendering bug
// (https://bugs.chromium.org/p/chromium/issues/detail?id=708757) that
// produces instability during screen capture. The following use of
// `requestAnimationFrame` is intended as a short-term workaround, though
// it is not guaranteed to resolve the issue.
//
// For further detail, see:
// https://github.com/jugglinmike/chrome-screenshot-race/issues/1
requestAnimationFrame(function() {
requestAnimationFrame(function() {
var el = document[document.compatMode==='CSS1Compat'?'documentElement':'body'];
var height = el.scrollHeight;
var width = el.scrollWidth;
callback({height: height, width: width});
});
});
}
}
var root = document.documentElement;
var observer = new MutationObserver(test);
observer.observe(root, {attributes: true});
if (document.readyState != "complete") {
onload = test;
} else {
test();
}
"""
class Run(object):
def __init__(self, func, webdriver, url, timeout):
self.func = func
self.result = None
self.webdriver = webdriver
self.url = url
self.timeout = timeout
self.result_flag = threading.Event()
def run(self):
timeout = self.timeout
try:
self.webdriver.set_script_timeout((timeout + extra_timeout) * 1000)
except exceptions.ErrorInResponseException:
logging.error("Lost WebDriver connection")
return False, "Lost"
executor = threading.Thread(target=self._run)
executor.start()
flag = self.result_flag.wait(timeout + 2 * extra_timeout)
if self.result is None:
assert not flag
self.result = False, ("EXTERNAL-TIMEOUT", None)
return self.result
def _run(self):
try:
self.result = True, self.func(self.webdriver, self.url, self.timeout)
except exceptions.TimeoutException:
self.result = False, ("EXTERNAL-TIMEOUT", None)
except (socket.timeout, exceptions.ErrorInResponseException):
self.result = False, ("CRASH", None)
except Exception as e:
message = getattr(e, "message", "")
if message:
message += "\n"
message += traceback.format_exc(e)
self.result = False, ("ERROR", e)
finally:
self.result_flag.set()
class Executor(object):
def __init__(self, webdriver):
self.webdriver = webdriver
def do_test(self, test):
logging.info("run")
return self.screenshot(test, None, None)
def screenshot(self, test, viewport_size, dpi):
# https://github.com/w3c/wptrunner/issues/166
assert viewport_size is None
assert dpi is None
return Run(self._screenshot,
self.webdriver,
test,
10).run()
def _screenshot(self, webdriver, url, timeout):
webdriver.get(url)
rv = webdriver.execute_async_script(wait)
return rv
def main():
fp = open("sizes.csv", "wb")
fieldnames = ['url', 'width', 'height']
writer = csv.DictWriter(fp, fieldnames=fieldnames)
writer.writeheader()
man = manifest.load("/", "MANIFEST.json")
wd = webdriver.Firefox(firefox_binary=firefox_binary)
wd.set_window_size(600, 671)
ex = Executor(wd)
for _, _, path_tests in man.itertypes("reftest"):
for test in path_tests:
url = "http://web-platform.test:8000" + test.url
logging.info("running %s" % url)
ok, result = ex.do_test(url)
if ok:
logging.info("ok")
row = {}
row["url"] = url
row.update(result)
writer.writerow(row)
else:
logging.info("not ok")
try:
wd.quit()
except:
pass
wd = webdriver.Firefox(firefox_binary=firefox_binary)
wd.set_window_size(600, 671)
ex = Executor(wd)
wd.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment