@@ -158,27 +158,22 @@ def _test_menu_dropdown(self, is_narrow=False, is_medium=False):
158158 if is_narrow :
159159 # Do not test when menu is not visible
160160 return
161- with self .subTest ('Test menu dropdown when menu is close ' ):
161+ with self .subTest ('Test menu dropdown when menu is closed ' ):
162162 self ._close_menu ()
163163 # Test mg_dropdown is not visible
164164 self .assertEqual (mg_dropdown .is_displayed (), False )
165165 self .assertEqual (mg_label .is_displayed (), False )
166166 self .assertEqual (mg_icon .is_displayed (), True )
167167 # Test mg dropdown gets visible on clicking mg head
168168 mg_head .click ()
169- is_visible = True
170169 try :
171170 WebDriverWait (self .web_driver , 2 ).until (
172171 EC .visibility_of_element_located (
173172 (By .CSS_SELECTOR , '#mg-dropdown-32' )
174173 )
175174 )
176175 except TimeoutException :
177- is_visible = False
178- self .assertEqual (
179- is_visible ,
180- True ,
181- )
176+ self .fail ('drop down not visible' )
182177 self .assertEqual (mg_dropdown_label .is_displayed (), True )
183178 # Test mg dropdown gets invisible on clicking mg head
184179 mg_head .click ()
@@ -188,12 +183,16 @@ def _test_menu_dropdown(self, is_narrow=False, is_medium=False):
188183 main_content .click ()
189184 self .assertEqual (mg_dropdown .is_displayed (), False )
190185
191- with self .subTest ('Test visibilty of menu label when menu close ' ):
186+ with self .subTest ('Test visibilty of menu label when menu is closed ' ):
192187 self ._close_menu ()
193188 actions = ActionChains (self .web_driver )
194189 actions .move_to_element (mg_head )
190+ # clicking twice forces the hover action
191+ # to work consistently, without this it may
192+ # yield inconsistent results in automated tests
193+ actions .click (mg_head )
194+ actions .click (mg_head )
195195 actions .perform ()
196- is_visible = True
197196 try :
198197 WebDriverWait (self .web_driver , 2 ).until (
199198 EC .visibility_of_element_located (
@@ -204,8 +203,7 @@ def _test_menu_dropdown(self, is_narrow=False, is_medium=False):
204203 )
205204 )
206205 except TimeoutException :
207- is_visible = False
208- self .assertEqual (is_visible , True )
206+ self .fail ('label not visible' )
209207 mg_head .click ()
210208 actions .move_to_element (mg_head )
211209 actions .perform ()
@@ -731,18 +729,47 @@ def test_autocomplete_shelf_filter(self):
731729 ),
732730 self .web_driver .page_source ,
733731 )
734- self .web_driver .find_element (By .CSS_SELECTOR , filter_css_selector ).click ()
735- self .web_driver .find_element (By .CSS_SELECTOR , '.select2-container--open' )
736- WebDriverWait (self .web_driver , 10 ).until (
737- EC .presence_of_element_located (
738- (By .XPATH , f'//*[contains(text(), "{ horror_shelf .name } ")]' )
732+ try :
733+ WebDriverWait (self .web_driver , 2 ).until (
734+ EC .presence_of_element_located ((By .CSS_SELECTOR , filter_css_selector ))
739735 )
740- )
736+ except TimeoutException :
737+ self .fail (f'{ filter_css_selector } not available as expected' )
738+ else :
739+ self .web_driver .find_element (By .CSS_SELECTOR , filter_css_selector ).click ()
740+ try :
741+ WebDriverWait (self .web_driver , 2 ).until (
742+ EC .presence_of_element_located (
743+ (By .CSS_SELECTOR , '.select2-container--open' )
744+ )
745+ )
746+ except TimeoutException :
747+ self .fail ('select2 widget did not open as expected' )
748+ try :
749+ WebDriverWait (self .web_driver , 2 ).until (
750+ EC .presence_of_element_located (
751+ (By .XPATH , f'//*[contains(text(), "{ horror_shelf .name } ")]' )
752+ )
753+ )
754+ except TimeoutException :
755+ self .fail (f'"{ horror_shelf .name } " not found' )
741756 self .assertIn (horror_shelf .name , self .web_driver .page_source )
742757 self .assertIn (factual_shelf .name , self .web_driver .page_source )
743- self .web_driver .find_element (By .XPATH , filter_option_xpath ).click ()
758+ try :
759+ WebDriverWait (self .web_driver , 2 ).until (
760+ EC .presence_of_element_located ((By .XPATH , filter_option_xpath ))
761+ )
762+ except TimeoutException :
763+ self .fail (f'"{ filter_option_xpath } " not found' )
764+ else :
765+ self .web_driver .find_element (By .XPATH , filter_option_xpath ).click ()
744766 self .assertIn (str (factual_shelf .id ), self .web_driver .current_url )
745- self .web_driver .find_element (By .CSS_SELECTOR , filter_css_selector )
767+ try :
768+ WebDriverWait (self .web_driver , 2 ).until (
769+ EC .presence_of_element_located ((By .CSS_SELECTOR , filter_css_selector ))
770+ )
771+ except TimeoutException :
772+ self .fail (f'"{ filter_css_selector } " not found' )
746773 self .assertNotIn (horror_shelf .name , self .web_driver .page_source )
747774 self .assertIn (factual_shelf .name , self .web_driver .page_source )
748775 with self .assertRaises (NoSuchElementException ):
@@ -778,11 +805,32 @@ def test_autocomplete_owner_filter(self):
778805 ),
779806 self .web_driver .page_source ,
780807 )
781- self .web_driver .find_element (By .CSS_SELECTOR , filter_css_selector ).click ()
782- self .web_driver .find_element (By .CSS_SELECTOR , '.select2-container--open' )
808+ try :
809+ WebDriverWait (self .web_driver , 2 ).until (
810+ EC .presence_of_element_located ((By .CSS_SELECTOR , filter_css_selector ))
811+ )
812+ except TimeoutException :
813+ self .fail (f'{ filter_css_selector } not available as expected' )
814+ else :
815+ self .web_driver .find_element (By .CSS_SELECTOR , filter_css_selector ).click ()
816+ try :
817+ WebDriverWait (self .web_driver , 2 ).until (
818+ EC .presence_of_element_located (
819+ (By .CSS_SELECTOR , '.select2-container--open' )
820+ )
821+ )
822+ except TimeoutException :
823+ self .fail ('select2 widget did not open as expected' )
783824 self .assertIn (self .admin .username , self .web_driver .page_source )
784825 self .assertIn (user .username , self .web_driver .page_source )
785- self .web_driver .find_element (By .XPATH , filter_null_option_xpath ).click ()
826+ try :
827+ WebDriverWait (self .web_driver , 2 ).until (
828+ EC .presence_of_element_located ((By .XPATH , filter_null_option_xpath ))
829+ )
830+ except TimeoutException :
831+ self .fail (f'{ filter_null_option_xpath } not available as expected' )
832+ else :
833+ self .web_driver .find_element (By .XPATH , filter_null_option_xpath ).click ()
786834 self ._get_filter_button ().click ()
787835 self .assertIn ('owner_id__isnull=true' , self .web_driver .current_url )
788836 with self .assertRaises (NoSuchElementException ):
0 commit comments