1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved. 2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be 3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# found in the LICENSE file. 4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)"""Launches Chrome. 6424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)This script launches Chrome and waits until its window shows up. 8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)""" 9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import optparse 11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import sys 12424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import time 1368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)import win32process 14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)import chrome_helper 16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)def WaitForWindow(process_id, class_pattern): 19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) """Waits until a window specified by |process_id| and class name shows up. 20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Args: 22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) process_id: The ID of the process that owns the window. 23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) class_pattern: The regular expression pattern of the window class name. 24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Returns: 26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) A boolean value indicating whether the specified window shows up within 27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 30 seconds. 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) """ 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) start_time = time.time() 30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) while time.time() - start_time < 30: 31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if chrome_helper.WindowExists([process_id], class_pattern): 32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return True 3368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) time.sleep(0.1) 34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return False 35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)def main(): 3858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) usage = 'usage: %prog chrome_path' 3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) parser = optparse.OptionParser(usage, description='Launch Chrome.') 4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) _, args = parser.parse_args() 4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) if len(args) != 1: 4258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) parser.error('Incorrect number of arguments.') 4358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) chrome_path = args[0] 4458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 4568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) # Use CreateProcess rather than subprocess.Popen to avoid side effects such as 4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) # handle interitance. 4768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) _, _, process_id, _ = win32process.CreateProcess(None, chrome_path, None, 4868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) None, 0, 0, None, None, 4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) win32process.STARTUPINFO()) 5068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) if not WaitForWindow(process_id, 'Chrome_WidgetWin_'): 51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) raise Exception('Could not launch Chrome.') 52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return 0 53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)if __name__ == '__main__': 56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) sys.exit(main()) 57