
u^                 @   s   d  d l  m Z d  d l m Z d  d l m Z d  d l m Z d  d l m	 Z
 Gd d   d e  Z e d d	 d
 i  Gd d   d e e   Z d S)    )StaticLiveServerTestCase)modify_settings)SeleniumTestCase)MiddlewareMixin)gettextc               @   s"   e  Z d  Z d Z d d   Z d S)CSPMiddlewarez5The admin's JavaScript should be compatible with CSP.c             C   s   d | d <| S)Nzdefault-src 'self'zContent-Security-Policy )selfrequestresponser   r   </tmp/pip-build-8lau8j11/django/django/contrib/admin/tests.pyprocess_response
   s    
zCSPMiddleware.process_responseN)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   Z
MIDDLEWAREappendz(django.contrib.admin.tests.CSPMiddlewarec               @   s   e  Z d  Z d d d d d g Z d d d  Z d	 d d
 d  Z d d d  Z d d d  Z d d d  Z d d d  Z	 d d d  Z
 d d   Z d d d  Z d d   Z d d   Z d d    Z d! d"   Z d# d$   Z d% d&   Z d' S)(AdminSeleniumTestCasezdjango.contrib.adminzdjango.contrib.authzdjango.contrib.contenttypeszdjango.contrib.sessionszdjango.contrib.sites
   c             C   s-   d d l  m } | |  j |  j |  d S)a%  
        Block the execution of the tests until the specified callback returns a
        value that is not falsy. This method can be called, for example, after
        clicking a link or submitting a form. See the other public methods that
        call this function for more details.
        r   )WebDriverWaitN)Zselenium.webdriver.support.waitr   seleniumZuntil)r	   callbacktimeoutr   r   r   r   
wait_until   s    z AdminSeleniumTestCase.wait_until   c                s    |  j    f d d   |  d S)z
        Block until `num_windows` are present (usually 2, but can be
        overridden in the case of pop-ups opening other pop-ups).
        c                s   t  |  j    k S)N)lenZwindow_handles)d)num_windowsr   r   <lambda>)   s    z6AdminSeleniumTestCase.wait_for_popup.<locals>.<lambda>N)r   )r	   r   r   r   )r   r   wait_for_popup$   s    z$AdminSeleniumTestCase.wait_for_popupc             C   sF   d d l  m } d d l m } |  j | j | j | f  |  d S)zB
        Block until a CSS selector is found on the page.
        r   )By)expected_conditionsN)selenium.webdriver.common.byr    selenium.webdriver.supportr!   r   Zpresence_of_element_locatedCSS_SELECTOR)r	   css_selectorr   r    ecr   r   r   wait_for+   s
    zAdminSeleniumTestCase.wait_forc             C   sI   d d l  m } d d l m } |  j | j | j | f |  |  d S)zD
        Block until the text is found in the CSS selector.
        r   )r    )r!   N)r"   r    r#   r!   r   Ztext_to_be_present_in_elementr$   )r	   r%   textr   r    r&   r   r   r   wait_for_text6   s    z#AdminSeleniumTestCase.wait_for_textc             C   sI   d d l  m } d d l m } |  j | j | j | f |  |  d S)zE
        Block until the value is found in the CSS selector.
        r   )r    )r!   N)r"   r    r#   r!   r   Z#text_to_be_present_in_element_valuer$   )r	   r%   r(   r   r    r&   r   r   r   wait_for_valueB   s    z$AdminSeleniumTestCase.wait_for_valuec             C   sF   d d l  m } d d l m } |  j | j | j | f  |  d S)zS
        Block until the element described by the CSS selector is visible.
        r   )r    )r!   N)r"   r    r#   r!   r   Zvisibility_of_element_locatedr$   )r	   r%   r   r    r&   r   r   r   wait_until_visibleN   s
    z(AdminSeleniumTestCase.wait_until_visiblec             C   sF   d d l  m } d d l m } |  j | j | j | f  |  d S)zU
        Block until the element described by the CSS selector is invisible.
        r   )r    )r!   N)r"   r    r#   r!   r   Zinvisibility_of_element_locatedr$   )r	   r%   r   r    r&   r   r   r   wait_until_invisibleY   s
    z*AdminSeleniumTestCase.wait_until_invisiblec             C   s:   d d l  m } y |  j d  Wn | k
 r5 Yn Xd S)z7
        Block until page has started to load.
        r   )TimeoutExceptionbodyN)selenium.common.exceptionsr-   r'   )r	   r-   r   r   r   wait_page_loadedd   s
    z&AdminSeleniumTestCase.wait_page_loadedz/admin/c             C   s   |  j  j d |  j | f  |  j  j d  } | j |  |  j  j d  } | j |  t d  } |  j  j d |  j   |  j   d S)z&
        Log in to the admin.
        z%s%susernamepasswordzLog inz//input[@value="%s"]N)	r   getZlive_server_urlZfind_element_by_nameZ	send_keys_Zfind_element_by_xpathZclickr0   )r	   r1   r2   Z	login_urlZusername_inputZpassword_inputZ
login_textr   r   r   admin_loginr   s    	z!AdminSeleniumTestCase.admin_loginc             C   s   |  j  j d | | f  S)z
        Return the value for the CSS attribute of a DOM element specified by
        the given selector. Uses the jQuery that ships with Django.
        z$return django.jQuery("%s").css("%s"))r   Zexecute_script)r	   selector	attributer   r   r   get_css_value   s    	z#AdminSeleniumTestCase.get_css_valuec             C   sj   d d l  m } |  j j d |  } x' | D] } | j d  | k r- | Sq- W| d | | f   d S)z
        Return the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   )NoSuchElementExceptionz%s > optionvaluezOption "%s" not found in "%s"N)r/   r9   r   find_elements_by_css_selectorget_attribute)r	   r6   r:   r9   optionsoptionr   r   r   get_select_option   s    z'AdminSeleniumTestCase.get_select_optionc                s   | rX |  j  j    } g  } x$ | D] } | j | j d   q% W|  j | |  n- |  j    |  j   f d d    Wd  QRXd  S)Nr:   c                s   |  j     S)N)r;   )Zdriver)options_selectorr   r   r      s    z<AdminSeleniumTestCase._assertOptionsValues.<locals>.<lambda>)r   r;   r   r<   ZassertEqualZdisable_implicit_waitr   )r	   r@   valuesr=   Zactual_valuesr>   r   )r@   r   _assertOptionsValues   s    z*AdminSeleniumTestCase._assertOptionsValuesc             C   s   |  j  d | |  d S)z{
        Assert that the <SELECT> widget identified by `selector` has the
        options with the given `values`.
        z%s > optionN)rB   )r	   r6   rA   r   r   r   assertSelectOptions   s    z)AdminSeleniumTestCase.assertSelectOptionsc             C   s   |  j  d | |  d S)z
        Assert that the <SELECT> widget identified by `selector` has the
        selected options with the given `values`.
        z%s > option:checkedN)rB   )r	   r6   rA   r   r   r   assertSelectedOptions   s    z+AdminSeleniumTestCase.assertSelectedOptionsc             C   s(   |  j  j |  j d  j |  d k S)zh
        Return True if the element identified by `selector` has the CSS class
        `klass`.
        class   )r   Zfind_element_by_css_selectorr<   find)r	   r6   klassr   r   r   has_css_class   s    z#AdminSeleniumTestCase.has_css_classN)r   r   r   Zavailable_appsr   r   r'   r)   r*   r+   r,   r0   r5   r8   r?   rB   rC   rD   rJ   r   r   r   r   r      s(   	
r   N)Z"django.contrib.staticfiles.testingr   Zdjango.testr   Zdjango.test.seleniumr   Zdjango.utils.deprecationr   Zdjango.utils.translationr   r4   r   r   r   r   r   r   <module>   s   