
^Q\4                 @   s7  d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l Z Gd d   d e	  Z
 Gd d	   d	 e
  Z Gd
 d   d e  Z Gd d   d e  Z Gd d   d e
  Z Gd d   d e  Z Gd d   d e	  Z Gd d   d e e  Z Gd d   d e e  Z Gd d   d e e  Z d S)z8User stories, functional tests for AutocompleteTestCase.    )unicode_literalsN)six)StaleElementReferenceExceptionc               @   s  e  Z d  Z d Z d d d d d d d d d d d 	 Z e j d e j d   d d    Z d	 d
   Z	 d d   Z
 d d   Z d d   Z e j d e j d   d d    Z d d   Z e j d e j d   d d    Z d d   Z d d   Z e j d e j 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 |  _ | pB |  j  j |  _ | pW |  j  j |  _ | pl |  j  j |  _ | p |  j  j |  _ | p |  j  j |  _ |	 p |  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_%sz%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   E/var/www/dbchiro/venv/lib/python3.4/site-packages/dal/test/stories.py__init__   s$    	zBaseStory.__init__stop   c             C   sP   |  j  j j |  j  } x! | D] } | | j k r | Sq Wt d |   d S)z(Incremental sleep until option appeared.zOption %s not foundN)r   browserfind_by_cssr   text	Exception)r   r   optionsoptionr   r   r   find_option3   s
    zBaseStory.find_optionc             C   s   d |  j  |  j f S)z+Return CSS selector for field option label.z%s %s)r   r   )r   r   r   r   get_field_label_selector>   s    z"BaseStory.get_field_label_selectorc             C   s!   |  j  j j d |  j    d S)z5Clean child nodes before checking (ie. clear option).z$("%s *").remove()N)r   r   execute_scriptr!   )r   r   r   r   clean_label_from_remove_butonB   s    z'BaseStory.clean_label_from_remove_butonc             C   s>   |  j  j j |  j    } |  j   |  j t j | j   S)z!Return autocomplete widget label.)	r   r   r   r!   r#   clean_labelr   	text_typer   )r   labelr   r   r   	get_labelH   s    
zBaseStory.get_labelc             C   s   | S)z.Given an option text, return the actual label.r   )r   r&   r   r   r   r$   Q   s    zBaseStory.clean_labelc             C   s.   t  j |  t  j |  j    k s* t  d S)z0Assert that the autocomplete label matches text.N)r   r%   r'   AssertionError)r   r   r   r   r   assert_labelU   s    zBaseStory.assert_labelc             C   s    |  j  j j |  j  } | d S)z$Return the autocomplete field value.value)r   r   r   r   )r   fieldr   r   r   	get_valueZ   s    zBaseStory.get_valuec             C   s%   |  j    t j |  k s! t  d S)z1Assart that the actual field value matches value.N)r,   r   r%   r(   )r   r*   r   r   r   assert_valuea   s    zBaseStory.assert_valuec             C   s   |  j  |  |  j |  d S)z1Assert value is selected and has the given label.N)r)   r-   )r   r*   r&   r   r   r   assert_selectionf   s    zBaseStory.assert_selectionc             C   s8   |  j  | |  |  j   |  j |  |  j |  d S)z9Assert value and lebel, submit the form and assert again.N)r.   submitr)   r-   )r   r*   r&   r   r   r   assert_selection_persistsk   s    
z#BaseStory.assert_selection_persistsc             C   s(   t  |  t  |  j    k s$ t  d S)z7Retrying assert that suggestions match expected labels.N)sortedget_suggestions_labelsr(   )r   expectedr   r   r   assert_suggestion_labels_arer   s    z&BaseStory.assert_suggestion_labels_arec             C   s,   |  j  j j d |  j  j j _ d |  _ d S)zSwitch to popup window.   TN)r   r   windowscurrentr   )r   r   r   r   switch_to_popupw   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   r6   r7   r   )r   r   r   r   switch_to_main|   s    zBaseStory.switch_to_mainc          	   C   s   d } |  j  s | d 7} n  |  j j j |  j } | j   d } x` | r t |  j j j  d k ru |  j  ru Pn  y | j Wn	 PYn X| d 8} t	 j
 d  qG W|  j  s |  j j   n  d S)zSubmit the form.zinput[type=submit]z[name=_continue]d   r5   g?N)r   r   r   r   firstclicklenr6   visibletimesleepZwait_script)r   seleltriesr   r   r   r/      s"    	
	$
	zBaseStory.submitc             C   s$   |  j  j d |  j |  j f  d S)zOpen the autocomplete dropdown.z%s %sN)r   r<   r   r   )r   r   r   r   toggle_autocomplete   s    zBaseStory.toggle_autocompletec             C   s.   |  j    |  j j j |  j  |  j    d S)zRe-open the autocomplete box.N)rD   r   r   is_element_not_present_by_cssr   )r   r   r   r   refresh_autocomplete   s    
zBaseStory.refresh_autocompletec                sg     f d d   } d d   } |   } x, | |  d k rU t  j d  |   } q* Wd d   | D S)	z7Return the list of suggestions in the autocomplete box.c                  s     j  j j   j  S)N)r   r   r   r   r   )r   r   r   get_options   s    z.BaseStory.get_suggestions.<locals>.get_optionsc             S   sG   y d |  d j  k SWn+ t k
 r- d SYn t k
 rB d SYn Xd  S)NZ	Searchingr   T)r   r   
IndexError)r   r   r   r   is_searching   s    z/BaseStory.get_suggestions.<locals>.is_searchingTg?c             S   s#   g  |  ] } | d  | j  f  q S)r*   )r   ).0or   r   r   
<listcomp>   s   	 z-BaseStory.get_suggestions.<locals>.<listcomp>)r?   r@   )r   rG   rI   r   r   )r   r   get_suggestions   s    	zBaseStory.get_suggestionsc             C   s   d d   |  j    D S)z6Return labels for suggestions in the autocomplete box.c             S   s   g  |  ] } | d   q S)r5   r   )rJ   rK   r   r   r   rL      s   	 z4BaseStory.get_suggestions_labels.<locals>.<listcomp>)rM   )r   r   r   r   r2      s    z BaseStory.get_suggestions_labels)__name__
__module____qualname____doc__r   tenacityretryZstop_after_delayr    r!   r#   r'   r$   r)   r,   r-   r.   r0   r4   r8   r9   r/   rD   rF   rM   r2   r   r   r   r   r      s8   '	'''r   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)SelectOptionzUser selects an option.c             C   s|   |  j  j j |  j  } t |  s/ | j r< |  j   n  |  j  j |  j  |  j  j |  j	 |  |  j
 |  j   d S)z7Assert that selecting option "text" sets input's value.N)r   r   r   r   r=   r>   rD   Zassert_visible
enter_textr
   r    r<   )r   r   Zdropdownr   r   r   select_option   s    zSelectOption.select_optionc             C   s   |  j  j |  j  d S)zClear current option.N)r   r<   r   )r   r   r   r   clear_option   s    zSelectOption.clear_optionN)rN   rO   rP   rQ   rV   rW   r   r   r   r   rT      s   
rT   c                   s+   e  Z d  Z d Z d   f d d  Z   S)InlineSelectOptionz/Same as UserCanSelectOption, in a given inline.Nc                s$  | |  _  | p | j |  _ t t |   j | |  d |  j |  j  |  j f |  _ d |  j |  j  |  j f |  _ |  j j	 j
 d  j } t |  j j	 j d |  j   } xq | |  j  d k  r| j   y8 |  j j	 j |  j j t |  j   t |    j Wn w Yn X| d 7} q Wd S)z
        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-%sr5   N)inline_numberinline_related_namesuperrX   r   r	   r   r   r   r   Zfind_link_by_partial_textr;   r=   r   r<   replacestr)r   r   rY   rZ   kwargsaddnum)	__class__r   r   r      s0    	
	zInlineSelectOption.__init__)rN   rO   rP   rQ   r   r   r   )ra   r   rX      s   rX   c               @   s"   e  Z d  Z d Z d d   Z d S)RenameOptionz/User story to rename an option in django admin.c             C   sx   |  j  j d |  j  |  j   |  j  j j d  } |  j  j | d |  |  j  j d |  |  j   |  j	   d S)z0Click the change button and rename in the popup.z#change_id_%sZid_namer*   z#id_nameN)
r   r<   r	   r8   r   
find_by_idassertEqualsrU   r/   r9   )r   Zcurrent_nameZadd_keysZ
name_inputr   r   r   rename_option
  s    
	
zRenameOption.rename_optionN)rN   rO   rP   rQ   re   r   r   r   r   rb     s   rb   c               @   s"   e  Z d  Z d Z d d   Z d S)AddAnotherOptionzAdd-another user story.c             C   sU   |  j  j j d |  j  j   |  j   |  j  j d |  |  j   |  j   d S)z9Click the add button and add another option in the popup.z	add_id_%sz#id_nameN)	r   r   rc   r	   r<   r8   rU   r/   r9   )r   namer   r   r   add_another  s
     

zAddAnotherOption.add_anotherN)rN   rO   rP   rQ   rh   r   r   r   r   rf     s   rf   c               @   s"   e  Z d  Z d Z d d   Z d S)CreateOptionzCreate an option on the fly.c             C   s]   |  j    |  j j |  j |  |  j 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)	rD   r   rU   r
   r   Zis_element_present_by_textr<   r   rE   )r   rg   r   r   r   create_option+  s    
zCreateOption.create_optionN)rN   rO   rP   rQ   rj   r   r   r   r   ri   (  s   ri   c               @   sv   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   s   d |  j  |  j f S)z+Return CSS selector for field option label.z%s %s)r   r   )r   r   r   r   get_field_labels_selector>  s    z'MultipleMixin.get_field_labels_selectorc             C   s!   |  j  j j d |  j    d S)z5Clean child nodes before checking (ie. clear option).z$("%s span").remove()N)r   r   r"   rl   )r   r   r   r   r#   B  s    z+MultipleMixin.clean_label_from_remove_butonc                s<     j      j j j   j    }   f d d   | D S)z!Return autocomplete widget label.c                s+   g  |  ]! }   j  t j | j    q Sr   )r$   r   r%   r   )rJ   r&   )r   r   r   rL   Q  s   	z,MultipleMixin.get_labels.<locals>.<listcomp>)r#   r   r   r   rl   )r   labelsr   )r   r   
get_labelsH  s
    
zMultipleMixin.get_labelsc             C   s3   d |  j  } |  j j j |  |  j j j d  S)z$Return the autocomplete field value.z
        window.GET_VALUES = [];
        $('%s option:selected').each(function() {
            GET_VALUES.push($(this).attr('value'));
        });
        zwindow.GET_VALUES)r   r   r   r"   Zevaluate_script)r   scriptr   r   r   
get_valuesU  s    zMultipleMixin.get_valuesc             C   sS   |  j    } x! | D] } |  j j | |  q W|  j j t |  t |   d S)z#Assert that all labels match texts.N)rn   r   assertInrd   r=   )r   Ztextsrm   r   r   r   r   assert_labels`  s    zMultipleMixin.assert_labelsc             C   sf   d d   | D } |  j    } x! | D] } |  j j | |  q& W|  j j t |  t |   d S)z3Assart that the actual field values matches values.c             S   s   g  |  ] } t  j |   q Sr   )r   r%   )rJ   vr   r   r   rL   k  s   	 z/MultipleMixin.assert_values.<locals>.<listcomp>N)rp   r   rq   rd   r=   )r   valuesZtext_valuesZactual_valuesZactual_valuer   r   r   assert_valuesi  s    	zMultipleMixin.assert_valuesc             C   s   |  j  |  |  j |  d S)z)Assert selections have values and labels.N)rr   ru   )r   rt   rm   r   r   r   r.   v  s    zMultipleMixin.assert_selectionc             C   s8   |  j  | |  |  j   |  j |  |  j |  d S)z9Same as above, but also submits the form and check again.N)r.   r/   rr   ru   )r   rt   rm   r   r   r   r0   {  s    
z'MultipleMixin.assert_selection_persistsN)rN   rO   rP   rQ   rl   r#   rn   rp   rr   ru   r.   r0   r   r   r   r   rk   ;  s   	rk   c               @   s   e  Z d  Z d Z d S)CreateOptionMultiplez"Multiple version of CreateOptions.N)rN   rO   rP   rQ   r   r   r   r   rv     s   rv   c               @   s   e  Z d  Z d Z d S)SelectOptionMultiplez"Multiple version of CreateOptions.N)rN   rO   rP   rQ   r   r   r   r   rw     s   rw   c                   s+   e  Z d  Z d Z d   f d d  Z   S)InlineSelectOptionMultiplez(Multiple options for InlineSelectOption.Nc                s?   t  t |   j | | d | | d |  j |  j f |  _ d S)z1Set input_selector with field_container_selector.rZ   z%s %sN)r[   rx   r   r   r
   )r   r   rY   rZ   r^   )ra   r   r   r     s    z#InlineSelectOptionMultiple.__init__)rN   rO   rP   rQ   r   r   r   )ra   r   rx     s   rx   )rQ   
__future__r   r?   Zdjango.utilsr   Zselenium.common.exceptionsr   rR   objectr   rT   rX   rb   rf   ri   rk   rv   rw   rx   r   r   r   r   <module>   s   2H