35
35
import org .openqa .selenium .chrome .ChromeOptions ;
36
36
import org .openqa .selenium .devtools .DevTools ;
37
37
import org .openqa .selenium .devtools .v136 .page .Page ;
38
+ import org .openqa .selenium .devtools .v136 .page .model .ScreencastFrameMetadata ;
38
39
import org .openqa .selenium .interactions .Action ;
39
40
import org .openqa .selenium .interactions .Actions ;
40
41
@@ -57,6 +58,9 @@ public final class ChromeDriverPlayer implements BrowserPlayer {
57
58
private final DevTools tools ;
58
59
59
60
private volatile byte [] frameBuffer ;
61
+ private volatile long frameWidth ;
62
+ private volatile long frameHeight ;
63
+
60
64
private volatile CompletableFuture <Void > captureTask ;
61
65
private volatile CompletableFuture <Void > processingTask ;
62
66
@@ -112,6 +116,9 @@ private void startScreenCapture() {
112
116
final Base64 .Decoder decoder = Base64 .getDecoder ();
113
117
this .tools .addListener (Page .screencastFrame (), frame -> {
114
118
if (this .running .get ()) {
119
+ final ScreencastFrameMetadata frameMetadata = frame .getMetadata ();
120
+ this .frameWidth = (long ) frameMetadata .getDeviceWidth ();
121
+ this .frameHeight = (long ) frameMetadata .getDeviceHeight ();
115
122
this .frameBuffer = decoder .decode (frame .getData ());
116
123
this .tools .send (Page .screencastFrameAck (frame .getSessionId ()));
117
124
}
@@ -131,6 +138,7 @@ private void processFrames() {
131
138
while (this .running .get ()) {
132
139
if (this .frameBuffer != null ) {
133
140
final StaticImage staticImage = StaticImage .bytes (this .frameBuffer );
141
+ staticImage .resize (metadata .getVideoWidth (), metadata .getVideoHeight ());
134
142
VideoPipelineStep current = this .videoPipeline ;
135
143
while (current != null ) {
136
144
current .process (staticImage , metadata );
@@ -152,13 +160,25 @@ public void sendMouseEvent(final int x, final int y, final MouseClick type) {
152
160
if (!this .running .get ()) {
153
161
return ;
154
162
}
163
+ final int [] translated = this .translateCoordinates (x , y );
164
+ final int newX = translated [0 ];
165
+ final int newY = translated [1 ];
155
166
final Actions actions = new Actions (this .driver );
156
- final Actions move = actions .moveToLocation (x , y );
167
+ final Actions move = actions .moveToLocation (newX , newY );
157
168
final Actions modified = this .getAction (type , move );
158
169
final Action action = modified .build ();
159
170
action .perform ();
160
171
}
161
172
173
+ private int [] translateCoordinates (final int x , final int y ) {
174
+ final VideoMetadata videoMetadata = this .source .getMetadata ();
175
+ final int targetWidth = videoMetadata .getVideoWidth ();
176
+ final int targetHeight = videoMetadata .getVideoHeight ();
177
+ final int newX = (int ) (((float ) x / this .frameWidth ) * targetWidth );
178
+ final int newY = (int ) (((float ) y / this .frameHeight ) * targetHeight );
179
+ return new int [] { newX , newY };
180
+ }
181
+
162
182
/**
163
183
* {@inheritDoc}
164
184
*/
0 commit comments