{"id":1119,"date":"2025-06-03T06:32:20","date_gmt":"2025-06-03T06:32:20","guid":{"rendered":"https:\/\/razvanvancea.ro\/blog\/?p=1119"},"modified":"2025-06-03T07:21:44","modified_gmt":"2025-06-03T07:21:44","slug":"how-to-copy-paste-text-from-clipboard-using-cypress","status":"publish","type":"post","link":"https:\/\/razvanvancea.ro\/blog\/2025\/06\/03\/how-to-copy-paste-text-from-clipboard-using-cypress\/","title":{"rendered":"How to copy-paste text from clipboard using Cypress"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div>\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"1119\" class=\"elementor elementor-1119\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-ca38491 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"ca38491\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-4bb8b6a\" data-id=\"4bb8b6a\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-00e16ac elementor-widget elementor-widget-text-editor\" data-id=\"00e16ac\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"320\" data-end=\"692\">I recently had to simulate copy-paste interactions in Cypress and it was not easy to find a solution on Google, making this a tricky feature to test. Cypress allows you to <strong data-start=\"601\" data-end=\"630\">stub clipboard operations<\/strong> and simulate a full copy-paste flow in a clean, testable way.<\/p><p>\u00a0<\/p><p data-start=\"694\" data-end=\"821\">In this article, I will show you how to stub clipboard content and simulate pasting text into an input field using Cypress.<br \/><br \/>We want to test that a user can paste a value from their clipboard into an input field. Since Cypress doesn\u2019t support direct clipboard interaction for security reasons, we\u2019ll <strong data-start=\"1047\" data-end=\"1091\">mock the clipboard&#8217;s <code data-start=\"1070\" data-end=\"1082\">readText()<\/code> method<\/strong> to return a known value and assert that the value is pasted correctly into the input.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-1c6cfd4 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"1c6cfd4\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-5d6f27c\" data-id=\"5d6f27c\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-1f772ff elementor-widget elementor-widget-heading\" data-id=\"1f772ff\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Test Example<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-15a3a79 elementor-widget elementor-widget-image\" data-id=\"15a3a79\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"640\" height=\"358\" src=\"https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog-1024x572.png\" class=\"attachment-large size-large wp-image-1121\" alt=\"cy-clipboard\" srcset=\"https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog-1024x572.png.webp 1024w, https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog-300x168.png.webp 300w, https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog-768x429.png.webp 768w, https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog-850x475.png.webp 850w, https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog-24x13.png.webp 24w, https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog-36x20.png.webp 36w, https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog-48x27.png.webp 48w, https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/cy_blog.png.webp 1276w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-3ec0e8f elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"3ec0e8f\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-f76faf2\" data-id=\"f76faf2\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-621ceae elementor-widget elementor-widget-heading\" data-id=\"621ceae\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Breakdown<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-630991c elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"630991c\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-ae88535\" data-id=\"ae88535\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-11a16de elementor-widget elementor-widget-text-editor\" data-id=\"11a16de\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre><strong>1. visit the application<\/strong><br \/>cy.visit(\"https:\/\/todomvc-app-for-testing.surge.sh\/\");<br \/><br \/><strong>2. Stud Clipboard API<\/strong><\/pre><p>cy.window().then((win) =&gt; {<br \/>cy.stub(win.navigator.clipboard, &#8220;readText&#8221;).resolves(&#8220;Razvan&#8221;);<br \/>});<br \/><br \/>We stub <code data-start=\"2292\" data-end=\"2324\">navigator.clipboard.readText()<\/code> so whenever the test calls it, it returns <code data-start=\"2367\" data-end=\"2377\">\"Razvan\"<\/code>\u2014our fake clipboard content.<br \/><br \/><\/p><p><strong>3. Trigger Paste Behavior<br \/><\/strong>cy.window().then((win) =&gt; {<br \/>return win.navigator.clipboard.readText().then((text) =&gt; {<br \/>cy.get(&#8220;input.new-todo&#8221;).type(text);<br \/>});<br \/>});<br \/><br \/>We call the stubbed <code data-start=\"2619\" data-end=\"2631\">readText()<\/code> and use the result to simulate a user pasting it into the <code data-start=\"2690\" data-end=\"2706\">input.new-todo<\/code> field.<strong><br \/><br \/><\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7a386e7 elementor-widget elementor-widget-heading\" data-id=\"7a386e7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Conclusion<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-cffa80f elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"cffa80f\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3520b26\" data-id=\"3520b26\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-16219f6 elementor-widget elementor-widget-text-editor\" data-id=\"16219f6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Clipboard interactions might be essential parts of many applications and using this approach in Cypress we can simulate the same behavior and ensure robust E2E tests.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-34c2560 elementor-widget elementor-widget-spacer\" data-id=\"34c2560\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"spacer.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-spacer\">\n\t\t\t<div class=\"elementor-spacer-inner\"><\/div>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-7c9d616 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"7c9d616\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-896adf2\" data-id=\"896adf2\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-bcd8071 elementor-widget elementor-widget-text-editor\" data-id=\"bcd8071\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Enjoyed this article? Make sure to subscribe to my\u00a0<em><a href=\"https:\/\/www.youtube.com\/@LearnwithRV\" target=\"_blank\" rel=\"noopener\">YouTube Channel<\/a><\/em>\u00a0for more Test Automation tutorials, and follow me on\u00a0<a href=\"https:\/\/www.linkedin.com\/in\/razvanvancea\/\" target=\"_blank\" rel=\"noopener\"><em>LinkedIn<\/em><\/a>\u00a0and\u00a0<em><a href=\"https:\/\/twitter.com\/razvanvancea\" target=\"_blank\" rel=\"noopener\">Twitter<\/a><\/em>\u00a0for regular insights.<span style=\"font-weight: bold;\"><br \/>Looking to improve your test automation skills?<br \/><\/span>I&#8217;ve created a<span style=\"font-weight: bold;\">\u00a0personalized 1-on-1 Mentoring program\u00a0<\/span>refined to<span style=\"font-weight: bold;\">\u00a0boost YOUR CURRENT skills.\u00a0<\/span>Reach out at\u00a0<a rel=\"noopener\">iamrv@razvanvancea.ro<\/a>\u00a0for more details and\u00a0<span style=\"font-weight: bold;\">together\u00a0<\/span>will create a<span style=\"font-weight: bold;\">\u00a0learning path\u00a0<\/span>adapted to\u00a0<span style=\"font-weight: bold;\">your skills and goals\u00a0<\/span>that you are aiming for<span style=\"font-weight: bold;\">,<\/span>\u00a0in a<span style=\"font-weight: bold;\">\u00a0timely-efficient manner\ud83d\ude80<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>I recently had to simulate copy-paste interactions in Cypress and it was not easy to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1134,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-qa"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/razvanvancea.ro\/blog\/wp-content\/uploads\/2025\/06\/Copy-of-Copy-of-MacOS-Tools-to-BOOST-Productivity-3.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/posts\/1119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/comments?post=1119"}],"version-history":[{"count":15,"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/posts\/1119\/revisions"}],"predecessor-version":[{"id":1141,"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/posts\/1119\/revisions\/1141"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/media\/1134"}],"wp:attachment":[{"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/media?parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/categories?post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/razvanvancea.ro\/blog\/wp-json\/wp\/v2\/tags?post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}