OLD | NEW |
| (Empty) |
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 import chromium_proxy_server_ex | |
7 import commands | |
8 import logging | |
9 import optparse | |
10 import os | |
11 import re | |
12 import selenium.selenium | |
13 import shutil | |
14 import subprocess | |
15 import sys | |
16 import tempfile | |
17 import time | |
18 import unittest | |
19 import _winreg | |
20 import selenium.webdriver.common.keys | |
21 | |
22 from selenium import webdriver | |
23 from selenium.webdriver.common.keys import Keys as Keys | |
24 from selenium.webdriver.support.ui import WebDriverWait | |
25 | |
26 class RlzTest(unittest.TestCase): | |
27 | |
28 proxy_server_file = '' | |
29 chrome_driver_path = '' | |
30 | |
31 def setUp(self): | |
32 """Performs necessary setup work before running each test in this class.""" | |
33 # Delete RLZ key Folder | |
34 self.deleteRegistryKey() | |
35 # Launch Proxy Server | |
36 print ('Serving clients: 127.0.0.1') | |
37 self.proxy_server = subprocess.Popen([ | |
38 'python', | |
39 RlzTest.proxy_server_file, | |
40 '--client=127.0.0.1', | |
41 '--port=8080', | |
42 '--urls=http://clients1.google.com/tools/pso/ping,www.google.com']) | |
43 print('\nLaunching Chrome...') | |
44 # Launch chrome and set proxy. | |
45 self.temp_data_dir = tempfile.mkdtemp() | |
46 self.launchChrome(self.temp_data_dir) | |
47 | |
48 def tearDown(self): | |
49 """Kills the chrome driver after after the test method has been called.""" | |
50 # Terminate the chrome driver. | |
51 print '\nTerminating Chrome Driver...' | |
52 self.driver.quit() | |
53 | |
54 # Kill proxy server. | |
55 print '\nKilling Proxy Server...' | |
56 subprocess.Popen.kill(self.proxy_server) | |
57 | |
58 # Delete temp profile directory | |
59 try: | |
60 shutil.rmtree(self.temp_data_dir) # delete directory | |
61 except OSError, e: | |
62 if e.errno != 2: # code 2 - no such file or directory | |
63 raise | |
64 | |
65 def launchChrome(self, data_directory): | |
66 """Launch chrome using chrome driver. | |
67 | |
68 Args: | |
69 data_directory: Temp directory to store preference data. | |
70 """ | |
71 service = webdriver.chrome.service.Service(RlzTest.chrome_driver_path) | |
72 service.start() | |
73 self.driver = webdriver.Remote( | |
74 service.service_url, { | |
75 'proxy': {'proxyType': 'manual', 'httpProxy': 'localhost:8080'}, | |
76 'chrome.nativeEvents': True, | |
77 'chrome.switches': ['disable-extensions', | |
78 r'user-data-dir=' + data_directory]}) | |
79 | |
80 def deleteRegistryKey(self): | |
81 """Delete RLZ key Folder from win registry.""" | |
82 try: | |
83 hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, | |
84 'Software\\Google\\Common\\Rlz') | |
85 except _winreg.error, err: | |
86 return True | |
87 if(hkey): | |
88 _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, | |
89 'Software\\Google\\Common\\Rlz\\Events\\C') | |
90 _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, | |
91 'Software\\Google\\Common\\Rlz\\StatefulEvents\\C') | |
92 _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, | |
93 'Software\\Google\\Common\\Rlz\\Events') | |
94 _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, | |
95 'Software\\Google\\Common\\Rlz\\StatefulEvents') | |
96 _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, | |
97 'Software\\Google\\Common\\Rlz\\PTimes') | |
98 _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, | |
99 'Software\\Google\\Common\\Rlz\\RLZs') | |
100 _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, | |
101 'Software\\Google\\Common\\Rlz') | |
102 | |
103 def GetKeyValueNames(self, key, subkey): | |
104 """Get the values for particular subkey | |
105 | |
106 Args: | |
107 key: Key is one of the predefined HKEY_* constants. | |
108 subkey: It is a string that identifies the sub_key to delete. | |
109 """ | |
110 list_names = [] | |
111 counter = 0 | |
112 try: | |
113 hkey = _winreg.OpenKey(key, subkey) | |
114 except _winreg.error, err: | |
115 return -1 | |
116 while True: | |
117 try: | |
118 value = _winreg.EnumValue(hkey, counter) | |
119 list_names.append(value[0]) | |
120 counter += 1 | |
121 except _winreg.error: | |
122 break | |
123 hkey.Close() | |
124 return list_names | |
125 | |
126 def _AssertEventsInPing(self, log_file, excepted_event_list, readFile=1): | |
127 """ Asserts events in ping appended. | |
128 | |
129 Args: | |
130 contents: String variable contains contents of log file. | |
131 excepted_event_list: List of expected events in ping. | |
132 readFile: Reading order for file. Default is 1 (Top to Bottom). | |
133 """ | |
134 for line in log_file[::readFile]: | |
135 if(re.search('events=', line)): | |
136 event_start = line.find('events=') | |
137 event_end = line.find('&rep', event_start) | |
138 events = line[event_start + 7 : event_end] | |
139 events_List = events.split(',') | |
140 print 'event_list',events_List | |
141 break | |
142 # Validate events in url ping. | |
143 for event in excepted_event_list: | |
144 self.assertTrue(event in events_List) | |
145 # Validate brand code in url ping. | |
146 self.assertTrue(re.search('CHMZ', line)) | |
147 # Print first chrome launch ping on Console. | |
148 start = line.find('http://clients1.google.com/tools/'+ | |
149 'pso/ping?as=chrome&brand=CHMZ') | |
150 end = line.find('http://www', start) | |
151 print '\nChrome Launch ping sent :\n', line[start:end] | |
152 | |
153 | |
154 def _AssertEventsInRegistry(self, excepted_reg_keys): | |
155 """ Asserts events reported in win registry. | |
156 | |
157 Args: | |
158 excepted_reg_keys: List of expected events in win registry. | |
159 """ | |
160 list_key=self.GetKeyValueNames(_winreg.HKEY_CURRENT_USER, | |
161 'Software\Google\Common\Rlz\StatefulEvents\C') | |
162 print ('\nList of event reported to registry-' | |
163 'Software\Google\Common\Rlz\StatefulEvents:', list_key) | |
164 for key in excepted_reg_keys: | |
165 self.assertTrue(key in list_key) | |
166 | |
167 def _AssertRlzValues(self, log_file, readFile=1): | |
168 """ Asserts RLZ values. | |
169 | |
170 Args: | |
171 log_file: String variable contains contents of log file. | |
172 readFile: Reading order for file. Default is 1 (Top to Bottom). | |
173 """ | |
174 for line in log_file[::readFile]: | |
175 if(re.search('events=', line)): | |
176 event_start = line.find('rlz=') | |
177 event_end = line.find('&id', event_start) | |
178 events = line[event_start + 4 : event_end] | |
179 events_List = events.split(',') | |
180 self.assertTrue('C1:' in events_List) | |
181 self.assertTrue('C2:' in events_List) | |
182 | |
183 def _searchFromOmnibox(self, searchString): | |
184 """ Asserts RLZ values. | |
185 | |
186 Args: | |
187 searchString: Input string to be searched. | |
188 """ | |
189 self.driver.switch_to_active_element().send_keys(Keys.CONTROL + 'l') | |
190 self.driver.switch_to_active_element().send_keys(searchString) | |
191 self.driver.switch_to_active_element().send_keys(Keys.ENTER) | |
192 time.sleep(2) | |
193 | |
194 def testRlzPingAtFirstChromeLaunch(self): | |
195 """Test rlz ping when chrome is launched for first time.""" | |
196 # Wait for 100 sec till chrome sends ping to server. | |
197 time.sleep(100) | |
198 self.driver.get('http://www.google.com') | |
199 | |
200 # Open log file. | |
201 log_file = open( | |
202 os.getcwd() + '\chromium_proxy_server.log', 'r', 1).readlines() | |
203 | |
204 # Validate events first chrome launch(C1I,C2I,C1S) are appended in ping. | |
205 excepted_events = ['C1S', 'C1I', 'C2I'] | |
206 self._AssertEventsInPing(log_file, excepted_events) | |
207 | |
208 # Validate events in win registry. | |
209 excepted_reg_keys = ['C1I', 'C2I'] | |
210 self._AssertEventsInRegistry(excepted_reg_keys) | |
211 | |
212 def testRlzPingForFirstSearch(self): | |
213 """Test rlz ping when first search is performed in chrome.""" | |
214 # Type search string in omnibox. | |
215 self._searchFromOmnibox('java') | |
216 print '\nCurrent Url before chrome ping sent:\n', self.driver.current_url | |
217 | |
218 # Assert brand code 'CHMZ' is not appended in search string. | |
219 self.assertFalse(re.search('CHMZ', self.driver.current_url)) | |
220 | |
221 # Wait for 100 sec till chrome sends ping to server. | |
222 time.sleep(100) | |
223 | |
224 # Open log file. | |
225 log_file = open( | |
226 os.getcwd() + '\chromium_proxy_server.log', 'r', 1).readlines() | |
227 | |
228 # Validate events first chrome launch(C1I,C2I,C1S) and | |
229 # first search(C1F) are appended in ping. | |
230 excepted_events = ['C1S', 'C1I', 'C2I', 'C1F'] | |
231 self._AssertEventsInPing(log_file, excepted_events) | |
232 | |
233 # Assert C1, C2 rlz value appended in ping | |
234 self._AssertRlzValues(log_file) | |
235 | |
236 # Type search string in omnibox after ping is sent to server. | |
237 self._searchFromOmnibox('java') | |
238 print '\nCurrent Url after chrome ping sent:\n', self.driver.current_url | |
239 | |
240 # Assert brand code 'CHMZ' is appended in search string. | |
241 self.assertTrue(re.search('CHMZ', self.driver.current_url)) | |
242 | |
243 # Validate events in win registry. | |
244 excepted_reg_keys=['C1I', 'C2I', 'C1F'] | |
245 self._AssertEventsInRegistry(excepted_reg_keys) | |
246 | |
247 # Assert the log for search ping with query string/brand code appended. | |
248 log_file = open( | |
249 os.getcwd() + '\chromium_proxy_server.log', 'r', 1).readlines() | |
250 searchStringFound = False | |
251 for line in log_file[::-1]: | |
252 if(re.search('search?', line)): | |
253 self.assertTrue(re.search('java', line)) | |
254 self.assertTrue(re.search('CHMZ', line)) | |
255 print '\nChrome search ping send\n', line | |
256 searchStringFound = True | |
257 break | |
258 self.assertTrue(searchStringFound, 'Search Query String Not Found') | |
259 | |
260 def rlzInput(): | |
261 proxy_server_file = raw_input("Enter Proxy Server File Name: ") | |
262 chrome_driver_path = raw_input("Enter chrome driver path in"+ | |
263 "your system(c:\\chrome\\..):") | |
264 return (proxy_server_file, chrome_driver_path) | |
265 | |
266 if __name__ == '__main__': | |
267 server, driver = rlzInput() | |
268 RlzTest.proxy_server_file = server | |
269 RlzTest.chrome_driver_path = driver | |
270 unittest.main() | |
OLD | NEW |