Coverage for selenible/drivers/phantom.py: 12%

42 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-05 14:24 +0000

1import urllib.parse 

2 

3from selenium import webdriver 

4 

5from . import Base 

6 

7 

8class Phantom(Base): 

9 def boot_driver(self): 

10 self.log.debug("phantom args: %s", self.browser_args) 

11 return webdriver.PhantomJS(**self.browser_args) 

12 

13 def printpdf(self, output_fn): 

14 page_format = 'this.paperSize = {format: "A4", orientation: "portrait" };' 

15 self.execute(page_format, []) 

16 render = f'''this.render("{output_fn}")''' 

17 self.execute(render, []) 

18 

19 def do_config(self, param): 

20 """ 

21 - name: config phantomjs 

22 config: 

23 cookie_flag: false 

24 proxy: 

25 url: http://proxy.host:8080/ 

26 - name: config common 

27 config: 

28 wait: 10 

29 cookie: 

30 var1: val1 

31 window: 

32 width: 600 

33 height: 480 

34 """ 

35 super().do_config(param) 

36 if "cookie_flag" in param: 

37 if param.get("cookie_flag", True): 

38 self.execute("phantom.cookiesEnabled=true") 

39 else: 

40 self.execute("phantom.cookiesEnabled=false") 

41 if "proxy" in param: 

42 prox = param.get("proxy") 

43 host = prox.get("host") 

44 port = prox.get("port", 8080) 

45 ptype = prox.get("type", "http") 

46 username = prox.get("username") 

47 password = prox.get("password") 

48 with_page = prox.get("page", False) 

49 url = prox.get("url") 

50 if url is not None: 

51 u = urllib.parse.urlparse(url) 

52 if u.scheme in ("", b""): 

53 self.log.debug("proxy not set. pass") 

54 return 

55 proxyscript = f'''setProxy("{u.hostname}", {u.port}, "{ptype}", "{u.username}", "{u.password}")''' 

56 elif host is None: 

57 proxyscript = """setProxy("")""" 

58 elif username is not None and password is not None: 

59 proxyscript = f'''setProxy("{host}", {port}, "{ptype}", "{username}", "{password}")''' 

60 else: 

61 proxyscript = f'''setProxy("{host}", {port}, "{ptype}")''' 

62 if with_page: 

63 prefix = "page" 

64 else: 

65 prefix = "phantom" 

66 self.execute(prefix + "." + proxyscript, [])