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!
Is there a way to access clipboard contents?
Asked Answered
Did you try simulating a "CTRL+V" key press somewhere to assert that the content pasted is ok ? –
Sokul
No, I don't have anywhere on the page to paste it other than that single textbox, which seems like an odd test. Plus I don't think CTRL+V works in testcafe right now? github.com/DevExpress/testcafe/issues/2466 –
Lightsome
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
© 2022 - 2024 — McMap. All rights reserved.