Getting Clipboard content from remote Selenium Chrome Nodes

AMIT RAWAT
4 min readDec 16, 2019

In case your application has a clipboard feature where users can copy the screen content to clipboard in a specific format then it could be a good candidate for automated testing.

Testing the text content and format visually is a tiresome job so one would definitely like to automate it.

To make you understand the scenario better let’s see the example of this demo website where you have to test the copy functionality.

We will type some text in to the text box and then click on copy button. Then as an assertion point we have to read the content from the system’s clipboard and verify the clipboard value matches with the original text which we have set in the text box

https://googlechrome.github.io/samples/async-clipboard/

If we have to automate this on a local browser then it is quite simple as shown below. We will use the Java Toolkit api to read content from the local clipboard.

How about if the execution is happening on a selenium grid where the browser is not running on the local test machine then the Toolkit api can also not help.

Selenium does not offer any solution to read the clipboard content from a remote node machine. If you search stackoverflow with this problem, the question is still unanswered.

In another post, it is clearly stated:

The nodes are going to be getting only JSONWireProtocol compliant commands. Copying to/from the clipboard is NOT part of the JSONWireProtocol spec and so there is no way the node would be getting requests to access clipboard.

So how to solve it ? To solve this problem we can think of coming up of some small service agent which could be a small program written in any language and it should expose a REST or RPC method to get the clipboard content from the remote machine to the host machine.

But this is too much of work to solve this tiny problem, building this small service is not a big task but to deploy it to each selenium node and then maintain it at regular basis makes it a formidable task.

We have to come up with something which is completely out of the box and should work with all operating systems and browsers.

As I am a big fan of Javascript so I thought of exploring that route, there should be a javascript way to read this content from local clipboard and pass it back via the standard JSONWireProtocol.

One step at a time, let’s find the JS api to read this clipboard content.

navigator.clipboard.readText();

Great, let try to use it. Strangely we will get this exception which kind of says we do not have permission to read clipboard contents.

To fix this error , we have to understand the security and permissioning around clipboard access.

Clipboard access has always presented a security concern for browsers. Without proper permissions in place, a page could silently copy all manner of malicious content to a user’s clipboard that would produce catastrophic results when pasted. Imagine a web page that silently copies rm -rf / or a decompression bomb image to your clipboard.

navigator.clipboard API is only supported:

  • for pages served over HTTPS
  • clipboard access is only allowed when a page is the active tab
  • Pages in active tabs can write to the clipboard without requesting permission
  • reading from the clipboard always requires permission

The last bullet is the key for our problem, so we have to somehow give clipboard read permission to our browser which we can give manually as shown below:

But how to give this permission programmatically to your browser ??

I was able to figure it out, how to give it for chrome browser but for other browsers like Firefox and IE, I will leave up to you guys to explore it.

For chrome we have a user profile preference where we can define this permission for one or multiple hosts.

profile.content_settings.exceptions.clipboard : {
"[*.],*": {
"last_modified": "1576491240619",
"setting": 1
}
}

This preference can be set using the ChromeOptions class as shown below.

Now as we have solved the permissioning issue, we should be able to get the clipboard text.

Here is the complete javascript with proper error handling.

The JUnit tests can be seen here.

The complete code is here.

I hope you enjoyed this blog, please feel free to reach me to provide your feedback. I can be be reached via comments section or over here.

--

--

AMIT RAWAT

I am a Civil Engineer by qualification, an Engineering Manager by profession and a Developer by passion. (amitrawat.dev)