Skip to content

Commit 8bcf64c

Browse files
authored
[java] implement separate Origin class for WheelInput (#10636)
[skip ci]
1 parent 38b780f commit 8bcf64c

4 files changed

Lines changed: 37 additions & 28 deletions

File tree

java/src/org/openqa/selenium/interactions/Actions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public Actions release(WebElement target) {
271271
return moveInTicks(target, 0, 0).tick(getActivePointer().createPointerUp(LEFT.asArg()));
272272
}
273273

274-
public Actions scroll( int x, int y, int deltaX, int deltaY, Origin origin) {
274+
public Actions scroll( int x, int y, int deltaX, int deltaY, WheelInput.Origin origin) {
275275
return tick(getActiveWheel().createScroll(x, y, deltaX, deltaY, Duration.ofMillis(250), origin));
276276
}
277277

java/src/org/openqa/selenium/interactions/WheelInput.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import java.util.Optional;
2626
import java.util.UUID;
2727

28-
import org.openqa.selenium.interactions.PointerInput.Origin;
28+
import org.openqa.selenium.WebElement;
29+
import org.openqa.selenium.WrapsElement;
2930
import org.openqa.selenium.internal.Require;
3031

3132
/**
@@ -89,8 +90,6 @@ protected ScrollInteraction(
8990
this.deltaY = deltaY;
9091
this.duration = nonNegative(duration);
9192
this.origin = Require.nonNull("Origin of scroll", origin);
92-
Require.precondition(!origin.asArg().equals("pointer"),
93-
"Wheel input only accepts viewport or element origin." );
9493
}
9594

9695
@Override
@@ -108,4 +107,28 @@ public Map<String, Object> encode() {
108107
return toReturn;
109108
}
110109
}
110+
111+
public static final class Origin {
112+
private final Object originObject;
113+
114+
public Object asArg() {
115+
Object arg = originObject;
116+
while (arg instanceof WrapsElement) {
117+
arg = ((WrapsElement) arg).getWrappedElement();
118+
}
119+
return arg;
120+
}
121+
122+
private Origin(Object originObject) {
123+
this.originObject = originObject;
124+
}
125+
126+
public static WheelInput.Origin viewport() {
127+
return new WheelInput.Origin("viewport");
128+
}
129+
130+
public static WheelInput.Origin fromElement(WebElement element) {
131+
return new WheelInput.Origin(Require.nonNull("Element", element));
132+
}
133+
}
111134
}

java/test/org/openqa/selenium/interactions/DefaultWheelTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void shouldScrollToElement() {
5050
0,
5151
0,
5252
0,
53-
PointerInput.Origin.fromElement(iframe)).perform();
53+
WheelInput.Origin.fromElement(iframe)).perform();
5454

5555
assertTrue(inViewport(iframe));
5656
}
@@ -65,7 +65,7 @@ public void shouldScrollFromElementByGivenAmount() {
6565
0,
6666
0,
6767
200,
68-
PointerInput.Origin.fromElement(iframe)).perform();
68+
WheelInput.Origin.fromElement(iframe)).perform();
6969

7070
driver.switchTo().frame(iframe);
7171

@@ -83,7 +83,7 @@ public void shouldScrollFromElementByGivenAmountWithOffset() {
8383
-50,
8484
0,
8585
200,
86-
PointerInput.Origin.fromElement(footer)).perform();
86+
WheelInput.Origin.fromElement(footer)).perform();
8787

8888
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
8989

@@ -101,7 +101,7 @@ public void throwErrorWhenElementOriginIsOutOfViewport() {
101101
50,
102102
0,
103103
200,
104-
PointerInput.Origin.fromElement(footer)).perform();
104+
WheelInput.Origin.fromElement(footer)).perform();
105105
}
106106

107107
@Test
@@ -116,7 +116,7 @@ public void shouldScrollFromViewportByGivenAmount() {
116116
0,
117117
0,
118118
y,
119-
PointerInput.Origin.viewport()).perform();
119+
WheelInput.Origin.viewport()).perform();
120120

121121
assertTrue(inViewport(footer));
122122
}
@@ -132,7 +132,7 @@ public void shouldScrollFromViewportByGivenAmountFromOrigin() {
132132
10,
133133
0,
134134
200,
135-
PointerInput.Origin.viewport()).perform();
135+
WheelInput.Origin.viewport()).perform();
136136

137137
driver.switchTo().frame(iframe);
138138

@@ -149,7 +149,7 @@ public void throwErrorWhenOriginOffsetIsOutOfViewport() {
149149
-10,
150150
0,
151151
200,
152-
PointerInput.Origin.viewport()).perform();
152+
WheelInput.Origin.viewport()).perform();
153153
}
154154

155155
private boolean inViewport(WebElement element) {

java/test/org/openqa/selenium/interactions/WheelInputTest.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void shouldEncodeWrappedElementInScrollOrigin() {
5050
0,
5151
0,
5252
Duration.ofMillis(100),
53-
Origin.fromElement(element));
53+
WheelInput.Origin.fromElement(element));
5454
Sequence sequence = new Sequence(wheelInput, 0).addAction(scroll);
5555

5656
String rawJson = new Json().toJson(sequence);
@@ -84,7 +84,7 @@ public void shouldEncodeScrollInteractionWithViewPortOrigin() {
8484
30,
8585
60,
8686
Duration.ofSeconds(1),
87-
Origin.viewport());
87+
WheelInput.Origin.viewport());
8888

8989
Map<String, Object> encodedResult = interaction.encode();
9090
assertThat(encodedResult)
@@ -110,7 +110,7 @@ public void shouldEncodeScrollInteractionWithElementOrigin() {
110110
30,
111111
60,
112112
Duration.ofSeconds(1),
113-
Origin.fromElement(innerElement));
113+
WheelInput.Origin.fromElement(innerElement));
114114

115115
Map<String, Object> encodedResult = interaction.encode();
116116
assertThat(encodedResult)
@@ -123,20 +123,6 @@ public void shouldEncodeScrollInteractionWithElementOrigin() {
123123
.containsEntry("origin", innerElement);
124124
}
125125

126-
@Test(expected = IllegalArgumentException.class)
127-
public void shouldNotAllowPointerOrigin() {
128-
129-
WheelInput wheelInput = new WheelInput("test-wheel");
130-
WheelInput.ScrollInteraction interaction = new WheelInput.ScrollInteraction(
131-
wheelInput,
132-
25,
133-
50,
134-
30,
135-
60,
136-
Duration.ofSeconds(1),
137-
Origin.pointer());
138-
}
139-
140126
private static class ActionSequenceJson {
141127

142128
List<ActionJson> actions;

0 commit comments

Comments
 (0)