How to use getClipboard method of io.appium.java_client.clipboard.HasClipboard class

Best io.appium code snippet using io.appium.java_client.clipboard.HasClipboard.getClipboard

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

HasClipboard.java

Source: HasClipboard.java Github

copy
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * See the NOTICE file distributed with this work for additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package io.appium.java_client.clipboard;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20import static io.appium.java_client.MobileCommand.GET_CLIPBOARD;
21import static io.appium.java_client.MobileCommand.SET_CLIPBOARD;
22import static io.appium.java_client.MobileCommand.prepareArguments;
23
24import io.appium.java_client.CommandExecutionHelper;
25import io.appium.java_client.ExecutesMethod;
26
27import java.nio.charset.StandardCharsets;
28import java.util.AbstractMap;
29import java.util.Base64;
30
31public interface HasClipboard extends ExecutesMethod {
32    /**
33     * Set the content of device's clipboard.
34     *
35     * @param contentType one of supported content types.
36     * @param base64Content base64-encoded content to be set.
37     */
38    default void setClipboard(ClipboardContentType contentType, byte[] base64Content) {
39        String[] parameters = new String[]{"content", "contentType"};
40        Object[] values = new Object[]{new String(checkNotNull(base64Content), StandardCharsets.UTF_8),
41                contentType.name().toLowerCase()};
42        CommandExecutionHelper.execute(this, new AbstractMap.SimpleEntry<>(SET_CLIPBOARD,
43                prepareArguments(parameters, values)));
44    }
45
46    /**
47     * Get the content of the clipboard.
48     *
49     * @param contentType one of supported content types.
50     * @return the actual content of the clipboard as base64-encoded string or an empty string if the clipboard is empty
51     */
52    default String getClipboard(ClipboardContentType contentType) {
53        return CommandExecutionHelper.execute(this, new AbstractMap.SimpleEntry<>(GET_CLIPBOARD,
54                prepareArguments("contentType", contentType.name().toLowerCase())));
55    }
56
57    /**
58     * Set the clipboard text.
59     *
60     * @param text The actual text to be set.
61     */
62    default void setClipboardText(String text) {
63        setClipboard(ClipboardContentType.PLAINTEXT, Base64
64                .getMimeEncoder()
65                .encode(text.getBytes(StandardCharsets.UTF_8)));
66    }
67
68    /**
69     * Get the clipboard text.
70     *
71     * @return Either the text, which is stored in the clipboard or an empty string if the clipboard is empty
72     */
73    default String getClipboardText() {
74        byte[] base64decodedBytes = Base64
75                .getMimeDecoder()
76                .decode(getClipboard(ClipboardContentType.PLAINTEXT));
77        return new String(base64decodedBytes, StandardCharsets.UTF_8);
78    }
79}
80
Full Screen

HasIOSClipboard.java

Source: HasIOSClipboard.java Github

copy
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * See the NOTICE file distributed with this work for additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package io.appium.java_client.ios;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import io.appium.java_client.clipboard.ClipboardContentType;
22import io.appium.java_client.clipboard.HasClipboard;
23
24import java.awt.image.BufferedImage;
25import java.io.ByteArrayInputStream;
26import java.io.ByteArrayOutputStream;
27import java.io.IOException;
28import java.net.MalformedURLException;
29import java.net.URL;
30import java.nio.charset.StandardCharsets;
31import java.util.Base64;
32import javax.imageio.ImageIO;
33
34public interface HasIOSClipboard extends HasClipboard {
35    /**
36     * Set an image to the clipboard.
37     *
38     * @param img the actual image to be set.
39     * @throws IOException if the image cannot be decoded in PNG representation
40     */
41    default void setClipboardImage(BufferedImage img) throws IOException {
42        try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
43            ImageIO.write(checkNotNull(img), "png", os);
44            setClipboard(ClipboardContentType.IMAGE, Base64
45                    .getMimeEncoder()
46                    .encode(os.toByteArray()));
47        }
48    }
49
50    /**
51     * Get an image from the clipboard.
52     *
53     * @return the actual image instance.
54     * @throws IOException If the returned image cannot be decoded or if the clipboard is empty.
55     */
56    default BufferedImage getClipboardImage() throws IOException {
57        final byte[] base64decodedBytes = Base64
58                .getMimeDecoder()
59                .decode(getClipboard(ClipboardContentType.IMAGE));
60        return ImageIO.read(new ByteArrayInputStream(base64decodedBytes));
61    }
62
63    /**
64     * Set an URL to the clipboard.
65     *
66     * @param url the actual URL to set.
67     */
68    default void setClipboardUrl(URL url) {
69        setClipboard(ClipboardContentType.URL, Base64
70                .getMimeEncoder()
71                .encode(checkNotNull(url).toString().getBytes(StandardCharsets.UTF_8)));
72    }
73
74    /**
75     * Get an URL from the clipboard.
76     *
77     * @return the actual URL instance.
78     * @throws MalformedURLException if the URL in the clipboard is not valid or if the clipboard is empty.
79     */
80    default URL getClipboardUrl() throws MalformedURLException {
81        final byte[] base64decodedBytes = Base64
82                .getMimeDecoder()
83                .decode(getClipboard(ClipboardContentType.URL));
84        return new URL(new String(base64decodedBytes, StandardCharsets.UTF_8));
85    }
86}
87
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger getClipboard code on LambdaTest Cloud Grid

Execute automation tests with getClipboard on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)