1 #!/usr/bin/env python 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 """A simple main function to print ui action sequences. 7 8 Action sequences are generated using chrome/test/functional/ui_model.py 9 and are output in the format required by automated_ui_tests build target. 10 11 Generate 100 command sequences to ui.txt: 12 ui_action_generator.py -o ui.txt -c 100 13 14 Generate 100 15-action-length sequences: 15 ui_action_generator.py -c 100 -a 15 16 17 Re-create command with seed 12345: 18 ui_action_generator.py -s 12345 19 """ 20 21 import optparse 22 import os 23 import sys 24 import xml.dom.minidom 25 26 27 def _AddTestPath(): 28 """Add chrome/test/functional to path to find script dependancies.""" 29 script_dir = os.path.dirname(__file__) 30 chrome_dir = os.path.join(script_dir, os.pardir, os.pardir) 31 test_dir = os.path.join(chrome_dir, 'test', 'functional') 32 sys.path += [test_dir] 33 34 _AddTestPath() 35 import ui_model 36 37 38 def CreateUIActionList(actions_per_command, num_commands, given_seed=None): 39 """Generate user-like pseudo-random action sequences. 40 41 Args: 42 actions_per_command: length of each ui action sequence. 43 num_commands: number of sequences to generate. 44 seed: optional rand seed for this list. 45 46 Returns: 47 XML format command list string, readable by automated_ui_tests. 48 """ 49 doc = xml.dom.minidom.Document() 50 command_list = doc.createElement('CommandList') 51 doc.appendChild(command_list) 52 for _ in xrange(num_commands): 53 command = doc.createElement('command') 54 command_list.appendChild(command) 55 seed = ui_model.Seed(given_seed) 56 command.setAttribute('seed', str(seed)) 57 browser = ui_model.BrowserState() 58 for _ in xrange(actions_per_command): 59 action = ui_model.GetRandomAction(browser) 60 browser = ui_model.UpdateState(browser, action) 61 action_tuple = action.split(';') 62 action_element = doc.createElement(action_tuple[0]) 63 if len(action_tuple) == 2: 64 action_element.setAttribute('url', action_tuple[1]) 65 command.appendChild(action_element) 66 return doc.toprettyxml() 67 68 69 def ParseCommandLine(): 70 """Returns the list of options and their values, and unparsed args. 71 """ 72 parser = optparse.OptionParser() 73 parser.add_option('-o', '--output', dest='output_file', type='string', 74 action='store', default='ui_actions.txt', 75 help='the file to output the command list to') 76 parser.add_option('-c', '--num_commands', dest='num_commands', 77 type='int', action='store', default=1, 78 help='number of commands to output') 79 parser.add_option('-a', '--actions-per-command', dest='actions_per_command', 80 type='int', action='store', default=25, 81 help='number of actions per command') 82 parser.add_option('-s', '--seed', dest='seed', type='int', action='store', 83 default=None, help='generate action sequence using a seed') 84 85 return parser.parse_args() 86 87 88 def main(): 89 """Generate command list and write it out in xml format. 90 91 For use as input for automated_ui_tests build target. 92 """ 93 options, args = ParseCommandLine() 94 command_list = CreateUIActionList(options.actions_per_command, 95 options.num_commands, 96 options.seed) 97 f = open(options.output_file, 'w') 98 f.write(command_list) 99 f.close() 100 print command_list 101 return 0 102 103 104 if __name__ == '__main__': 105 sys.exit(main()) 106