U
    ¾¸b²  ã                   @   sZ   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ da	dd„ Z
G dd„ dƒZdS )	é    N)Úasynccontextmanager)Úimport_module)ÚByc                   C   s   t stdƒa d S )Nz"selenium.webdriver.common.bidi.cdp)Úcdpr   © r   r   úA/tmp/pip-unpacked-wheel-3sqd7awy/selenium/webdriver/common/log.pyÚ
import_cdp   s    r   c                   @   sN   e Zd ZdZddœdd„Zeedœdd„ƒZedd	„ ƒZeedœd
d„ƒZ	dS )ÚLogzÓ
        This class allows access to logging APIs that use the new WebDriver Bidi
        protocol.

        This class is not to be used directly and should be used from the webdriver
        base classes.
    N)Úreturnc                 C   s`   t jdkst‚|| _|j| _|j| _|j| _d t 	d¡d d… ¡}t
 |d¡ d¡ ¡ | _d S )N©é   é   Ú.éÿÿÿÿzmutation-listener.jsÚutf8)ÚsysÚversion_infoÚAssertionErrorÚdriverÚsessionr   ÚdevtoolsÚjoinÚ__name__ÚsplitÚpkgutilÚget_dataÚdecodeÚstripÚ_mutation_listener_js)Úselfr   Zbidi_sessionZ_pkgr   r   r   Ú__init__-   s    zLog.__init__c              
   C  s^  t jdkst‚| j d¡}| | jj ¡ ¡I dH  | j d¡}| | jj	 ¡ ¡I dH  | | jj	 
d¡¡I dH  | j | j¡ | | jj | j¡¡I dH }| j | j|¡ | j d| j› ¡ i }| | jj	j¡4 I dH š}|V  W 5 Q I dH R X t |jj¡}| j tjd|d › ¡}|s*| d¡ |d	 |d
< |d |d< |d |d< |d |d< dS )aQ  
        Listens for mutation events and emits them as it finds them

        :Usage:
             ::
               async with driver.log.mutation_events() as event:
                    pages.load("dynamic.html")
                    driver.find_element(By.ID, "reveal").click()
                    WebDriverWait(driver, 5)                        .until(EC.visibility_of(driver.find_element(By.ID, "revealed")))

                assert event["attribute_name"] == "style"
                assert event["current_value"] == ""
                assert event["old_value"] == "display:none;"

        r   úpage.enableNúruntime.enableZ__webdriver_attributezreturn z*[data-__webdriver_id=Útargetr   ÚelementÚnameZattribute_nameÚvalueZcurrent_valueZoldValueÚ	old_value)r   r   r   r   Úget_session_contextÚexecuter   ÚpageÚenableÚruntimeZadd_bindingr   Z
pin_scriptr   Z&add_script_to_evaluate_on_new_documentZexecute_scriptÚwait_forZBindingCalledÚjsonÚloadsr&   ÚpayloadZfind_elementsr   ZCSS_SELECTORÚappend)r   r*   r,   Z
script_keyÚeventZevntr0   Úelementsr   r   r   Úmutation_events6   s*    
zLog.mutation_eventsc              
   C  s    | j  d¡}| | jj ¡ ¡I dH  | j  d¡}| | jj ¡ ¡I dH  | jj dd¡}| | jjj¡4 I dH š}|V  W 5 Q I dH R X |j	j
|_
|j	j|_dS )aª  
        Listens for JS errors and when the contextmanager exits check if there were JS Errors

        :Usage:
             ::

                async with driver.log.add_js_error_listener() as error:
                    driver.find_element(By.ID, "throwing-mouseover").click()
                assert bool(error)
                assert error.exception_details.stack_trace.call_frames[0].function_name == "onmouseover"
        r!   Nr"   )r   r(   r)   r   r*   r+   r,   ZExceptionThrownr-   r&   Ú	timestampZexception_details)r   r   Zjs_exceptionÚ	exceptionr   r   r   Úadd_js_error_listenera   s    
zLog.add_js_error_listenerc              
   C  sî   ddl m} | j d¡}| | jj ¡ ¡I dH  | j d¡}| | jj ¡ ¡I dH  dddœ}| 	| jjj
¡4 I dH š}|V  W 5 Q I dH R X ||jkr¼|jjd j|d< |jjd j|d< ||jkrê|jjd j|d< |jjd j|d< dS )	a‹  
        Listens for certain events that are passed in.

        :Args:
         - event_type: The type of event that we want to look at.

         :Usage:
             ::

                async with driver.log.add_listener(Console.log) as messages:
                    driver.execute_script("console.log('I like cheese')")
                assert messages["message"] == "I love cheese"

        r   )ÚConsoler!   Nr"   )ÚmessageÚlevelr9   r:   )Z&selenium.webdriver.common.bidi.consoler8   r   r(   r)   r   r*   r+   r,   r-   ZConsoleAPICalledÚERRORr&   ÚargsÚtype_ÚALL)r   Z
event_typer8   r   ÚconsoleÚmessagesr   r   r   Úadd_listenery   s     þ

zLog.add_listener)
r   Ú
__module__Ú__qualname__Ú__doc__r    r   Údictr4   r7   rA   r   r   r   r   r	   $   s   	*
r	   )r.   r   r   Ú
contextlibr   Ú	importlibr   Zselenium.webdriver.common.byr   r   r   r	   r   r   r   r   Ú<module>   s   