How to validate paste disabled input box using Selenium WebDriver

You must have seen some input text boxes where you can not copy-paste a value. For example:- In a banking application, account number field may not allow you to copy paste a value. If you try pasting a value in that input text box, it will not show any entered value.

A copy paste disabled web element can be created in multiple ways. An example is as below:-

 Enter Account Number : 

Save above html code in a .html file and open in a browser. Try to copy paste a value in to it. You will not be able to do so. Now enter some values in text box manually and try to copy it. You will not able to do so as well.

This may sound complex but it is not at all. Just you need to observe the html code of web element carefully and find out the attributes which make it copy – paste disabled.

Let’s start to find those attributes.

If see html code of above web element , you will see four attributes:-

oncopy , ondrag, ondrop and onpaste

In fact above four are event attributes and it is fired when specific action i.e. copy, paste, drag and drop are performed. If you see value of these attributes it is returning false which triggers no event and restrict copy, paste, drag and drop.

You just need to assert values of these attributes in Selenium WebDriver. Again I am repeating that developers may develop the same feature in different ways as well. You just need to find correct attributes.

package ScenarioBased;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class VerifyCopyPasteDisabledTextBox {

        @Test
        public void verifyCopyPasteDisabledTextBox() {
                
                // Browser initialization
                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                String fileURL = System.getProperty("user.dir");
                driver.get(fileURL + "/src/test/resources/htmlFiles/PasteDisabled.html");
                
                // Verify input box is copy disabled
                WebElement txtBoxAccountNumber = driver.findElement(By.name("ReceiveNo"));
                String onCopyVal = txtBoxAccountNumber.getAttribute("oncopy");
                System.out.println("On copy value = "+onCopyVal);
                Assert.assertTrue(onCopyVal.contains("false"));
                // Verify input box is paste disabled
                String onPasteVal = txtBoxAccountNumber.getAttribute("onpaste");
                System.out.println("On paste value = "+onPasteVal);
                Assert.assertTrue(onPasteVal.contains("false"));
                driver.quit();
        }

}

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning