Is there a way to get the last clipboard on mobile?

I need to make a test on a mobile app where i click on a button that add some text to the clipboard.
And i want to control it with an assertion to check if the last clipboard match with my data test, but i didn’t find how to get to the last clipboard without all the click to get it (open keyboard, click on clipboard button, and find the last one…)

Is there a way to get the last clipboard on a mobile app ?

Can you please provide us with Mobile OS you are working with?

It depends on yor tests implementation and by the Appium version used.
E.g appium 1.6.0 + have a driver.getClipboard() method.

https://appium.readthedocs.io/en/latest/en/commands/device/clipboard/get-clipboard/#description

1 Like

I’m on android V12

katalon support appium version 1.12.1

I don’t know if this is safe for katalon to upgrade the appium version.

1.12.1 > 1.6.0 :stuck_out_tongue:

i read it wrong…
Thanks for the answer :slight_smile:

1 Like

worry not, i was confused too, but after i re-read my post i got it, therefore i edited :smile:

@simon.ruiz sorry for taking so long to reply, I had to setup mobile testing on my side to troubleshoot.

Let us try the following:

  1. Import the modules
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.datatransfer.DataFlavor
  1. At the very start of your script add:
//Create clipboard object
def clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
  1. After the step where you click save to clipboard, run the following:
//Set clipboardData equal to contents of clipboard
def clipboardData = clipboard.getData(DataFlavor.stringFlavor)

//Print value of clipboardData
println('Clipboard Data is: ' + clipboardData)
  1. Check in your console logs if you see a line starting with 'Clipboard Data is: ’

1 Like

I came up with an other solution with the response of @bionel
i use this code io.appium.java_client.clipboard.HasClipboard.getClipboard java code examples | Tabnine

So my code look like :

import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
import io.appium.java_client.AppiumDriver
import com.kms.katalon.core.util.KeywordUtil

String getClipboardText() {
	byte[] base64decodedBytes = Base64
		.getMimeDecoder()
		.decode(getClipboard(ClipboardContentType.PLAINTEXT));
	return new String(base64decodedBytes, StandardCharsets.UTF_8);
}
// Click on the button to save to clipboard
AppiumDriver<?> driver = MobileDriverFactory.getDriver()
String clipboard = driver.getClipboardText()
println(clipboard)
3 Likes

I am not sure how this is supposed to work.
I am not familiar with awt but looks more like it will grab the content of the clipboard of the host where Katalon/KRE is running.
(which is not a bad thing, may be useful in some other cases)
The OP needs to access the clipboard of the (remote) mobile device subject to test, so using DriverFactory is more appropiate… and readable, IMHO

I think this is not needed at all, you are accesing anyway the method provided by appium driver.

Yeah, maybe this is the important ligne : getClipboard(ClipboardContentType.PLAINTEXT)

Nope.
Those two do the magic:

AppiumDriver<?> driver = MobileDriverFactory.getDriver()
String clipboard = driver.getClipboardText()

the String getClipboardText() { method (where you used the driver.getClipboard(ClipboardContentType.PLAINTEXT); signature) is useless, it does nothing :slight_smile:
(at least for the current case)

1 Like