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