B
    3\                 @   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Zd dlmZ d dl	Z	d dl
mZ d dlmZmZmZ ddlmZ ejjZdd	 Zejd
ddd Zdd Zejd
ddd Zejdddd Zejdd ZdS )    N)Popen)TemporaryDirectory)urljoin)FirefoxRemoteChrome   )Notebookc          
   C   s~   xnt dD ]b}|  dk	r"tdtj|rbyt|}t|S Q R X W n t	k
r`   Y nX t
d q
W td|dS )z0Wait 30 seconds for the notebook server to starti,  NzNotebook server failed to startg?zDidn't find %s in 30 seconds)rangeZpollRuntimeErrorospathexistsopenjsonload
ValueErrortimeZsleep)procinfo_file_pathif r   ?lib/python3.7/site-packages/notebook/tests/selenium/conftest.py_wait_for_server   s    
r   Zsession)Zscopec           	   c   s  i } t  }t|d }| d< tt|dd tt|dd t|dt|dt|d	d
| d< tj }|| d  tjdddd|dg}t	d| t
|||d }| d< t|dd|j }| t|| t	d|  | V  W d Q R X tjt| d ddd| d  id d S )NZ	notebooksnbdiru
   sub ∂ir1u   sub ∂ir 1au
   sub ∂ir2u   sub ∂ir 1bZjupyter_configZjupyter_runtimeZipython)ZJUPYTER_CONFIG_DIRZJUPYTER_RUNTIME_DIRZ
IPYTHONDIRZ	extra_envz-mnotebookz--no-browserz--notebook-dirz--NotebookApp.base_url=/a@b/zcommand=)cwdenvpopenznbserver-%i.jsonzNotebook server info:Zurlzapi/shutdownZAuthorizationztoken token)Zheaders)r   pjoinr   makedirsenvironcopyupdatesys
executableprintr   pidr   requestsZpostr   )infoZtdr   r   Zcommandr   r   r   r   r   notebook_server"   s.    


r,   c              C   s~   t jd } t jd }t jd t jd t jd dgdt jd d	d
}|d dkrVd|d< d| |f }td t|d| d}|S )zThis function helps travis create a driver on Sauce Labs.

    This function will err if used without specifying the variables expected
    in that context.
    SAUCE_USERNAMEZSAUCE_ACCESS_KEYZTRAVIS_JOB_NUMBERZTRAVIS_BUILD_NUMBERZTRAVIS_PYTHON_VERSIONZCIz
Windows 10JUPYTER_TEST_BROWSERZlatest)ztunnel-identifierZbuildZtagsplatformbrowserNameversionr0   Zfirefoxz57.0r1   z%s:%s@localhost:4445z&Connecting remote driver on Sauce Labszhttp://%s/wd/hub)Zdesired_capabilitiesZcommand_executor)r   r#   r(   r   )ZusernameZ
access_keyZcapabilitiesZhub_urldriverr   r   r   make_sauce_driverG   s    

r3   c              c   sD   t jdrt } nt jddkr,t } nt } | V  |   d S )Nr-   r.   Zchrome)r   r#   getr3   r   r   quit)r2   r   r   r   selenium_driverb   s    r6   modulec             C   s   || _ | djf | | S )Nz{url}?token={token})Zjupyter_server_infor4   format)r6   r,   r   r   r   authenticated_browserq   s    r9   c             C   s
   t | S )N)r	   Znew_notebook)r9   r   r   r   r   w   s    r   )r   r   Zpytestr*   
subprocessr   r&   Ztestpath.tempdirr   r   Zurllib.parser   Zselenium.webdriverr   r   r   Zutilsr	   r   joinr!   r   Zfixturer,   r3   r6   r9   r   r   r   r   r   <module>   s"   %