Unable to send instance variable value to a sendkeys() method


Now I have a String variable declared as an instance variable, I’m defining the value to the variable inside a method, but when I try to use the value of the same variable inside another method I get this error:

Keys to send should be a not null CharSequence

Now I cannot event set the return type of first method as String because that method accepts an argument hence that is of no use.

This is my code:

    String data;
    def getFirstRecord(TestObject listData)
        List<WebElement> firstRecord = WebUiCommonHelper.findWebElements(listData, 20);
        data = firstRecord.get(0).getText();

    def setSearchData(TestObject obj)
        WebElement txtSearchBox = WebUiCommonHelper.findWebElement(obj, 20);


I dont have the reputation to comment so Answering here with possibilities.

Please check below cases for your code to see if these things resolve the issue

Case 1) As mentioned by Nandan A in the comments, check if the method 2 is getting called before method 1.
–> If this is the case then please check your configuraiton for test cases and see why this is happening.

Case 2) If the method 2 is getting called after method 1 as expected by your code.
–> Then as per your coment replied I can see in method 2 the String value is still null. And hence it is possible that this framework is creating new instance of your class for executing your @Keyword implementations everytime. You can solve this in few different ways.

  • One easiest way to try is make your String variable as static. This way, the value will remain same for all instances, as the static variables are stored on the class level instead of instance level in Java.
  • Another one can be : from first method write the value in to a properties file, and from the second method read the same properties file and the same key’s value.

Let me know if this helps you.

Answered By – Nikhil Pandit

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published