OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Automates IE to visit a list of web sites while running CF in full tab mode. |
| 6 |
| 7 The page cycler automates IE and navigates it to a series of URLs. It is |
| 8 designed to be run with Chrome Frame configured to load every URL inside |
| 9 CF full tab mode. |
| 10 |
| 11 TODO(robertshield): Make use of the python unittest module as per |
| 12 review comments. |
| 13 """ |
| 14 |
| 15 import optparse |
| 16 import sys |
| 17 import time |
| 18 import win32com.client |
| 19 import win32gui |
| 20 |
| 21 def LoadSiteList(path): |
| 22 """Loads a list of URLs from |path|. |
| 23 |
| 24 Expects the URLs to be separated by newlines, with no leading or trailing |
| 25 whitespace. |
| 26 |
| 27 Args: |
| 28 path: The path to a file containing a list of new-line separated URLs. |
| 29 |
| 30 Returns: |
| 31 A list of strings, each one a URL. |
| 32 """ |
| 33 f = open(path) |
| 34 urls = f.readlines() |
| 35 f.close() |
| 36 return urls |
| 37 |
| 38 def LaunchIE(): |
| 39 """Starts up IE, makes it visible and returns the automation object. |
| 40 |
| 41 Returns: |
| 42 The IE automation object. |
| 43 """ |
| 44 ie = win32com.client.Dispatch("InternetExplorer.Application") |
| 45 ie.visible = 1 |
| 46 win32gui.SetForegroundWindow(ie.HWND) |
| 47 return ie |
| 48 |
| 49 def RunTest(url, ie): |
| 50 """Loads |url| into the InternetExplorer.Application instance in |ie|. |
| 51 |
| 52 Waits for the Document object to be created and then waits for |
| 53 the document ready state to reach READYSTATE_COMPLETE. |
| 54 Args: |
| 55 url: A string containing the url to navigate to. |
| 56 ie: The IE automation object to navigate. |
| 57 """ |
| 58 |
| 59 print "Navigating to " + url |
| 60 ie.Navigate(url) |
| 61 timer = 0 |
| 62 |
| 63 READYSTATE_COMPLETE = 4 |
| 64 |
| 65 last_ready_state = -1 |
| 66 for retry in xrange(60): |
| 67 try: |
| 68 # TODO(robertshield): Become an event sink instead of polling for |
| 69 # changes to the ready state. |
| 70 last_ready_state = ie.Document.ReadyState |
| 71 if last_ready_state == READYSTATE_COMPLETE: |
| 72 break |
| 73 except: |
| 74 # TODO(robertshield): Find the precise exception related to ie.Document |
| 75 # being not accessible and handle it here. |
| 76 print "Unexpected error:", sys.exc_info()[0] |
| 77 raise |
| 78 time.sleep(1) |
| 79 |
| 80 if last_ready_state != READYSTATE_COMPLETE: |
| 81 print "Timeout waiting for " + url |
| 82 |
| 83 def main(): |
| 84 parser = optparse.OptionParser() |
| 85 parser.add_option('-u', '--url_list', default='urllist', |
| 86 help='The path to the list of URLs') |
| 87 (opts, args) = parser.parse_args() |
| 88 |
| 89 urls = LoadSiteList(opts.url_list) |
| 90 ie = LaunchIE() |
| 91 for url in urls: |
| 92 RunTest(url, ie) |
| 93 time.sleep(1) |
| 94 ie.visible = 0 |
| 95 ie.Quit() |
| 96 |
| 97 |
| 98 if __name__ == '__main__': |
| 99 main() |
OLD | NEW |