// assert that phantomjs is in path for webdriver
detect_phantomjs()
driver = webdriver.PhantomJS()
// generate the html as tempfile
temp_html = tempfile.NamedTemporaryFile(suffix=".html").name
save(obj, filename=temp_html, resources=INLINE, title="")
if filename is None:
filename = _detect_filename("png")
driver.get("file:///" + temp_html)
class element_to_start_resizing(object):
An expectation for checking if an element has started resizing
def __init__(self, element):
self.element = element
self.previous_width = self.element.size["width"]
def __call__(self, driver):
current_width = self.element.size["width"]
if self.previous_width != current_width:
return True
else:
self.previous_width = current_width
return False
class element_to_finish_resizing(object):
An expectation for checking if an element has finished resizing
def __init__(self, element):
self.element = element
self.previous_width = self.element.size["width"]
def __call__(self, driver):
current_width = self.element.size["width"]
if self.previous_width == current_width:
return True
else:
self.previous_width = current_width
return False
def wait_for_canvas_resize(canvas, test_driver):
try:
wait = WebDriverWait(test_driver, 1)
wait.until(element_to_start_resizing(canvas))
wait.until(element_to_finish_resizing(canvas))
except TimeoutException:
// Resize may or may not happen instantaneously,
// Put the waits in to give some time, but allow test to
// try and process.
pass
canvas = driver.find_element_by_tag_name("canvas")
wait_for_canvas_resize(canvas, driver)
driver.maximize_window()
browser_logs = driver.get_log("browser")severe_errors = [l for l in browser_logs if l.get("level") == "SEVERE"]
if len(severe_errors) > 0:
logger.warn("There were severe browser errors that may have affected your export: {}".format(severe_errors))
driver.save_screenshot(filename)