U
    8ÄT_ð  ã                   @   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 G dd„ deeƒƒZed	ƒG d
d„ deed�ƒZdS )é    N)Úcontextmanager)ÚLiveServerTestCaseÚtag)Úclassproperty©Úimport_string)Úcapfirstc                       sd   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‡  ZS )ÚSeleniumTestCaseBaseNFc           
         sÒ   t ƒ  | |||¡}|js.tdd„ | ¡ D ƒƒs2|S |jrÄ|jd }||_|jsT|jnd}||_| j|_t	j
|j }|jdd… D ]>}|  | dt|ƒ|f |f||| j|jdœ¡}	t||	j|	ƒ q€|S t 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   s$   | ]\}}|  d ¡ot|ƒV  qdS )ÚtestN)Ú
startswithÚcallable)Ú.0ÚnameÚvalue© r   ú8/tmp/pip-unpacked-wheel-dv63caxw/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      s0    
üüzSeleniumTestCaseBase.__new__c                 C   s   t d| ƒS )Nz)selenium.webdriver.%s.webdriver.WebDriverr   ©r$   r   r   r   r   Úimport_webdriver?   s    z%SeleniumTestCaseBase.import_webdriverc                 C   s   t d| ƒS )Nz%selenium.webdriver.%s.options.Optionsr   r*   r   r   r   Úimport_optionsC   s    z#SeleniumTestCaseBase.import_optionsc                 C   s   ddl m} t|| ¡ ƒ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   s8   |   | j¡ƒ }| jr4z
d|_W n tk
r2   Y n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| j |  | j¡d�S |  | j¡|  ¡ d�S )Nr   )Ú	webdriver)Zcommand_executorZdesired_capabilities)r4   )r   Úseleniumr6   ZRemoter0   r   r+   r5   )r3   r6   r   r   r   Úcreate_webdriverU   s    
þz%SeleniumTestCaseBase.create_webdriver)r!   r   Ú__qualname__r   r   r   r   r1   r   Úclassmethodr+   r,   r0   r5   r8   Ú__classcell__r   r   r(   r   r	      s   (


	r	   r7   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‡  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                    s&   |   ¡ | _| j | j¡ tƒ  ¡  d S rA   )r8   r7   Úimplicitly_waitÚimplicit_waitr   Ú
setUpClassr?   r(   r   r   rE   l   s    
zSeleniumTestCase.setUpClassc                    s"   t | dƒr| j ¡  tƒ  ¡  d S )Nr7   )Úhasattrr7   Úquitr   Ú_tearDownClassInternalr?   r(   r   r   rH   r   s    

z'SeleniumTestCase._tearDownClassInternalc              	   c   s,   | j  d¡ z
dV  W 5 | j  | j¡ X dS )z"Disable the default implicit wait.r   N)r7   rC   rD   )r3   r   r   r   Údisable_implicit_wait{   s    
z&SeleniumTestCase.disable_implicit_wait)r!   r   r9   rD   r   r   r@   rB   r:   rE   rH   r   rI   r;   r   r   r(   r   r<   _   s   

r<   )Ú	metaclass)r   r"   Ú
contextlibr   Zdjango.testr   r   Zdjango.utils.functionalr   Zdjango.utils.module_loadingr   Zdjango.utils.textr   Útyper	   r<   r   r   r   r   Ú<module>   s   T