|
| 1 | +import subprocess |
| 2 | +import time |
| 3 | + |
| 4 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
| 5 | + |
| 6 | +HOST_NAME = '' |
| 7 | +PORT_NUMBER = 9000 |
| 8 | + |
| 9 | +CLEAN_DRIVERS_CACHE = r'C:\\Selenium\\bin\\kill_browsers_drivers_proc.bat' |
| 10 | +START_HUB = 'start C:\\Selenium\\bin\\start_selenium_hub.bat' |
| 11 | +REGISTER_NODES = 'start C:\\Selenium\\bin\\start_selenium_node.bat' |
| 12 | +STOP_HUB = r'C:\\Selenium\\bin\\stop_selenium_hub.bat' |
| 13 | +UNREGISTER_NODES = r'C:\\Selenium\\bin\\unregister_selenium_node.bat' |
| 14 | + |
| 15 | + |
| 16 | +class SeleniumHubHandler(BaseHTTPRequestHandler): |
| 17 | + |
| 18 | + def do_HEAD(self): |
| 19 | + paths = { |
| 20 | + '/clean': self.clean_driver_cache, |
| 21 | + '/start': self.start_hub, |
| 22 | + '/stop': self.stop_hub, |
| 23 | + '/restart': self.restart_hub |
| 24 | + } |
| 25 | + |
| 26 | + if self.path in paths: |
| 27 | + self.respond_head(paths[self.path]) |
| 28 | + else: |
| 29 | + self.respond_bad_request_error(self.path) |
| 30 | + |
| 31 | + def do_GET(self): |
| 32 | + self.do_HEAD() |
| 33 | + |
| 34 | + def respond_head(self, symbol): |
| 35 | + response = self.handle_http(symbol, self.path) |
| 36 | + self.wfile.write(response) |
| 37 | + |
| 38 | + def handle_http(self, batch_file, path): |
| 39 | + batch_file() |
| 40 | + |
| 41 | + self.send_response(200) |
| 42 | + self.send_header('Content-type', 'text/html') |
| 43 | + self.end_headers() |
| 44 | + |
| 45 | + content = ''' |
| 46 | + <html> |
| 47 | + <head> |
| 48 | + <title>UI Automation Test Hub</title> |
| 49 | + </head> |
| 50 | + <body> |
| 51 | + <p>You accessed path: {}.</p> |
| 52 | + </body> |
| 53 | + </html> |
| 54 | + '''.format(path) |
| 55 | + return bytes(content, 'UTF-8') |
| 56 | + |
| 57 | + def clean_driver_cache(self): |
| 58 | + subprocess.call(CLEAN_DRIVERS_CACHE) |
| 59 | + time.sleep(1) |
| 60 | + |
| 61 | + def start_hub(self): |
| 62 | + subprocess.Popen(START_HUB, shell=True) |
| 63 | + time.sleep(1) |
| 64 | + subprocess.Popen(REGISTER_NODES, shell=True) |
| 65 | + |
| 66 | + def stop_hub(self): |
| 67 | + subprocess.call(UNREGISTER_NODES) |
| 68 | + time.sleep(1) |
| 69 | + subprocess.call(STOP_HUB) |
| 70 | + |
| 71 | + def restart_hub(self): |
| 72 | + self.stop_hub() |
| 73 | + time.sleep(2) |
| 74 | + self.start_hub() |
| 75 | + |
| 76 | + def respond_bad_request_error(self, path): |
| 77 | + self.send_response(400) |
| 78 | + self.send_header('Content-type', 'text/html') |
| 79 | + self.end_headers() |
| 80 | + |
| 81 | + content = ''' |
| 82 | + <html> |
| 83 | + <head> |
| 84 | + <title>UI Automation Test Hub</title> |
| 85 | + </head> |
| 86 | + <body> |
| 87 | + <p>You accessed path: {}, which is not legal.</p> |
| 88 | + </body> |
| 89 | + </html> |
| 90 | + '''.format(path) |
| 91 | + self.wfile.write(bytes(content, 'UTF-8')) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + server_class = HTTPServer |
| 96 | + httpd = server_class((HOST_NAME, PORT_NUMBER), SeleniumHubHandler) |
| 97 | + print(time.asctime(), 'Server Starts - %s:%s' % (HOST_NAME, PORT_NUMBER)) |
| 98 | + |
| 99 | + try: |
| 100 | + httpd.serve_forever() |
| 101 | + except KeyboardInterrupt: |
| 102 | + print(time.asctime(), 'Force management server to stop.') |
| 103 | + pass |
| 104 | + |
| 105 | + httpd.server_close() |
| 106 | + print(time.asctime(), 'Server Stops - %s:%s' % (HOST_NAME, PORT_NUMBER)) |
0 commit comments