@@ -73,29 +73,26 @@ def post_form_select_tag(tag_name, create_new = false)
7373 find ( '.select2-search__field' ) . fill_in ( with : tag_name )
7474 end
7575
76- # Get the first item listed that is not the "Searching..." item
77- first_option = find ( '#select2-post_tags_cache-results li:first-child' ) { |el | el . text != 'Searching…' }
76+ # Wait for tag search to finish
77+ assert_text ( 'Searching…' )
78+ assert_no_text ( 'Searching…' )
7879
79- if first_option . first ( 'span' ) . text == tag_name
80- # If the text matches the tag name, first check whether we are creating a new tag.
81- # If so, confirm that we are allowed to. If all is good, actually click on the item.
82- if create_new || !first_option . text . include? ( 'Create new tag' )
83- first_option . click
84- else
85- raise "Expected to find tag with the name #{ tag_name } , " \
86- 'but could not select it from options without creating a new tag.'
87- end
80+ # Get the first and last options listed
81+ first_option = find ( '#select2-post_tags_cache-results li:first-child' )
82+ last_option = find ( '#select2-post_tags_cache-results li:last-child' )
83+
84+ # If the first option matches the tag name, and the tag already exists, click it.
85+ if first_option . first ( 'span' ) . text == tag_name && !first_option . text . include? ( 'Create new tag' )
86+ first_option . click
87+ # If we are allowed to create a tag, select the last option from the list, which is always the tag creation.
8888 elsif create_new
89- # The first item returned is not the tag we were looking for (another tag partial match + not existing)
90- # If we are allowed to create a tag, select the last option from the list, which is always the tag creation.
91- last_option = find ( '#select2-post_tags_cache-results li:last-child' )
9289 if last_option . first ( 'span' ) . text == tag_name
9390 last_option . click
9491 else
9592 raise "Tried to select tag #{ tag_name } for creation, but it does not seem to be a presented option."
9693 end
94+ # The first option is not the tag we were looking for, and we are not allowed to create a tag.
9795 else
98- # The first item returned is not the tag we were looking for, and we are not allowed to create a tag.
9996 raise "Expected to find tag with the name #{ tag_name } , " \
10097 'but could not select it from options without creating a new tag.'
10198 end
0 commit comments