forked from owiber/wpt-automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpt_batch_lib.py
141 lines (115 loc) · 4.24 KB
/
wpt_batch_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python2.6
#
# Copyright 2011 Google Inc. All Rights Reserved.
"""A library to support batch processing of WebPageTest tests.
This module provides a set of APIs for batch processing of
WebPageTest tests. A complete test cycle typically consists of
test submission, checking test status, test result download, etc.
A sample usage of this library can be found in wpt_batch.py.
"""
__author__ = '[email protected] (Qi Zhao)'
import re
import urllib
from xml.dom import minidom
def __LoadEntity(url, urlopen=urllib.urlopen):
"""A helper function to load an entity such as an URL.
Args:
url: the URL to load
urlopen: the callable to be used to load the url
Returns:
The response message
"""
response = urlopen(url)
return response
def ImportUrls(url_filename):
"""Load the URLS in the file into memory.
Args:
url_filename: the file name of the list of URLs
Returns:
The list of URLS
"""
url_list = []
for line in open(url_filename, 'rb'):
# Remove newline and trailing whitespaces
url = line.rstrip(' \r\n')
if url:
url_list.append(url)
return url_list
def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
urlopen=urllib.urlopen):
"""Submit the tests to WebPageTest server.
Args:
url_list: the list of interested URLs
test_params: the user-configured test parameters
server_url: the URL of the WebPageTest server
urlopen: the callable to be used to load the request
Returns:
A dictionary which maps a WPT test id to its URL if submission
is successful.
"""
#the browser list is actually the location parameter, that's just how it's handled
browser_list = ["bvlabs_wpt_ie9_east_wptdriver:Chrome", "bvlabs_wpt_ie9_east_wptdriver:Firefox"] #"bvlabs_wpt_ie9_east", "bvlabs_wpt_ie8_2", "bvlabs_wpt_ie7_east"]
id_url_dict = {}
for url in url_list:
for browser_type in browser_list:
test_params['url'] = url
test_params['location'] = browser_type
# print test_params['location']
request = server_url + 'runtest.php?%s' % urllib.urlencode(test_params)
request = request.replace("%3A", ":")
print request
response = __LoadEntity(request, urlopen)
# print response
return_code = response.getcode()
# print return_code
if return_code == 200:
dom = minidom.parseString(response.read())
# print dom
nodes = dom.getElementsByTagName('statusCode')
# print nodes
status = nodes[0].firstChild.wholeText
# print status
if status == '200':
test_id = dom.getElementsByTagName('testId')[0].firstChild.wholeText
# print test_id
id_url_dict[test_id] = url
return id_url_dict
def CheckBatchStatus(test_ids, server_url='http://www.webpagetest.org/',
urlopen=urllib.urlopen):
"""Check the status of tests.
Args:
test_ids: the list of interested test ids
server_url: the URL of the WebPageTest server
urlopen: the callable to be used to load the request
Returns:
A dictionary where key is the test id and content is its status.
"""
id_status_dict = {}
for test_id in test_ids:
request = server_url + 'testStatus.php?f=xml&test=' + test_id
response = __LoadEntity(request, urlopen)
if response.getcode() == 200:
dom = minidom.parseString(response.read())
nodes = dom.getElementsByTagName('statusCode')
status_code = nodes[0].firstChild.wholeText
id_status_dict[test_id] = status_code
return id_status_dict
def GetXMLResult(test_ids, server_url='http://www.webpagetest.org/',
urlopen=urllib.urlopen):
"""Obtain the test result in XML format.
Args:
test_ids: the list of interested test ids
server_url: the URL of WebPageTest server
urlopen: the callable to be used to load the request
Returns:
A dictionary where the key is test id and the value is a DOM object of the
test result.
"""
id_dom_dict = {}
for test_id in test_ids:
request = server_url + 'xmlResult/' + test_id + '/'
response = __LoadEntity(request, urlopen)
if response.getcode() == 200:
dom = minidom.parseString(response.read())
id_dom_dict[test_id] = dom
return id_dom_dict