Skip to content

Commit 762648c

Browse files
committed
Pause as ISourceAction
1 parent 3cd459c commit 762648c

File tree

6 files changed

+69
-59
lines changed

6 files changed

+69
-59
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using OpenQA.Selenium.BiDi.Modules.Input;
22
using System;
3-
using System.Collections.Generic;
43
using System.Linq;
54
using System.Text.Json;
65
using System.Text.Json.Serialization;
@@ -27,7 +26,7 @@ public override void Write(Utf8JsonWriter writer, SourceActions value, JsonSeria
2726
case KeyActions keys:
2827
writer.WriteString("type", "key");
2928
writer.WritePropertyName("actions");
30-
JsonSerializer.Serialize(writer, keys.Actions.Select(a => a as Key), options);
29+
JsonSerializer.Serialize(writer, keys.Actions.Select(a => a as IKeySourceAction), options);
3130

3231
break;
3332
case PointerActions pointers:
@@ -39,19 +38,19 @@ public override void Write(Utf8JsonWriter writer, SourceActions value, JsonSeria
3938
}
4039

4140
writer.WritePropertyName("actions");
42-
JsonSerializer.Serialize(writer, pointers.Actions.Select(a => a as Pointer), options);
41+
JsonSerializer.Serialize(writer, pointers.Actions.Select(a => a as IPointerSourceAction), options);
4342

4443
break;
4544
case WheelActions wheels:
4645
writer.WriteString("type", "wheel");
4746
writer.WritePropertyName("actions");
48-
JsonSerializer.Serialize(writer, wheels.Actions.Select(a => a as Wheel), options);
47+
JsonSerializer.Serialize(writer, wheels.Actions.Select(a => a as IWheelSourceAction), options);
4948

5049
break;
5150
case NoneActions none:
5251
writer.WriteString("type", "none");
5352
writer.WritePropertyName("actions");
54-
JsonSerializer.Serialize(writer, none.Actions.Select(a => a as None), options);
53+
JsonSerializer.Serialize(writer, none.Actions.Select(a => a as INoneSourceAction), options);
5554

5655
break;
5756
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ namespace OpenQA.Selenium.BiDi.Modules.Input;
22

33
partial record Key
44
{
5-
public static char Shift { get; } = '\xE008';
5+
public const char Shift = '\uE008';
6+
7+
public const char Pause = '\uE00B';
68
}

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,35 @@ namespace OpenQA.Selenium.BiDi.Modules.Input;
66

77
public interface ISequentialSourceActions : IEnumerable<SourceActions>
88
{
9-
public SequentialSourceActions Type(string text);
9+
public ISequentialSourceActions Pause(int duration);
1010

11-
public SequentialSourceActions KeyDown(char key);
11+
public ISequentialSourceActions Type(string text);
12+
13+
public ISequentialSourceActions KeyDown(char key);
1214
}
1315

1416
public record SequentialSourceActions : ISequentialSourceActions
1517
{
1618
private readonly KeyActions _keyActions = [];
1719
private readonly PointerActions _pointerActions = [];
1820
private readonly WheelActions _wheelActions = [];
21+
private readonly WheelActions _noneActions = [];
1922

20-
public SequentialSourceActions Type(string text)
23+
public ISequentialSourceActions Pause(int duration)
24+
{
25+
_noneActions.Add(new Pause { Duration = duration });
26+
27+
return Normalized();
28+
}
29+
30+
public ISequentialSourceActions Type(string text)
2131
{
2232
_keyActions.Type(text);
2333

2434
return Normalized();
2535
}
2636

27-
public SequentialSourceActions KeyDown(char key)
37+
public ISequentialSourceActions KeyDown(char key)
2838
{
2939
_keyActions.Add(new Key.Down(key));
3040

@@ -33,21 +43,26 @@ public SequentialSourceActions KeyDown(char key)
3343

3444
private SequentialSourceActions Normalized()
3545
{
36-
var max = new[] { _keyActions.Count(), _pointerActions.Count(), _wheelActions.Count() }.Max();
46+
var max = new[] { _keyActions.Count(), _pointerActions.Count(), _wheelActions.Count(), _noneActions.Count() }.Max();
3747

3848
for (int i = _keyActions.Count(); i < max; i++)
3949
{
40-
_keyActions.Add(new Key.Pause());
50+
_keyActions.Add(new Pause());
4151
}
4252

4353
for (int i = _pointerActions.Count(); i < max; i++)
4454
{
45-
_pointerActions.Add(new Pointer.Pause());
55+
_pointerActions.Add(new Pause());
4656
}
4757

4858
for (int i = _wheelActions.Count(); i < max; i++)
4959
{
50-
_wheelActions.Add(new Pointer.Pause());
60+
_wheelActions.Add(new Pause());
61+
}
62+
63+
for (int i = _noneActions.Count(); i < max; i++)
64+
{
65+
_noneActions.Add(new Pause());
5166
}
5267

5368
return this;
@@ -59,7 +74,8 @@ public IEnumerator<SourceActions> GetEnumerator()
5974
{
6075
_keyActions,
6176
_pointerActions,
62-
_wheelActions
77+
_wheelActions,
78+
_noneActions
6379
};
6480
return sourceActions.GetEnumerator();
6581
}

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

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public abstract record SourceActions
1212
public string Id { get; } = Guid.NewGuid().ToString();
1313
}
1414

15+
public interface ISourceAction;
16+
1517
public record SourceActions<T> : SourceActions, IEnumerable<ISourceAction> where T : ISourceAction
1618
{
1719
public IList<ISourceAction> Actions { get; set; } = [];
@@ -23,7 +25,13 @@ public record SourceActions<T> : SourceActions, IEnumerable<ISourceAction> where
2325
public void Add(ISourceAction action) => Actions.Add(action);
2426
}
2527

26-
public record KeyActions : SourceActions<Key>
28+
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
29+
[JsonDerivedType(typeof(Pause), "pause")]
30+
[JsonDerivedType(typeof(Key.Down), "keyDown")]
31+
[JsonDerivedType(typeof(Key.Up), "keyUp")]
32+
public interface IKeySourceAction : ISourceAction;
33+
34+
public record KeyActions : SourceActions<IKeySourceAction>
2735
{
2836
public KeyActions Type(string text)
2937
{
@@ -37,45 +45,40 @@ public KeyActions Type(string text)
3745
}
3846
}
3947

40-
public record PointerActions : SourceActions<Pointer>
48+
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
49+
[JsonDerivedType(typeof(Pause), "pause")]
50+
[JsonDerivedType(typeof(Pointer.Down), "pointerDown")]
51+
[JsonDerivedType(typeof(Pointer.Up), "pointerUp")]
52+
[JsonDerivedType(typeof(Pointer.Move), "pointerMove")]
53+
public interface IPointerSourceAction : ISourceAction;
54+
55+
public record PointerActions : SourceActions<IPointerSourceAction>
4156
{
4257
public PointerParameters? Options { get; set; }
4358
}
4459

45-
public record WheelActions : SourceActions<Wheel>;
46-
47-
public record NoneActions : SourceActions<None>;
60+
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
61+
[JsonDerivedType(typeof(Pause), "pause")]
62+
[JsonDerivedType(typeof(Wheel.Scroll), "scroll")]
63+
public interface IWheelSourceAction : ISourceAction;
4864

49-
public interface ISourceAction;
65+
public record WheelActions : SourceActions<IWheelSourceAction>;
5066

5167
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
5268
[JsonDerivedType(typeof(Pause), "pause")]
53-
[JsonDerivedType(typeof(Down), "keyDown")]
54-
[JsonDerivedType(typeof(Up), "keyUp")]
55-
public abstract partial record Key : ISourceAction
56-
{
57-
public record Pause : Key
58-
{
59-
public long? Duration { get; set; }
60-
}
69+
public interface INoneSourceAction : ISourceAction;
70+
71+
public record NoneActions : SourceActions<None>;
6172

73+
public abstract partial record Key : IKeySourceAction
74+
{
6275
public record Down(char Value) : Key;
6376

6477
public record Up(char Value) : Key;
6578
}
6679

67-
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
68-
[JsonDerivedType(typeof(Pause), "pause")]
69-
[JsonDerivedType(typeof(Down), "pointerDown")]
70-
[JsonDerivedType(typeof(Up), "pointerUp")]
71-
[JsonDerivedType(typeof(Move), "pointerMove")]
72-
public abstract record Pointer : ISourceAction
80+
public abstract record Pointer : IPointerSourceAction
7381
{
74-
public record Pause : Pointer
75-
{
76-
public long? Duration { get; set; }
77-
}
78-
7982
public record Down(int Button) : Pointer, IPointerCommonProperties
8083
{
8184
public int? Width { get; set; }
@@ -105,16 +108,8 @@ public record Move(int X, int Y) : Pointer, IPointerCommonProperties
105108
}
106109
}
107110

108-
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
109-
[JsonDerivedType(typeof(Pause), "pause")]
110-
[JsonDerivedType(typeof(Scroll), "scroll")]
111-
public abstract record Wheel : ISourceAction
111+
public abstract record Wheel : IWheelSourceAction
112112
{
113-
public record Pause : Wheel
114-
{
115-
public long? Duration { get; set; }
116-
}
117-
118113
public record Scroll(int X, int Y, int DeltaX, int DeltaY) : Wheel
119114
{
120115
public int? Duration { get; set; }
@@ -123,14 +118,11 @@ public record Scroll(int X, int Y, int DeltaX, int DeltaY) : Wheel
123118
}
124119
}
125120

126-
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
127-
[JsonDerivedType(typeof(Pause), "pause")]
128-
public abstract record None : ISourceAction
121+
public abstract record None : INoneSourceAction;
122+
123+
public record Pause : ISourceAction, IKeySourceAction, IPointerSourceAction, IWheelSourceAction, INoneSourceAction
129124
{
130-
public record Pause : None
131-
{
132-
public long? Duration { get; set; }
133-
}
125+
public long? Duration { get; set; }
134126
}
135127

136128
public record PointerParameters

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ await context.Input.PerformActionsAsync([new PointerActions {
2323

2424
await context.Input.PerformActionsAsync([new KeyActions {
2525
new Key.Down('U'),
26-
new Key.Up('U')
26+
new Key.Up('U'),
27+
new Pause { Duration = 3000 }
2728
}]);
2829

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ await context.Input.PerformActionsAsync([new KeyActions
2727
{
2828
new Key.Down('A'),
2929
new Key.Down('B'),
30-
new Key.Pause()
30+
new Pause()
3131
}]);
3232

3333
await context.Input.PerformActionsAsync([new PointerActions
@@ -42,7 +42,7 @@ public async Task PerformCombined()
4242
{
4343
await context.NavigateAsync("https://nuget.org", new() { Wait = ReadinessState.Complete });
4444

45-
await context.Input.PerformActionsAsync(new SequentialSourceActions().Type("Hello").KeyDown(Key.Shift).Type("World"));
45+
await context.Input.PerformActionsAsync(new SequentialSourceActions().Type("Hello").Pause(2000).KeyDown(Key.Shift).Type("World"));
4646

4747
await Task.Delay(3000);
4848
}

0 commit comments

Comments
 (0)