
vÅÏ^ð  ã               @   s¶   d  d l  Z  d  d l Z d  d l m Z d  d l m Z m Z d  d l m Z d  d l	 m
 Z
 d  d l m Z Gd d „  d e e ƒ ƒ Z e d	 ƒ Gd
 d „  d e d e ƒƒ Z d S)é    N)Úcontextmanager)ÚLiveServerTestCaseÚtag)Úclassproperty)Úimport_string)Úcapfirstc                   sŽ   e  Z d  Z g  Z d Z d Z d Z d Z ‡  f d d †  Z e	 d d „  ƒ Z
 e	 d d „  ƒ Z e	 d	 d
 „  ƒ Z d d „  Z d d „  Z ‡  S)ÚSeleniumTestCaseBaseNFc       
         s5  t  ƒ  j |  | | | ƒ } | j sD t d d „  | j ƒ  Dƒ ƒ rH | S| j r"| j d } | | _ | j sy | j n d } | | _ |  j | _ t	 j
 | j } xw | j d d … D]b } |  j |  d t | ƒ | f | f d | d	 | d
 |  j d | j i ƒ }	 t | |	 j |	 ƒ q¸ W| St j d ƒ | ƒ S)z¤
        Dynamically create new classes and add them to the test module when
        multiple browsers specs are provided (e.g. --selenium=firefox,chrome).
        c             s   s0   |  ]& \ } } | j  d  ƒ o' t | ƒ Vq d S)ÚtestN)Ú
startswithÚcallable)Ú.0ÚnameÚvalue© r   ú6/tmp/pip-build-8lau8j11/django/django/test/selenium.pyú	<genexpr>   s    z/SeleniumTestCaseBase.__new__.<locals>.<genexpr>r   z0.0.0.0é   Nz%s%sÚbrowserÚhostÚexternal_hostÚ
__module__zNo browsers specified.)ÚsuperÚ__new__r   ÚanyÚitemsÚbrowsersÚselenium_hubr   r   ÚsysÚmodulesr   r   ÚsetattrÚ__name__ÚunittestÚskip)
Úclsr   ÚbasesÚattrsZ
test_classZfirst_browserr   Úmoduler   Zbrowser_test_class)Ú	__class__r   r   r      s,    )				zSeleniumTestCaseBase.__new__c             C   s   t  d | ƒ S)Nz)selenium.webdriver.%s.webdriver.WebDriver)r   )r#   r   r   r   r   Úimport_webdriver?   s    z%SeleniumTestCaseBase.import_webdriverc             C   s   t  d | ƒ S)Nz%selenium.webdriver.%s.options.Options)r   )r#   r   r   r   r   Úimport_optionsC   s    z#SeleniumTestCaseBase.import_optionsc             C   s#   d d l  m } t | | j ƒ  ƒ S)Nr   )ÚDesiredCapabilities)Z.selenium.webdriver.common.desired_capabilitiesr*   ÚgetattrÚupper)r#   r   r*   r   r   r   Úget_capabilityG   s    z#SeleniumTestCaseBase.get_capabilityc             C   sD   |  j  |  j ƒ ƒ  } |  j r@ y d | _ Wn t k
 r? Yn X| S)NT)r)   r   ÚheadlessÚAttributeError)ÚselfÚoptionsr   r   r   Úcreate_optionsL   s    	z#SeleniumTestCaseBase.create_optionsc             C   s]   |  j  r> d d l m } | j d |  j  d |  j |  j ƒ ƒ S|  j |  j ƒ d |  j ƒ  ƒ S)Nr   )Ú	webdriverZcommand_executorZdesired_capabilitiesr1   )r   Úseleniumr3   ZRemoter-   r   r(   r2   )r0   r3   r   r   r   Úcreate_webdriverU   s    			z%SeleniumTestCaseBase.create_webdriver)r    r   Ú__qualname__r   r   r   r   r.   r   Úclassmethodr(   r)   r-   r2   r5   r   r   )r'   r   r      s   (	r   r4   c                   s‚   e  Z d  Z d Z d Z e d d „  ƒ Z e d d „  ƒ Z e ‡  f d d †  ƒ Z	 e ‡  f d	 d
 †  ƒ Z
 e d d „  ƒ Z ‡  S)ÚSeleniumTestCaseé
   Nc             C   s    d |  j  p |  j |  j j f S)Nzhttp://%s:%s)r   r   Zserver_threadÚport)r#   r   r   r   Úlive_server_urld   s    z SeleniumTestCase.live_server_urlc             C   s   |  j  p |  j S)N)r   r   )r#   r   r   r   Úallowed_hosth   s    zSeleniumTestCase.allowed_hostc                s3   |  j  ƒ  |  _ |  j j |  j ƒ t ƒ  j ƒ  d  S)N)r5   r4   Úimplicitly_waitÚimplicit_waitr   Ú
setUpClass)r#   )r'   r   r   r?   l   s    zSeleniumTestCase.setUpClassc                s-   t  |  d ƒ r |  j j ƒ  t ƒ  j ƒ  d  S)Nr4   )Úhasattrr4   Úquitr   Ú_tearDownClassInternal)r#   )r'   r   r   rB   r   s    z'SeleniumTestCase._tearDownClassInternalc             c   s4   |  j  j d ƒ z	 d VWd |  j  j |  j ƒ Xd S)z"Disable the default implicit wait.r   N)r4   r=   r>   )r0   r   r   r   Údisable_implicit_wait{   s    	z&SeleniumTestCase.disable_implicit_wait)r    r   r6   r>   r   r   r;   r<   r7   r?   rB   r   rC   r   r   )r'   r   r8   _   s   	r8   Ú	metaclass)r   r!   Ú
contextlibr   Zdjango.testr   r   Zdjango.utils.decoratorsr   Zdjango.utils.module_loadingr   Zdjango.utils.textr   Útyper   r8   r   r   r   r   Ú<module>   s   T