def save_screenshot(drv, path="", filename=""): """ Get a browser to take a screenshot with, and a path to save the screenshot to. The filename will be decided using the browser's name. Returns the screenshot entity. """ scr = Screenshotter() logger = logging.getLogger('obs.Screenshotter') wid, hei = drv.get_window_size().values() logger.debug("Window size: " + str(wid) + "x" + str(hei)) if "firefox" in drv.name: sshot_path = scr.save_screenshot_firefox(drv, path, filename) elif "internet explorer" in drv.name: sshot_path = scr.save_screenshot_ie(drv, path, filename) elif "chrome" in drv.name: sshot_path = scr.save_screenshot_chrome(drv, path, filename) elif "phantomjs" in drv.name: sshot_path = scr.save_screenshot_phantomjs(drv, path, filename) else: logger.error("Non recognized browser!\nOnly Firefox, Chrome and IE are supported (for now, anyways.)") raise ValueError("Non recognized browser!") s = Screenshot(sshot_path, drv.current_url, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), drv.name, wid, hei) if "$hash" in s.path: # Hash value must be replaced after writing the file hashed_path = s.path.replace("$hash", s.hashvalue) os.renames(s.path, hashed_path) s.path = hashed_path logger.debug("Taken screenshot with path: " + s.path) return s