Retrieving Clipboard Text from a Real iOS Device

     We saw that in most of the mobile application have the feature of showing a pop-over layout with Copy URL or Copy Link, so this is an easiest way to copy the details from clipboard and share with required fields. We will how it can be done in real iOS devices using Appium’s capability. If we were working with an iOS simulator or an Android device, the solution would be simple: we could use driver.getClipboardText and we already discussed in my one of previous article. But driver.getClipboardText method is not available for real iOS device. In this article, we will see how can we retrieve the clipboard text from a real iOS device.

Pasting and Reading the Value

     The main thing to decide when using this technique is: where should we paste the clipboard contents? We want a place where we can have a straightforward automation path to being able to paste, and we want to make sure that existing text or content doesn’t get in the way. For this reason, we chose the Notes application as a good place to paste, because:

  • When you launch the Notes app, no matter where you are, you have the ability to create a new (blank) note with one tap.
  • The Notes app has one main text field, whose contents Appium has access to.

The flow we need to implement to make this happen in more detail looks like this:

  1. Get the data into the clipboard (get data like Copy URL).
  2. Activate the Notes app.
  3. Create a new blank note.
  4. Paste the clipboard.
  5. Find the element containing the pasted contents.
  6. Read the text from that element.

     If we can do all these things, we will have the value of the iOS real device clipboard in our test script, and can then do whatever we want with it. So let’s have a look at the implementation,

// open up the notes app (this only exists on a real device)

// create a new blank note

// tap the note field to bring up the ‘paste’ button
WebElement note = wait.until(ExpectedConditions.presenceOfElementLocated(NOTE_CONTENT));;

// paste the content

// retrieve the content and do something with it
System.out.println(“Clipboard text was: ” + note.getText());

     One thing to keep in mind is that if there is rich content in the clipboard (a URL, image, etc…), it will be pasted as its own element or set of elements within the note. In that case, you will need to find that element instead of the main note element when retrieving content.

     I hope the above workflow logic will help you to get the clipboard text and transfer the text into different fields in your application during your automation on real iOS devices.

Reference: Appium Pro

make it perfect!