Skip to content

Commit e2c114c

Browse files
kblokMeir017
authored andcommitted
Typing emoji (#624)
* Progress * Feature complete * cr * cr * cr
1 parent 5d3f59b commit e2c114c

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

lib/PuppeteerSharp.Tests/InputTests/InputTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,5 +595,28 @@ public async Task ShouldThrowOnUnknownKeys()
595595

596596
await Assert.ThrowsAsync<KeyNotFoundException>(() => Page.Keyboard.PressAsync("😊"));
597597
}
598+
599+
[Fact]
600+
public async Task ShouldTypeEmoji()
601+
{
602+
await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");
603+
await Page.TypeAsync("textarea", "👹 Tokyo street Japan \uD83C\uDDEF\uD83C\uDDF5");
604+
Assert.Equal(
605+
"👹 Tokyo street Japan \uD83C\uDDEF\uD83C\uDDF5",
606+
await Page.QuerySelectorAsync("textarea").EvaluateFunctionAsync<string>("t => t.value"));
607+
}
608+
609+
[Fact]
610+
public async Task ShouldTypeEmojiIntoAniframe()
611+
{
612+
await Page.GoToAsync(TestConstants.EmptyPage);
613+
await FrameUtils.AttachFrameAsync(Page, "emoji-test", TestConstants.ServerUrl + "/input/textarea.html");
614+
var frame = Page.Frames[1];
615+
var textarea = await frame.QuerySelectorAsync("textarea");
616+
await textarea.TypeAsync("👹 Tokyo street Japan \uD83C\uDDEF\uD83C\uDDF5");
617+
Assert.Equal(
618+
"👹 Tokyo street Japan \uD83C\uDDEF\uD83C\uDDF5",
619+
await frame.QuerySelectorAsync("textarea").EvaluateFunctionAsync<string>("t => t.value"));
620+
}
598621
}
599622
}

lib/PuppeteerSharp/Input/Keyboard.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Globalization;
23
using System.Threading.Tasks;
34

45
namespace PuppeteerSharp.Input
@@ -41,7 +42,8 @@ public Task DownAsync(string key, DownOptions options = null)
4142

4243
var text = options?.Text == null ? description.Text : options.Text;
4344

44-
return _client.SendAsync("Input.dispatchKeyEvent", new Dictionary<string, object>(){
45+
return _client.SendAsync("Input.dispatchKeyEvent", new Dictionary<string, object>
46+
{
4547
{"type", text != null ? "keyDown" : "rawKeyDown"},
4648
{"modifiers", Modifiers},
4749
{"windowsVirtualKeyCode", description.KeyCode},
@@ -67,7 +69,8 @@ public Task UpAsync(string key)
6769
Modifiers &= ~ModifierBit(key);
6870
_pressedKeys.Remove(description.Code);
6971

70-
return _client.SendAsync("Input.dispatchKeyEvent", new Dictionary<string, object>(){
72+
return _client.SendAsync("Input.dispatchKeyEvent", new Dictionary<string, object>
73+
{
7174
{"type", "keyUp"},
7275
{"modifiers", Modifiers},
7376
{"key", description.Key},
@@ -83,15 +86,7 @@ public Task UpAsync(string key)
8386
/// <param name="charText">Character to send into the page</param>
8487
/// <returns>Task</returns>
8588
public Task SendCharacterAsync(string charText)
86-
{
87-
return _client.SendAsync("Input.dispatchKeyEvent", new Dictionary<string, object>(){
88-
{"type", "char"},
89-
{"modifiers", Modifiers},
90-
{"text", charText },
91-
{"key", charText},
92-
{"unmodifiedText", charText }
93-
});
94-
}
89+
=> _client.SendAsync("Input.insertText", new Dictionary<string, object> { ["text"] = charText });
9590

9691
/// <summary>
9792
/// Sends a <c>keydown</c>, <c>keypress</c>/<c>input</c>, and <c>keyup</c> event for each character in the text.
@@ -109,8 +104,11 @@ public async Task TypeAsync(string text, TypeOptions options = null)
109104
{
110105
delay = (int)options.Delay;
111106
}
112-
foreach (var letter in text)
107+
108+
var textParts = StringInfo.GetTextElementEnumerator(text);
109+
while (textParts.MoveNext())
113110
{
111+
var letter = textParts.Current;
114112
if (KeyDefinitions.ContainsKey(letter.ToString()))
115113
{
116114
await PressAsync(letter.ToString(), new PressOptions { Delay = delay }).ConfigureAwait(false);

0 commit comments

Comments
 (0)