o
    US`cO                     @   sR   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 d S )Nz"selenium.webdriver.common.bidi.cdp)cdpr    r   r   H/usr/local/lib/python3.10/dist-packages/selenium/webdriver/common/log.py
import_cdp   s   r   c                   @   sJ   e Zd ZdZdddZedefddZedd	 Zedef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.
    returnNc                 C   sR   || _ |j| _|j| _|j| _dtdd d }t|d	d
 | _d S )N.zmutation-listener.jsutf8)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  sj  | 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 d  I dH  n1 I dH s{w   Y  t|jj}| jtjd|d  }|s|d |d |d	< |d
 |d< |d |d< |d |d< dS )aQ  
        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=targetr   elementnameZattribute_namevalue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evntr+   elementsr   r   r   mutation_events4   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 d  I dH  n1 I dH sMw   Y  |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_listener]   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 d  I dH  n1 I dH sPw   Y  ||jkrl|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 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levelr4   r5   )Z&selenium.webdriver.common.bidi.consoler3   r   r#   r$   r   r%   r&   r'   r(   ZConsoleAPICalledERRORr    argstype_ALL)r   Z
event_typer3   r   consolemessagesr   r   r   add_listeneru   s&   (

zLog.add_listener)r
   N)
r   
__module____qualname____doc__r   r   dictr/   r2   r<   r   r   r   r   r	   #   s    
(
r	   )r)   r   
contextlibr   	importlibr   Zselenium.webdriver.common.byr   r   r   r	   r   r   r   r   <module>   s   