@@ -89,70 +89,54 @@ def upload_icomoon(self, icomoon_json_path: str):
8989 :raises TimeoutException: happens when elements are not found.
9090 """
9191 print ("Uploading icomoon.json file..." )
92- try :
93- self .click_hamburger_input ()
94-
95- # find the file input and enter the file path
96- import_btn = self .driver .find_element (By .XPATH , "(//li[@class='file'])[1]//input" )
97- import_btn .send_keys (icomoon_json_path )
98- except SeleniumTimeoutException as e :
99- print (e .stacktrace )
100- print ("Selenium timed out. Couldn't find import button." )
101- self .close ()
102- raise e
103- except Exception as e :
104- self .close ()
105- raise e
92+ self .click_hamburger_input ()
93+
94+ # find the file input and enter the file path
95+ import_btn = self .driver .find_element (By .XPATH , "(//li[@class='file'])[1]//input" )
96+ import_btn .send_keys (icomoon_json_path )
10697
10798 try :
10899 confirm_btn = WebDriverWait (self .driver , SeleniumRunner .MED_WAIT_IN_SEC ).until (
109100 ec .element_to_be_clickable ((By .XPATH , "//div[@class='overlay']//button[text()='Yes']" ))
110101 )
111102 confirm_btn .click ()
112103 except SeleniumTimeoutException as e :
113- print (e .stacktrace )
114- print ("Cannot find the confirm button when uploading the icomoon.json" ,
115- "Ensure that the icomoon.json is in the correct format for Icomoon.io" ,
116- sep = '\n ' )
117- self .close ()
104+ raise Exception ("Cannot find the confirm button when uploading the icomoon.json" \
105+ "Ensure that the icomoon.json is in the correct format for Icomoon.io" )
118106
119107 print ("JSON file uploaded." )
120108
121- def upload_svgs (self , svgs : List [str ]):
109+ def upload_svgs (self , svgs : List [str ], screenshot_folder : str = "" ):
122110 """
123111 Upload the SVGs provided in folder_info
124112 :param svgs: a list of svg Paths that we'll upload to icomoon.
113+ :param screenshot_folder: the name of the screenshot_folder. If
114+ the value is provided, it means the user want to take a screenshot
115+ of each icon.
125116 """
126- try :
127- print ("Uploading SVGs..." )
117+ print ("Uploading SVGs..." )
128118
129- edit_mode_btn = self .driver .find_element_by_css_selector (
130- "div.btnBar button i.icon-edit"
131- )
132- edit_mode_btn .click ()
133-
134- self .click_hamburger_input ()
135-
136- for svg in svgs :
137- import_btn = self .driver .find_element_by_css_selector (
138- "li.file input[type=file]"
139- )
140- import_btn .send_keys (svg )
141- print (f"Uploaded { svg } " )
142- self .test_for_possible_alert (self .SHORT_WAIT_IN_SEC , "Dismiss" )
143- self .remove_color_from_icon ()
144-
145- # take a screenshot of the icons that were just added
146- self .driver .save_screenshot ("new_icons.png" );
147- self .click_hamburger_input ()
148- select_all_button = WebDriverWait (self .driver , self .LONG_WAIT_IN_SEC ).until (
149- ec .element_to_be_clickable ((By .XPATH , "//button[text()='Select All']" ))
119+ edit_mode_btn = self .driver .find_element_by_css_selector (
120+ "div.btnBar button i.icon-edit"
121+ )
122+ edit_mode_btn .click ()
123+
124+ self .click_hamburger_input ()
125+
126+ for i in range (len (svgs )):
127+ import_btn = self .driver .find_element_by_css_selector (
128+ "li.file input[type=file]"
150129 )
151- select_all_button .click ()
152- print ("Finished uploading the svgs..." )
153- except Exception as e :
154- self .close ()
155- raise e
130+ import_btn .send_keys (svgs [i ])
131+ print (f"Uploaded { svgs [i ]} " )
132+ self .test_for_possible_alert (self .SHORT_WAIT_IN_SEC , "Dismiss" )
133+ self .click_on_just_added_icon (screenshot_folder , i )
134+
135+ # take a screenshot of the icons that were just added
136+ new_icons_path = str (Path (screenshot_folder , "new_icons.png" ).resolve ())
137+ self .driver .save_screenshot (new_icons_path );
138+
139+ print ("Finished uploading the svgs..." )
156140
157141 def click_hamburger_input (self ):
158142 """
@@ -161,20 +145,16 @@ def click_hamburger_input(self):
161145 input two times before the menu appears.
162146 :return: None.
163147 """
164- try :
165- hamburger_input = self .driver .find_element_by_xpath (
166- "(//i[@class='icon-menu'])[2]"
167- )
148+ hamburger_input = self .driver .find_element_by_xpath (
149+ "(//i[@class='icon-menu'])[2]"
150+ )
168151
169- menu_appear_callback = ec .element_to_be_clickable (
170- (By .CSS_SELECTOR , "h1 ul.menuList2" )
171- )
152+ menu_appear_callback = ec .element_to_be_clickable (
153+ (By .CSS_SELECTOR , "h1 ul.menuList2" )
154+ )
172155
173- while not menu_appear_callback (self .driver ):
174- hamburger_input .click ()
175- except Exception as e :
176- self .close ()
177- raise e
156+ while not menu_appear_callback (self .driver ):
157+ hamburger_input .click ()
178158
179159 def test_for_possible_alert (self , wait_period : float , btn_text : str ):
180160 """
@@ -191,22 +171,37 @@ def test_for_possible_alert(self, wait_period: float, btn_text: str):
191171 )
192172 dismiss_btn .click ()
193173 except SeleniumTimeoutException :
194- pass
174+ pass # nothing found => everything is good
175+
176+ def click_on_just_added_icon (self , screenshot_folder : str , index : int ):
177+ """
178+ Click on the most recently added icon so we can remove the colors
179+ and take a snapshot if needed.
180+ """
181+ recently_uploaded_icon = WebDriverWait (self .driver , self .LONG_WAIT_IN_SEC ).until (
182+ ec .element_to_be_clickable ((By .XPATH , "//div[@id='set0']//mi-box[1]//div" ))
183+ )
184+ recently_uploaded_icon .click ()
185+
186+ self .remove_color_from_icon ()
187+
188+ if screenshot_folder :
189+ screenshot_path = str (Path (screenshot_folder , f"screenshot_{ index } .png" ).resolve ())
190+ self .driver .save_screenshot (screenshot_path )
191+ print ("Took screenshot and saved it at " + screenshot_path )
192+
193+ close_btn = self .driver \
194+ .find_element_by_css_selector ("div.overlayWindow i.icon-close" )
195+ close_btn .click ()
195196
196197 def remove_color_from_icon (self ):
197198 """
198199 Remove the color from the most recent uploaded icon.
199- :return: None.
200+ This is because some SVG have colors in them and we don't want to
201+ force contributors to remove them in case people want the colored SVGs.
202+ The color removal is also necessary so that the Icomoon-generated
203+ icons fit within one font symbol/ligiature.
200204 """
201- try :
202- recently_uploaded_icon = WebDriverWait (self .driver , self .LONG_WAIT_IN_SEC ).until (
203- ec .element_to_be_clickable ((By .XPATH , "//div[@id='set0']//mi-box[1]//div" ))
204- )
205- recently_uploaded_icon .click ()
206- except Exception as e :
207- self .close ()
208- raise e
209-
210205 try :
211206 color_tab = WebDriverWait (self .driver , self .SHORT_WAIT_IN_SEC ).until (
212207 ec .element_to_be_clickable ((By .CSS_SELECTOR , "div.overlayWindow i.icon-droplet" ))
@@ -217,42 +212,35 @@ def remove_color_from_icon(self):
217212 .find_element_by_css_selector ("div.overlayWindow i.icon-droplet-cross" )
218213 remove_color_btn .click ()
219214 except SeleniumTimeoutException :
220- pass
221- except Exception as e :
222- self .close ()
223- raise e
224-
225- try :
226- close_btn = self .driver \
227- .find_element_by_css_selector ("div.overlayWindow i.icon-close" )
228- close_btn .click ()
229- except Exception as e :
230- self .close ()
231- raise e
215+ pass # do nothing cause sometimes, the color tab doesn't appear in the site
232216
233217 def download_icomoon_fonts (self , zip_path : Path ):
234218 """
235219 Download the icomoon.zip from icomoon.io.
236220 :param zip_path: the path to the zip file after it's downloaded.
237221 """
238- try :
239- print ("Downloading Font files..." )
240- self .driver .find_element_by_css_selector (
241- "a[href='#/select/font']"
242- ).click ()
243-
244- self .test_for_possible_alert (self .MED_WAIT_IN_SEC , "Continue" )
245- download_btn = WebDriverWait (self .driver , SeleniumRunner .LONG_WAIT_IN_SEC ).until (
246- ec .presence_of_element_located ((By .CSS_SELECTOR , "button.btn4 span" ))
247- )
248- download_btn .click ()
249- if self .wait_for_zip (zip_path ):
250- print ("Font files downloaded." )
251- else :
252- raise TimeoutError (f"Couldn't find { zip_path } after download button was clicked." )
253- except Exception as e :
254- self .close ()
255- raise e
222+ # select all the svgs so that the newly added svg are part of the collection
223+ self .click_hamburger_input ()
224+ select_all_button = WebDriverWait (self .driver , self .LONG_WAIT_IN_SEC ).until (
225+ ec .element_to_be_clickable ((By .XPATH , "//button[text()='Select All']" ))
226+ )
227+ select_all_button .click ()
228+
229+ print ("Downloading Font files..." )
230+ font_tab = self .driver .find_element_by_css_selector (
231+ "a[href='#/select/font']"
232+ )
233+ font_tab .click ()
234+
235+ self .test_for_possible_alert (self .MED_WAIT_IN_SEC , "Continue" )
236+ download_btn = WebDriverWait (self .driver , SeleniumRunner .LONG_WAIT_IN_SEC ).until (
237+ ec .presence_of_element_located ((By .CSS_SELECTOR , "button.btn4 span" ))
238+ )
239+ download_btn .click ()
240+ if self .wait_for_zip (zip_path ):
241+ print ("Font files downloaded." )
242+ else :
243+ raise TimeoutError (f"Couldn't find { zip_path } after download button was clicked." )
256244
257245 def wait_for_zip (self , zip_path : Path ) -> bool :
258246 """
0 commit comments