Skip to content

Commit 98ad139

Browse files
kblokclaude
andauthored
feat(cdp): support autofilling address fields (upstream PR #14826) (#3428)
Extends AutofillData to optionally accept address fields in addition to credit card data, forwarding them to the CDP Autofill.trigger command. - Add AutofillAddressField static class with CDP field name constants - Add AutofillAddressData and AutofillAddressFieldEntry types - Update AutofillData to include optional Address property - Update AutofillTriggerRequest to include Address field - Update CdpElementHandle.AutofillAsync to pass address data - Add address.html test asset and corresponding test Implements puppeteer/puppeteer#14826 Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 8d20758 commit 98ad139

8 files changed

Lines changed: 163 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5+
</head>
6+
<body>
7+
<form id="testform" method="post">
8+
<table>
9+
<tbody>
10+
<tr>
11+
<td><label for="name">Name</label></td>
12+
<td><input size="40" id="name" name="name" autocomplete="name" /></td>
13+
</tr>
14+
<tr>
15+
<td><label for="street">Street</label></td>
16+
<td><input size="40" id="street" name="street" autocomplete="street-address" /></td>
17+
</tr>
18+
<tr>
19+
<td><label for="city">City</label></td>
20+
<td><input size="40" id="city" name="city" autocomplete="address-level2" /></td>
21+
</tr>
22+
<tr>
23+
<td><label for="zipcode">Zip Code</label></td>
24+
<td><input size="10" id="zipcode" name="zipcode" autocomplete="postal-code" /></td>
25+
</tr>
26+
</tbody>
27+
</table>
28+
<input type="submit" value="Submit">
29+
</form>
30+
</body>
31+
</html>

lib/PuppeteerSharp.Tests/AutofillTests/AutofillTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Threading.Tasks;
23
using NUnit.Framework;
34
using PuppeteerSharp.Nunit;
@@ -32,5 +33,34 @@ await name.AutofillAsync(new AutofillData
3233
}");
3334
Assert.That(result, Is.EqualTo("John Smith,4444444444444444,01,2030,Submit"));
3435
}
36+
37+
[Test, PuppeteerTest("autofill.spec", "ElementHandle.autofill", "should fill out an address")]
38+
public async Task ShouldFillOutAnAddress()
39+
{
40+
await Page.GoToAsync(TestConstants.ServerUrl + "/address.html");
41+
var name = await Page.WaitForSelectorAsync("#name");
42+
await name.AutofillAsync(new AutofillData
43+
{
44+
Address = new AutofillAddressData
45+
{
46+
Fields = new List<AutofillAddressFieldEntry>
47+
{
48+
new AutofillAddressFieldEntry { Name = AutofillAddressField.NameFull, Value = "Jane Doe" },
49+
new AutofillAddressFieldEntry { Name = AutofillAddressField.AddressHomeStreetAddress, Value = "123 Main St" },
50+
new AutofillAddressFieldEntry { Name = AutofillAddressField.AddressHomeCity, Value = "Anytown" },
51+
new AutofillAddressFieldEntry { Name = AutofillAddressField.AddressHomeZip, Value = "12345" },
52+
},
53+
},
54+
});
55+
56+
var result = await Page.EvaluateFunctionAsync<string>(@"() => {
57+
const result = [];
58+
for (const el of document.querySelectorAll('input')) {
59+
result.push(el.value);
60+
}
61+
return result.join(',');
62+
}");
63+
Assert.That(result, Is.EqualTo("Jane Doe,123 Main St,Anytown,12345,Submit"));
64+
}
3565
}
3666
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace PuppeteerSharp
4+
{
5+
/// <summary>
6+
/// Address data for autofilling.
7+
/// See https://chromedevtools.github.io/devtools-protocol/tot/Autofill/#type-Address.
8+
/// </summary>
9+
public class AutofillAddressData
10+
{
11+
/// <summary>
12+
/// Gets or sets the address fields to fill.
13+
/// Each entry has a <c>Name</c> (see <see cref="AutofillAddressField"/>) and a <c>Value</c>.
14+
/// </summary>
15+
public List<AutofillAddressFieldEntry> Fields { get; set; }
16+
}
17+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace PuppeteerSharp
2+
{
3+
/// <summary>
4+
/// Supported autofill address field names.
5+
/// See https://source.chromium.org/chromium/chromium/src/+/main:components/autofill/core/browser/field_types.cc
6+
/// for the full list of supported fields.
7+
/// </summary>
8+
public static class AutofillAddressField
9+
{
10+
/// <summary>First name.</summary>
11+
public const string NameFirst = "NAME_FIRST";
12+
13+
/// <summary>Middle name.</summary>
14+
public const string NameMiddle = "NAME_MIDDLE";
15+
16+
/// <summary>Last name.</summary>
17+
public const string NameLast = "NAME_LAST";
18+
19+
/// <summary>Full name.</summary>
20+
public const string NameFull = "NAME_FULL";
21+
22+
/// <summary>Email address.</summary>
23+
public const string EmailAddress = "EMAIL_ADDRESS";
24+
25+
/// <summary>Phone home number.</summary>
26+
public const string PhoneHomeNumber = "PHONE_HOME_NUMBER";
27+
28+
/// <summary>Phone home city and number.</summary>
29+
public const string PhoneHomeCityAndNumber = "PHONE_HOME_CITY_AND_NUMBER";
30+
31+
/// <summary>Phone home whole number.</summary>
32+
public const string PhoneHomeWholeNumber = "PHONE_HOME_WHOLE_NUMBER";
33+
34+
/// <summary>Address home line 1.</summary>
35+
public const string AddressHomeLine1 = "ADDRESS_HOME_LINE1";
36+
37+
/// <summary>Address home line 2.</summary>
38+
public const string AddressHomeLine2 = "ADDRESS_HOME_LINE2";
39+
40+
/// <summary>Address home street address.</summary>
41+
public const string AddressHomeStreetAddress = "ADDRESS_HOME_STREET_ADDRESS";
42+
43+
/// <summary>Address home city.</summary>
44+
public const string AddressHomeCity = "ADDRESS_HOME_CITY";
45+
46+
/// <summary>Address home state.</summary>
47+
public const string AddressHomeState = "ADDRESS_HOME_STATE";
48+
49+
/// <summary>Address home zip code.</summary>
50+
public const string AddressHomeZip = "ADDRESS_HOME_ZIP";
51+
52+
/// <summary>Address home country.</summary>
53+
public const string AddressHomeCountry = "ADDRESS_HOME_COUNTRY";
54+
}
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace PuppeteerSharp
2+
{
3+
/// <summary>
4+
/// Represents a single autofill address field name/value pair.
5+
/// </summary>
6+
public class AutofillAddressFieldEntry
7+
{
8+
/// <summary>
9+
/// Gets or sets the field type name.
10+
/// Use constants from <see cref="AutofillAddressField"/> or a raw CDP field name string.
11+
/// </summary>
12+
public string Name { get; set; }
13+
14+
/// <summary>
15+
/// Gets or sets the field value.
16+
/// </summary>
17+
public string Value { get; set; }
18+
}
19+
}

lib/PuppeteerSharp/AutofillData.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ namespace PuppeteerSharp
22
{
33
/// <summary>
44
/// Data for autofilling form fields.
5+
/// Provide either <see cref="CreditCard"/> or <see cref="Address"/>, but not both.
56
/// </summary>
67
public class AutofillData
78
{
89
/// <summary>
910
/// Gets or sets the credit card data.
11+
/// See https://chromedevtools.github.io/devtools-protocol/tot/Autofill/#type-CreditCard.
1012
/// </summary>
1113
public CreditCardData CreditCard { get; set; }
14+
15+
/// <summary>
16+
/// Gets or sets the address data.
17+
/// See https://chromedevtools.github.io/devtools-protocol/tot/Autofill/#type-Address.
18+
/// </summary>
19+
public AutofillAddressData Address { get; set; }
1220
}
1321
}

lib/PuppeteerSharp/Cdp/CdpElementHandle.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ await Client.SendAsync(
198198
FieldId = fieldId,
199199
FrameId = frameId,
200200
Card = data.CreditCard,
201+
Address = data.Address,
201202
}).ConfigureAwait(false);
202203
}
203204

lib/PuppeteerSharp/Cdp/Messaging/AutofillTriggerRequest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ internal class AutofillTriggerRequest
77
public string FrameId { get; set; }
88

99
public CreditCardData Card { get; set; }
10+
11+
public AutofillAddressData Address { get; set; }
1012
}
1113
}

0 commit comments

Comments
 (0)