PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/windmill/browser/chrome.py

https://github.com/romainpreston/windmill
Python | 125 lines | 37 code | 17 blank | 71 comment | 7 complexity | 2eb11b9acb30083dc6ae45899961faa9 MD5 | raw file
  1. # Copyright (c) 2009 Mikeal Rogers <mikeal.rogers@gmail.com>
  2. # Copyright (c) 2009 Domen Kozar <domen@dev.si>
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import commands
  16. import tempfile
  17. import logging
  18. import signal
  19. import subprocess
  20. import sys, os
  21. if not sys.version.startswith('2.4'):
  22. from urlparse import urlparse
  23. else:
  24. # python 2.4
  25. from windmill.tools.urlparse_25 import urlparse
  26. import windmill
  27. logger = logging.getLogger(__name__)
  28. import safari
  29. class Chrome(safari.Safari):
  30. def __init__(self):
  31. self.chrome_binary = windmill.settings['CHROME_BINARY']
  32. self.test_url = windmill.settings['TEST_URL']
  33. # def create_redirect(self):
  34. # self.redirection_page = tempfile.mktemp(suffix='.html')
  35. # f = open(self.redirection_page, 'w')
  36. # test_url = windmill.get_test_url(windmill.settings['TEST_URL'])
  37. # f.write( html_redirection.replace('{replace}', test_url) )
  38. # f.flush() ; f.close()
  39. # def set_proxy_mac(self):
  40. # """Set local Proxy"""
  41. # self.netsetup_binary = windmill.settings['NETWORKSETUP_BINARY']
  42. # interface_name = find_default_interface_name()
  43. # uri = urlparse.urlparse(self.test_url)
  44. # set_proxy_command = ' '.join([ self.netsetup_binary,
  45. # '-setwebproxy',
  46. # '"'+interface_name+'"',
  47. # '127.0.0.1',
  48. # str(windmill.settings['SERVER_HTTP_PORT'])
  49. # ])
  50. # commands.getoutput(set_proxy_command)
  51. # enable_proxy_command = ' '.join([ self.netsetup_binary,
  52. # '-setwebproxystate',
  53. # '"'+interface_name+'"',
  54. # 'on'
  55. # ])
  56. # commands.getoutput(enable_proxy_command)
  57. # self.create_redirect()
  58. # self.interface_name = interface_name
  59. #
  60. # def unset_proxy_mac(self):
  61. # commands.getoutput(' '.join([self.netsetup_binary, '-setwebproxystate', '"'+self.interface_name+'"', 'off']))
  62. def get_chrome_command(self):
  63. #default options, what was used for windows chrome build
  64. chrome_options = ['--homepage', self.test_url+'/windmill-serv/start.html','--disable-popup-blocking']
  65. if sys.platform in ('cygwin', 'win32','linux2'):
  66. # options for running in windows and linux (same options format)
  67. # Run Proxy using option
  68. chrome_options = ['--homepage', self.test_url+'/windmill-serv/start.html','--disable-popup-blocking','--proxy-server='+'127.0.0.1:'+str(windmill.settings['SERVER_HTTP_PORT'])]
  69. return [self.chrome_binary]+chrome_options
  70. #def set_proxy_windows(self):
  71. # import ie
  72. # self.ie_obj = ie.InternetExplorer()
  73. # self.ie_obj.set_proxy()
  74. #
  75. # def unset_proxy_windows(self):
  76. # self.ie_obj.unset_proxy()
  77. def start(self):
  78. """Start Chrome"""
  79. # if sys.platform == 'darwin':
  80. # self.set_proxy_mac()
  81. #if sys.platform in ('cygwin', 'win32'):
  82. # self.set_proxy_windows()
  83. # Workaround for bug in nose
  84. if hasattr(sys.stdout, 'fileno'):
  85. kwargs = {'stdout':sys.stdout ,'stderr':sys.stderr, 'stdin':sys.stdin}
  86. else:
  87. kwargs = {'stdout':sys.__stdout__ ,'stderr':sys.__stderr__, 'stdin':sys.stdin}
  88. command = self.get_chrome_command()
  89. self.p_handle = subprocess.Popen(command, **kwargs)
  90. logger.info(command)
  91. def kill(self, kill_signal=None):
  92. """Stop Chrome"""
  93. if not sys.version.startswith('2.6'):
  94. raise Exception("Kill doesn't work for Chrome on Python version pre-2.6")
  95. #if sys.platform in ('cygwin', 'win32'):
  96. # self.unset_proxy_windows()
  97. #
  98. try:
  99. self.p_handle.kill()
  100. except:
  101. logger.exception('Cannot kill Chrome')
  102. # def stop(self):
  103. # self.kill(signal.SIGTERM)
  104. #
  105. # def is_alive(self):
  106. # if self.p_handle.poll() is None:
  107. # return False
  108. # return True