Home > Back-end >  Unable to send instance variable value to a sendkeys() method
Unable to send instance variable value to a sendkeys() method

Time:09-17

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;
    @Keyword
    def getFirstRecord(TestObject listData)
    {
        List<WebElement> firstRecord = WebUiCommonHelper.findWebElements(listData, 20);
        data = firstRecord.get(0).getText();
    }

    @Keyword
    def setSearchData(TestObject obj)
    {
        WebElement txtSearchBox = WebUiCommonHelper.findWebElement(obj, 20);
        txtSearchBox.sendKeys(data);
    }

CodePudding user response:

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.

  • Related