3
W­E^ð  ã               @   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| |||ƒ}|js0tdd„ |jƒ D ƒƒ r4|S |jrÊ|jd }||_|jsV|jnd}||_| j|_t	j
|j }xP|jdd… D ]>}| j| dt|ƒ|f |f||| j|jdœƒ}	t||	j|	ƒ q„W |S t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   s$   | ]\}}|j d ƒot|ƒV  qdS )ÚtestN)Ú
startswithÚcallable)Ú.0ÚnameÚvalue© r   ú6/tmp/pip-build-78s3knkw/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   s8   | j | jƒƒ }| jr4y
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| jƒd�S | j| jƒ| jƒ d�S )Nr   )Ú	webdriver)Zcommand_executorZdesired_capabilities)r1   )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   Ú__classcell__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‡  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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   rC   r   s    

z'SeleniumTestCase._tearDownClassInternalc             c   s,   | j jdƒ z
dV  W d| j j| jƒ X d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@   rC   r   rD   r8   r   r   )r'   r   r9   _   s   	r9   )Ú	metaclass)r   r!   Ú
contextlibr   Zdjango.testr   r   Zdjango.utils.decoratorsr   Zdjango.utils.module_loadingr   Zdjango.utils.textr   Útyper   r9   r   r   r   r   Ú<module>   s   T