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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
|
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import os
import pickle
import re
import simplejson
import autofill_dataset_converter
import autofill_dataset_generator
import pyauto_functional # Must be imported before pyauto
import pyauto
import test_utils
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_pages import settings
class AutofillTest(pyauto.PyUITest):
"""Tests that autofill works correctly"""
def setUp(self):
pyauto.PyUITest.setUp(self)
self._driver = self.NewWebDriver()
def Debug(self):
"""Test method for experimentation.
This method will not run automatically.
"""
while True:
raw_input('Hit <enter> to dump info.. ')
self.pprint(self.GetAutofillProfile())
def testFillProfile(self):
"""Test filling profiles and overwriting with new profiles."""
profiles = [{'NAME_FIRST': ['Bob',],
'NAME_LAST': ['Smith',], 'ADDRESS_HOME_ZIP': ['94043',],},
{'EMAIL_ADDRESS': ['sue@example.com',],
'COMPANY_NAME': ['Company X',],}]
credit_cards = [{'CREDIT_CARD_NUMBER': '6011111111111117',
'CREDIT_CARD_EXP_MONTH': '12',
'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2011'},
{'CREDIT_CARD_NAME': 'Bob C. Smith'}]
self.FillAutofillProfile(profiles=profiles, credit_cards=credit_cards)
profile = self.GetAutofillProfile()
self.assertEqual(profiles, profile['profiles'])
self.assertEqual(credit_cards, profile['credit_cards'])
profiles = [ {'NAME_FIRST': ['Larry']}]
self.FillAutofillProfile(profiles=profiles)
profile = self.GetAutofillProfile()
self.assertEqual(profiles, profile['profiles'])
self.assertEqual(credit_cards, profile['credit_cards'])
def testFillProfileMultiValue(self):
"""Test filling a profile with multi-value data."""
profile_expected = [{'NAME_FIRST': ['Bob', 'Joe'],
'NAME_LAST': ['Smith', 'Jones'],
'ADDRESS_HOME_ZIP': ['94043',],},]
self.FillAutofillProfile(profiles=profile_expected)
profile_actual = self.GetAutofillProfile()
self.assertEqual(profile_expected, profile_actual['profiles'])
def testFillProfileCrazyCharacters(self):
"""Test filling profiles with unicode strings and crazy characters."""
# Adding autofill profiles.
file_path = os.path.join(self.DataDir(), 'autofill', 'functional',
'crazy_autofill.txt')
profiles = self.EvalDataFrom(file_path)
self.FillAutofillProfile(profiles=profiles)
self.assertEqual(profiles, self.GetAutofillProfile()['profiles'],
msg='Autofill profile data does not match.')
# Adding credit cards.
file_path = os.path.join(self.DataDir(), 'autofill', 'functional',
'crazy_creditcards.txt')
test_data = self.EvalDataFrom(file_path)
credit_cards_input = test_data['input']
self.FillAutofillProfile(credit_cards=credit_cards_input)
self.assertEqual(test_data['expected'],
self.GetAutofillProfile()['credit_cards'],
msg='Autofill credit card data does not match.')
def testGetProfilesEmpty(self):
"""Test getting profiles when none have been filled."""
profile = self.GetAutofillProfile()
self.assertEqual([], profile['profiles'])
self.assertEqual([], profile['credit_cards'])
def testAutofillInvalid(self):
"""Test filling in invalid values for profiles are saved as-is.
Phone information entered into the prefs UI is not validated or rejected
except for duplicates.
"""
# First try profiles with invalid ZIP input.
without_invalid = {'NAME_FIRST': ['Will',],
'ADDRESS_HOME_CITY': ['Sunnyvale',],
'ADDRESS_HOME_STATE': ['CA',],
'ADDRESS_HOME_ZIP': ['my_zip',],
'ADDRESS_HOME_COUNTRY': ['United States',]}
# Add invalid data for phone field.
with_invalid = without_invalid.copy()
with_invalid['PHONE_HOME_WHOLE_NUMBER'] = ['Invalid_Phone_Number',]
self.FillAutofillProfile(profiles=[with_invalid])
self.assertNotEqual(
[without_invalid], self.GetAutofillProfile()['profiles'],
msg='Phone data entered into prefs UI is validated.')
def testAutofillPrefsStringSavedAsIs(self):
"""Test invalid credit card numbers typed in prefs should be saved as-is."""
credit_card = {'CREDIT_CARD_NUMBER': 'Not_0123-5Checked'}
self.FillAutofillProfile(credit_cards=[credit_card])
self.assertEqual([credit_card],
self.GetAutofillProfile()['credit_cards'],
msg='Credit card number in prefs not saved as-is.')
def _WaitForWebpageFormReadyToFillIn(self, form_profile, tab_index, windex):
"""Waits until an autofill form on a webpage is ready to be filled in.
A call to NavigateToURL() may return before all form elements on the page
are ready to be accessed. This function waits until they are ready to be
filled in.
Args:
form_profile: A dictionary representing an autofill profile in which the
keys are strings corresponding to webpage element IDs.
tab_index: The index of the tab containing the webpage form to check.
windex: The index of the window containing the webpage form to check.
"""
field_check_code = ''.join(
['if (!document.getElementById("%s")) ready = "false";' %
key for key in form_profile.keys()])
js = """
var ready = 'true';
if (!document.getElementById("testform"))
ready = 'false';
%s
window.domAutomationController.send(ready);
""" % field_check_code
self.assertTrue(
self.WaitUntil(lambda: self.ExecuteJavascript(js, tab_index, windex),
expect_retval='true'),
msg='Timeout waiting for webpage form to be ready to be filled in.')
def _FillFormAndSubmit(self, datalist, filename, tab_index=0, windex=0):
"""Navigate to the form, input values into the fields, and submit the form.
If multiple profile dictionaries are specified as input, this function will
repeatedly navigate to the form, fill it out, and submit it, once for each
specified profile dictionary.
Args:
datalist: A list of dictionaries, where each dictionary represents the
key/value pairs for profiles or credit card values.
filename: HTML form website file. The file is the basic file name and not
the path to the file. File is assumed to be located in
autofill/functional directory of the data folder.
tab_index: Integer index of the tab to work on; defaults to 0 (first tab).
windex: Integer index of the browser window to work on; defaults to 0
(first window).
"""
url = self.GetHttpURLForDataPath('autofill', 'functional', filename)
for profile in datalist:
self.NavigateToURL(url)
self._WaitForWebpageFormReadyToFillIn(profile, tab_index, windex)
# Fill in and submit the form.
js = ''.join(['document.getElementById("%s").value = "%s";' %
(key, value) for key, value in profile.iteritems()])
js += 'document.getElementById("testform").submit();'
self.SubmitAutofillForm(js, tab_index=tab_index, windex=windex)
def _LuhnCreditCardNumberValidator(self, number):
"""Validates whether a number is valid or invalid using the Luhn test.
Validation example:
1. Example number: 49927398716
2. Reverse the digits: 61789372994
3. Sum the digits in the odd-numbered position for s1:
6 + 7 + 9 + 7 + 9 + 4 = 42
4. Take the digits in the even-numbered position: 1, 8, 3, 2, 9
4.1. Two times each digit in the even-numbered position: 2, 16, 6, 4, 18
4.2. For each resulting value that is now 2 digits, add the digits
together: 2, 7, 6, 4, 9
(0 + 2 = 2, 1 + 6 = 7, 0 + 6 = 6, 0 + 4 = 4, 1 + 8 = 9)
4.3. Sum together the digits for s2: 2 + 7 + 6 + 4 + 9 = 28
5. Sum together s1 + s2 and if the sum ends in zero, the number passes the
Luhn test: 42 + 28 = 70 which is a valid credit card number.
Args:
number: the credit card number being validated, as a string.
Returns:
boolean whether the credit card number is valid or not.
"""
# Filters out non-digit characters.
number = re.sub('[^0-9]', '', number)
reverse = [int(ch) for ch in str(number)][::-1]
# The divmod of the function splits a number into two digits, ready for
# summing.
return ((sum(reverse[0::2]) + sum(sum(divmod(d*2, 10))
for d in reverse[1::2])) % 10 == 0)
def testInvalidCreditCardNumberIsNotAggregated(self):
"""Test credit card info with an invalid number is not aggregated.
When filling out a form with an invalid credit card number (one that
does not pass the Luhn test) the credit card info should not be saved into
Autofill preferences.
"""
invalid_cc_info = {'CREDIT_CARD_NAME': 'Bob Smith',
'CREDIT_CARD_NUMBER': '4408 0412 3456 7890',
'CREDIT_CARD_EXP_MONTH': '12',
'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'}
cc_number = invalid_cc_info['CREDIT_CARD_NUMBER']
self._FillFormAndSubmit([invalid_cc_info], 'autofill_creditcard_form.html',
tab_index=0, windex=0)
self.assertFalse(self._LuhnCreditCardNumberValidator(cc_number),
msg='This test requires an invalid credit card number.')
cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars']
self.assertFalse(
cc_infobar, msg='Save credit card infobar offered to save CC info.')
def testWhitespacesAndSeparatorCharsStrippedForValidCCNums(self):
"""Test whitespaces and separator chars are stripped for valid CC numbers.
The credit card numbers used in this test pass the Luhn test.
For reference: http://www.merriampark.com/anatomycc.htm
"""
credit_card_info = [{'CREDIT_CARD_NAME': 'Bob Smith',
'CREDIT_CARD_NUMBER': '4408 0412 3456 7893',
'CREDIT_CARD_EXP_MONTH': '12',
'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'},
{'CREDIT_CARD_NAME': 'Jane Doe',
'CREDIT_CARD_NUMBER': '4417-1234-5678-9113',
'CREDIT_CARD_EXP_MONTH': '10',
'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2013'}]
url = self.GetHttpURLForDataPath(
'autofill', 'functional', 'autofill_creditcard_form.html')
for cc_info in credit_card_info:
self.assertTrue(
self._LuhnCreditCardNumberValidator(cc_info['CREDIT_CARD_NUMBER']),
msg='This test requires a valid credit card number.')
self.NavigateToURL(url)
self._WaitForWebpageFormReadyToFillIn(cc_info, 0, 0)
# Fill in and submit the form.
js = ''.join(['document.getElementById("%s").value = "%s";' %
(key, value) for key, value in cc_info.iteritems()])
js += 'document.getElementById("testform").submit();'
self.SubmitAutofillForm(js, tab_index=0, windex=0)
# Verify the filled-in credit card number against the aggregated number.
aggregated_cc_1 = (
self.GetAutofillProfile()['credit_cards'][0]['CREDIT_CARD_NUMBER'])
aggregated_cc_2 = (
self.GetAutofillProfile()['credit_cards'][1]['CREDIT_CARD_NUMBER'])
self.assertFalse((' ' in aggregated_cc_1 or ' ' in aggregated_cc_2 or
'-' in aggregated_cc_1 or '-' in aggregated_cc_2),
msg='Whitespaces or separator chars not stripped.')
def testAggregatesMinValidProfile(self):
"""Test that Autofill aggregates a minimum valid profile.
The minimum required address fields must be specified: First Name,
Last Name, Address Line 1, City, Zip Code, and State.
"""
profile = {'NAME_FIRST': 'Bob',
'NAME_LAST': 'Smith',
'ADDRESS_HOME_LINE1': '1234 H St.',
'ADDRESS_HOME_CITY': 'Mountain View',
'ADDRESS_HOME_STATE': 'CA',
'ADDRESS_HOME_ZIP': '95110'}
self._FillFormAndSubmit(
[profile], 'duplicate_profiles_test.html', tab_index=0, windex=0)
self.assertTrue(self.GetAutofillProfile()['profiles'],
msg='Profile with minimum address values not aggregated.')
def testProfilesNotAggregatedWithNoAddress(self):
"""Test Autofill does not aggregate profiles with no address info.
The minimum required address fields must be specified: First Name,
Last Name, Address Line 1, City, Zip Code, and State.
"""
profile = {'NAME_FIRST': 'Bob',
'NAME_LAST': 'Smith',
'EMAIL_ADDRESS': 'bsmith@example.com',
'COMPANY_NAME': 'Company X',
'ADDRESS_HOME_CITY': 'Mountain View',
'PHONE_HOME_WHOLE_NUMBER': '650-555-4567',}
self._FillFormAndSubmit(
[profile], 'duplicate_profiles_test.html', tab_index=0, windex=0)
self.assertFalse(self.GetAutofillProfile()['profiles'],
msg='Profile with no address info was aggregated.')
def testProfilesNotAggregatedWithInvalidEmail(self):
"""Test Autofill does not aggregate profiles with an invalid email."""
profile = {'NAME_FIRST': 'Bob',
'NAME_LAST': 'Smith',
'EMAIL_ADDRESS': 'garbage',
'ADDRESS_HOME_LINE1': '1234 H St.',
'ADDRESS_HOME_CITY': 'San Jose',
'ADDRESS_HOME_STATE': 'CA',
'ADDRESS_HOME_ZIP': '95110',
'COMPANY_NAME': 'Company X',
'PHONE_HOME_WHOLE_NUMBER': '408-871-4567',}
self._FillFormAndSubmit(
[profile], 'duplicate_profiles_test.html', tab_index=0, windex=0)
self.assertFalse(self.GetAutofillProfile()['profiles'],
msg='Profile with invalid email was aggregated.')
def testComparePhoneNumbers(self):
"""Test phone fields parse correctly from a given profile.
The high level key presses execute the following: Select the first text
field, invoke the autofill popup list, select the first profile within the
list, and commit to the profile to populate the form.
"""
profile_path = os.path.join(self.DataDir(), 'autofill', 'functional',
'phone_pinput_autofill.txt')
profile_expected_path = os.path.join(
self.DataDir(), 'autofill', 'functional',
'phone_pexpected_autofill.txt')
profiles = self.EvalDataFrom(profile_path)
profiles_expected = self.EvalDataFrom(profile_expected_path)
self.FillAutofillProfile(profiles=profiles)
url = self.GetHttpURLForDataPath(
'autofill', 'functional', 'form_phones.html')
for profile_expected in profiles_expected:
self.NavigateToURL(url)
self.assertTrue(self.AutofillPopulateForm('NAME_FIRST'),
msg='Autofill form could not be populated.')
form_values = {}
for key, value in profile_expected.iteritems():
js_returning_field_value = (
'var field_value = document.getElementById("%s").value;'
'window.domAutomationController.send(field_value);'
) % key
form_values[key] = self.ExecuteJavascript(
js_returning_field_value, 0, 0)
self.assertEqual(
[form_values[key]], value,
msg=('Original profile not equal to expected profile at key: "%s"\n'
'Expected: "%s"\nReturned: "%s"' % (
key, value, [form_values[key]])))
def testProfileSavedWithValidCountryPhone(self):
"""Test profile is saved if phone number is valid in selected country.
The data file contains two profiles with valid phone numbers and two
profiles with invalid phone numbers from their respective country.
"""
profiles_list = self.EvalDataFrom(
os.path.join(self.DataDir(), 'autofill', 'functional',
'phonechecker.txt'))
self._FillFormAndSubmit(profiles_list, 'autofill_test_form.html',
tab_index=0, windex=0)
num_profiles = len(self.GetAutofillProfile()['profiles'])
self.assertEqual(2, num_profiles,
msg='Expected 2 profiles, but got %d.' % num_profiles)
def testCharsStrippedForAggregatedPhoneNumbers(self):
"""Test aggregated phone numbers are standardized (not saved "as-is")."""
profiles_list = self.EvalDataFrom(
os.path.join(self.DataDir(), 'autofill', 'functional',
'phonecharacters.txt'))
self._FillFormAndSubmit(profiles_list, 'autofill_test_form.html',
tab_index=0, windex=0)
us_phone = self.GetAutofillProfile()[
'profiles'][0]['PHONE_HOME_WHOLE_NUMBER']
de_phone = self.GetAutofillProfile()[
'profiles'][1]['PHONE_HOME_WHOLE_NUMBER']
self.assertEqual(
['+1 408-871-4567',], us_phone,
msg='Aggregated US phone number %s not standardized.' % us_phone)
self.assertEqual(
['+49 40/808179000',], de_phone,
msg='Aggregated Germany phone number %s not standardized.' % de_phone)
def testAppendCountryCodeForAggregatedPhones(self):
"""Test Autofill appends country codes to aggregated phone numbers.
The country code is added for the following case:
The phone number contains the correct national number size and
is a valid format.
"""
profile = {'NAME_FIRST': 'Bob',
'NAME_LAST': 'Smith',
'ADDRESS_HOME_LINE1': '1234 H St.',
'ADDRESS_HOME_CITY': 'San Jose',
'ADDRESS_HOME_STATE': 'CA',
'ADDRESS_HOME_ZIP': '95110',
'ADDRESS_HOME_COUNTRY': 'Germany',
'PHONE_HOME_WHOLE_NUMBER': '(08) 450 777-777',}
self._FillFormAndSubmit(
[profile], 'autofill_test_form.html', tab_index=0, windex=0)
de_phone = self.GetAutofillProfile()[
'profiles'][0]['PHONE_HOME_WHOLE_NUMBER']
self.assertEqual(
'+49', de_phone[0][:3],
msg='Country code missing from phone number %s.' % de_phone)
def testCCInfoNotStoredWhenAutocompleteOff(self):
"""Test CC info not offered to be saved when autocomplete=off for CC field.
If the credit card number field has autocomplete turned off, then the credit
card infobar should not offer to save the credit card info. The credit card
number must be a valid Luhn number.
"""
credit_card_info = {'CREDIT_CARD_NAME': 'Bob Smith',
'CREDIT_CARD_NUMBER': '4408041234567893',
'CREDIT_CARD_EXP_MONTH': '12',
'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'}
self._FillFormAndSubmit(
[credit_card_info], 'cc_autocomplete_off_test.html',
tab_index=0, windex=0)
cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars']
self.assertFalse(cc_infobar,
msg='Save credit card infobar offered to save CC info.')
def testNoAutofillForReadOnlyFields(self):
"""Test that Autofill does not fill in read-only fields."""
profile = {'NAME_FIRST': ['Bob',],
'NAME_LAST': ['Smith',],
'EMAIL_ADDRESS': ['bsmith@gmail.com',],
'ADDRESS_HOME_LINE1': ['1234 H St.',],
'ADDRESS_HOME_CITY': ['San Jose',],
'ADDRESS_HOME_STATE': ['CA',],
'ADDRESS_HOME_ZIP': ['95110',],
'COMPANY_NAME': ['Company X',],
'PHONE_HOME_WHOLE_NUMBER': ['408-871-4567',],}
self.FillAutofillProfile(profiles=[profile])
url = self.GetHttpURLForDataPath(
'autofill', 'functional', 'read_only_field_test.html')
self.NavigateToURL(url)
self.assertTrue(self.AutofillPopulateForm('firstname'),
msg='Autofill form could not be populated.')
js_return_readonly_field = (
'var field_value = document.getElementById("email").value;'
'window.domAutomationController.send(field_value);')
readonly_field_value = self.ExecuteJavascript(
js_return_readonly_field, 0, 0)
js_return_addrline1_field = (
'var field_value = document.getElementById("address").value;'
'window.domAutomationController.send(field_value);')
addrline1_field_value = self.ExecuteJavascript(
js_return_addrline1_field, 0, 0)
self.assertNotEqual(
readonly_field_value, profile['EMAIL_ADDRESS'][0],
'Autofill filled in value "%s" for a read-only field.'
% readonly_field_value)
self.assertEqual(
addrline1_field_value, profile['ADDRESS_HOME_LINE1'][0],
'Unexpected value "%s" in the Address field.' % addrline1_field_value)
def testFormFillableOnReset(self):
"""Test form is fillable from a profile after form was reset.
Steps:
1. Fill form using a saved profile.
2. Reset the form.
3. Fill form using a saved profile.
"""
profile = {'NAME_FIRST': ['Bob',],
'NAME_LAST': ['Smith',],
'EMAIL_ADDRESS': ['bsmith@gmail.com',],
'ADDRESS_HOME_LINE1': ['1234 H St.',],
'ADDRESS_HOME_CITY': ['San Jose',],
'PHONE_HOME_WHOLE_NUMBER': ['4088714567',],}
self.FillAutofillProfile(profiles=[profile])
url = self.GetHttpURLForDataPath(
'autofill', 'functional', 'autofill_test_form.html')
self.NavigateToURL(url)
# Fill form using an address profile.
self.assertTrue(self.AutofillPopulateForm('NAME_FIRST'),
msg='Autofill form could not be populated.')
# Reset the form.
self.ExecuteJavascript('document.getElementById("testform").reset();'
'window.domAutomationController.send("done");',
0, 0)
# Fill in the form using an Autofill profile.
self.assertTrue(self.AutofillPopulateForm('NAME_FIRST'),
msg='Autofill form could not be populated.')
# Verify value in fields match value in the profile dictionary.
form_values = {}
for key, value in profile.iteritems():
js_returning_field_value = (
'var field_value = document.getElementById("%s").value;'
'window.domAutomationController.send(field_value);'
) % key
form_values[key] = self.ExecuteJavascript(
js_returning_field_value, 0, 0)
self.assertEqual(
[form_values[key]], value,
msg=('Original profile not equal to expected profile at key: "%s"\n'
'Expected: "%s"\nReturned: "%s"' % (
key, value, [form_values[key]])))
def testDistinguishMiddleInitialWithinName(self):
"""Test Autofill distinguishes a middle initial in a name."""
profile = {'NAME_FIRST': ['Bob',],
'NAME_MIDDLE': ['Leo',],
'NAME_LAST': ['Smith',],
'EMAIL_ADDRESS': ['bsmith@gmail.com',],
'ADDRESS_HOME_LINE1': ['1234 H St.',],
'ADDRESS_HOME_CITY': ['San Jose',],
'PHONE_HOME_WHOLE_NUMBER': ['4088714567',],}
middle_initial = profile['NAME_MIDDLE'][0][0]
self.FillAutofillProfile(profiles=[profile])
url = self.GetHttpURLForDataPath(
'autofill', 'functional', 'autofill_middleinit_form.html')
self.NavigateToURL(url)
# Fill form using an address profile.
self.assertTrue(self.AutofillPopulateForm('NAME_FIRST'),
msg='Autofill form could not be populated.')
js_return_middleinit_field = (
'var field_value = document.getElementById("NAME_MIDDLE").value;'
'window.domAutomationController.send(field_value);')
middleinit_field_value = self.ExecuteJavascript(
js_return_middleinit_field, 0, 0)
self.assertEqual(middleinit_field_value, middle_initial,
msg=('Middle initial "%s" not distinguished from "%s".' %
(middleinit_field_value, profile['NAME_MIDDLE'])))
def testMultipleEmailFilledByOneUserGesture(self):
"""Test forms with multiple email addresses are filled properly.
Entire form should be filled with one user gesture.
"""
profile = {'NAME_FIRST': ['Bob',],
'NAME_LAST': ['Smith',],
'EMAIL_ADDRESS': ['bsmith@gmail.com',],
'PHONE_HOME_WHOLE_NUMBER': ['4088714567',],}
self.FillAutofillProfile(profiles=[profile])
url = self.GetHttpURLForDataPath(
'autofill', 'functional', 'autofill_confirmemail_form.html')
self.NavigateToURL(url)
# Fill form using an address profile.
self.assertTrue(self.AutofillPopulateForm('NAME_FIRST'),
msg='Autofill form could not be populated.')
js_return_confirmemail_field = (
'var field_value = document.getElementById("EMAIL_CONFIRM").value;'
'window.domAutomationController.send(field_value);')
confirmemail_field_value = self.ExecuteJavascript(
js_return_confirmemail_field, 0, 0)
self.assertEqual([confirmemail_field_value], profile['EMAIL_ADDRESS'],
msg=('Confirmation Email address "%s" not equal to Email\n'
'address "%s".' % ([confirmemail_field_value],
profile['EMAIL_ADDRESS'])))
def testProfileWithEmailInOtherFieldNotSaved(self):
"""Test profile not aggregated if email found in non-email field."""
profile = {'NAME_FIRST': 'Bob',
'NAME_LAST': 'Smith',
'ADDRESS_HOME_LINE1': 'bsmith@gmail.com',
'ADDRESS_HOME_CITY': 'San Jose',
'ADDRESS_HOME_STATE': 'CA',
'ADDRESS_HOME_ZIP': '95110',
'COMPANY_NAME': 'Company X',
'PHONE_HOME_WHOLE_NUMBER': '408-871-4567',}
self._FillFormAndSubmit(
[profile], 'duplicate_profiles_test.html', tab_index=0, windex=0)
self.assertFalse(self.GetAutofillProfile()['profiles'],
msg='Profile with email in a non-email field was '
'aggregated.')
def FormFillLatencyAfterSubmit(self):
"""Test latency time on form submit with lots of stored Autofill profiles.
This test verifies when a profile is selected from the Autofill dictionary
that consists of thousands of profiles, the form does not hang after being
submitted.
The high level key presses execute the following: Select the first text
field, invoke the autofill popup list, select the first profile within the
list, and commit to the profile to populate the form.
This test is partially automated. The bulk of the work is done, such as
generating 1500 plus profiles, inserting those profiles into Autofill,
selecting a profile from the list. The tester will need to click on the
submit button and check if the browser hangs.
"""
# HTML file needs to be run from a http:// url.
url = self.GetHttpURLForDataPath(
'autofill', 'functional', 'latency_after_submit_test.html')
# Run the generator script to generate the dictionary list needed for the
# profiles.
gen = autofill_dataset_generator.DatasetGenerator(
logging_level=logging.ERROR)
list_of_dict = gen.GenerateDataset(num_of_dict_to_generate=1501)
self.FillAutofillProfile(profiles=list_of_dict)
self.NavigateToURL(url)
self.assertTrue(self.AutofillPopulateForm('NAME_FIRST'),
msg='Autofill form could not be populated.')
# TODO(dyu): add automated form hang or crash verification.
raw_input(
'Verify the test manually. Test hang time after submitting the form.')
def AutofillCrowdsourcing(self):
"""Test able to send POST request of web form to Autofill server.
The Autofill server processes the data offline, so it can take a few days
for the result to be detectable. Manual verification is required.
"""
# HTML file needs to be run from a specific http:// url to be able to verify
# the results a few days later by visiting the same url.
url = 'http://www.corp.google.com/~dyu/autofill/crowdsourcing-test.html'
# Adding crowdsourcing Autofill profile.
file_path = os.path.join(self.DataDir(), 'autofill', 'functional',
'crowdsource_autofill.txt')
profiles = self.EvalDataFrom(file_path)
self.FillAutofillProfile(profiles=profiles)
# Autofill server captures 2.5% of the data posted.
# Looping 1000 times is a safe minimum to exceed the server's threshold or
# noise.
for i in range(1000):
fname = self.GetAutofillProfile()['profiles'][0]['NAME_FIRST'][0]
lname = self.GetAutofillProfile()['profiles'][0]['NAME_LAST'][0]
email = self.GetAutofillProfile()['profiles'][0]['EMAIL_ADDRESS'][0]
# Submit form to collect crowdsourcing data for Autofill.
self.NavigateToURL(url, 0, 0)
profile = {'fn': fname, 'ln': lname, 'em': email}
self._WaitForWebpageFormReadyToFillIn(profile, 0, 0)
js = ''.join(['document.getElementById("%s").value = "%s";' %
(key, value) for key, value in profile.iteritems()])
js += 'document.getElementById("testform").submit();'
self.SubmitAutofillForm(js, tab_index=0, windex=0)
def testSameAddressProfilesAddInPrefsDontMerge(self):
"""Test profiles added through prefs with same address do not merge."""
profileA = {'NAME_FIRST': ['John',],
'NAME_LAST': ['Doe',],
'ADDRESS_HOME_LINE1': ['123 Cherry St',],
'ADDRESS_HOME_CITY': ['Mountain View',],
'ADDRESS_HOME_STATE': ['CA',],
'ADDRESS_HOME_ZIP': ['94043',],
'PHONE_HOME_WHOLE_NUMBER': ['650-555-1234',],}
profileB = {'NAME_FIRST': ['Jane',],
'NAME_LAST': ['Smith',],
'ADDRESS_HOME_LINE1': ['123 Cherry St',],
'ADDRESS_HOME_CITY': ['Mountain View',],
'ADDRESS_HOME_STATE': ['CA',],
'ADDRESS_HOME_ZIP': ['94043',],
'PHONE_HOME_WHOLE_NUMBER': ['650-253-1234',],}
profiles_list = [profileA, profileB]
self.FillAutofillProfile(profiles=profiles_list)
self.assertEqual(2, len(self.GetAutofillProfile()['profiles']),
msg='Profiles in prefs with same address merged.')
def testMergeAggregatedProfilesWithSameAddress(self):
"""Test that profiles merge for aggregated data with same address.
The criterion for when two profiles are expected to be merged is when their
'Address Line 1' and 'City' data match. When two profiles are merged, any
remaining address fields are expected to be overwritten. Any non-address
fields should accumulate multi-valued data.
"""
self._AggregateProfilesIntoAutofillPrefs('dataset_2.txt')
# Expecting 3 profiles out of the original 14 within Autofill preferences
self.assertEqual(3, len(self.GetAutofillProfile()['profiles']),
msg='Aggregated profiles did not merge correctly.')
def testProfilesNotMergedWhenNoMinAddressData(self):
"""Test profiles are not merged without mininum address values.
Mininum address values needed during aggregation are: address line 1, city,
state, and zip code.
Profiles are merged when data for address line 1 and city match.
"""
self._AggregateProfilesIntoAutofillPrefs('dataset_no_address.txt')
self.assertFalse(self.GetAutofillProfile()['profiles'],
msg='Profile with no min address data was merged.')
def MergeAggregatedDuplicatedProfiles(self):
"""Test Autofill ability to merge duplicate profiles and throw away junk."""
num_of_profiles = self._AggregateProfilesIntoAutofillPrefs('dataset.txt')
# Verify total number of inputted profiles is greater than the final number
# of profiles after merging.
self.assertTrue(
num_of_profiles > len(self.GetAutofillProfile()['profiles']))
def _AggregateProfilesIntoAutofillPrefs(self, data):
"""Aggregate profiles from forms into Autofill preferences.
Args:
data: Name of the data set file.
Returns:
Number of profiles in the dictionary list.
"""
# HTML file needs to be run from a http:// url.
url = self.GetHttpURLForDataPath(
'autofill', 'functional', 'duplicate_profiles_test.html')
# Run the parser script to generate the dictionary list needed for the
# profiles.
c = autofill_dataset_converter.DatasetConverter(
os.path.abspath(
os.path.join(self.DataDir(), 'autofill', 'functional', data)),
logging_level=logging.INFO) # Set verbosity to INFO, WARNING, ERROR.
list_of_dict = c.Convert()
for profile in list_of_dict:
self.NavigateToURL(url)
self._WaitForWebpageFormReadyToFillIn(profile, 0, 0)
js = ''.join(['document.getElementById("%s").value = "%s";' %
(key, value) for key, value in profile.iteritems()])
js += 'document.getElementById("testform").submit();'
self.SubmitAutofillForm(js, tab_index=0, windex=0)
return len(list_of_dict)
def _SelectOptionXpath(self, value):
"""Returns an xpath query used to select an item from a dropdown list.
Args:
value: Option selected for the drop-down list field.
Returns:
The value of the xpath query.
"""
return '//option[@value="%s"]' % value
def testPostalCodeAndStateLabelsBasedOnCountry(self):
"""Verify postal code and state labels based on selected country."""
data_file = os.path.join(self.DataDir(), 'autofill', 'functional',
'state_zip_labels.txt')
test_data = simplejson.loads(open(data_file).read())
page = settings.AutofillEditAddressDialog.FromNavigation(self._driver)
# Initial check of State and ZIP labels.
self.assertEqual('State', page.GetStateLabel())
self.assertEqual('ZIP code', page.GetPostalCodeLabel())
for country_code in test_data:
page.Fill(country_code=country_code)
# Compare postal code labels.
actual_postal_label = page.GetPostalCodeLabel()
self.assertEqual(
test_data[country_code]['postalCodeLabel'],
actual_postal_label,
msg=('Postal code label "%s" does not match Country "%s"' %
(actual_postal_label, country_code)))
# Compare state labels.
actual_state_label = page.GetStateLabel()
self.assertEqual(
test_data[country_code]['stateLabel'],
actual_state_label,
msg=('State label "%s" does not match Country "%s"' %
(actual_state_label, country_code)))
def testNoDuplicatePhoneNumsInPrefs(self):
"""Test duplicate phone numbers entered in prefs are removed."""
page = settings.AutofillEditAddressDialog.FromNavigation(self._driver)
non_duplicates = ['111-1111', '222-2222']
duplicates = ['111-1111']
page.Fill(phones=non_duplicates + duplicates)
self.assertEqual(non_duplicates, page.GetPhones(),
msg='Duplicate phone number in prefs unexpectedly saved.')
def testDisplayLineItemForEntriesWithNoCCNum(self):
"""Verify Autofill creates a line item for CC entries with no CC number."""
self.NavigateToURL('chrome://settings-frame/autofillEditCreditCard')
self._driver.find_element_by_id('name-on-card').send_keys('Jane Doe')
query_month = self._SelectOptionXpath('12')
query_year = self._SelectOptionXpath('2014')
self._driver.find_element_by_id('expiration-month').find_element_by_xpath(
query_month).click()
self._driver.find_element_by_id('expiration-year').find_element_by_xpath(
query_year).click()
self._driver.find_element_by_id(
'autofill-edit-credit-card-apply-button').click()
# Refresh the page to ensure the UI is up-to-date.
self._driver.refresh()
list_entry = self._driver.find_element_by_class_name('autofill-list-item')
self.assertTrue(list_entry.is_displayed)
self.assertEqual('Jane Doe', list_entry.text,
msg='Saved CC line item not same as what was entered.')
def _GetElementList(self, container_elem, fields_to_select):
"""Returns all sub elements of specific characteristics.
Args:
container_elem: An element that contains other elements.
fields_to_select: A list of fields to select with strings that
help create an xpath string, which in turn identifies
the elements needed.
For example: ['input', 'button']
['div[@id]', 'button[@disabled]']
['*[class="example"]']
Returns:
List of all subelements found in the container element.
"""
self.assertTrue(fields_to_select, msg='No fields specified for selection.')
fields_to_select = ['.//' + field for field in fields_to_select]
xpath_arg = ' | '.join(fields_to_select)
field_elems = container_elem.find_elements_by_xpath(xpath_arg)
return field_elems
def _GetElementInfo(self, element):
"""Returns visual comprehensive info about an element.
This function identifies the text of the correspoinding label when tab
ordering fails.
This info consists of:
The labels, buttons, ids, placeholder attribute values, or the element id.
Args:
element: The target element.
Returns:
A string that identifies the element in the page.
"""
element_info = ''
if element.tag_name == 'button':
element_info = element.text
element_info = (element_info or element.get_attribute('id') or
element.get_attribute('placeholder') or
element.get_attribute('class') or element.id)
return '%s: %s' % (element.tag_name, element_info)
def _LoadPageAndGetFieldList(self):
"""Navigate to autofillEditAddress page and finds the elements with focus.
These elements are of input, select, and button types.
Returns:
A list with all elements that can receive focus.
"""
url = 'chrome://settings-frame/autofillEditAddress'
self._driver.get(url)
container_elem = self._driver.find_element_by_id(
'autofill-edit-address-overlay')
# The container element contains input, select and button fields. Some of
# the buttons are disabled so they are ignored.
field_list = self._GetElementList(container_elem,
['input', 'select',
'button[not(@disabled)]'])
self.assertTrue(field_list, 'No fields found in "%s".' % url)
return field_list
def testTabOrderForEditAddress(self):
"""Verify the TAB ordering for Edit Address page is correct."""
tab_press = ActionChains(self._driver).send_keys(Keys.TAB)
field_list = self._LoadPageAndGetFieldList()
# Creates a dictionary where a field key returns the value of the next field
# in the field list. The last field of the field list is mapped to the first
# field of the field list.
field_nextfield_dict = dict(
zip(field_list, field_list[1:] + field_list[:1]))
# Tab pressed for the first time.
tab_press.perform()
# Wait until a field of |field_list| has received the focus.
self.WaitUntil(lambda:
self._driver.switch_to_active_element().id in
[f.id for f in field_list])
# The first field is expected to receive the focus.
self.assertEqual(self._driver.switch_to_active_element().id,
field_list[0].id,
msg='The first field did not receive tab focus.')
for field in field_list:
tab_press.perform()
# Wait until a field of |field_list|, other than the current field, has
# received the focus.
self.WaitUntil(lambda:
self._driver.switch_to_active_element().id != field.id and
self._driver.switch_to_active_element().id in
[f.id for f in field_list])
self.assertEqual(self._driver.switch_to_active_element().id,
field_nextfield_dict[field].id,
msg=('The TAB ordering is broken. Previous field: "%s"\n'
'Field expected to receive focus: "%s"\n'
'Field that received focus instead: "%s"')
% (self._GetElementInfo(field),
self._GetElementInfo(field_nextfield_dict[field]),
self._GetElementInfo(
self._driver.switch_to_active_element())))
if __name__ == '__main__':
pyauto_functional.Main()
|