From 1038b7a774fe3b8f9418376626bdd8b38f479d16 Mon Sep 17 00:00:00 2001 From: Thomas Bui Date: Fri, 1 Jan 2021 00:42:53 -0800 Subject: [PATCH] Added color tab error handling and better logging --- .../scripts/build_assets/SeleniumRunner.py | 177 ++++++++---------- .github/scripts/icomoon_build.py | 2 +- .github/scripts/icomoon_peek.py | 2 +- 3 files changed, 79 insertions(+), 102 deletions(-) diff --git a/.github/scripts/build_assets/SeleniumRunner.py b/.github/scripts/build_assets/SeleniumRunner.py index 9fbb9207..b95d4a7c 100644 --- a/.github/scripts/build_assets/SeleniumRunner.py +++ b/.github/scripts/build_assets/SeleniumRunner.py @@ -89,20 +89,11 @@ class SeleniumRunner: :raises TimeoutException: happens when elements are not found. """ print("Uploading icomoon.json file...") - try: - self.click_hamburger_input() - - # find the file input and enter the file path - import_btn = self.driver.find_element(By.XPATH, "(//li[@class='file'])[1]//input") - import_btn.send_keys(icomoon_json_path) - except SeleniumTimeoutException as e: - print(e.stacktrace) - print("Selenium timed out. Couldn't find import button.") - self.close() - raise e - except Exception as e: - self.close() - raise e + self.click_hamburger_input() + + # find the file input and enter the file path + import_btn = self.driver.find_element(By.XPATH, "(//li[@class='file'])[1]//input") + import_btn.send_keys(icomoon_json_path) try: confirm_btn = WebDriverWait(self.driver, SeleniumRunner.MED_WAIT_IN_SEC).until( @@ -110,11 +101,8 @@ class SeleniumRunner: ) confirm_btn.click() except SeleniumTimeoutException as e: - print(e.stacktrace) - print("Cannot find the confirm button when uploading the icomoon.json", - "Ensure that the icomoon.json is in the correct format for Icomoon.io", - sep='\n') - self.close() + raise Exception("Cannot find the confirm button when uploading the icomoon.json" \ + "Ensure that the icomoon.json is in the correct format for Icomoon.io") print("JSON file uploaded.") @@ -126,39 +114,29 @@ class SeleniumRunner: the value is provided, it means the user want to take a screenshot of each icon. """ - try: - print("Uploading SVGs...") + print("Uploading SVGs...") - edit_mode_btn = self.driver.find_element_by_css_selector( - "div.btnBar button i.icon-edit" + edit_mode_btn = self.driver.find_element_by_css_selector( + "div.btnBar button i.icon-edit" + ) + edit_mode_btn.click() + + self.click_hamburger_input() + + for i in range(len(svgs)): + import_btn = self.driver.find_element_by_css_selector( + "li.file input[type=file]" ) - edit_mode_btn.click() + import_btn.send_keys(svgs[i]) + print(f"Uploaded {svgs[i]}") + self.test_for_possible_alert(self.SHORT_WAIT_IN_SEC, "Dismiss") + self.click_on_just_added_icon(screenshot_folder, i) - self.click_hamburger_input() + # take a screenshot of the icons that were just added + new_icons_path = str(Path(screenshot_folder, "new_icons.png").resolve()) + self.driver.save_screenshot(new_icons_path); - for i in range(len(svgs)): - import_btn = self.driver.find_element_by_css_selector( - "li.file input[type=file]" - ) - import_btn.send_keys(svgs[i]) - print(f"Uploaded {svgs[i]}") - self.test_for_possible_alert(self.SHORT_WAIT_IN_SEC, "Dismiss") - self.click_on_just_added_icon(screenshot_folder, i) - - # take a screenshot of the icons that were just added - new_icons_path = str(Path(screenshot_folder, "new_icons.png").resolve()) - self.driver.save_screenshot(new_icons_path); - - # select all the svgs so that the newly added svg are part of the collection - self.click_hamburger_input() - select_all_button = WebDriverWait(self.driver, self.LONG_WAIT_IN_SEC).until( - ec.element_to_be_clickable((By.XPATH, "//button[text()='Select All']")) - ) - select_all_button.click() - print("Finished uploading the svgs...") - except Exception as e: - self.close() - raise e + print("Finished uploading the svgs...") def click_hamburger_input(self): """ @@ -167,20 +145,16 @@ class SeleniumRunner: input two times before the menu appears. :return: None. """ - try: - hamburger_input = self.driver.find_element_by_xpath( - "(//i[@class='icon-menu'])[2]" - ) + hamburger_input = self.driver.find_element_by_xpath( + "(//i[@class='icon-menu'])[2]" + ) - menu_appear_callback = ec.element_to_be_clickable( - (By.CSS_SELECTOR, "h1 ul.menuList2") - ) + menu_appear_callback = ec.element_to_be_clickable( + (By.CSS_SELECTOR, "h1 ul.menuList2") + ) - while not menu_appear_callback(self.driver): - hamburger_input.click() - except Exception as e: - self.close() - raise e + while not menu_appear_callback(self.driver): + hamburger_input.click() def test_for_possible_alert(self, wait_period: float, btn_text: str): """ @@ -204,25 +178,21 @@ class SeleniumRunner: Click on the most recently added icon so we can remove the colors and take a snapshot if needed. """ - try: - recently_uploaded_icon = WebDriverWait(self.driver, self.LONG_WAIT_IN_SEC).until( - ec.element_to_be_clickable((By.XPATH, "//div[@id='set0']//mi-box[1]//div")) - ) - recently_uploaded_icon.click() + recently_uploaded_icon = WebDriverWait(self.driver, self.LONG_WAIT_IN_SEC).until( + ec.element_to_be_clickable((By.XPATH, "//div[@id='set0']//mi-box[1]//div")) + ) + recently_uploaded_icon.click() - self.remove_color_from_icon() + self.remove_color_from_icon() - if screenshot_folder: - screenshot_path = str(Path(screenshot_folder, f"screenshot_{index}.png").resolve()) - self.driver.save_screenshot(screenshot_path) - print("Took screenshot and saved it at " + screenshot_path) + if screenshot_folder: + screenshot_path = str(Path(screenshot_folder, f"screenshot_{index}.png").resolve()) + self.driver.save_screenshot(screenshot_path) + print("Took screenshot and saved it at " + screenshot_path) - close_btn = self.driver \ - .find_element_by_css_selector("div.overlayWindow i.icon-close") - close_btn.click() - except Exception as e: - self.close() - raise e + close_btn = self.driver \ + .find_element_by_css_selector("div.overlayWindow i.icon-close") + close_btn.click() def remove_color_from_icon(self): """ @@ -232,38 +202,45 @@ class SeleniumRunner: The color removal is also necessary so that the Icomoon-generated icons fit within one font symbol/ligiature. """ - color_tab = WebDriverWait(self.driver, self.SHORT_WAIT_IN_SEC).until( - ec.element_to_be_clickable((By.CSS_SELECTOR, "div.overlayWindow i.icon-droplet")) - ) - color_tab.click() + try: + color_tab = WebDriverWait(self.driver, self.SHORT_WAIT_IN_SEC).until( + ec.element_to_be_clickable((By.CSS_SELECTOR, "div.overlayWindow i.icon-droplet")) + ) + color_tab.click() - remove_color_btn = self.driver \ - .find_element_by_css_selector("div.overlayWindow i.icon-droplet-cross") - remove_color_btn.click() + remove_color_btn = self.driver \ + .find_element_by_css_selector("div.overlayWindow i.icon-droplet-cross") + remove_color_btn.click() + except SeleniumTimeoutException: + pass # do nothing cause sometimes, the color tab doesn't appear in the site def download_icomoon_fonts(self, zip_path: Path): """ Download the icomoon.zip from icomoon.io. :param zip_path: the path to the zip file after it's downloaded. """ - try: - print("Downloading Font files...") - self.driver.find_element_by_css_selector( - "a[href='#/select/font']" - ).click() + # select all the svgs so that the newly added svg are part of the collection + self.click_hamburger_input() + select_all_button = WebDriverWait(self.driver, self.LONG_WAIT_IN_SEC).until( + ec.element_to_be_clickable((By.XPATH, "//button[text()='Select All']")) + ) + select_all_button.click() - self.test_for_possible_alert(self.MED_WAIT_IN_SEC, "Continue") - download_btn = WebDriverWait(self.driver, SeleniumRunner.LONG_WAIT_IN_SEC).until( - ec.presence_of_element_located((By.CSS_SELECTOR, "button.btn4 span")) - ) - download_btn.click() - if self.wait_for_zip(zip_path): - print("Font files downloaded.") - else: - raise TimeoutError(f"Couldn't find {zip_path} after download button was clicked.") - except Exception as e: - self.close() - raise e + print("Downloading Font files...") + font_tab = self.driver.find_element_by_css_selector( + "a[href='#/select/font']" + ) + font_tab.click() + + self.test_for_possible_alert(self.MED_WAIT_IN_SEC, "Continue") + download_btn = WebDriverWait(self.driver, SeleniumRunner.LONG_WAIT_IN_SEC).until( + ec.presence_of_element_located((By.CSS_SELECTOR, "button.btn4 span")) + ) + download_btn.click() + if self.wait_for_zip(zip_path): + print("Font files downloaded.") + else: + raise TimeoutError(f"Couldn't find {zip_path} after download button was clicked.") def wait_for_zip(self, zip_path: Path) -> bool: """ diff --git a/.github/scripts/icomoon_build.py b/.github/scripts/icomoon_build.py index 545042af..303a7ffc 100644 --- a/.github/scripts/icomoon_build.py +++ b/.github/scripts/icomoon_build.py @@ -32,7 +32,7 @@ def main(): filehandler.rename_extracted_files(args.download_path) print("Task completed.") except TimeoutException as e: - sys.exit("Selenium Time Out Error: \n" + e) + sys.exit("Selenium Time Out Error: \n" + str(e)) except Exception as e: sys.exit(e) finally: diff --git a/.github/scripts/icomoon_peek.py b/.github/scripts/icomoon_peek.py index 2ef39f51..cccfce5d 100644 --- a/.github/scripts/icomoon_peek.py +++ b/.github/scripts/icomoon_peek.py @@ -38,7 +38,7 @@ def main(): runner.upload_svgs(svgs, screenshot_folder) print("Task completed.") except TimeoutException as e: - sys.exit("Selenium Time Out Error: \n" + e) + sys.exit("Selenium Time Out Error: \n" + str(e)) except Exception as e: sys.exit(e) finally: