o
    iE                     @   s   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 e jjZd+ddZd+ddZd+ddZd,ddZd-ddZd.ddZG dd deZG dd dZd/ddZedd  Zd!d" Zd#d$ Zd%d& Z d'd( Z!d)d* Z"dS )0    N)ActionChains)By)Keys)WebDriverWait)expected_conditions)
WebElement)contextmanager
   F   c                 C   2   |dkrt | tj||||S t| tj|||||S Nr
   )_wait_for_multipler   CSS_SELECTOR	_wait_for)driverselectortimeoutvisiblesingle
wait_for_nobscures r   X/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/tests/selenium/utils.pywait_for_selector   
   r   c                 C   r   r   )r   r   TAG_NAMEr   )r   tagr   r   r   r   r   r   r   r   wait_for_tag   r   r   c                 C   r   r   )r   r   XPATHr   )r   xpathr   r   r   r   r   r   r   r   wait_for_xpath   r   r    c                    s   t | | fdd d S )Nc                    s
   |   S N)execute_script)dscriptr   r   <lambda>$      
 z0wait_for_script_to_return_true.<locals>.<lambda>r   until)r   r%   r   r   r$   r   wait_for_script_to_return_true#   s   r*   c           	      C   sR   t | |}|rtj}n|r|rtj}ntj}n	|rtj}ntj}||||fS )a  Waits `timeout` seconds for the specified condition to be met. Condition is
    met if any matching element is found. Returns located element(s) when found.

    Args:
        driver: Selenium web driver instance
        locator_type: type of locator (e.g. By.CSS_SELECTOR or By.TAG_NAME)
        locator: name of tag, class, etc. to wait for
        timeout: how long to wait for presence/visibility of element
        visible: if True, require that element is not only present, but visible
        single: if True, return a single element, otherwise return a list of matching
        elements
        obscures: if True, waits until the element becomes invisible
    )r   ECinvisibility_of_element_locatedvisibility_of_element_locatedpresence_of_element_located"visibility_of_all_elements_located presence_of_all_elements_locatedr)   )	r   locator_typelocatorr   r   r   r   waitconditionalr   r   r   r   '   s   
r   c                    s&   t | |} fdd}||S )a  Waits until `wait_for_n` matching elements to be present (or visible).
    Returns located elements when found.

    Args:
        driver: Selenium web driver instance
        locator_type: type of locator (e.g. By.CSS_SELECTOR or By.TAG_NAME)
        locator: name of tag, class, etc. to wait for
        timeout: how long to wait for presence/visibility of element
        wait_for_n: wait until this number of matching elements are present/visible
        visible: if True, require that elements are not only present, but visible
    c                    s2   |   }rdd |D }t|k rdS |S )Nc                 S   s   g | ]}|  r|qS r   )is_displayed).0er   r   r   
<listcomp>V   s    z>_wait_for_multiple.<locals>.multiple_found.<locals>.<listcomp>F)find_elementslen)r   elementsr2   r1   r   r   r   r   multiple_foundS   s   z*_wait_for_multiple.<locals>.multiple_foundr(   )r   r1   r2   r   r   r   r3   r=   r   r<   r   r   E   s   

r   c                   @   s   e Zd ZdddZdS )CellTypeError c                 C   s
   || _ d S r!   )message)selfr@   r   r   r   __init__`      
zCellTypeError.__init__N)r?   )__name__
__module____qualname__rB   r   r   r   r   r>   ^   s    r>   c                   @   sn  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	dd Z
e	dd Ze	dd Zdd Zdd Zdd ZdYddZdZddZd[dd Zd\d"d#Zd$d% Zd&d' Zd(d) Zd]d+d,Zd^d.d/Zd_d1d2Zd3d4 ZdYd5d6Zd7d8 Zd`d;d<Zdad=d>Zdbd@dAZdbdBdCZ dDdE Z!dcdGdHZ"d!dIdJdKZ#dLdM Z$dNdO Z%dPdQ Z&dRdS Z'dTdU Z(e)dddWdXZ*d9S )eNotebookc                 C   s   || _ |   |   d S r!   )browser_wait_for_start#disable_autosave_and_onbeforeunload)rA   rH   r   r   r   rB   f   s   zNotebook.__init__c                 C   s
   t | jS r!   )r:   cellsrA   r   r   r   __len__k   rC   zNotebook.__len__c                 C   s
   | j | S r!   rK   )rA   keyr   r   r   __getitem__n   rC   zNotebook.__getitem__c                 C   s"   t |tr| j||dd d S d S )NFindexcontentrender)
isinstanceint	edit_cell)rA   rO   itemr   r   r   __setitem__q   s   
zNotebook.__setitem__c                 C   s   dd | j D S )Nc                 s   s    | ]}|V  qd S r!   r   )r6   cellr   r   r   	<genexpr>}   s    z$Notebook.__iter__.<locals>.<genexpr>rN   rL   r   r   r   __iter__|      zNotebook.__iter__c                    s*   t  jd t jd fdd dS )zBWait until the notebook interface is loaded and the kernel startedz.cellr	   c                    s      S r!   )is_kernel_running)drvrrL   r   r   r&      s    z*Notebook._wait_for_start.<locals>.<lambda>N)r   rH   r   r)   rL   r   rL   r   rI      s   
zNotebook._wait_for_startc                 C      | j tjdS )Nbody)rH   find_elementr   r   rL   r   r   r   ra      s   zNotebook.bodyc                 C   r`   )z/Gets all cells once they are visible.

        rZ   )rH   r9   r   
CLASS_NAMErL   r   r   r   rK      s   zNotebook.cellsc                 C   s   |  | jS r!   )rR   current_cellrL   r   r   r   current_index   s   zNotebook.current_indexc                 C   s   | j |S r!   )rK   rR   rA   rZ   r   r   r   rR      s   zNotebook.indexc                 C   s   | j d | j d dS )z|Disable request to save before closing window and autosave.

        This is most easily done by using js directly.
        zwindow.onbeforeunload = null;z)Jupyter.notebook.set_autosave_interval(0)NrH   r"   rL   r   r   r   rJ      s   z,Notebook.disable_autosave_and_onbeforeunloadc                 C   s   | j tj | jd dS )z@Changes us into command mode on currently focused cell

        zireturn Jupyter.notebook.handle_command_mode(Jupyter.notebook.get_cell(Jupyter.notebook.get_edit_index()))N)ra   	send_keysr   ESCAPErH   r"   rL   r   r   r   to_command_mode   s   zNotebook.to_command_moder   c                 C   s$   | j | }|  |   || _d S r!   )rK   clickrj   rd   )rA   rR   rZ   r   r   r   
focus_cell   s   

zNotebook.focus_cellc                 C   s4   |  | |   t|| D ]}t| jd qd S )Nj)rl   rj   rangeshiftrH   )rA   initial_indexfinal_indexir   r   r   select_cell_range   s
   
zNotebook.select_cell_ranger?   c                 C   s   |  | |   | jd t| jddd | jtjd	  | jtjd| | jtjd| | jtjd	  d S )	Nfz#find-and-replaceTr   findreplace_allcells_btnfindreplace_find_inpfindreplace_replace_inpfindreplace_replaceall_btn)
rl   rj   ra   rh   r   rH   rb   r   IDrk   )rA   rR   find_txtreplace_txtr   r   r   find_and_replace   s   
zNotebook.find_and_replacecodec                 C   s~   |  | | j| }|dkr| jd n|dkr | jd n|dkr+| jd nt| d| | |  | | jS )Nmarkdownmrawrr~   yz: is not a valid cell type,use 'code', 'markdown', or 'raw')rl   rK   rd   rh   r>   wait_for_stale_cell)rA   rR   	cell_typerZ   r   r   r   convert_cell_type   s   



zNotebook.convert_cell_typec                 C   s    t | jd}|t|}dS )z This is needed to switch a cell's mode and refocus it, or to render it.

        Warning: there is currently no way to do this when changing between
        markdown and raw cells.
        r	   N)r   rH   r)   r+   staleness_of)rA   rZ   r3   elementr   r   r   r      s   zNotebook.wait_for_stale_cellc                 C   s   t | jtj|dd d S )NT)r   )r   rH   r   rc   )rA   r   r   r   r   wait_for_element_availability   s   z&Notebook.wait_for_element_availabilityc                 C   s   d}| j |S )NzKreturn Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})rg   )rA   JSr   r   r   get_cells_contents   s   zNotebook.get_cells_contentsdiv .CodeMirror-codec                 C   s   | j | tj|jS r!   )rK   rb   r   r   text)rA   rR   r   r   r   r   get_cell_contents   s   zNotebook.get_cell_contentsoutput_subareac                 C   s   | j | tj|S r!   )rK   r9   r   rc   )rA   rR   outputr   r   r   get_cell_output   s   zNotebook.get_cell_outputr	   c                    s   t j| fddS )Nc                    s
     S r!   )r   )brR   rA   r   r   r&      r'   z/Notebook.wait_for_cell_output.<locals>.<lambda>)r   rH   r)   )rA   rR   r   r   r   r   wait_for_cell_output   s   zNotebook.wait_for_cell_outputc                 C   s"   d| d| d| }| j |S )NJupyter.notebook.get_cell(z).metadata.z = rg   )rA   rR   rO   valuer   r   r   r   set_cell_metadata   s   zNotebook.set_cell_metadatac                 C   s   d| d}| j |S )Nz!return Jupyter.notebook.get_cell(z).cell_typerg   rA   rR   r   r   r   r   get_cell_type   s   zNotebook.get_cell_typec                 C   s"   d| d| d}| j | d S )Nr   z).set_input_prompt()rg   )rA   rR   	prmpt_valr   r   r   r   set_cell_input_prompt   s   zNotebook.set_cell_input_promptNFc                 C   s   |dur	|  |}| | | jtj t| jd | jtj t	|
 D ]\}}|dkr8| jtjd | jtj| q(|rK| | j dS dS )zLSet the contents of a cell to *content*, by cell object or by index
        Nar   
)rR   rl   rd   rh   r   ENTERcmdtrlrH   DELETE	enumerate
splitlinesexecute_cellre   )rA   rZ   rR   rS   rT   line_noliner   r   r   rW      s   

zNotebook.edit_cellc                 C   sN   t |tr|}nt |tr| |}ntd| | | jtj	tj
 d S )Nz0execute_cell only accepts a WebElement or an int)rU   rV   r   rR   	TypeErrorrl   rd   rh   r   CONTROLr   )rA   cell_or_indexrR   r   r   r   r   
  s   


zNotebook.execute_cellc                 C   sZ   |  | | jd |dkr|d n|}|r| j||d |dkr+| j||d d S d S )Nr   r   r
   )rR   rS   r~   )rR   r   )rl   rd   rh   rW   r   )rA   rR   r   rS   	new_indexr   r   r   add_cell  s   
zNotebook.add_cellc                 C   s   | j |||d | | d S )N)rR   r   rS   )r   r   )rA   rR   r   rS   r   r   r   add_and_execute_cell  s   zNotebook.add_and_execute_cellc                 C   s"   |  | |   | jd d S )Ndd)rl   rj   rd   rh   )rA   rR   r   r   r   delete_cell!  s   
zNotebook.delete_cellTc                 C   s"   | j |dd | j|||d d S )Nr   r   rQ   )r   rW   )rA   rR   rS   rT   r   r   r   add_markdown_cell&  s   zNotebook.add_markdown_cellr   c                G   s<   t |D ]\}}t|tr| j||d qtd|d S )N)r   rS   z Don't know how to add cell from )r   rU   strr   r   )rA   r   valuesrr   r   r   r   r   append*  s   
zNotebook.appendc                 C   s   | j |  d S r!   )r   )rA   r   r   r   r   extend2  s   zNotebook.extendc                 C   s   | D ]}|  | qd S r!   )r   rf   r   r   r   run_all5  s   zNotebook.run_allc                 C   s   t | j| d S r!   )trigger_keystrokesra   )rA   keysr   r   r   trigger_keydown9  r]   zNotebook.trigger_keydownc                 C   s   | j dS )NzHreturn Jupyter.notebook.kernel && Jupyter.notebook.kernel.is_connected()rg   rL   r   r   r   r^   <  s   zNotebook.is_kernel_runningc                 C   s   d| d}| j | d S )NzJupyter.notebook.clear_output(r   rg   r   r   r   r   clear_cell_outputA  s   zNotebook.clear_cell_outputkernel-python3c                 C   sB   t | t||d W d    | |S 1 sw   Y  | |S )N)kernel_name)
new_windowselect_kernel)clsrH   r   r   r   r   new_notebookE  s   

zNotebook.new_notebook)r   )r   r   )r   r?   r?   )r   r~   )r   r   )r   r   )r   r	   )Nr   r?   Fr!   )r   r~   r?   )r   r?   Tr   )+rD   rE   rF   rB   rM   rP   rY   r\   rI   propertyra   rK   re   rR   rJ   rj   rl   rs   r}   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r   r   r   r   r   r   r^   r   classmethodr   r   r   r   r   rG   d   sV    



	



	








	
rG   r   c                 C   sN   t | d}|ttjdf}|  d| d}t| |dd}|  dS )zCClicks the "new" button and selects a kernel from the options.
    r	   znew-dropdown-button#z aTru   N)r   r)   r+   element_to_be_clickabler   rz   rk   r   )rH   r   r3   
new_buttonkernel_selectorkernelr   r   r   r   L  s   
r   c                 #   sB    | j  dV   fdd| j D }|std| j|d  dS )u  Contextmanager for switching to & waiting for a window created.

    This context manager gives you the ability to create a new window inside
    the created context and it will switch you to that new window.

    Usage example:

        from notebook.tests.selenium.utils import new_window, Notebook

        ⋮ # something that creates a browser object

        with new_window(browser):
            select_kernel(browser, kernel_name=kernel_name)
        nb = Notebook(browser)

    Nc                    s   g | ]}| vr|qS r   r   )r6   windowinitial_window_handlesr   r   r8   k  s    znew_window.<locals>.<listcomp>z$No new windows opened during contextr   )window_handles	Exception	switch_tor   )rH   new_window_handlesr   r   r   r   W  s   r   c                 C      t | d|  dS )zSend key combination Shift+(k)zshift-%sNr   rH   kr   r   r   ro   q     ro   c                 C   s4   t  d dkrt| d|  dS t| d|  dS )z6Send key combination Ctrl+(k) or Command+(k) for MacOSr   Darwinz
command-%sz
control-%sN)osunamer   r   r   r   r   r   u  s   4r   c                 C   r   )zSend key combination Alt+(k)zalt-%sNr   r   r   r   r   alty  r   r   c                 G   s   |D ]P}| d}t|dkrCdd |dd D }t| }|D ]}||}q ||d  |ddd D ]}||}q6|  q| tt|d 	 |d  qdS )a@   Send the keys in sequence to the browser.
    Handles following key combinations
    1. with modifiers eg. 'control-alt-a', 'shift-c'
    2. just modifiers eg. 'alt', 'esc'
    3. non-modifiers eg. 'abc'
    Modifiers : http://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html
    -r
   c                 S   s   g | ]	}t t| qS r   )getattrr   upper)r6   xr   r   r   r8     s    z&trigger_keystrokes.<locals>.<listcomp>Nr   r   )
splitr:   r   key_downrh   key_upperformr   r   r   )rH   r   each_key_combinationmodifiers_keysacrr   r   r   r   r   }  s   

 r   c           	         s    fdd} fdd}d} j |}d} j |}d} j |}||gks+J |dkr9|d	kr9td
| ||ks?J |dkrQ|dsIJ |dsOJ dS |d	kra||s[J ||scJ dS dS )aL  Validate the entire dual mode state of the notebook.
    Checks if the specified cell is selected, and the mode and keyboard mode are the same.
    Depending on the mode given:
        Command: Checks that no cells are in focus or in edit mode.
        Edit:    Checks that only the specified cell is in focus and in edit mode.
    c                    sp   d} j |}| d u r|D ]	}|dkr dS qdS t|D ]\}}|| kr.|dkr- dS q|dkr5 dS qdS )NzEreturn Jupyter.notebook.get_cells().map(function(c) {return c.mode;})editFT)rH   r"   r   )rR   r   
cells_modemoderr   notebookr   r   is_only_cell_edit  s"   z2validate_dualmode_state.<locals>.is_only_cell_editc                    sX   d} j |}| d u r|dkS |dkrdS d} j |}d|  } j |}||kS )Nz:return $('#notebook .CodeMirror-focused textarea').length;r   r
   Fz6return $('#notebook .CodeMirror-focused textarea')[0];z@return IPython.notebook.get_cell(%s).code_mirror.getInputField()rg   )rR   r   focused_cellsfocused_cellrZ   r   r   r   is_focused_on  s   z.validate_dualmode_state.<locals>.is_focused_onz%return IPython.keyboard_manager.mode;zreturn IPython.notebook.mode;z5return Jupyter.notebook.get_selected_cells_indices();commandr   z%An unknown mode was send: mode = "%s"N)rH   r"   r   )	r   r   rR   r   r   r   keyboard_modenotebook_mode
cell_indexr   r   r   validate_dualmode_state  s(   r   )r	   FFr
   F)r	   )r	   FFF)Fr   )#r   selenium.webdriverr   selenium.webdriver.common.byr   selenium.webdriver.common.keysr   selenium.webdriver.support.uir   selenium.webdriver.supportr   r+   $selenium.webdriver.remote.webelementr   
contextlibr   pathjoinpjoinr   r   r    r*   r   r   
ValueErrorr>   rG   r   r   ro   r   r   r   r   r   r   r   r   <module>   s4    





 
i
