U
    Pf^                     @   s   d dl Z d dl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 G d
d deZdS )    N)contextmanager)BytesIO)DriverFinder)	WebDriver   )Options)FirefoxRemoteConnection)Servicec                       s   e Zd ZdZdZdZdeeedd fddZ	dd	 fd
dZ
dd	ddZedd Zded	ddZdd	ddZed	ddZed	ddZed	ddZed	ddZ  ZS )r   z=Controls the GeckoDriver and allows you to drive the browser.ZchromecontentNT)optionsservice
keep_alivereturnc                    s   |r|nt  | _|r|nt }t| j|}| rB| |_d|_| | j_| j	  t
| jj||jd}zt j||d W n tk
r   |    Y nX d| _dS )a  Creates a new instance of the Firefox driver. Starts the service and
        then creates new instance of Firefox driver.

        :Args:
         - options - Instance of ``options.Options``.
         - service - (Optional) service instance for managing the starting and stopping of the driver.
         - keep_alive - Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive.
        N)Zremote_server_addrr   Zignore_proxy)Zcommand_executorr   F)r	   r   r   r   Zget_browser_pathZbinary_locationZbrowser_versionZget_driver_pathpathstartr   Zservice_urlZ_ignore_local_proxysuper__init__	ExceptionquitZ
_is_remote)selfr   r   r   finderexecutor	__class__ H/tmp/pip-unpacked-wheel-ds5t_0qj/selenium/webdriver/firefox/webdriver.pyr   &   s&    

zWebDriver.__init__)r   c                    s:   z(zt   W n tk
r$   Y nX W 5 | j   X dS )z=Closes the browser and shuts down the GeckoDriver executable.N)r   stopr   r   r   r   r   r   r   r   N   s
    
zWebDriver.quitc                 C   s   |  dd|i d S )NZSET_CONTEXTcontextexecute)r   r   r   r   r   set_contextX   s    zWebDriver.set_contextc              	   c   s6   |  dd}| | z
dV  W 5 | | X dS )a  Sets the context that Selenium commands are running in using a
        `with` statement. The state of the context on the server is saved
        before entering the block, and restored upon exiting it.

        :param context: Context, may be one of the class properties
            `CONTEXT_CHROME` or `CONTEXT_CONTENT`.

        Usage example::

            with selenium.context(selenium.CONTEXT_CHROME):
                # chrome scope
                ... do stuff ...
        ZGET_CONTEXTvalueN)r    popr!   )r   r   Zinitial_contextr   r   r   r   [   s
    

zWebDriver.contextFc              	   C   s   t j|rt }t j|}t|d }t|dtjH}t 	|D ]6\}}}|D ]&}	t j
||	}
||
|
|d  qTqFW 5 Q R X t| d}n*t|d}t| d}W 5 Q R X ||d}| d|d S )	a  Installs Firefox addon.

        Returns identifier of installed addon. This identifier can later
        be used to uninstall addon.

        :param temporary: allows you to load browser extensions temporarily during a session
        :param path: Absolute path to the addon that will be installed.

        :Usage:
            ::

                driver.install_addon('/path/to/firebug.xpi')
        r   wNzUTF-8rb)addon	temporaryZINSTALL_ADDONr"   )osr   isdirr   normpathlenzipfileZipFileZIP_DEFLATEDwalkjoinwritebase64	b64encodegetvaluedecodeopenreadr    )r   r   r'   fpZ	path_rootZzippedbase_filesZfylefilenamer&   filepayloadr   r   r   install_addonq   s    "
zWebDriver.install_addonc                 C   s   |  dd|i dS )zUninstalls Firefox addon using its identifier.

        :Usage:
            ::

                driver.uninstall_addon('addon@foo.com')
        ZUNINSTALL_ADDONidNr   )r   
identifierr   r   r   uninstall_addon   s    zWebDriver.uninstall_addonc              	   C   sp   |  dstdt |  }zDz$t|d}|| W 5 Q R X W n tk
rb   Y W 
dS X W 5 ~X dS )a  Saves a full document 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_full_page_screenshot_as_file('/Screenshots/foo.png')
        z.pngz^name used for saved screenshot does not match file type. It should end with a `.png` extensionwbFT)	lowerendswithwarningswarnUserWarningget_full_page_screenshot_as_pngr6   r1   OSError)r   r<   Zpngfr   r   r    get_full_page_screenshot_as_file   s    z*WebDriver.get_full_page_screenshot_as_filec                 C   s
   |  |S )a  Saves a full document 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.save_full_page_screenshot('/Screenshots/foo.png')
        )rL   )r   r<   r   r   r   save_full_page_screenshot   s    z#WebDriver.save_full_page_screenshotc                 C   s   t |  dS )zGets the full document screenshot of the current window as a binary
        data.

        :Usage:
            ::

                driver.get_full_page_screenshot_as_png()
        ascii)r2   	b64decode"get_full_page_screenshot_as_base64encoder   r   r   r   rI      s    	z)WebDriver.get_full_page_screenshot_as_pngc                 C   s   |  dd S )zGets the full document screenshot of the current window as a base64
        encoded string which is useful in embedded images in HTML.

        :Usage:
            ::

                driver.get_full_page_screenshot_as_base64()
        ZFULL_PAGE_SCREENSHOTr"   r   r   r   r   r   rP      s    	z,WebDriver.get_full_page_screenshot_as_base64)NNT)F)__name__
__module____qualname____doc__ZCONTEXT_CHROMEZCONTEXT_CONTENTr   r	   boolr   r   r!   r   r   strr?   rB   rL   rM   bytesrI   rP   __classcell__r   r   r   r   r       s,      (

"
r   )r2   r(   rF   r,   
contextlibr   ior   Z'selenium.webdriver.common.driver_finderr   Z#selenium.webdriver.remote.webdriverr   ZRemoteWebDriverr   r   Zremote_connectionr   r   r	   r   r   r   r   <module>   s   