
    iE                         d Z ddlZddlZddlmZmZm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dlmZ  G d d          ZdS )z
Browser Primitives Mixin - Low-level browser operations

Provides atomic browser actions like navigation, clicking, typing, scrolling.
Used as a mixin for BrowserNavigator to keep code organized.
    N)OptionalDictAny)ManagedPage)ClickElement)TypeText)
PressEnter)PressTab)	PasteText)
ScrollPage)ZoomPagec                   f   e Zd ZdZddedee         deeef         fdZddee         deeef         fdZ	ddedee         deeef         fd	Z
dd
ededee         deeef         fdZddedee         deeef         fdZddee         deeef         fdZddedee         deeef         fdZddee         deeef         fdZddedee         deeef         fdZddedee         deeef         fdZdd
edededee         deeef         f
dZddee         deeef         fdZdS ) PrimitiveBrowserOpsz
    Mixin providing primitive browser operations.
    
    Requires parent to have:
    - _get_page_to_use(page_id) method
    - slowmo attribute
    Nurlpage_idreturnc           	        K   |                      |           d{V }dt          t          j                    dz            dz   }t          d| d|            |                    d|            d{V  	 t          d| d| d	|j                    |j                            |d
           d{V }t          j	        | j
        dz             d{V  d||j        |r|j        nd|j        j        d}t          d| d|d                     |t          d| d|j         d           |                                 d{V  S # t          $ ro}d||j        t          |          d}t          d| d|            |cY d}~t          d| d|j         d           |                                 d{V  S d}~ww xY w# t          d| d|j         d           |                                 d{V  w xY w)z<
        Navigate to a URL using an available page.
        Nnav_  i'  u   🧭 [z] Starting navigation to: 	navigate_z] Navigating to: z using page domcontentloaded)
wait_untilT)successr   r   status_code	final_urlu   ✅ [z] Navigation successful to: r   z] Setting page z back to idleF)r   r   r   erroru   ❌ [z] Navigation failed: )_get_page_to_useinttimeprintset_busyr   pagegotoasynciosleepslowmostatusr   set_idle	Exceptionstr)	selfr   r   managed_page
request_idresponseresulteerror_results	            ;/home/byschii/byschiidev/penelope/lib/browser_primitives.pynavigatezPrimitiveBrowserOps.navigate!   s      "227;;;;;;;;=C	d 233e;==
BzBBSBBCCC ##$<
$<$<========= 	*_:____I]__``` *.33CDV3WWWWWWWWH -d 2333333333  '/2:Dx).2 F W*WW&BUWWXXX Y:YYl6JYYYZZZ''))))))))))  	  	  	  '/Q	 L >*>>1>>??? Y:YYl6JYYYZZZ''))))))))))	  Y:YYl6JYYYZZZ''))))))))))s1   ;BE 
G/GGG
 GG
 
7Hc                   K   |                      |           d{V }	 t          d|j                    |j                                         d{V }|j                                         d{V }|j        j        }d|j        |||t          |          d}t          dt          |           d           ||                                 d{V  S # t          $ rP}d|j        t          |          d}t          d	|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)
a  
        Extract content from a page.
        
        Args:
            page_id (str, optional): Specific page ID to extract from, or None for any available page
            
        Returns:
            Dict containing extracted content and page information
        Nu"   📄 Extracting content from page T)r   r   r   titlecontentcontent_lengthu   ✅ Content extracted: z charactersFr   r   r   u   ❌ Content extraction failed: )r   r    r   r"   r5   r6   r   lenr(   r)   r*   )	r+   r   r,   r5   r6   r   r/   r0   r1   s	            r2   extract_contentz#PrimitiveBrowserOps.extract_contentO   s      "227;;;;;;;;	*M|7KMMNNN '+1133333333E(-5577777777G#'C  '/""%g,, F ECLLEEEFFF ''))))))))))  	  	  	  '/Q L
 7A77888 ''))))))))))	  ''))))))))))s0   BC 
D1!+D,D1D4 ,D11D4 4Eselectorc                 "  K   |                      |           d{V }	 t          d| d|j                    t                      }|                    |j        || j                   d{V }|j        |d<   ||                                 d{V  S # t          $ rQ}d|j        |t          |          d}t          d|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)a)  
        Click an element on a page.

        Args:
            selector (str): CSS selector of element to click
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing click result and element information
        Nu   👆 Clicking element 'z
' on page r   F)r   r   r;   r   u   ❌ Element click failed: )
r   r    r   r   click_selectorr"   r&   r(   r)   r*   )r+   r;   r   r,   clickerr/   r0   r1   s           r2   click_elementz!PrimitiveBrowserOps.click_element{   s      "227;;;;;;;;	*VHVV@TVVWWW #nnG"11,2CXt{[[[[[[[[F !- 4F9 ''))))))))))  	  	  	  '/$Q	 L 2q22333 ''))))))))))	  ''))))))))))s0   AB 
C/,C*
C/C2 *C//C2 2Dxyc                 &  K   |                      |           d{V }	 t          d| d| d|j                    t                      }|                    |||| j                   d{V }|j        |d<   ||                                 d{V  S # t          $ rT}d|j        ||dt          |          d}t          d	|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)
a[  
        Click at specific (x, y) coordinates on a page.

        Args:
            x (float): X coordinate to click
            y (float): Y coordinate to click
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing click result and position information
        Nu   👆 Clicking position (, z
) on page r   F)r@   rA   )r   r   positionr   u   ❌ Position click failed: )	r   r    r   r   click_positionr&   r(   r)   r*   )	r+   r@   rA   r   r,   r>   r/   r0   r1   s	            r2   rE   z"PrimitiveBrowserOps.click_position   s      "227;;;;;;;;	*UQUU!UU|?SUUVVV #nnG"11,1dkRRRRRRRRF !- 4F9 ''))))))))))  	  	  	  '/"#!,,Q	 L 333444 ''))))))))))	  ''))))))))))s0   AB 
C1/C,C1C4 ,C11C4 4Dr   distancec                 "  K   |                      |           d{V }	 t          d|j         d| d           t                      }|                    |j        || j                   d{V }|j        |d<   ||                                 d{V  S # t          $ rP}d|j        t          |          d}t          d|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)	aP  
        Scroll the page vertically by a specified distance.

        Args:
            distance (int): Distance in pixels to scroll down (default: 1000)
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing scroll result and page information
        Nu   🖱️ Scrolling page z by z pixelsr   Fr8   u   ❌ Page scroll failed: )
r   r    r   r   scrollr"   r&   r(   r)   r*   )r+   rF   r   r,   scrollerr/   r0   r1   s           r2   scroll_pagezPrimitiveBrowserOps.scroll_page   s      "227;;;;;;;;	*WL,@WWhWWWXXX "||H#??<+<hTTTTTTTTF !- 4F9 ''))))))))))  	  	  	  '/Q L
 0Q00111 ''))))))))))	  ''))))))))))s0   AB 
C/+C*
C/C2 *C//C2 2Dc                   K   |                      |           d{V }	 t          d|j                    |j                                         d{V }t          j        |                              d          }|j                            d           d{V }t          j        |                              d          }d|j        |||j        j        d}t          d           ||	                                 d{V  S # t          $ rP}d|j        t          |          d	}	t          d
|            |	cY d}~|	                                 d{V  S d}~ww xY w# |	                                 d{V  w xY w)a  
        Take a screenshot of the current page.
        
        Args:
            page_id (str, optional): Specific page ID to use, or None for any available page
            
        Returns:
            Dict containing screenshot result and page information
        Nu   📸 Taking screenshot of page zutf-8T)	full_page)r   r   
screenshotfull_screenshotr   u   ✅ Screenshot savedFr8   u   ❌ Screenshot failed: )r   r    r   r"   rM   base64	b64encodedecoder   r(   r)   r*   )
r+   r   r,   screenshot_bytesscreenshot_base64full_screenshot_bytesfull_screenshot_base64r/   r0   r1   s
             r2   rM   zPrimitiveBrowserOps.screenshot   s      +/*?*?*H*H$H$H$H$H$H$H	*JL4HJJKKK &2%6%A%A%C%CCCCCCC & 01A B B I I' R R*6*;*F*FQU*F*V*V$V$V$V$V$V$V!%+%56K%L%L%S%ST[%\%\"  '//#9#(, F )*** ''))))))))))  	  	  	  '/Q L
 /A//000 ''))))))))))	  ''))))))))))s0   CD 
E +E;E <E# E  E# #E?textc                    K   |                      |           d{V }	 t          d|j                    t                      }|                    |j        || j        dz             d{V }|j        |d<   ||                                 d{V  S # t          $ rP}d|j        t          |          d}t          d|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)a  
        Type text at the currently focused element on a page.

        IMPORTANT: An element should already be clicked/focused before calling this method.
        This operation will type at whatever element currently has focus.

        Args:
            text (str): Text to type
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing typing result and text information
        Nu   ⌨️ Typing text on page 
   r   Fr8   u   ❌ Text typing failed: )
r   r    r   r   typer"   r&   r(   r)   r*   )r+   rV   r   r,   typerr/   r0   r1   s           r2   	type_textzPrimitiveBrowserOps.type_text  s      "227;;;;;;;;	*F0DFFGGG JJE ::l&7t{b?PQQQQQQQQF !- 4F9 ''))))))))))  	  	  	  '/Q L
 0Q00111 ''))))))))))	  ''))))))))))s0   AB 
C.+C)	C.
C1 )C..C1 1Dc                   K   |                      |           d{V }	 t          d|j                    t                      }|                    |j        | j                   d{V }|j        |d<   ||                                 d{V  S # t          $ rP}d|j        t          |          d}t          d|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)a  
        Press the Enter key at the currently focused element on a page.

        IMPORTANT: An element should already be clicked/focused before calling this method.
        This operation will press Enter at whatever element currently has focus.

        Args:
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing press result and timing information
        Nu   ⏎ Pressing Enter on page r   Fr8   u   ❌ Press Enter failed: )
r   r    r   r	   pressr"   r&   r(   r)   r*   )r+   r   r,   presserr/   r0   r1   s          r2   press_enterzPrimitiveBrowserOps.press_enterB  s|      "227;;;;;;;;	*F0DFFGGG !llG"==):DKHHHHHHHHF !- 4F9 ''))))))))))  	  	  	  '/Q L
 0Q00111 ''))))))))))	  ''))))))))))s0   AB 
C*+C%C*C- %C**C- -D	F
with_shiftc                 ,  K   |                      |           d{V }	 |rdnd}t          d| d|j                    t                      }|                    |j        || j                   d{V }|j        |d<   ||                                 d{V  S # t          $ rP}d|j        t          |          d}t          d	|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)
a	  
        Press the Tab key to move focus to the next element on a page.

        IMPORTANT: The page should be in a state where Tab navigation is expected.
        This operation will move focus to the next focusable element.

        Args:
            with_shift (bool): If True, press Shift+Tab to move focus backwards (default: False)
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing press result and timing information
        Nz	Shift+TabTabu   ⇥ Pressing z	 on page r   Fr8   u   ❌ Press Tab failed: )
r   r    r   r
   r]   r"   r&   r(   r)   r*   )	r+   r`   r   r,   	key_combor^   r/   r0   r1   s	            r2   	press_tabzPrimitiveBrowserOps.press_tabi  s      "227;;;;;;;;	*'1<uIL)LLl6JLLMMM jjG"==):JTTTTTTTTF !- 4F9 ''))))))))))  	  	  	  '/Q L
 .1../// ''))))))))))	  ''))))))))))s0   A B 
C4$+C/C4C7 /C44C7 7Dc                   K   |                      |           d{V }	 t          d|j                    t                      }|                    |j        |           d{V }|j        |d<   ||                                 d{V  S # t          $ rP}d|j        t          |          d}t          d|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)a+  
        Paste text at the currently focused element on a page using clipboard.

        IMPORTANT: An element should already be clicked/focused before calling this method.
        This operation will paste at whatever element currently has focus.
        Use this for longer text content where typing would be too slow.

        Args:
            text (str): Text to paste
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing paste result and text information
        Nu   📋 Pasting text on page r   Fr8   u   ❌ Text pasting failed: )	r   r    r   r   paster"   r(   r)   r*   )r+   rV   r   r,   pasterr/   r0   r1   s           r2   
paste_textzPrimitiveBrowserOps.paste_text  sz      "227;;;;;;;;	*E|/CEEFFF [[F!<<(94@@@@@@@@F !- 4F9 ''))))))))))  	  	  	  '/Q L
 1a11222 ''))))))))))	  ''))))))))))0   AB 
C%+C  C%C(  C%%C( (D   
zoom_levelc           
      &  K   |                      |           d{V }	 t          d|j         d| d| d|            t                      }|                    ||||| j                   d{V }|j        |d<   ||                                 d{V  S # t          $ rP}d|j        t          |          d}	t          d	|            |	cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)
a  
        Zoom the page at specified coordinates.

        Args:
            x (float): X coordinate for zoom center
            y (float): Y coordinate for zoom center
            zoom_level (int): Number of zoom ticks (default: 1)
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing zoom result and information
        Nu   🔍 Zooming page z at (rC   z) level r   Fr8   u   ❌ Page zoom failed: )	r   r    r   r   zoomr&   r(   r)   r*   )
r+   r@   rA   rk   r   r,   zoomerr/   r0   r1   s
             r2   	zoom_pagezPrimitiveBrowserOps.zoom_page  s      "227;;;;;;;;	*^|';^^!^^q^^R\^^___ ZZF!;;|Q:t{SSSSSSSSF !- 4F9 ''))))))))))  	  	  	  '/Q L
 .1../// ''))))))))))	  ''))))))))))s0   AB 
C1!+C,C1C4 ,C11C4 4Dc                   K   |                      |           d{V }	 t          d|j                    t                      }|                    || j                   d{V }|j        |d<   ||                                 d{V  S # t          $ rP}d|j        t          |          d}t          d|            |cY d}~|                                 d{V  S d}~ww xY w# |                                 d{V  w xY w)z
        Reset page zoom to default level (100%).

        Args:
            page_id (str, optional): Specific page ID to use, or None for any available page

        Returns:
            Dict containing reset result and information
        Nu   🔍 Resetting zoom on page r   Fr8   u   ❌ Zoom reset failed: )	r   r    r   r   resetr&   r(   r)   r*   )r+   r   r,   rn   r/   r0   r1   s          r2   
reset_zoomzPrimitiveBrowserOps.reset_zoom  sy      "227;;;;;;;;	*G1EGGHHH ZZF!<<dkBBBBBBBBF !- 4F9 ''))))))))))  	  	  	  '/Q L
 /A//000 ''))))))))))	  ''))))))))))ri   )N)r   N)FN)rj   N)__name__
__module____qualname____doc__r*   r   r   r   r3   r:   r?   floatrE   r   rJ   rM   r[   r_   boolrd   rh   ro   rr        r2   r   r      s        ,* ,*# ,* ,*cSVh ,* ,* ,* ,*\** **Xc] **d3PS8n ** ** ** **X$* $*C $*(3- $*SWX[]`X`Sa $* $* $* $*L%* %*e %* %* %*Y]^acf^fYg %* %* %* %*N#* #*# #*x} #*X\]`be]eXf #* #* #* #*J+* +* +*c3h +* +* +* +*Z&* &*C &*(3- &*4PSUXPX> &* &* &* &*P%* %*# %*$sCx. %* %* %* %*N'* '*$ '*# '*Z^_bdg_gZh '* '* '* '*R'* '*S '*8C= '*DQTVYQYN '* '* '* '*R%* %* %*5 %*c %*PXY\P] %*imnqsvnviw %* %* %* %*N"* "* "*c3h "* "* "* "* "* "*rz   r   )rv   r$   r   typingr   r   r   rO   lib.page_managementr   lib.primitive.click_elementr   lib.primitive.type_textr   lib.primitive.press_enterr	   lib.primitive.press_tabr
   lib.primitive.paste_textr   lib.primitive.scroll_pager   lib.primitive.zoom_pager   r   ry   rz   r2   <module>r      s      & & & & & & & & & &  + + + + + + 4 4 4 4 4 4 , , , , , , 0 0 0 0 0 0 , , , , , , . . . . . . 0 0 0 0 0 0 , , , , , ,l* l* l* l* l* l* l* l* l* l*rz   