1
1
package com .aerokube .lightning .extensions ;
2
2
3
+ import com .aerokube .lightning .FileUtils ;
3
4
import com .aerokube .lightning .WebDriver ;
5
+ import com .aerokube .lightning .WebDriverException ;
4
6
import com .aerokube .lightning .WebDriverExtension ;
5
7
import com .aerokube .lightning .api .AerokubeApi ;
6
- import com .aerokube .lightning .model .StringResponse ;
8
+ import com .aerokube .lightning .model .ClipboardData ;
7
9
8
10
import javax .annotation .Nonnull ;
11
+ import java .nio .charset .StandardCharsets ;
12
+ import java .nio .file .Path ;
9
13
import java .util .Base64 ;
10
14
import java .util .List ;
11
15
@@ -19,20 +23,48 @@ public MoonCommands(WebDriver webDriver) {
19
23
}
20
24
21
25
@ Nonnull
22
- public MoonCommands updateClipboard (@ Nonnull byte [] content ) {
26
+ public MoonCommands updateClipboardText (@ Nonnull String text ) {
23
27
execute (() -> {
24
- String encodedContent = Base64 .getEncoder ().encodeToString (content );
25
- aerokubeApi .updateClipboard (getSessionId (), new StringResponse ().value (encodedContent ));
28
+ aerokubeApi .updateClipboard (getSessionId (), new ClipboardData ().value (text ));
26
29
return null ;
27
30
});
28
31
return this ;
29
32
}
30
33
31
34
@ Nonnull
32
- public byte [] getClipboard () {
35
+ public MoonCommands updateClipboardImage (@ Nonnull Path image ) {
36
+ execute (() -> {
37
+ String encodedImage = FileUtils .encodeFileToBase64 (image );
38
+ aerokubeApi .updateClipboard (
39
+ getSessionId (),
40
+ new ClipboardData ()
41
+ .value (encodedImage )
42
+ .media (ClipboardData .MediaEnum .IMAGE_PNG )
43
+ );
44
+ return null ;
45
+ });
46
+ return this ;
47
+ }
48
+
49
+ @ Nonnull
50
+ public String getClipboardText () {
33
51
return execute (() -> {
34
- String encodedBytes = aerokubeApi .getClipboard (getSessionId ()).getValue ();
35
- return Base64 .getDecoder ().decode (encodedBytes );
52
+ ClipboardData clipboard = aerokubeApi .getClipboard (getSessionId ());
53
+ if (clipboard .getMedia () != null ) {
54
+ throw new WebDriverException (String .format ("Received non-text data from the clipboard: %s" , clipboard .getMedia ()));
55
+ }
56
+ return clipboard .getValue ();
57
+ });
58
+ }
59
+
60
+ @ Nonnull
61
+ public byte [] getClipboardImage () {
62
+ return execute (() -> {
63
+ ClipboardData clipboard = aerokubeApi .getClipboard (getSessionId ());
64
+ if (clipboard .getMedia () == null || !ClipboardData .MediaEnum .IMAGE_PNG .equals (clipboard .getMedia ())) {
65
+ throw new WebDriverException ("Received non-image data from the clipboard" );
66
+ }
67
+ return Base64 .getDecoder ().decode (clipboard .getValue ().getBytes (StandardCharsets .UTF_8 ));
36
68
});
37
69
}
38
70
0 commit comments