In the above sample code, we are transferring a value of Java/Groovy variable of java.lang.String
into a String literal in JavaScript. You need to be careful for translating special characters such as NEWLINE, quotes, etc. There are a lot of more characters you need to look after. For example, the sample code can not deal with a backslash character \
appropriately. If the input text contains one or more \
, the code will break. You have to modify the code to escape more special characters. This problem sucks.
An alternative is available. As I mentioned in the post
You would be able to edit the content text of the <body contenteditable="true">
element in your target web page using WebUI.click()
, WebUI.clear()
and WebUI.sendKeys()
. This way you do not have to look after escaping special characters. The WebDriver protocol implentation (e.g. ChromeDriver, FirefoxDriver) will look after it for you.