0

I was trying to do write a test a case for a website that takes in the Java code , compiles it and runs it. However, while writing the test case , the text-area that takes input did not have all the characters. I saw it was missing '(' and '!'. I tried using the uni-code characters as well as ascii characters.

Here is code snippet:

   textarea.sendKeys("public class HelloWorld{public static String greet");
            textarea.sendKeys("\u0028"+"\u0029");
            textarea.sendKeys("{ return "+"\"hello world"+"\u0021"+"\";}}"); 

Output: "}}public class HelloWorld{public static String greet){return "hello world

I also believe '!' is making cursor to move to starting position.

1
  • Please refer answer and let us know if any query.
    – Sagar007
    Commented Mar 22, 2016 at 8:20

2 Answers 2

1

You have to require basic understanding of Java. Java have some inbuilt functionality for converting this type of character.

Solution :

import import org.apache.commons.lang3.StringEscapeUtils;

textarea.sendKeys("public class HelloWorld{public static String greet");
textarea.sendKeys(StringEscapeUtils.unescapeJava("\\u0028") + StringEscapeUtils.unescapeJava("\\u0029"));
textarea.sendKeys("{ return " + "\"hello world" + StringEscapeUtils.unescapeJava("\\u0021") + "\";}}"); 

Explanation :

Here sendKeys is taking only string or character sequence arguments. So as per java definitions we have to pass value like that. StringEscapeUtils.unescapeJava("\\u0021") convert value into java string. Please refer this question for more information. Further Information

Please let me know If you have found any problems.

0

It might be worthwile to look at the source code for the Java KEYS class.

The characters you mentioned aren't readily available there, but you could implement any other character in a similar fashion.

An example of how to use the Keys class is found in this StackOverflow thread. Of course, you can concatenate the Keys enum with string literals, as in your example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.