
    i                     
   d Z ddlZddlmc mZ ddlZddlm	Z	 ddl
mZmZmZ ddlZej                  j!                  d e e	e      j&                  j&                               ddlmZmZ d Zd Zd Zd	 Zej6                  j8                  d
        Zej6                  j8                  d        Zej6                  j8                  d        Zej6                  j8                  d        Z ej6                  j8                  d        Z!y)ua   
TDD: test_browser_remote_cdp.py
browser.py의 --remote-cdp 옵션에 대한 테스트 스위트
    N)Path)	AsyncMock	MagicMockpatch)build_parserget_pagec                  $   t               } t        j                  t              5  | j	                  dg       ddd       | j	                  g d      }d}t        ||      }|sddt        j                         v st        j                  t
              rt        j                  t
              nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|j                  }d	}||k(  }|st        j                  d
|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}y# 1 sw Y   xY w)u4   build_parser에 --remote-cdp 옵션이 있어야 함z--helpN)--remote-cdphttp://localhost:9222navigatehttps://example.com
remote_cdpz5assert %(py5)s
{%(py5)s = %(py0)s(%(py1)s, %(py3)s)
}hasattrargs)py0py1py3py5r   ==z2%(py2)s
{%(py2)s = %(py0)s.remote_cdp
} == %(py5)sr   py2r   assert %(py7)spy7)r   pytestraises
SystemExit
parse_argsr   @py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprAssertionError_format_explanationr   _call_reprcompare)parserr   @py_assert2@py_assert4@py_format6@py_assert1@py_assert3@py_format8s           </home/jay/workspace/scripts/tests/test_browser_remote_cdp.pytest_remote_cdp_option_existsr0      s7   ^F	z	" &8*%& ijD%&74&&&&&&&&7&&&7&&&&&&4&&&4&&&&&&&&&&&&&??555?55555?555555545554555?55555555555& &s   HHc                     t               } | j                  ddg      }|j                  }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            dx}x}}y)
u+   --remote-cdp 기본값이 None이어야 함r   r   N)is)z2%(py2)s
{%(py2)s = %(py0)s.remote_cdp
} is %(py5)sr   r   r   r   r   r   r   r"   r'   r    r!   r#   r$   r%   r&   r(   r   r,   r*   r-   r+   r.   s          r/   test_remote_cdp_default_is_noner5   #   s    ^Fj*?@AD??"d"?d""""?d""""""4"""4"""?"""d"""""""    c                  (   t               } d}| j                  d|ddg      }|j                  }||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndd	z  }d
d|iz  }t        t        j                  |            dx}}y)u8   --remote-cdp가 URL 형식의 값을 받아들여야 함http://100.116.204.95:9222r
   r   r   r   )z2%(py2)s
{%(py2)s = %(py0)s.remote_cdp
} == %(py4)sr   test_url)r   r   py4zassert %(py6)spy6Nr3   )r(   r9   r   r,   r-   @py_format5@py_format7s          r/   test_remote_cdp_accepts_urlr>   *   s    ^F+Hnh
DYZ[D??&?h&&&&?h&&&&&&4&&&4&&&?&&&&&&h&&&h&&&&&&&r6   c                     t               } | j                  g d      }|j                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d	x}x}}|j                  }d
}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d	x}x}}|j                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d	x}x}}y	)uC   --remote-cdp가 다른 옵션과 함께 사용될 수 있어야 함)r
   r   r   r   z	--timeout30000z
--viewport	1920x1080r   r   r   r   r   r   r   Ni0u  )z/%(py2)s
{%(py2)s = %(py0)s.timeout
} == %(py5)srA   )z0%(py2)s
{%(py2)s = %(py0)s.viewport
} == %(py5)s)r   r   r   r"   r'   r    r!   r#   r$   r%   r&   timeoutviewportr4   s          r/   (test_remote_cdp_works_with_other_optionsrD   2   sk   ^F		
D ??555?55555?555555545554555?55555555555<< 5 <5    <5      4   4   <   5       =='K'=K''''=K''''''4'''4'''='''K'''''''r6   c                  b  K   t               } d| _        d| _        d| _        d| _        d| _        t               }t               }t               }t               }t        |      |j                  _        |g|_	        |g|_
        t               |_        t               |_        t        ||        d{   \  }}|j                  j                  j                  d       ||k(  }|st        j                   d|fd||f      d	t#        j$                         v st        j&                  |      rt        j(                  |      nd	d
t#        j$                         v st        j&                  |      rt        j(                  |      nd
dz  }dd|iz  }	t+        t        j,                  |	            d}y7 w)u9   remote_cdp가 제공되면 원격 CDP에 연결해야 함r8   '  1280x720Freturn_valueNr   z%(py0)s == %(py2)spage	mock_pager   r   assert %(py4)sr:   )r   r   rB   rC   stealthblock_resourcesr   chromiumconnect_over_cdpcontextspagesset_default_timeoutset_viewport_sizer   assert_called_once_withr"   r'   r    r!   r#   r$   r%   r&   
r   mock_pwmock_browsermock_contextrL   browserrK   r,   @py_format3r<   s
             r/   +test_get_page_uses_remote_cdp_when_providedr^   K   s/     ;D2DODLDMDL D kG;L;LI )2|(LG%)NL#L$-KI!"++I #7D11MGT %%==>Z[ 9494499 2s   B.F/0F-1C=F/c                  l  K   t               } d| _        d| _        d| _        d| _        d| _        t               }t               }t               }t               }t        |      |j                  _        g |_	        t        |      |_
        |g|_        t               |_        t               |_        t        ||        d{   \  }}|j                  j                          ||k(  }|st!        j"                  d|fd||f      d	t%        j&                         v st!        j(                  |      rt!        j*                  |      nd	d
t%        j&                         v st!        j(                  |      rt!        j*                  |      nd
dz  }dd|iz  }	t-        t!        j.                  |	            d}y7 w)uH   원격 브라우저에 컨텍스트가 없으면 새로 생성해야 함r   rF   rG   FrH   Nr   rJ   rK   rL   rM   rN   r:   )r   r   rB   rC   rO   rP   r   rQ   rR   rS   new_contextrT   rU   rV   r   assert_called_oncer"   r'   r    r!   r#   r$   r%   r&   rX   s
             r/   0test_get_page_creates_new_context_if_no_contextsrb   m   s)     ;D-DODLDMDL DkG;L;LI(1|(LG%L(lCL#L$-KI!"++I"7D11MGT //19494499	 2   B>F4 F2C2F4c                  l  K   t               } d| _        d| _        d| _        d| _        d| _        t               }t               }t               }t               }t        |      |j                  _        |g|_	        g |_
        t        |      |_        t               |_        t               |_        t        ||        d{   \  }}|j                  j                          ||k(  }|st!        j"                  d|fd||f      d	t%        j&                         v st!        j(                  |      rt!        j*                  |      nd	d
t%        j&                         v st!        j(                  |      rt!        j*                  |      nd
dz  }dd|iz  }	t-        t!        j.                  |	            d}y7 w)u>   컨텍스트에 페이지가 없으면 새로 생성해야 함r   rF   rG   FrH   Nr   rJ   rK   rL   rM   rN   r:   )r   r   rB   rC   rO   rP   r   rQ   rR   rS   rT   new_pagerU   rV   r   ra   r"   r'   r    r!   r#   r$   r%   r&   rX   s
             r/   *test_get_page_creates_new_page_if_no_pagesrf      s)     ;D-DODLDMDL DkG;L;LI(1|(LG%)NLL%9=L$-KI!"++I"7D11MGT ,,.9494499	 2rc   c                  j  K   t               } d| _        d| _        d| _        d| _        d| _        t               }t               }t               }t               }t        |      |j                  _        |g|_	        |g|_
        t               |_        t               |_        t               |_        t        ||        d{   \  }}|j                  j                          ||k(  }|st!        j"                  d|fd	||f      d
t%        j&                         v st!        j(                  |      rt!        j*                  |      nd
dt%        j&                         v st!        j(                  |      rt!        j*                  |      nddz  }dd|iz  }	t-        t!        j.                  |	            d}y7 w)uG   block_resources가 활성화되면 리소스 차단을 적용해야 함r   rF   rG   FTrH   Nr   rJ   rK   rL   rM   rN   r:   )r   r   rB   rC   rO   rP   r   rQ   rR   rS   rT   rU   rV   router   ra   r"   r'   r    r!   r#   r$   r%   r&   rX   s
             r/   2test_get_page_applies_block_resources_when_enabledri      s%     ;D-DODLDMDLDkG;L;LI(1|(LG%)NL#L$-KI!"++IkIO"7D11MGT OO&&(9494499	 2s   B=F3?F1 C2F3c            
      b  K   t               } d| _        d| _        d| _        d| _        d| _        t               }t        dd      5  t        dd      5  t        d	      5  t               }t               }t               }t        |      |j                  _	        g |_
        t        |      |_        g |_        t        |      |_        t               |_        t               |_        t!        ||        d{   \  }}|j                  j                  j#                          |j                  j                  j$                  d
   d
   }d}||v }	|	st'        j(                  d|	fd||f      t'        j*                  |      dt-        j.                         v st'        j0                  |      rt'        j*                  |      nddz  }
dd|
iz  }t3        t'        j4                  |            dx}}	ddd       ddd       ddd       y7 # 1 sw Y   xY w# 1 sw Y   !xY w# 1 sw Y   yxY ww)uG   remote_cdp가 없으면 기존 로컬 Chrome 로직을 사용해야 함NrF   rG   Fzbrowser.load_sessionrH   zbrowser.launch_chromei90  zbrowser.save_sessionr   z	127.0.0.1)in)z%(py1)s in %(py3)scall_arg)r   r   zassert %(py5)sr   )r   r   rB   rC   rO   rP   r   r   rQ   rR   rS   r`   rT   re   rU   rV   r   ra   	call_argsr"   r'   r$   r    r!   r#   r%   r&   )r   rY   rZ   r[   rL   r\   rK   rl   @py_assert0r)   @py_format4r+   s               r/   2test_get_page_uses_local_chrome_when_no_remote_cdprp      s     ;DDODLDMDL DkG 
%D	9 /*? 	/-. /({({%K	4=<4X  1(*%+4,+O(%'"(1y(I%09	-.7k	+&.w&= =   11DDF"++<<FFqI!L".{h....{h...{......h...h.......%/	// / !>/ /	/ 	// /sb   AH/H#H!BH8H
9C6H/H7H#?	H/HHHH 	H##H,(H/)"__doc__builtinsr    _pytest.assertion.rewrite	assertionrewriter"   syspathlibr   unittest.mockr   r   r   r   pathinsertstr__file__parentr\   r   r   r0   r5   r>   rD   markasyncior^   rb   rf   ri   rp    r6   r/   <module>r      s   
  
  5 5  3tH~,,334 5 *
6#'(2  B  8  8  8  /  /r6   