
    ~@;jW                    B   d dl m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 dlm	Z	 d dl
mZ d dlmZ d d	lmZ d d
lmZ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$ d dl%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z,  G d de-          Z.dS )    )annotationsN)byref)CDLL)c_long)HWND)Path)Popen)time)AnyDictTupleOptional)	ImageGrab)AccessBridgeVersionInfo)ActorScheduler)By)JABException)Logger)Service)
Win32Utils)	JOBJECT64)TIMEOUT)
JABElement)JABFixedFuncc                  L    e Zd ZdZddddddefdV fdZd Zd Zd Ze	dWd            Z
e
j        dXd            Z
e	dYd            Zej        dZd            Ze	d[d            Zej        d\d            Ze	d]d            Zej        d^d            Ze	d_d             Zej        d`d!            Ze	dad#            Zej        dbd%            Ze	dcd'            Zej        ddd)            Zded*Zded+Zdfd-Zdgd/Zd0 Zdhd2Zdid4Zefdjd5Zdkdld9Zdkdld:Zdkdld;Zdkdld<Z	 dkdmd=Z	 dkdmd>Z	 dkdmd?Z dkdld@Z!e"j#        dd6fdndCZ$	 dkdodEZ%	 dkdodFZ&	 dkdodGZ'	 dkdodHZ(	 dkdpdIZ)	 dkdpdJZ*	 dkdpdKZ+	 dkdodLZ,e"j#        dd6fdqdMZ-dN Z.dO Z/e"j#        defdrdPZ0dQ Z1dR Z2dS Z3dT Z4dU Z5 xZ6S )s	JABDriverzControls a Java application by Java Access Bridge.

    Args:
        Service ([type]): Host system to initialize the JAB and load JAB dll file.
     Ntitlestr	file_pathr   
bridge_dllhwndr   vmidr   accessible_contextr   timeoutintreturnNonec                   t          t          |                                            t                      | _        || _        || _        | j        r|                                  t                      | _	        t          d          | _        d| _        || _        || _        || _        || _        d| _        || _        d| _        d| _        |                                  t-          | j                                                   dS )a  Create a new jab driver.

        Args:
            title (str, optional): Window title of Java application need to bind. Defaults to "".
            file_path (Path, optional): File path of the application to launch. Defaults to None.
            bridge_dll (str, optional): WindowsAccessBridge dll file path. Defaults to "".
            hwnd (HWND, optional): HWND of Java Window. Defaults to None.
            vmid (c_long, optional): vmid of Java Window. Defaults to None.
            accessible_context (JOBJECT64, optional): Any Accessible Context Component in Java Window.
            Defaults to None.
            timeout (int, optional): Default timeout set for JABDriver waiting. Defaults to TIMEOUT.
        pyjabN)superr   __init__r   
win32utilsr    _titleopen_applicationr   servr   logger
latest_log_bridge_dll_timeout_hwnd_vmid_pid_accessible_context_bridge_root_elementinit_jabr   bridge_fix_bridge_functions)	selfr   r    r!   r"   r#   r$   r%   	__class__s	           ]C:\Users\Administrator\AppData\Local\Temp\2\pip-target-b0z114gj\lib\python\pyjab/jabdriver.pyr,   zJABDriver.__init__$   s    , 	i'')))$,,"> 	$!!###II	Woo%

	#5 !T[!!7799999    c                    | S N r>   s    r@   	__enter__zJABDriver.__enter__N   s    rA   c                N    t          j        | j        t          j                   d S rC   )oskillpidsignalSIGTERM)r>   exc_typeexc_valexc_tbs       r@   __exit__zJABDriver.__exit__Q   s    
&.)))))rA   c                    | j         j        dk    r)d                    dt          | j                   g          nt          | j                   }t	          |d          }|                                 |S )Njnlp javawsT)shell)r    suffixjoinr   r	   wait)r>   cmdps      r@   r/   zJABDriver.open_applicationT   sh    ;?>;PTZ;Z;Zchh#dn"5"56777`cdhdr`s`s#T"""	rA   c                    | j         S rC   r.   rE   s    r@   r   zJABDriver.titleZ   s
    {rA   c                    || _         d S rC   r\   )r>   r   s     r@   r   zJABDriver.title^   s    rA   c                    | j         S rC   r5   rE   s    r@   r"   zJABDriver.hwndb   
    zrA   c                    || _         d S rC   r_   r>   r"   s     r@   r"   zJABDriver.hwndf       


rA   c                    | j         S rC   r7   rE   s    r@   rJ   zJABDriver.pidj   s
    yrA   rJ   c                    || _         d S rC   re   )r>   rJ   s     r@   rJ   zJABDriver.pidn   s    			rA   c                    | j         S rC   r6   rE   s    r@   r#   zJABDriver.vmidr   r`   rA   c                    || _         d S rC   rh   )r>   r#   s     r@   r#   zJABDriver.vmidv   rc   rA   c                    | j         S rC   r8   rE   s    r@   r$   zJABDriver.accessible_contextz   s    ''rA   c                    || _         d S rC   rk   )r>   r$   s     r@   r$   zJABDriver.accessible_context~   s    #5   rA   r   c                    | j         S rC   r9   rE   s    r@   r<   zJABDriver.bridge   s
    |rA   r<   c                    || _         d S rC   rn   )r>   r<   s     r@   r<   zJABDriver.bridge   s    rA   r   c                    | j         S rC   r:   rE   s    r@   root_elementzJABDriver.root_element   s    !!rA   rr   c                    || _         d S rC   rq   )r>   rr   s     r@   rr   zJABDriver.root_element   s    )rA   c                    t                      }|                    d| j                                                   |                                 d S )Nr*   )r   	new_actorr-   setup_msg_pumprun)r>   scheds     r@   _run_actor_schedzJABDriver._run_actor_sched   s@      !?!?!A!ABBB		rA   c                   | j                             d           | j                            | j                  | _        | j                                         |                                  | j        s4| j	        r| j
        s&|                     | j        | j                  | _        | j        r(|                     | j                  \  | _
        | _	        nh| j	        rR| j
        rK| j                            | j	        | j
                  }| j                            | j	        |          | _        nt#          d          |                     | j                  st#          d| j         d          |                                 | _        t+          | j        | j        | j	        | j
                  | _        | j                             d           d S )Nzinit jab)r   r%   z8At least hwnd or vmid and accessible_context is requiredzHWND:z" is not Java Window, please check!)r<   r"   r#   r$   zinit jab success)r1   infor0   load_libraryr3   r<   Windows_runry   r"   r#   r$   wait_java_window_by_titler   r4   !_get_accessible_context_from_hwndgetTopLevelObjectgetHWNDFromAccessibleContextRuntimeError_is_java_windowget_pid_from_hwndrJ   r   rr   )r>   top_level_objects     r@   r;   zJABDriver.init_jab   s   $$$i,,T-=>>!!!	 	di 	D,C 	66j$- 7  DI 9 	151W1W	2 2.D#TYY Y 	42 	#{<<	42    @@	+ DII J   ##DI.. 	VTtyTTTUUU))++&;#6	
 
 
 	+,,,,,rA   boolc                P    t          | j                            |                    S )zReturn the specific window is or not a Java Window

        Args:
            hwnd (HWND): The hwnd of window.

        Returns:
            bool: True if is a Java Window. False is not a Java Window.
        )r   r<   isJavaWindowrb   s     r@   r   zJABDriver._is_java_window   s"     DK,,T22333rA   Tuple[JOBJECT64, int]c                    t                      }t                      }| j                            |t	          |          t	          |                     ||j        fS )zGets the AccessibleContext and vmID values for the given window.

        Args:
            hwnd (HWND): hwnd (HWND): The hwnd of window.

        Returns:
            Tuple: tuple of AccessibleContext and vmID
        )r   r   r<   getAccessibleContextFromHWNDr   value)r>   r"   r#   r$   s       r@   r   z+JABDriver._get_accessible_context_from_hwnd   sV     xx&[[00%++u%788	
 	
 	
 "4:--rA   c                >    t          j        | j                  \  }}|S rC   )win32processGetWindowThreadProcessIdr"   )r>   _rJ   s      r@   r   zJABDriver.get_pid_from_hwnd   s    6tyAA3
rA   Dict[str, str]c                    t                      }| j                            | j        t	          |                     |j        |j        |j        |j        dS )aM  Gets the version information of the instance of Java Access Bridge instance your application is using.

        Returns:
            Dict[str]: Dict of AccessBridgeVersionInfo, contains:
                VMVersion
                bridgeJavaClassVersion
                bridgeJavaDLLVersion
                bridgeWinDLLVersion
        )	VMVersionbridgeJavaClassVersionbridgeJavaDLLVersionbridgeWinDLLVersion)	r   r<   getVersionInfor#   r   r   r   r   r   )r>   r{   s     r@   get_version_infozJABDriver.get_version_info   sU     '((""49eDkk:::&*&A$($=#'#;	
 
 	
rA   Optional[HWND]c                t    | j                             |          D ]}|                     |          r|c S dS )zGet Java Window hwnd by title.

        Args:
            title (str): Java window title

        Returns:
            Optional[HWND]: HWND if found Java Window, otherwise return None
        r   N)r-   get_hwnds_by_titler   )r>   r   r"   s      r@   get_java_window_hwndzJABDriver.get_java_window_hwnd   sT     O66U6CC 	 	D##D)) 	 	rA   c                Z   t                      }	 |                     |          x}r|S d| d}| j        |k    r!| j                            |           || _        t                      }t          ||z
            }||k    rt          d| d| d          |                                  )a  Wait until a Java Window exists in specific seconds.

        Args:
            title (str): The title of specific Java Window need to wait.
            timeout (int, optional): The timeout seconds. Defaults to TIMEOUT.

        Raises:
            TimeoutError: Timeout error occurs when wait time over the specific timeout

        Returns:
            HWND of Java window found in specific seconds.
        Tr   zno java window found by title ''z' in 'z'seconds)r
   r   r2   r1   debugroundTimeoutErrorry   )r>   r   r%   startr"   log_outcurrentelapseds           r@   r~   z#JABDriver.wait_java_window_by_title  s     	$00u0===t @@@@G'))!!'***")ffGGeO,,G'!!"TeTT7TTT   !!###	$rA   Fr   visiblec                h    || j         j        k    r| j         S | j                             ||          S )z:
        Find an JABElement given a name locator.
        r   r   )rr   namefind_element_by_namer>   r   r   s      r@   r   zJABDriver.find_element_by_name!  :     D%***$$$99w9WWWrA   c                h    || j         j        k    r| j         S | j                             ||          S )zA
        Find an JABElement given a description locator.
        r   )rr   descriptionfind_element_by_descriptionr   s      r@   r   z%JABDriver.find_element_by_description*  s;     D%111$$$@@uV]@^^^rA   c                h    || j         j        k    r| j         S | j                             ||          S )z:
        Find an JABElement given a role locator.
        r   )rr   rolefind_element_by_roler   s      r@   r   zJABDriver.find_element_by_role3  r   rA   c                h    || j         j        k    r| j         S | j                             ||          S )z;
        Find an JABElement given a state locator.
        r   )rr   statesfind_element_by_statesr   s      r@   r   z JABDriver.find_element_by_states<  sC     D%,,,$$$;;W <   rA   c                h    || j         j        k    r| j         S | j                             ||          S )zC
        Find an JABElement given an object depth locator.
        r   )rr   object_depthfind_element_by_object_depthr   s      r@   r   z&JABDriver.find_element_by_object_depthG  sD     D%222$$$AAW B   rA   c                h    || j         j        k    r| j         S | j                             ||          S )zD
        Find an JABElement given a children count locator.
        r   )rr   children_countfind_element_by_children_countr   s      r@   r   z(JABDriver.find_element_by_children_countT  sD     D%444$$$CCW D   rA   c                h    || j         j        k    r| j         S | j                             ||          S zF
        Find an JABElement given an index in parent locator.
        r   )rr   index_in_parentfind_element_by_index_in_parentr   s      r@   r   z)JABDriver.find_element_by_index_in_parenta  sD     D%555$$$DDW E   rA   c                :    | j                             ||          S r   )rr   find_element_by_xpathr   s      r@   r   zJABDriver.find_element_by_xpathn  s       66UG6TTTrA   byr   c                   t           j        | j        t           j        | j        t           j        | j        t           j        | j        t           j	        | j
        t           j        | j        t           j        | j        t           j        | j        i}||                                vrt%          d| d           ||         ||          S )zE
        Find an JABElement given a By strategy and locator.
        incorrect by strategy 'r   r   )r   NAMEr   DESCRIPTIONr   ROLEr   STATESr   OBJECT_DEPTHr   CHILDREN_COUNTr   INDEX_IN_PARENTr   XPATHr   keysr   )r>   r   r   r   	dict_finds        r@   find_elementzJABDriver.find_elementt  s     GT.ND<GT.It2OT>tB DHd0	
	 Y^^%%%%>>>>???y}5'::::rA   list[JABElement]c                    g }|| j         j        k    r|                    | j                    |                    | j                             ||                     |S )z?
        Find list of JABElement given a name locator.
        r   )rr   r   appendextendfind_elements_by_namer>   r   r   jabelementss       r@   r   zJABDriver.find_elements_by_name  j     D%***t011133%3QQ	
 	
 	
 rA   c                    g }|| j         j        k    r|                    | j                    |                    | j                             ||                     |S )zF
        Find list of JABElement given a description locator.
        r   )rr   r   r   r   find_elements_by_descriptionr   s       r@   r   z&JABDriver.find_elements_by_description  sk     D%111t0111::PW:XX	
 	
 	
 rA   c                    g }|| j         j        k    r|                    | j                    |                    | j                             ||                     |S )z?
        Find list of JABElement given a role locator.
        r   )rr   r   r   r   find_elements_by_roler   s       r@   r   zJABDriver.find_elements_by_role  r   rA   c                    g }|| j         j        k    r|                    | j                    |                    | j                             ||                     |S )z@
        Find list of JABElement given a state locator.
        r   )rr   r   r   r   find_elements_by_statesr   s       r@   r   z!JABDriver.find_elements_by_states  sj     D%,,,t011155E75SS	
 	
 	
 rA   c                    g }|| j         j        k    r|                    | j                    |                    | j                             ||                     |S )zH
        Find list of JABElement given an object depth locator.
        r   )rr   r   r   r   find_elements_by_object_depthr   s       r@   r   z'JABDriver.find_elements_by_object_depth  sq     D%222t0111;;W <  	
 	
 	

 rA   c                    g }|| j         j        k    r|                    | j                    |                    | j                             ||                     |S )zI
        Find list of JABElement given a children count locator.
        r   )rr   r   r   r   find_elements_by_children_countr   s       r@   r   z)JABDriver.find_elements_by_children_count  sq     D%444t0111==W >  	
 	
 	

 rA   c                    g }|| j         j        k    r|                    | j                    |                    | j                             ||                     |S zK
        Find list of JABElement given an index in parent locator.
        r   )rr   r   r   r    find_elements_by_index_in_parentr   s       r@   r   z*JABDriver.find_elements_by_index_in_parent  sq     D%555t0111>>W ?  	
 	
 	

 rA   c                :    | j                             ||          S r   )rr   find_elements_by_xpathr   s      r@   r   z JABDriver.find_elements_by_xpath  s       77eW7UUUrA   c                   t           j        | j        t           j        | j        t           j        | j        t           j        | j        t           j	        | j
        t           j        | j        t           j        | j        t           j        | j        i}||                                vrt%          d| d           ||         ||          S )zJ
        Find list of JABElement given a By strategy and locator.
        r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r>   r   r   r   
dict_findss        r@   find_elementszJABDriver.find_elements  s     GT/ND=GT/It3OT?tC EHd1	

 Z__&&&&>>>>???z"~E7;;;;rA   c                P    | j                             | j        j                   dS )zK
        Maximizes the current java window that jabdriver is using
        r"   N)r-   _set_window_maximizerr   r"   rE   s    r@   maximize_windowzJABDriver.maximize_window  )     	,,$2C2H,IIIIIrA   c                P    | j                             | j        j                   dS )zJ
        Invokes the window manager-specific 'minimize' operation
        r   N)r-   _set_window_minimizerr   r"   rE   s    r@   minimize_windowzJABDriver.minimize_window  r   rA   c           	        t                      }	 t                      }t          ||z
            }t          ||z
            }| j                            d| d|            ||k    rt	          d| d| d| d          	 |                     ||          S # t          $ r8 d| d| d	}| j        |k    r!| j                            |           || _        Y nw xY w)
NTzelapsed => z, remain => zJABElement with locator 'z' 'z' does not found in z seconds)r   r   z' does not found)r
   r   r1   r   r   r   r2   warning)	r>   r   r   r%   r   r   r   remainr   s	            r@   wait_until_element_existz"JABDriver.wait_until_element_exist  s#    	.ffGGeO,,G7W,--FKIGIIIIJJJ'!!"cccuccRYccc  .((Be(<<< . . .TbTTUTTT?g--K''000&-DO	.	.s   B ?CCc                X    |                                  }|                    |           dS )a  
        Saves a screenshot of the current window to a PNG image file. Returns
           False if there is any IOError, else returns True. Use full paths in
           your filename.

        :Args:
         - filename: The full path you wish to save your screenshot to. This
           should end with a `.png` extension.

        :Usage:
            driver.get_screenshot_as_file('/Screenshots/foo.png')
        N)get_screenshotsave)r>   filenameims      r@   get_screenshot_as_filez JABDriver.get_screenshot_as_file-  s-       ""
rA   c                N   | j                             | j        j                   | j        j        }|                    d          }|                    d          }|                    d          }|                    d          }t          j        ||||z   ||z   fdd          S )	z
        Gets the screenshot of the current window as a base64 encoded string
           which is useful in embedded images in HTML.

        :Usage:
            driver.get_screenshot_as_base64()
        r   xywidthheightFT)bboxinclude_layered_windowsall_screens)r-   _set_window_foregroundrr   r"   boundsgetr   grab)r>   r  r   r   r   r  s         r@   r   zJABDriver.get_screenshot=  s     	..D4E4J.KKK")JJsOOJJsOO

7##H%%~E	F
	 %*	
 	
 	
 		
rA   c                T    | j                             | j        j        ||           dS )a  
        Sets the width and height of the current window. (window.resizeTo)

        :Args:
         - width: the width in pixels to set the window to
         - height: the height in pixels to set the window to

        :Usage:
            driver.set_window_size(800,600)
        )r"   r   r  N)r-   _set_window_sizerr   r"   )r>   r   r  s      r@   set_window_sizezJABDriver.set_window_sizeV  s:     	(("'uV 	) 	
 	
 	
 	
 	
rA   c                T    | j                             | j        j        ||           dS )a#  
        Sets the x,y position of the current window. (window.moveTo)

        :Args:
         - x: the x-coordinate in pixels to set the window position
         - y: the y-coordinate in pixels to set the window position

        :Usage:
            driver.set_window_position(0,0)
        )r"   lefttopN)r-   _set_window_positionrr   r"   )r>   r   r   s      r@   set_window_positionzJABDriver.set_window_positione  s:     	,,"'aQ 	- 	
 	
 	
 	
 	
rA   c                L    | j                             | j        j                  S )zx
        Gets the x,y position of the current window.

        :Usage:
            driver.get_window_position()
        r   )r-   _get_window_positionrr   r"   rE   s    r@   get_window_positionzJABDriver.get_window_positiont  s#     339J9O3PPPrA   )r   r   r    r   r!   r   r"   r   r#   r   r$   r   r%   r&   r'   r(   )r'   r   )r   r   r'   r(   )r'   r   )r"   r   r'   r(   )r'   r&   )rJ   r&   r'   r(   )r'   r   )r#   r   r'   r(   )r'   r   )r$   r   r'   r(   )r'   r   )r<   r   r'   r(   )r'   r   )rr   r   r'   r(   )r'   r(   )r"   r   r'   r   )r"   r   r'   r   )r'   r   )r   r   r'   r   )r   r   r%   r&   r'   r   )F)r   r   r   r   r'   r   )r   r&   r   r   r'   r   )r   r   r   r   r   r   r'   r   )r   r   r   r   r'   r   )r   r&   r   r   r'   r   )r   r   r   r   r   r   r'   r   )r   r   r   r   r%   r&   r'   r   )7__name__
__module____qualname____doc__r   r,   rF   rP   r/   propertyr   setterr"   rJ   r#   r$   r<   rr   ry   r;   r   r   r   r   r   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  __classcell__)r?   s   @r@   r   r      s         " ,0"(: (: (: (: (: (: (:T  * * *      X \   \    X 
[   [    X 	Z   Z    X 
[   [ ( ( ( X( 6 6 6 6    X ]   ] " " " X" * * * *   )- )- )- )-X	4 	4 	4 	4. . . .   
 
 
 
&    DK $ $ $ $ $<X X X X X_ _ _ _ _X X X X X	 	 	 	 	 /4     /4     /4    U U U U U G$; ; ; ; ;* /4     /4     /4     /4     /4    " /4    " /4    " /4V V V V V G$< < < < <(J J JJ J J G$w. . . . .*   
 
 
2
 
 

 
 
Q Q Q Q Q Q QrA   r   )/
__future__r   rH   rK   ctypesr   r   r   ctypes.wintypesr   pathlibr   
subprocessr	   r
   typingr   r   r   r   r   PILr   pyjab.accessibleinfor   pyjab.common.actorschedulerr   pyjab.common.byr   pyjab.common.exceptionsr   pyjab.common.loggerr   pyjab.common.servicer   pyjab.common.win32utilsr   pyjab.common.typesr   pyjab.configr   pyjab.jabelementr   pyjab.jabfixedfuncr   objectr   rD   rA   r@   <module>r.     s   " " " " " " 				                                                  - - - - - - - - - - - -           8 8 8 8 8 8 6 6 6 6 6 6       0 0 0 0 0 0 & & & & & & ( ( ( ( ( ( . . . . . . ( ( ( ( ( (             ' ' ' ' ' ' + + + + + +^	Q ^	Q ^	Q ^	Q ^	Q ^	Q ^	Q ^	Q ^	Q ^	QrA   