Inserting a large String in an input field


Although the sendKeys() method is widely used in test automation, it is not a viable option in all cases. If the String that has to be inserted in an input field is generated from a text file and is large (50+ lines), inserting it in one go will cause the browser to freeze and inserting it line by line will take forever.

An workaround in this case is to use a JavascriptExecutor:


WebElement inputField;
// read the string
FileInputStream inputStream = new FileInputStream("./file.txt");
String text = IOUtils.toString(inputStream);
// insert the string via the JavascriptExecutor
((JavascriptExecutor) webDriver).executeScript("arguments[0].value = arguments[1];", inputField, text);


Add a Comment

Your email address will not be published. Required fields are marked *