Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to access clipboard contents?

I am testing a page that has an embed modal with a textbox with an embed code and a "Copy" button that should copy the contents of the textbox onto the clipboard so a user can paste it elsewhere. Is there a way to test clicking the "Copy" button and verifying that the clipboard contents match the contents of the textbox? Thanks!

like image 414
Samantha Blasbalg Avatar asked Sep 12 '18 15:09

Samantha Blasbalg


People also ask

Is there a way to see all clipboard history?

Here's how to view the clipboard history using Clipboard Manager: Install the Clipboard Manager app from the Play Store. Open the app. You'll find the copied items under the Clipboard section, with the most recent ones at the top.


1 Answers

TestCafe cannot automate a browser's built-in behavior, including the Copy & Paste functionality. It is expected that this functionality works correctly as it is tested by browser developers.

You can try to check that your script/button executes the copy command in the following way:

const overwriteCopyCommand = ClientFunction(() => {
    document.execCommand = command => window.lastExecutedCommand = command;
});

const getLastExecutedCommand = ClientFunction(() => window.lastExecutedCommand);

await overwriteCopyCommand();
await t
    .click('.copy-url-button')
    .expect(getLastExecutedCommand()).eql('copy');

Unfortunately, according to JavaScript restrictions, I don't see a way how to check the copied text.

See additional workarounds in these threads:

Support 'ctrl+c' and 'ctrl+v' key combinations for copying/pasting selected text

Allow to use HTML5 Clipboard API in tests

like image 101
Marion Avatar answered Dec 04 '22 18:12

Marion