o
    e-6                     @   s   d Z ddlmZ ddlZddlmZ ddlZG dd deZG dd deZ	G d	d
 d
e	Z
G dd de	ZG dd deZG dd de	ZG dd deZG dd deeZG dd dee	ZG dd dee
ZdS )z8User stories, functional tests for AutocompleteTestCase.    )unicode_literalsN)StaleElementReferenceExceptionc                   @   s  e Zd ZdZ									d+ddZejedddd Zd	d
 Z	dd Z
dd Zdd Zejedddd Zdd Zejedddd Zdd Zdd Zejedddd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* ZdS ),	BaseStoryzBase UserStory class.Nc                 C   s   || _ |p| j j| _|p| j j| _|p| j j| _|p| j j| _|p$| j j| _|p+| j j| _|p2| j j| _|	p9| j j| _|
p@| j j	| _	d| j | _
d| j | _d| j
| jf | _d| _dS )z2If any kwarg is None, get it from case attributes.zfieldset.aligned .field-%sz#id_%s%s %sFN)caseclear_selectordropdown_selector
field_nameinput_selectorlabel_selectorlabels_selectormodeloption_selectorwidget_selectorfield_container_selectorfield_selectorfield_clear_selectorin_popup)selfr   r   r   r	   r
   r   r   r   r   r    r   \/var/www/html/Testing_prj/Navya-Bakers/venv/lib/python3.10/site-packages/dal/test/stories.py__init__   s*   
zBaseStory.__init__   )stopc                 C   s8   | j j| j}|D ]}||jv r|  S q
td| )z(Incremental sleep until option appeared.zOption %s not found)r   browserfind_by_cssr   text	Exception)r   r   optionsoptionr   r   r   find_option1   s   
zBaseStory.find_optionc                 C      d| j | jf S z+Return CSS selector for field option label.r   )r   r   r   r   r   r   get_field_label_selector<      z"BaseStory.get_field_label_selectorc                 C      | j jd|    dS )5Clean child nodes before checking (ie. clear option).z
            document.querySelectorAll("%s *").forEach(function(node) {
                node.parentNode.removeChild(node);
            });
            N)r   r   execute_scriptr$   r#   r   r   r   clean_label_from_remove_buton@   
   z'BaseStory.clean_label_from_remove_butonc                 C   s&   | j j|  }|   | |jS )!Return autocomplete widget label.)r   r   r   r$   r)   clean_labelr   r   labelr   r   r   	get_labelJ   s
   zBaseStory.get_labelc                 C   s   |S )z.Given an option text, return the actual label.r   r-   r   r   r   r,   S   s   zBaseStory.clean_labelc                 C   s   ||   ksJ dS )z0Assert that the autocomplete label matches text.N)r/   )r   r   r   r   r   assert_labelW   s   zBaseStory.assert_labelc                 C   s   | j j| j}|d S )$Return the autocomplete field value.value)r   r   r   r   )r   fieldr   r   r   	get_value\   s   zBaseStory.get_valuec                 C   s   |   t|ks
J dS )z1Assart that the actual field value matches value.N)r4   str)r   r2   r   r   r   assert_valuec   s   zBaseStory.assert_valuec                 C      |  | | | dS )z1Assert value is selected and has the given label.N)r0   r6   r   r2   r.   r   r   r   assert_selectionh      
zBaseStory.assert_selectionc                 C   ,   |  || |   | | | | dS )z9Assert value and lebel, submit the form and assert again.N)r9   submitr0   r6   r8   r   r   r   assert_selection_persistsm      
z#BaseStory.assert_selection_persistsc                 C   s   t |t |  ksJ dS )z7Retrying assert that suggestions match expected labels.N)sortedget_suggestions_labels)r   expectedr   r   r   assert_suggestion_labels_aret   s   z&BaseStory.assert_suggestion_labels_arec                 C   sR   d}|rt | jjjdkrntd |d8 }|s| jjjd | jjj_d| _dS )z$Wait for and switch to popup window.
      皙?   TN)lenr   r   windowstimesleepcurrentr   )r   triesr   r   r   switch_to_popupy   s   

zBaseStory.switch_to_popupc                 C   s    | j jjd | j jj_d| _dS )zSwitch back to main window.r   FN)r   r   rH   rK   r   r#   r   r   r   switch_to_main   s   
zBaseStory.switch_to_mainc                 C   s   d}| j s	|d7 }| jj|j}|  d}|r<t| jjjdkr&| j r&nz|j W n   Y n|d8 }t	
d |s| j sF| j  dS dS )zSubmit the form.zinput[type=submit]z[name=_continue]d   rF   g?N)r   r   r   r   firstclickrG   rH   visiblerI   rJ   wait_script)r   selelrL   r   r   r   r<      s&   

zBaseStory.submitc                 C   s   | j d| j| jf  dS )zOpen the autocomplete dropdown.r   N)r   rQ   r   r   r#   r   r   r   toggle_autocomplete   s   zBaseStory.toggle_autocompletec                 C   s$   |    | jj| j |    dS )zRe-open the autocomplete box.N)rV   r   r   is_element_not_present_by_cssr   r#   r   r   r   refresh_autocomplete   s   zBaseStory.refresh_autocompletec                    sP    fdd}dd }| }||du r!t d | }||du sdd |D S )	z7Return the list of suggestions in the autocomplete box.c                      s    j j jS N)r   r   r   r   r   r#   r   r   get_options   s   z.BaseStory.get_suggestions.<locals>.get_optionsc                 S   s8   zd| d j v W S  ty   Y dS  ty   Y dS w )N	Searchingr   T)r   r   
IndexError)r   r   r   r   is_searching   s   z/BaseStory.get_suggestions.<locals>.is_searchingTrE   c                 S   s   g | ]	}|d  |j fqS )r2   )r   .0or   r   r   
<listcomp>   s    z-BaseStory.get_suggestions.<locals>.<listcomp>)rI   rJ   )r   rZ   r]   r   r   r#   r   get_suggestions   s   
zBaseStory.get_suggestionsc                 C   s   dd |   D S )z6Return labels for suggestions in the autocomplete box.c                 S   s   g | ]}|d  qS )rF   r   r^   r   r   r   ra          z4BaseStory.get_suggestions_labels.<locals>.<listcomp>)rb   r#   r   r   r   r@         z BaseStory.get_suggestions_labels)	NNNNNNNNN)__name__
__module____qualname____doc__r   tenacityretrystop_after_delayr    r$   r)   r/   r,   r0   r4   r6   r9   r=   rB   rM   rN   r<   rV   rX   rb   r@   r   r   r   r   r      sD    
!


	


r   c                   @   s    e Zd ZdZdd Zdd ZdS )SelectOptionzUser selects an option.c                 C   sV   | j j| j}t|r|js|   | j | j | j | j	| | 
|  dS )z7Assert that selecting option "text" sets input's value.N)r   r   r   r   rG   rR   rV   assert_visible
enter_textr
   r    rQ   )r   r   dropdownr   r   r   select_option   s   zSelectOption.select_optionc                 C   s   | j | j dS )zClear current option.N)r   rQ   r   r#   r   r   r   clear_option   rd   zSelectOption.clear_optionN)re   rf   rg   rh   rp   rq   r   r   r   r   rl      s    
rl   c                       "   e Zd ZdZd fdd	Z  ZS )InlineSelectOptionz/Same as UserCanSelectOption, in a given inline.Nc                    s   || _ |p|j| _tt| j|fi | d| j| j | jf | _d| j| j | jf | _| jj	j
dj}t| jj	d| j }|| j d k rr|  z| jj	| jt| j t|j W n   Y q@|d7 }|| j d k sGdS dS )z
        Do the same as UserCanSelectOption, in inline inline_number.

        Where inline_related_name should be the related_name option for the
        foreign key used for the InlineModelAdmin.
        z#%s-%s .field-%sz#id_%s-%s-%szAdd anotherz.dynamic-%srF   N)inline_numberinline_related_namesuperrs   r   r	   r   r   r   r   linksfind_by_partial_textrP   rG   r   rQ   replacer5   )r   r   rt   ru   kwargsaddnum	__class__r   r   r      sB   zInlineSelectOption.__init__rY   re   rf   rg   rh   r   __classcell__r   r   r}   r   rs          rs   c                   @      e Zd ZdZdd ZdS )RenameOptionz/User story to rename an option in django admin.c                 C   s\   | j d| j  |   | j jd}| j |d | | j d| |   | 	  dS )z0Click the change button and rename in the popup.z#change_id_%sid_namer2   #id_nameN)
r   rQ   r	   rM   r   
find_by_idassertEqualrn   r<   rN   )r   current_nameadd_keys
name_inputr   r   r   rename_option  s   zRenameOption.rename_optionN)re   rf   rg   rh   r   r   r   r   r   r         r   c                   @   r   )AddAnotherOptionzAdd-another user story.c                 C   sB   | j jd| j   |   | j d| |   |   dS )z9Click the add button and add another option in the popup.z	add_id_%sr   N)	r   r   r   r	   rQ   rM   rn   r<   rN   r   namer   r   r   add_another'  s
   zAddAnotherOption.add_anotherN)re   rf   rg   rh   r   r   r   r   r   r   $  r   r   c                   @   r   )CreateOptionzCreate an option on the fly.c                 C   sF   |    | j| j| | jj| | j| j | jjd dS )zf
        Select the only option after typing name and submit.

        name should be unique.
        z.select2-results__optionsN)	rV   r   rn   r
   r   is_element_present_by_textrQ   r   rW   r   r   r   r   create_option4  s   zCreateOption.create_optionN)re   rf   rg   rh   r   r   r   r   r   r   1  r   r   c                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )MultipleMixinz,Enable multiple choice support with stories.c                 C   r!   r"   )r   r   r#   r   r   r   get_field_labels_selectorG  r%   z'MultipleMixin.get_field_labels_selectorc                 C   r&   )r'   z
            document.querySelectorAll("%s span").forEach(function(node) {
                node.parentNode.removeChild(node);
            });
            N)r   r   r(   r   r#   r   r   r   r)   K  r*   z+MultipleMixin.clean_label_from_remove_butonc                    s,        jj  } fdd|D S )r+   c                    s   g | ]}  |jqS r   )r,   r   )r_   r.   r#   r   r   ra   ]  s    
z,MultipleMixin.get_labels.<locals>.<listcomp>)r)   r   r   r   r   )r   labelsr   r#   r   
get_labelsU  s   
zMultipleMixin.get_labelsc                 C   s&   d| j  }| jj| | jjdS )r1   z
        window.GET_VALUES = [];
        document.querySelectorAll("%s option:checked").forEach(function(opt) {
            GET_VALUES.push(opt.value);
        });
        zwindow.GET_VALUES)r   r   r   r(   evaluate_script)r   scriptr   r   r   
get_valuesb  s
   zMultipleMixin.get_valuesc                 C   s:   |   }|D ]	}| j|| q| jt|t| dS )z#Assert that all labels match texts.N)r   r   assertInr   rG   )r   textsr   r   r   r   r   assert_labelsm  s   zMultipleMixin.assert_labelsc                 C   sH   dd |D }|   }|D ]	}| j|| q| jt|t| dS )z3Assert that the actual field values matches values.c                 S   s   g | ]}t |qS r   )r5   )r_   vr   r   r   ra   x  rc   z/MultipleMixin.assert_values.<locals>.<listcomp>N)r   r   r   r   rG   )r   valuestext_valuesactual_valuesactual_valuer   r   r   assert_valuesv  s   zMultipleMixin.assert_valuesc                 C   r7   )z)Assert selections have values and labels.N)r   r   r   r   r   r   r   r   r9     r:   zMultipleMixin.assert_selectionc                 C   r;   )z@Do the same as above, but also submits the form and check again.N)r9   r<   r   r   r   r   r   r   r=     r>   z'MultipleMixin.assert_selection_persistsN)re   rf   rg   rh   r   r)   r   r   r   r   r9   r=   r   r   r   r   r   D  s    
	r   c                   @      e Zd ZdZdS )CreateOptionMultiple"Multiple version of CreateOptions.Nre   rf   rg   rh   r   r   r   r   r         r   c                   @   r   )SelectOptionMultipler   Nr   r   r   r   r   r     r   r   c                       rr   )InlineSelectOptionMultiplez(Multiple options for InlineSelectOption.Nc                    s4   t t| j||fd|i| d| j| jf | _dS )z1Set input_selector with field_container_selector.ru   r   N)rv   r   r   r   r
   )r   r   rt   ru   rz   r}   r   r   r     s   
z#InlineSelectOptionMultiple.__init__rY   r   r   r   r}   r   r     r   r   )rh   
__future__r   rI   selenium.common.exceptionsr   ri   objectr   rl   rs   r   r   r   r   r   r   r   r   r   r   r   <module>   s      @2L