Hi everyone!!! I need to insert text into a field, the good people on this forum (thank you @Russ_Thomas) advised me a js solution something like:
String js = ('document.querySelector(".cke_wysiwyg_frame").contentWindow.document.querySelector("body").innerText = '' + Letter + '';')
In this case, “Letter” is a global variable. And everything seems to work, but except for letters that are separated by lines. This is the case for signatures, something like:
Warmest wishes, Katerina
When a letter is inserted from a variable with a single solid line, it works. If not, I get the following - SyntaxError: “” string literal contains an unescaped line break.
I have read on other forums that I need to insert \n in the text, but I doubt that this solution will work and even if it does, it doesn’t work for me because I have a lot of latters in Data Files and I have to separate them all with \n.
Can you tell me if there is some other way to get a string-separated letter out of a variable? It works fine with WebUI Set Text, but no way wants to with WebUI.executeJavaScript
These DEBUG message are tracing the escape function declared in the Test Case script. Once finished debugging, these messages are just useless. In order to get rid of the DEBUG messages, you should move the escape() function into a Groovy class under the Keywords folder. Katalon Studio will not emit any DEBUG messages for the classes under the Keywords.
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.
YES! Thank you The solution turned out to be quite simple.
It was enough from this one:
String js = ('document.querySelector(".cke_wysiwyg_frame").contentWindow.document.querySelector("body").innerText = "' + Letter + '";')
Make this one, replacing (") with (`):
String js = ('document.querySelector(".cke_wysiwyg_frame").contentWindow.document.querySelector("body").innerText = ’ + Letter + ';')
But in general, you’re right, a js solution isn’t really the best use case for this value. Because it is, after all, plain text for the user. That’s just the first thing I’ve come across. But there might be other things I don’t know yet, and I might get SyntaxError again. I’ll take your other hints as time permits.
There’s also some issue here that when I run the test case, everything works perfectly. But the next time it happens, I get this error again. I open the test case as a script and I see this line there:
I switched the TestCase editor from Script mode to Manual mode, change the Test Case a bit in Manual mode, save it, changed the editor from Manual mode to Script mode. Then I saw the line was changed by the editor automatically to:
I may have this because I have manual mode open by default when I open the test case tab. Ok, I’ll have a look in the settings, I saw somewhere that it can be fixed so that the script opens first.
2023-09-04 22:21:21.419 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2023-09-04 22:21:21.423 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/TC3
2023-09-04 22:21:22.204 DEBUG testcase.TC3 - 1: Letter = "Hello
world"
2023-09-04 22:21:22.206 DEBUG testcase.TC3 - 2: js = "document.querySelector(".cke_wysiwyg_frame").contentWindow.document.querySelector("body").innerText = `" + Letter + "`;"
2023-09-04 22:21:22.219 DEBUG testcase.TC3 - 3: comment(js)
2023-09-04 22:21:22.415 INFO c.k.k.c.keyword.builtin.CommentKeyword - document.querySelector(".cke_wysiwyg_frame").contentWindow.document.querySelector("body").innerText = `Hello
world`;
2023-09-04 22:21:22.459 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/TC3
Wow! The code worked fine. The code looks ugly but is valid. So I realized that I should not have written “the editor will break the code”. The code was prettified by Katalon Studio.
Sometimes it occurs because of the line breaks. So if you simply remove the line breaks in the string error can be corrected . you can use, $string = str_replace(array $string); The string literal does not end with 2048 cupcakes quote marks. This is easy to correct by closing the string literal with the needed quote mark . The string literal extends beyond a line. Long string literals can be broken into multiple literals and concatenated with a plus sign
No. The line break in this case is significant for @prosmartfony. He needs to retain the line break. Removing the line break is not an option for him. Why? — Have a look at the original post.