U
    g                     @   s   d dl Z d dlZ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	 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)Any)AsyncGenerator)Dict)Optional)Byc                   C   s   t stda d S )Nz"selenium.webdriver.common.bidi.cdp)cdpr    r
   r
   A/tmp/pip-unpacked-wheel-3ysq0mf8/selenium/webdriver/common/log.py
import_cdp    s    r   c                   @   s   e Zd ZdZddddZeeeee	f df dddZ
eeeee	f df ddd	Zeeeee	f df dd
dZdS )LogzThis 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   sf   || _ |j| _|j| _|j| _dtdd d }t|d}|d krRt	d|
d | _d S )N.zmutation-listener.jsz#Failed to load mutation-listener.jsutf8)driversessionr	   devtoolsjoin__name__splitpkgutilget_data
ValueErrordecodestrip_mutation_listener_js)selfr   Zbidi_sessionZ_pkgZ_mutation_listener_js_bytesr
   r
   r   __init__.   s    zLog.__init__c              
   C  sR  | 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  d}|s|d |d	 |d
< |d |d< |d |d< |d |d< dS )aH  Listen for mutation events and emit them as they are found.

        :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;"
        page.enableNruntime.enableZ__webdriver_attributezreturn z*[data-__webdriver_id=target]r   elementnameZattribute_namevalueZcurrent_valueZoldValue	old_value)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_events:   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  Listen 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_listenere   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s|j|jjkr|jjd j|d< |jjd j|d< dS )	a  Listen 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ALLr&   type_args)r   Z
event_typer8   r   consolemessagesr
   r
   r   add_listener}   s    
zLog.add_listener)r   
__module____qualname____doc__r   r   r   r   strr   r4   r7   r@   r
   r
   r
   r   r   &   s    * r   )r.   r   
contextlibr   	importlibr   typingr   r   r   r   Zselenium.webdriver.common.byr   r	   r   r   r
   r
   r
   r   <module>   s   