OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
| 6 import logging |
6 import os | 7 import os |
| 8 import pickle |
7 | 9 |
| 10 import dataset_converter |
8 import pyauto_functional # Must be imported before pyauto | 11 import pyauto_functional # Must be imported before pyauto |
9 import pyauto | 12 import pyauto |
10 | 13 |
11 | 14 |
12 class AutoFillTest(pyauto.PyUITest): | 15 class AutoFillTest(pyauto.PyUITest): |
13 """Tests that autofill works correctly""" | 16 """Tests that autofill works correctly""" |
14 | 17 |
15 def Debug(self): | 18 def Debug(self): |
16 """Test method for experimentation. | 19 """Test method for experimentation. |
17 | 20 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 self.assertEqual([without_invalid], | 89 self.assertEqual([without_invalid], |
87 self.GetAutoFillProfile()['profiles']) | 90 self.GetAutoFillProfile()['profiles']) |
88 | 91 |
89 # Then try credit cards with invalid input. Should strip off all non-digits | 92 # Then try credit cards with invalid input. Should strip off all non-digits |
90 credit_card = {'CREDIT_CARD_NUMBER': 'Not_0123-5Checked'} | 93 credit_card = {'CREDIT_CARD_NUMBER': 'Not_0123-5Checked'} |
91 expected_credit_card = {'CREDIT_CARD_NUMBER': '01235'} | 94 expected_credit_card = {'CREDIT_CARD_NUMBER': '01235'} |
92 self.FillAutoFillProfile(credit_cards=[credit_card]) | 95 self.FillAutoFillProfile(credit_cards=[credit_card]) |
93 self.assertEqual([expected_credit_card], | 96 self.assertEqual([expected_credit_card], |
94 self.GetAutoFillProfile()['credit_cards']) | 97 self.GetAutoFillProfile()['credit_cards']) |
95 | 98 |
96 def testAutofillCrowdSourcing(self): | 99 def testFilterIncompleteAddresses(self): |
97 """Test able to send POST request of web form to crowd source server. | 100 """Test Autofill filters out profile with incomplete address info.""" |
98 Require a loop of 1000 submits as the source server only collects 1% of | 101 profile = {'NAME_FIRST': 'Bob', |
99 the data posted.""" | 102 'NAME_LAST': 'Smith', |
| 103 'EMAIL_ADDRESS': 'bsmith@example.com', |
| 104 'COMPANY_NAME': 'Company X', |
| 105 'PHONE_HOME_WHOLE_NUMBER': '650-123-4567',} |
| 106 url = self.GetHttpURLForDataPath( |
| 107 os.path.join('autofill', 'dup-profiles-test.html')) |
| 108 self.NavigateToURL(url) |
| 109 for key, value in profile.iteritems(): |
| 110 script = ('document.getElementById("%s").value = "%s"; ' |
| 111 'window.domAutomationController.send("done");') % (key, value) |
| 112 self.ExecuteJavascript(script, 0, 0) |
| 113 js_code = """ |
| 114 document.getElementById("merge_dup").submit(); |
| 115 window.addEventListener("unload", function() { |
| 116 window.domAutomationController.send("done"); |
| 117 }); |
| 118 """ |
| 119 self.ExecuteJavascript(js_code, 0, 0) |
| 120 self.assertEqual([], self.GetAutoFillProfile()['profiles']) |
| 121 |
| 122 def testFilterMalformedEmailAddresses(self): |
| 123 """Test Autofill filters out malformed email address during form submit.""" |
| 124 profile = {'NAME_FIRST': 'Bob', |
| 125 'NAME_LAST': 'Smith', |
| 126 'EMAIL_ADDRESS': 'garbage', |
| 127 'ADDRESS_HOME_LINE1': '1234 H St.', |
| 128 'ADDRESS_HOME_CITY': 'San Jose', |
| 129 'ADDRESS_HOME_STATE': 'CA', |
| 130 'ADDRESS_HOME_ZIP': '95110', |
| 131 'COMPANY_NAME': 'Company X', |
| 132 'PHONE_HOME_WHOLE_NUMBER': '408-123-4567',} |
| 133 url = self.GetHttpURLForDataPath( |
| 134 os.path.join('autofill', 'dup-profiles-test.html')) |
| 135 self.NavigateToURL(url) |
| 136 for key, value in profile.iteritems(): |
| 137 script = ('document.getElementById("%s").value = "%s"; ' |
| 138 'window.domAutomationController.send("done");') % (key, value) |
| 139 self.ExecuteJavascript(script, 0, 0) |
| 140 js_code = """ |
| 141 document.getElementById("merge_dup").submit(); |
| 142 window.addEventListener("unload", function() { |
| 143 window.domAutomationController.send("done"); |
| 144 }); |
| 145 """ |
| 146 self.ExecuteJavascript(js_code, 0, 0) |
| 147 if 'EMAIL_ADDRESS' in self.GetAutoFillProfile()['profiles'][0]: |
| 148 raise KeyError('TEST FAIL: Malformed email address is saved in profiles.') |
| 149 |
| 150 def testAutofillCrowdsourcing(self): |
| 151 """Test able to send POST request of web form to Autofill server. |
| 152 |
| 153 The Autofill server processes the data offline, so it can take a few days |
| 154 for the result to be detectable. Manual verification is required. |
| 155 """ |
100 # HTML file needs to be run from a specific http:// url to be able to verify | 156 # HTML file needs to be run from a specific http:// url to be able to verify |
101 # the results a few days later by visiting the same url. | 157 # the results a few days later by visiting the same url. |
102 url = 'http://www.corp.google.com/~dyu/autofill/crowdsourcing-test.html' | 158 url = 'http://www.corp.google.com/~dyu/autofill/crowdsourcing-test.html' |
103 # Adding crowdsourcing Autofill profile. | 159 # Adding crowdsourcing Autofill profile. |
104 file_path = os.path.join(self.DataDir(), 'autofill', | 160 file_path = os.path.join(self.DataDir(), 'autofill', |
105 'crowdsource_autofill.txt') | 161 'crowdsource_autofill.txt') |
106 profiles = self.EvalDataFrom(file_path) | 162 profiles = self.EvalDataFrom(file_path) |
107 self.FillAutoFillProfile(profiles=profiles) | 163 self.FillAutoFillProfile(profiles=profiles) |
| 164 # Autofill server captures 2.5% of the data posted. |
| 165 # Looping 1000 times is a safe minimum to exceed the server's threshold or |
| 166 # noise. |
108 for i in range(1000): | 167 for i in range(1000): |
109 fname = self.GetAutoFillProfile()['profiles'][0]['NAME_FIRST'] | 168 fname = self.GetAutoFillProfile()['profiles'][0]['NAME_FIRST'] |
110 lname = self.GetAutoFillProfile()['profiles'][0]['NAME_LAST'] | 169 lname = self.GetAutoFillProfile()['profiles'][0]['NAME_LAST'] |
111 email = self.GetAutoFillProfile()['profiles'][0]['EMAIL_ADDRESS'] | 170 email = self.GetAutoFillProfile()['profiles'][0]['EMAIL_ADDRESS'] |
112 # Submit form to collect crowdsourcing data for Autofill. | 171 # Submit form to collect crowdsourcing data for Autofill. |
113 self.NavigateToURL(url, 0, 0) | 172 self.NavigateToURL(url, 0, 0) |
114 fname_field = 'document.getElementById("fn").value = "%s"; ' \ | 173 fname_field = ('document.getElementById("fn").value = "%s"; ' |
115 'window.domAutomationController.send("done")' % fname | 174 'window.domAutomationController.send("done");') % fname |
116 lname_field = 'document.getElementById("ln").value = "%s"; ' \ | 175 lname_field = ('document.getElementById("ln").value = "%s"; ' |
117 'window.domAutomationController.send("done")' % lname | 176 'window.domAutomationController.send("done");') % lname |
118 email_field = 'document.getElementById("em").value = "%s"; ' \ | 177 email_field = ('document.getElementById("em").value = "%s"; ' |
119 'window.domAutomationController.send("done")' % email | 178 'window.domAutomationController.send("done");') % email |
120 self.ExecuteJavascript(fname_field, 0, 0); | 179 self.ExecuteJavascript(fname_field, 0, 0); |
121 self.ExecuteJavascript(lname_field, 0, 0); | 180 self.ExecuteJavascript(lname_field, 0, 0); |
122 self.ExecuteJavascript(email_field, 0, 0); | 181 self.ExecuteJavascript(email_field, 0, 0); |
123 self.ExecuteJavascript('document.getElementById("frmsubmit").submit();' | 182 self.ExecuteJavascript('document.getElementById("frmsubmit").submit();' |
124 'window.domAutomationController.send("done")', | 183 'window.domAutomationController.send("done");', |
125 0, 0) | 184 0, 0) |
126 | 185 |
| 186 def testMergeDuplicateProfilesInAutofill(self): |
| 187 """Test Autofill ability to merge duplicate profiles and throw away junk.""" |
| 188 # HTML file needs to be run from a http:// url. |
| 189 url = self.GetHttpURLForDataPath( |
| 190 os.path.join('autofill', 'duplicate_profiles_test.html')) |
| 191 # Run the parser script to generate the dictionary list needed for the |
| 192 # profiles. |
| 193 c = dataset_converter.DatasetConverter( |
| 194 os.path.join(self.DataDir(), 'autofill', 'dataset.txt'), |
| 195 logging_level=logging.INFO) # Set verbosity to INFO, WARNING, ERROR. |
| 196 list_of_dict = c.Convert() |
| 197 |
| 198 for profile in list_of_dict: |
| 199 self.NavigateToURL(url) |
| 200 for key, value in profile.iteritems(): |
| 201 script = ('document.getElementById("%s").value = "%s"; ' |
| 202 'window.domAutomationController.send("done");') % (key, value) |
| 203 self.ExecuteJavascript(script, 0, 0) |
| 204 self.ExecuteJavascript('document.getElementById("merge_dup").submit();' |
| 205 'window.domAutomationController.send("done");', |
| 206 0, 0) |
| 207 # Verify total number of inputted profiles is greater than the final number |
| 208 # of profiles after merging. |
| 209 self.assertTrue( |
| 210 len(list_of_dict) > len(self.GetAutoFillProfile()['profiles'])) |
| 211 # Write profile dictionary to a file. |
| 212 merged_profile = os.path.join(self.DataDir(), 'autofill', |
| 213 'merged-profiles.txt') |
| 214 profile_dict = self.GetAutoFillProfile()['profiles'] |
| 215 output = open(merged_profile, 'wb') |
| 216 pickle.dump(profile_dict, output) |
| 217 output.close() |
| 218 |
127 | 219 |
128 if __name__ == '__main__': | 220 if __name__ == '__main__': |
129 pyauto_functional.Main() | 221 pyauto_functional.Main() |
OLD | NEW |