Skip to content

Commit 3cd459c

Browse files
committed
Open door to sequential input actions
1 parent 013d2b3 commit 3cd459c

File tree

9 files changed

+118
-25
lines changed

9 files changed

+118
-25
lines changed

dotnet/src/webdriver/BiDi/Communication/Json/Converters/Enumerable/InputSourceActionsConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters.Enumerable;
1111

12-
internal class InputSourceActionsConverter : JsonConverter<ISourceActions>
12+
internal class InputSourceActionsConverter : JsonConverter<SourceActions>
1313
{
14-
public override ISourceActions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14+
public override SourceActions Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1515
{
1616
throw new NotImplementedException();
1717
}
1818

19-
public override void Write(Utf8JsonWriter writer, ISourceActions value, JsonSerializerOptions options)
19+
public override void Write(Utf8JsonWriter writer, SourceActions value, JsonSerializerOptions options)
2020
{
2121
writer.WriteStartObject();
2222

dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextInputModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext;
88

99
public class BrowsingContextInputModule(BrowsingContext context, InputModule inputModule)
1010
{
11-
public Task PerformActionsAsync(IEnumerable<ISourceActions> actions, PerformActionsOptions? options = null)
11+
public Task PerformActionsAsync(IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
1212
{
1313
return inputModule.PerformActionsAsync(context, actions, options);
1414
}

dotnet/src/webdriver/BiDi/Modules/Input/InputModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OpenQA.Selenium.BiDi.Modules.Input;
88

99
public sealed class InputModule(Broker broker) : Module(broker)
1010
{
11-
public async Task PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<ISourceActions> actions, PerformActionsOptions? options = null)
11+
public async Task PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
1212
{
1313
var @params = new PerformActionsCommandParameters(context, actions);
1414

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace OpenQA.Selenium.BiDi.Modules.Input;
2+
3+
partial record Key
4+
{
5+
public static char Shift { get; } = '\xE008';
6+
}

dotnet/src/webdriver/BiDi/Modules/Input/PerformActionsCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace OpenQA.Selenium.BiDi.Modules.Input;
77

88
internal class PerformActionsCommand(PerformActionsCommandParameters @params) : Command<PerformActionsCommandParameters>(@params);
99

10-
internal record PerformActionsCommandParameters(BrowsingContext.BrowsingContext Context, IEnumerable<ISourceActions> Actions) : CommandParameters;
10+
internal record PerformActionsCommandParameters(BrowsingContext.BrowsingContext Context, IEnumerable<SourceActions> Actions) : CommandParameters;
1111

1212
public record PerformActionsOptions : CommandOptions;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace OpenQA.Selenium.BiDi.Modules.Input;
6+
7+
public interface ISequentialSourceActions : IEnumerable<SourceActions>
8+
{
9+
public SequentialSourceActions Type(string text);
10+
11+
public SequentialSourceActions KeyDown(char key);
12+
}
13+
14+
public record SequentialSourceActions : ISequentialSourceActions
15+
{
16+
private readonly KeyActions _keyActions = [];
17+
private readonly PointerActions _pointerActions = [];
18+
private readonly WheelActions _wheelActions = [];
19+
20+
public SequentialSourceActions Type(string text)
21+
{
22+
_keyActions.Type(text);
23+
24+
return Normalized();
25+
}
26+
27+
public SequentialSourceActions KeyDown(char key)
28+
{
29+
_keyActions.Add(new Key.Down(key));
30+
31+
return Normalized();
32+
}
33+
34+
private SequentialSourceActions Normalized()
35+
{
36+
var max = new[] { _keyActions.Count(), _pointerActions.Count(), _wheelActions.Count() }.Max();
37+
38+
for (int i = _keyActions.Count(); i < max; i++)
39+
{
40+
_keyActions.Add(new Key.Pause());
41+
}
42+
43+
for (int i = _pointerActions.Count(); i < max; i++)
44+
{
45+
_pointerActions.Add(new Pointer.Pause());
46+
}
47+
48+
for (int i = _wheelActions.Count(); i < max; i++)
49+
{
50+
_wheelActions.Add(new Pointer.Pause());
51+
}
52+
53+
return this;
54+
}
55+
56+
public IEnumerator<SourceActions> GetEnumerator()
57+
{
58+
var sourceActions = new List<SourceActions>
59+
{
60+
_keyActions,
61+
_pointerActions,
62+
_wheelActions
63+
};
64+
return sourceActions.GetEnumerator();
65+
}
66+
67+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
68+
}

dotnet/src/webdriver/BiDi/Modules/Input/SourceActions.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77

88
namespace OpenQA.Selenium.BiDi.Modules.Input;
99

10-
public interface ISourceActions
10+
public abstract record SourceActions
1111
{
12-
string Id { get; }
13-
14-
IList<ISourceAction> Actions { get; }
12+
public string Id { get; } = Guid.NewGuid().ToString();
1513
}
1614

17-
public abstract record SourceActions<T> : ISourceActions, IEnumerable<ISourceAction>
15+
public record SourceActions<T> : SourceActions, IEnumerable<ISourceAction> where T : ISourceAction
1816
{
19-
public string Id { get; } = Guid.NewGuid().ToString();
20-
21-
public IList<ISourceAction> Actions { get; } = [];
17+
public IList<ISourceAction> Actions { get; set; } = [];
2218

2319
public IEnumerator<ISourceAction> GetEnumerator() => Actions.GetEnumerator();
2420

@@ -27,9 +23,21 @@ public abstract record SourceActions<T> : ISourceActions, IEnumerable<ISourceAct
2723
public void Add(ISourceAction action) => Actions.Add(action);
2824
}
2925

30-
public record KeyActions : SourceActions<Key>, ISourceActions;
26+
public record KeyActions : SourceActions<Key>
27+
{
28+
public KeyActions Type(string text)
29+
{
30+
foreach (var character in text)
31+
{
32+
Add(new Key.Down(character));
33+
Add(new Key.Up(character));
34+
}
35+
36+
return this;
37+
}
38+
}
3139

32-
public record PointerActions : SourceActions<Pointer>, ISourceActions
40+
public record PointerActions : SourceActions<Pointer>
3341
{
3442
public PointerParameters? Options { get; set; }
3543
}
@@ -44,16 +52,16 @@ public interface ISourceAction;
4452
[JsonDerivedType(typeof(Pause), "pause")]
4553
[JsonDerivedType(typeof(Down), "keyDown")]
4654
[JsonDerivedType(typeof(Up), "keyUp")]
47-
public abstract record Key : ISourceAction
55+
public abstract partial record Key : ISourceAction
4856
{
4957
public record Pause : Key
5058
{
5159
public long? Duration { get; set; }
5260
}
5361

54-
public record Down(string Value) : Key;
62+
public record Down(char Value) : Key;
5563

56-
public record Up(string Value) : Key;
64+
public record Up(char Value) : Key;
5765
}
5866

5967
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]

dotnet/test/common/BiDi/Input/CombinedInputActionsTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ await context.Input.PerformActionsAsync([new PointerActions {
2222
}]);
2323

2424
await context.Input.PerformActionsAsync([new KeyActions {
25-
new Key.Down("U"),
26-
new Key.Up("U")
25+
new Key.Down('U'),
26+
new Key.Up('U')
2727
}]);
2828

2929
await context.Input.PerformActionsAsync([new PointerActions {

dotnet/test/common/BiDi/Input/DefaultMouseTest.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NUnit.Framework;
2+
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
23
using OpenQA.Selenium.BiDi.Modules.Input;
34
using System.Threading.Tasks;
45

@@ -16,16 +17,16 @@ await context.Input.PerformActionsAsync([
1617
{
1718
Actions =
1819
{
19-
new Key.Down("A"),
20-
new Key.Up("B")
20+
new Key.Down('A'),
21+
new Key.Up('B')
2122
}
2223
}
2324
]);
2425

2526
await context.Input.PerformActionsAsync([new KeyActions
2627
{
27-
new Key.Down("A"),
28-
new Key.Down("B"),
28+
new Key.Down('A'),
29+
new Key.Down('B'),
2930
new Key.Pause()
3031
}]);
3132

@@ -35,4 +36,14 @@ await context.Input.PerformActionsAsync([new PointerActions
3536
new Pointer.Up(0),
3637
}]);
3738
}
39+
40+
//[Test]
41+
public async Task PerformCombined()
42+
{
43+
await context.NavigateAsync("https://nuget.org", new() { Wait = ReadinessState.Complete });
44+
45+
await context.Input.PerformActionsAsync(new SequentialSourceActions().Type("Hello").KeyDown(Key.Shift).Type("World"));
46+
47+
await Task.Delay(3000);
48+
}
3849
}

0 commit comments

Comments
 (0)