1
2
3
4
5 """A library to support batch processing of WebPageTest tests.
6
7 This module provides a set of APIs for batch processing of
8 WebPageTest tests. A complete test cycle typically consists of
9 test submission, checking test status, test result download, etc.
10
11 A sample usage of this library can be found in wpt_batch.py.
12 """
13
14 __author__ = 'zhaoq@google.com (Qi Zhao)'
15
16 import re
17 import urllib
18 from xml.dom import minidom
19
20
22 """A helper function to load an entity such as an URL.
23
24 Args:
25 url: the URL to load
26 urlopen: the callable to be used to load the url
27
28 Returns:
29 The response message
30 """
31 response = urlopen(url)
32 return response
33
34
36 """Load the URLS in the file into memory.
37
38 Args:
39 url_filename: the file name of the list of URLs
40
41 Returns:
42 The list of URLS
43 """
44 url_list = []
45 for line in open(url_filename, 'rb'):
46
47 url = line.rstrip(' \r\n')
48 if url:
49 url_list.append(url)
50 return url_list
51
52
53 -def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
54 urlopen=urllib.urlopen):
55 """Submit the tests to WebPageTest server.
56
57 Args:
58 url_list: the list of interested URLs
59 test_params: the user-configured test parameters
60 server_url: the URL of the WebPageTest server
61 urlopen: the callable to be used to load the request
62
63 Returns:
64 A dictionary which maps a WPT test id to its URL if submission
65 is successful.
66 """
67 id_url_dict = {}
68 for url in url_list:
69 test_params['url'] = url
70 request = server_url + 'runtest.php?%s' % urllib.urlencode(test_params)
71 response = __LoadEntity(request, urlopen)
72 return_code = response.getcode()
73 if return_code == 200:
74 dom = minidom.parseString(response.read())
75 nodes = dom.getElementsByTagName('statusCode')
76 status = nodes[0].firstChild.wholeText
77 if status == '200':
78 test_id = dom.getElementsByTagName('testId')[0].firstChild.wholeText
79 id_url_dict[test_id] = url
80 return id_url_dict
81
82
83 -def CheckBatchStatus(test_ids, server_url='http://www.webpagetest.org/',
84 urlopen=urllib.urlopen):
85 """Check the status of tests.
86
87 Args:
88 test_ids: the list of interested test ids
89 server_url: the URL of the WebPageTest server
90 urlopen: the callable to be used to load the request
91
92 Returns:
93 A dictionary where key is the test id and content is its status.
94 """
95 id_status_dict = {}
96 for test_id in test_ids:
97 request = server_url + 'testStatus.php?f=xml&test=' + test_id
98 response = __LoadEntity(request, urlopen)
99 if response.getcode() == 200:
100 dom = minidom.parseString(response.read())
101 nodes = dom.getElementsByTagName('statusCode')
102 status_code = nodes[0].firstChild.wholeText
103 id_status_dict[test_id] = status_code
104 return id_status_dict
105
106
107 -def GetXMLResult(test_ids, server_url='http://www.webpagetest.org/',
108 urlopen=urllib.urlopen):
109 """Obtain the test result in XML format.
110
111 Args:
112 test_ids: the list of interested test ids
113 server_url: the URL of WebPageTest server
114 urlopen: the callable to be used to load the request
115
116 Returns:
117 A dictionary where the key is test id and the value is a DOM object of the
118 test result.
119 """
120 id_dom_dict = {}
121 for test_id in test_ids:
122 request = server_url + 'xmlResult/' + test_id + '/'
123 response = __LoadEntity(request, urlopen)
124 if response.getcode() == 200:
125 dom = minidom.parseString(response.read())
126 id_dom_dict[test_id] = dom
127 return id_dom_dict
128