Skip to content

Commit 7681685

Browse files
committed
Add input tests
1 parent 9b1b6d0 commit 7681685

File tree

2 files changed

+168
-3
lines changed

2 files changed

+168
-3
lines changed

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

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ namespace OpenQA.Selenium.BiDi.Modules.Input;
66

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

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

13-
public ISequentialSourceActions KeyDown(char key);
15+
ISequentialSourceActions PointerDown(int button, PointerDownOptions? options = null);
16+
ISequentialSourceActions PointerUp(int button);
17+
ISequentialSourceActions PointerMove(int x, int y, PointerMoveOptions? options = null);
1418
}
1519

1620
public record SequentialSourceActions : ISequentialSourceActions
@@ -41,6 +45,54 @@ public ISequentialSourceActions KeyDown(char key)
4145
return Normalized();
4246
}
4347

48+
public ISequentialSourceActions KeyUp(char key)
49+
{
50+
_keyActions.Add(new Key.Up(key));
51+
52+
return Normalized();
53+
}
54+
55+
public ISequentialSourceActions PointerDown(int button, PointerDownOptions? options = null)
56+
{
57+
_pointerActions.Add(new Pointer.Down(button)
58+
{
59+
Width = options?.Width,
60+
Height = options?.Height,
61+
Pressure = options?.Pressure,
62+
TangentialPressure = options?.TangentialPressure,
63+
Twist = options?.Twist,
64+
AltitudeAngle = options?.AltitudeAngle,
65+
AzimuthAngle = options?.AzimuthAngle
66+
});
67+
68+
return Normalized();
69+
}
70+
71+
public ISequentialSourceActions PointerUp(int button)
72+
{
73+
_pointerActions.Add(new Pointer.Up(button));
74+
75+
return Normalized();
76+
}
77+
78+
public ISequentialSourceActions PointerMove(int x, int y, PointerMoveOptions? options = null)
79+
{
80+
_pointerActions.Add(new Pointer.Move(x, y)
81+
{
82+
Duration = options?.Duration,
83+
Origin = options?.Origin,
84+
Width = options?.Width,
85+
Height = options?.Height,
86+
Pressure = options?.Pressure,
87+
TangentialPressure = options?.TangentialPressure,
88+
Twist = options?.Twist,
89+
AltitudeAngle = options?.AltitudeAngle,
90+
AzimuthAngle = options?.AzimuthAngle
91+
});
92+
93+
return Normalized();
94+
}
95+
4496
private SequentialSourceActions Normalized()
4597
{
4698
var max = new[] { _keyActions.Count(), _pointerActions.Count(), _wheelActions.Count(), _noneActions.Count() }.Max();
@@ -82,3 +134,28 @@ public IEnumerator<SourceActions> GetEnumerator()
82134

83135
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
84136
}
137+
138+
public record PointerDownOptions : IPointerCommonProperties
139+
{
140+
public int? Width { get; set; }
141+
public int? Height { get; set; }
142+
public double? Pressure { get; set; }
143+
public double? TangentialPressure { get; set; }
144+
public int? Twist { get; set; }
145+
public double? AltitudeAngle { get; set; }
146+
public double? AzimuthAngle { get; set; }
147+
}
148+
149+
public record PointerMoveOptions : IPointerCommonProperties
150+
{
151+
public int? Duration { get; set; }
152+
public Origin? Origin { get; set; }
153+
154+
public int? Width { get; set; }
155+
public int? Height { get; set; }
156+
public double? Pressure { get; set; }
157+
public double? TangentialPressure { get; set; }
158+
public int? Twist { get; set; }
159+
public double? AltitudeAngle { get; set; }
160+
public double? AzimuthAngle { get; set; }
161+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using NUnit.Framework;
2+
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
3+
using OpenQA.Selenium.BiDi.Modules.Input;
4+
using System.Threading.Tasks;
5+
6+
namespace OpenQA.Selenium.BiDi.Input;
7+
8+
class DefaultKeyboardTest : BiDiTestFixture
9+
{
10+
[Test]
11+
public async Task TestBasicKeyboardInput()
12+
{
13+
driver.Url = UrlBuilder.WhereIs("single_text_input.html");
14+
15+
var input = (await context.LocateNodesAsync(new Locator.Css("#textInput")))[0];
16+
17+
await context.Input.PerformActionsAsync(new SequentialSourceActions()
18+
.PointerMove(0, 0, new() { Origin = new Modules.Input.Origin.Element(new Modules.Script.SharedReference(input.SharedId)) })
19+
.PointerDown(0)
20+
.PointerUp(0)
21+
.Type("abc def"));
22+
23+
Assert.That(driver.FindElement(By.Id("textInput")).GetAttribute("value"), Is.EqualTo("abc def"));
24+
}
25+
26+
[Test]
27+
public async Task TestSendingKeyDownOnly()
28+
{
29+
driver.Url = UrlBuilder.WhereIs("key_logger.html");
30+
31+
var input = (await context.LocateNodesAsync(new Locator.Css("#theworks")))[0];
32+
33+
await context.Input.PerformActionsAsync(new SequentialSourceActions()
34+
.PointerMove(0, 0, new() { Origin = new Modules.Input.Origin.Element(new Modules.Script.SharedReference(input.SharedId)) })
35+
.PointerDown(0)
36+
.PointerUp(0)
37+
.KeyDown(Key.Shift));
38+
39+
Assert.That(driver.FindElement(By.Id("result")).Text, Does.EndWith("keydown"));
40+
}
41+
42+
[Test]
43+
public async Task TestSendingKeyUp()
44+
{
45+
driver.Url = UrlBuilder.WhereIs("key_logger.html");
46+
47+
var input = (await context.LocateNodesAsync(new Locator.Css("#theworks")))[0];
48+
49+
await context.Input.PerformActionsAsync(new SequentialSourceActions()
50+
.PointerMove(0, 0, new() { Origin = new Modules.Input.Origin.Element(new Modules.Script.SharedReference(input.SharedId)) })
51+
.PointerDown(0)
52+
.PointerUp(0)
53+
.KeyDown(Key.Shift)
54+
.KeyUp(Key.Shift));
55+
56+
Assert.That(driver.FindElement(By.Id("result")).Text, Does.EndWith("keyup"));
57+
}
58+
59+
[Test]
60+
public async Task TestSendingKeysWithShiftPressed()
61+
{
62+
driver.Url = UrlBuilder.WhereIs("key_logger.html");
63+
64+
var input = (await context.LocateNodesAsync(new Locator.Css("#theworks")))[0];
65+
66+
await context.Input.PerformActionsAsync(new SequentialSourceActions()
67+
.PointerMove(0, 0, new() { Origin = new Modules.Input.Origin.Element(new Modules.Script.SharedReference(input.SharedId)) })
68+
.PointerDown(0)
69+
.PointerUp(0)
70+
.KeyDown(Key.Shift)
71+
.Type("ab")
72+
.KeyUp(Key.Shift));
73+
74+
Assert.That(driver.FindElement(By.Id("result")).Text, Does.EndWith("keydown keydown keypress keyup keydown keypress keyup keyup"));
75+
Assert.That(driver.FindElement(By.Id("theworks")).GetAttribute("value"), Is.EqualTo("AB"));
76+
}
77+
78+
[Test]
79+
public async Task TestSendingKeysToActiveElement()
80+
{
81+
driver.Url = UrlBuilder.WhereIs("bodyTypingTest.html");
82+
83+
await context.Input.PerformActionsAsync(new SequentialSourceActions().Type("ab"));
84+
85+
Assert.That(driver.FindElement(By.Id("body_result")).Text, Does.EndWith("keypress keypress"));
86+
Assert.That(driver.FindElement(By.Id("result")).Text, Is.EqualTo(" "));
87+
}
88+
}

0 commit comments

Comments
 (0)