-
-
Notifications
You must be signed in to change notification settings - Fork 766
Add clipboard handlers #855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/io/appium/java_client/android/HasAndroidClipboard.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client.android; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static io.appium.java_client.MobileCommand.SET_CLIPBOARD; | ||
import static io.appium.java_client.MobileCommand.prepareArguments; | ||
|
||
import io.appium.java_client.CommandExecutionHelper; | ||
import io.appium.java_client.clipboard.ClipboardContentType; | ||
import io.appium.java_client.clipboard.HasClipboard; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.AbstractMap; | ||
import java.util.Base64; | ||
|
||
public interface HasAndroidClipboard extends HasClipboard { | ||
/** | ||
* Set the content of device's clipboard. | ||
* | ||
* @param label clipboard data label. | ||
* @param contentType one of supported content types. | ||
* @param base64Content base64-encoded content to be set. | ||
*/ | ||
default void setClipboard(String label, ClipboardContentType contentType, byte[] base64Content) { | ||
String[] parameters = new String[]{"content", "contentType", "label"}; | ||
Object[] values = new Object[]{new String(checkNotNull(base64Content), StandardCharsets.UTF_8), | ||
contentType.name().toLowerCase(), checkNotNull(label)}; | ||
CommandExecutionHelper.execute(this, new AbstractMap.SimpleEntry<>(SET_CLIPBOARD, | ||
prepareArguments(parameters, values))); | ||
} | ||
|
||
/** | ||
* Set the clipboard text. | ||
* | ||
* @param label clipboard data label. | ||
* @param text The actual text to be set. | ||
*/ | ||
default void setClipboardText(String label, String text) { | ||
setClipboard(label, ClipboardContentType.PLAINTEXT, Base64 | ||
.getEncoder() | ||
.encode(text.getBytes(StandardCharsets.UTF_8))); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/io/appium/java_client/clipboard/ClipboardContentType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client.clipboard; | ||
|
||
public enum ClipboardContentType { | ||
PLAINTEXT, IMAGE, URL | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/io/appium/java_client/clipboard/HasClipboard.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client.clipboard; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static io.appium.java_client.MobileCommand.GET_CLIPBOARD; | ||
import static io.appium.java_client.MobileCommand.SET_CLIPBOARD; | ||
import static io.appium.java_client.MobileCommand.prepareArguments; | ||
|
||
import io.appium.java_client.CommandExecutionHelper; | ||
import io.appium.java_client.ExecutesMethod; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.AbstractMap; | ||
import java.util.Base64; | ||
|
||
public interface HasClipboard extends ExecutesMethod { | ||
/** | ||
* Set the content of device's clipboard. | ||
* | ||
* @param contentType one of supported content types. | ||
* @param base64Content base64-encoded content to be set. | ||
*/ | ||
default void setClipboard(ClipboardContentType contentType, byte[] base64Content) { | ||
String[] parameters = new String[]{"content", "contentType"}; | ||
Object[] values = new Object[]{new String(checkNotNull(base64Content), StandardCharsets.UTF_8), | ||
contentType.name().toLowerCase()}; | ||
CommandExecutionHelper.execute(this, new AbstractMap.SimpleEntry<>(SET_CLIPBOARD, | ||
prepareArguments(parameters, values))); | ||
} | ||
|
||
/** | ||
* Get the content of the clipboard. | ||
* | ||
* @param contentType one of supported content types. | ||
* @return the actual content of the clipboard as base64-encoded string or an empty string if the clipboard is empty | ||
*/ | ||
default String getClipboard(ClipboardContentType contentType) { | ||
return CommandExecutionHelper.execute(this, new AbstractMap.SimpleEntry<>(GET_CLIPBOARD, | ||
prepareArguments("contentType", contentType.name().toLowerCase()))); | ||
} | ||
|
||
/** | ||
* Set the clipboard text. | ||
* | ||
* @param text The actual text to be set. | ||
*/ | ||
default void setClipboardText(String text) { | ||
setClipboard(ClipboardContentType.PLAINTEXT, Base64 | ||
.getEncoder() | ||
.encode(text.getBytes(StandardCharsets.UTF_8))); | ||
} | ||
|
||
/** | ||
* Get the clipboard text. | ||
* | ||
* @return Either the text, which is stored in the clipboard or an empty string if the clipboard is empty | ||
*/ | ||
default String getClipboardText() { | ||
byte[] base64decodedBytes = Base64 | ||
.getDecoder() | ||
.decode(getClipboard(ClipboardContentType.PLAINTEXT)); | ||
return new String(base64decodedBytes, StandardCharsets.UTF_8); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/io/appium/java_client/ios/HasIOSClipboard.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client.ios; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import io.appium.java_client.clipboard.ClipboardContentType; | ||
import io.appium.java_client.clipboard.HasClipboard; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import javax.imageio.ImageIO; | ||
|
||
public interface HasIOSClipboard extends HasClipboard { | ||
/** | ||
* Set an image to the clipboard. | ||
* | ||
* @param img the actual image to be set. | ||
* @throws IOException if the image cannot be decoded in PNG representation | ||
*/ | ||
default void setClipboardImage(BufferedImage img) throws IOException { | ||
try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { | ||
ImageIO.write(checkNotNull(img), "png", os); | ||
setClipboard(ClipboardContentType.IMAGE, Base64 | ||
.getEncoder() | ||
.encode(os.toByteArray())); | ||
} | ||
} | ||
|
||
/** | ||
* Get an image from the clipboard | ||
* | ||
* @return the actual image instance. | ||
* @throws IOException If the returned image cannot be decoded or if the clipboard is empty. | ||
*/ | ||
default BufferedImage getClipboardImage() throws IOException { | ||
final byte[] base64decodedBytes = Base64 | ||
.getDecoder() | ||
.decode(getClipboard(ClipboardContentType.IMAGE)); | ||
return ImageIO.read(new ByteArrayInputStream(base64decodedBytes)); | ||
} | ||
|
||
/** | ||
* Set an URL to the clipboard. | ||
* | ||
* @param url the actual URL to set. | ||
*/ | ||
default void setClipboardUrl(URL url) { | ||
setClipboard(ClipboardContentType.URL, Base64 | ||
.getEncoder() | ||
.encode(checkNotNull(url).toString().getBytes(StandardCharsets.UTF_8))); | ||
} | ||
|
||
/** | ||
* Get an URL from the clipboard | ||
* | ||
* @return the actual URL instance. | ||
* @throws MalformedURLException if the URL in the clipboard is not valid or if the clipboard is empty. | ||
*/ | ||
default URL getClipboardUrl() throws MalformedURLException { | ||
final byte[] base64decodedBytes = Base64 | ||
.getDecoder() | ||
.decode(getClipboard(ClipboardContentType.URL)); | ||
return new URL(new String(base64decodedBytes, StandardCharsets.UTF_8)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/io/appium/java_client/android/ClipboardTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client.android; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ClipboardTest extends BaseAndroidTest { | ||
|
||
@Before public void setUp() { | ||
driver.resetApp(); | ||
} | ||
|
||
@Test public void verifySetAndGetClipboardText() { | ||
final String text = "Happy testing"; | ||
driver.setClipboardText(text); | ||
assertEquals(driver.getClipboardText(), text); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/io/appium/java_client/ios/ClipboardTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client.ios; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class ClipboardTest extends AppIOSTest { | ||
|
||
@Test public void verifySetAndGetClipboardText() { | ||
final String text = "Happy testing"; | ||
driver.setClipboardText(text); | ||
assertEquals(driver.getClipboardText(), text); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getURLEncoder()
&getURLDecoder()
should be good than basic encoding/decoding as it is not URL safe. Also for text encoding, i would recommend to use MIME encoding over basic encoding. what say @mykola-mokhnach ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is not the case for Apple's NSData class, since it requires the data to be always padded: https://stackoverflow.com/questions/21406482/nsdata-wont-accept-valid-base64-encoded-string