Retrieve clipboard content of Android Device as part of automation

I am automating tests on android device which is connected to a remote host. I am able to run my test perfectly well. As part of an other usecase, I need to retrieve the data residing in the clipboard of the Android Device under test. Is there any way Appium can help me retrieve the clipboard content from Android device into my java code for further processing.

  1. Download this application and install on the device.
  2. Go to applications the installed device and launch the installed application.
  3. execute below command on shell
    am broadcast -a clipper.get

Alternatively you can automate this whole process.
Tested on Android 5.0.1
Reference

I do not want to depend on external apkā€™s as part of the automation process. Is there any alternative way to doing this. One quick solution I get is open any text editor inside the device, long press and copy the clipboard contents. The problem here is how do I get the data which is being displayed on the screen via Appium. If there is any way to get this data, my usecase is addressed.

You donā€™t need to install it every time, this would be needed for first time only and does not matter you do it via automation or manually.

using getText() on the text editor you are going to paste will give the clipboard text. I have tried with samsung memo app it does not show text in the UIAutomator.
You may need to install a third party note/text editor app depending on the device you have, but thats again would be an external apk however it would also be one time activity per device.

yeah looks good to me! Itā€™s a one time installation and I can do it. But how do I automate ā€œam broadcast -a clipper.getā€ command. And should adb be installed on the host the application is running. ā€˜amā€™ seems to be an unrecognised command.
Can you guide me with an example?

actual command will look like this
adb shell am broadcast -a clipper.get

execute it on shell/command prompt from the automation script
If you are using java here is example on how to do it.

Thanks for the info. I work on mac so, need to figure out come way to replace the cmd.exe. I will try it.

private static void runADBCommand(String command) throws Exception {
    Runtime.getRuntime().exec(command);
}

I have tried that on my mac. It gives me Cannot run program ā€œadbā€: error=2, No such file or directory error. I have adb already installed and the path in zshrc file

You need to have adb on the path, so that it can run from any directory

outside of appium, if you go to any folder and run ā€œadb devicesā€ in terminal it works ok?

yes it does work from any terminal

No idea then :slight_smile: Maybe try this:

Try executing below as command
/bin/bash -l -c adb shell am broadcast -a clipper.get

Just giving my approachā€¦ instead of reading from clipboard you can use following steps

  1. Save clipboard content as .txt file in sdcard [ this will be part of u r functional process]
  2. Pull file from Android device to local file system using driver.pullFile() method
  3. Retrieve contents of file using java.io library
1 Like