def connect_db(): """ Connect to the DB :return: None """ try: sqliteConn = sqlite3.connect('tls_screenshots.db') return sqliteConn except sqlite3.Error as error: logger.error("Error while connecting to the DB - {}".format(error))
def new_directory(item): """ Creates new directory if does not exist. :param item: Directory to be created :return: None """ if os.path.exists(item): logger.info("# Directory exists, not creating: {}".format(item)) else: logger.info("# Creating directory: {}".format(item)) try: os.makedirs(item) except OSError: logger.error("Error occured while creating: {}".format(item))
def remove_item(item): """ Removes the given directory. :param item: Directory to be removed :return: None """ if os.path.exists(item): logger.info("# Removing item: {}".format(item)) try: os.rmdir(item) except OSError: logger.error("Error occured while deleting item: {}".format(item)) else: logger.info("# Item does not exist, not removing: {}".format(item))
def clear_db(): """ Deletes all data from the DB :return: None """ try: sqliteConn = connect_db() cursor = sqliteConn.cursor() delete_query = "DELETE FROM collection" cursor.execute(delete_query) sqliteConn.commit() cursor.close() except sqlite3.Error as error: logger.error("Error while connecting to the DB - {}".format(error)) finally: disconnect_db(sqliteConn)
def open_opera(driver, browser, old_driver=False): """ Run screenshot in different thread. :param driver: Driver :param browser: Browser :param old_driver: True if opera runs old driver, False otherwise :return: None """ try: if old_driver: logger.info("Starting timeout_and_screenshot.") timeout_and_screenshot(driver, browser) else: open_browser(driver, browser.url) screenshot_website(driver, browser) except Exception as e: logger.error("Error in open_opera: {}".format(e))
def chromium(browser): """ Opens Chromium and makes a screenshot of the desired website. :param browser: Browser :return: None """ browser.set_short_browser_version() chromium_driver = prepare_driver(browser) driver = chromium_driver.create_chromium_driver() old_driver = browser.short_version < 74 try: open_chromium(driver, browser, old_driver=old_driver) except Exception as e: logger.error("Exception in chromium() - {}".format(e)) finally: driver.quit() kill_browser()
def open_browser_with_timeout(driver, browser): """ Opens Chromium, sets a timeout for the script to finish and takes a screenshot :param driver: Browser driver :param browser: Browser :return: None """ try: if browser.case == "expired" or browser.case == "wrong-host" or browser.case == "self-signed" \ or browser.case == "untrusted-root" or browser.case == "revoked": driver.set_page_load_timeout(5) driver.set_script_timeout(5) open_browser(driver, browser.url) screenshot_website(driver, browser) except Exception as e: logger.error("Exception occured {} - making screenshot.".format(e)) screenshot_website(driver, browser)
def open_chromium(driver, browser, old_driver=False): """ Opens Chromium and makes a screenshot of the desired website. If the driver version is older, it sets a timeout for the script to finish. :param driver: Browser driver :param browser: Browser :param old_driver: True if the driver version is older than 74 :return: None """ try: if old_driver: open_browser_with_timeout(driver, browser) else: open_browser(driver, browser.url) screenshot_website(driver, browser) except Exception as e: logger.error("Error in open_chromium: {}".format(e))
def opera(browser): """ Opens Opera and makes a screenshot of the desired website. :param browser: Browser :return: None """ browser.set_short_browser_version() opera_driver = prepare_driver(browser) driver = opera_driver.create_opera_driver() time.sleep(2) try: open_opera(driver, browser, old_driver=opera_driver.old) except Exception as e: logger.error("Exception in opera(): - {}".format(e)) finally: driver.quit() kill_browser()
def screenshot_website(driver, browser, opera=False, ie=False): """ Makes a screenshot of the opened website. :param driver: Browser WebDriver :param browser: Browser :param opera: True if older opera version, False otherwise :param ie: True if IE is the actual browser, False otherwise :return: None """ logger.info("Going to make screenshot.") new_directory(SCREENSHOT_PATH_BASE) # If alert window appears, Accept and continue to the website. logger.info("Waiting until the website is loaded.") try: load_website(driver, browser, opera=opera, ie=ie) except Exception as e: logger.error("Error occured in function 'shot()' - {}".format(e)) finally: kill_browser()
def get_ssl_screenshot(browser, version): """ Gets the screenshot of SSL warning in the given browsers version. :param browser: Browser :param version: Browser version :return: None """ logger.info("# Preparing iteration.") all_cases = len(cfg.get("cases")) for index, case in enumerate(cfg.get("cases")): progress = set_progress_percentage(index, all_cases) try: output(browser, str(version), case) open_webpage( cfg.get("browsers")[browser].get("binary"), cfg.get("cases")[case].get("url"), case, str(version), cfg.get("browsers")[browser].get("package")) except Exception as e: logger.error("Something went TERRIBLY wrong. - {}".format(e)) print_progress(progress, cases=True)
def firefox(browser): """ Opens Firefox and makes a screenshot of the desired website. :param browser: Browser :return: None """ browser.set_short_browser_version() ff_driver = prepare_driver(browser) driver = ff_driver.create_firefox_driver() try: open_browser(driver, browser.url) screenshot_website(driver, browser) except WebDriverException as e: logger.error( "Exception from Selenium but going to take a screenshot. -- {}". format(e)) screenshot_website(driver, browser) finally: driver.quit() kill_browser()
def create_db(): """ Create new DB :return: None """ try: sqliteConn = connect_db() cursor = sqliteConn.cursor() create_table_query = """CREATE TABLE collection ( browser TEXT, version TEXT, screenshots INTEGER );""" cursor.execute(create_table_query) sqliteConn.commit() cursor.close() except sqlite3.Error as error: logger.error("Error while connecting to the DB - {}".format(error)) finally: disconnect_db(sqliteConn)
def update_db(browser, version): """ Update DB :param browser: Browser :param version: Browser version :return: None """ try: sqliteConn = connect_db() cursor = sqliteConn.cursor() update_query = """UPDATE collection SET screenshots = screenshots + 1 WHERE browser = '{}' AND version = '{}'""".format( browser, version) cursor.execute(update_query) sqliteConn.commit() cursor.close() except sqlite3.Error as error: logger.error("Error while updating - {}".format(error)) finally: disconnect_db(sqliteConn)
def insert_into_db(browser, version, screenshots): """ Insert into DB :param browser: Browser :param version: Browser version :param screenshots: Number of screenshots collected :return: None """ try: sqliteConn = connect_db() cursor = sqliteConn.cursor() insert_query = """INSERT INTO collection(browser, version, screenshots) VALUES ('{}', '{}', {});""".format(browser, version, screenshots) cursor.execute(insert_query) sqliteConn.commit() cursor.close() except sqlite3.Error as error: logger.error("Error while inserting - {}".format(error)) finally: disconnect_db(sqliteConn)
def timeout_and_screenshot(driver, browser): """ Opens the url in different thread so that it is not waiting until the page is loaded. :param driver: Driver :param browser: Browser :return: None """ try: p1 = multiprocessing.Process(name="p1", target=open_browser, args=(driver, browser.url)) logger.info("Starting process for open_browser.") p1.start() p1.join(3) logger.info("Going to take screenshot from timeout_and_screenshot.") if opera: screenshot_website(driver, browser, True, False) except Exception as e: logger.error("Exception in multiprocessing: {}".format(e)) finally: logger.info("Checking if thread is active") terminate_thread(p1)
def collect_warnings(all_versions, browser): """ Iterates over all of the browser versions and runs the script for getting screenshots. :param all_versions: All browser versions for the collection :param browser: Browser :return: None """ all_versions_count = len(all_versions) for v_index, version in enumerate(all_versions): v_progress = set_progress_percentage(v_index, all_versions_count) logger.info("######## Processing {} v({})".format(browser, version)) if browser != "edge": return_code = install_browser(browser, version) insert_into_db(browser, version, 0) if return_code != 0: logger.error( "# Installation failed...Skipping to the next browser version." ) continue get_ssl_screenshot(browser, version) print_progress(v_progress, versions=True) uninstall_browser(browser)