Service Virtualization

Expand all | Collapse all

error in Beanshell Scripting in devtest

Ulrich_Vogt

Ulrich_VogtNov 14, 2017 09:21 AM

prat130193

prat130193Nov 14, 2017 09:55 AM

prat130193

prat130193Nov 14, 2017 10:29 AM

  • 1.  error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 08:20 AM

    I am getting error in below script

     

    Script is to get response body in one String and storing it as property.

     

    Script: 

    import com.itko.lisa.vse.stateful.model.response;
    String b=LASTRESPONSE.getBodyText();
    testExec.setStateValue("new",b);

     

    and below is the error

    ============================================================================
    | Error in Script
    ============================================================================
    | Step: Execute script (JSR-223)
    ----------------------------------------------------------------------------
    | Message: bsh.EvalError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' : Typed variable declaration : Error in method invocation: Method getBodyText() not found in class'com.itko.lisa.gui.XMLResultsCache' : at Line: 2 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' : LASTRESPONSE .getBodyText ( )
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' at line number 2
    ----------------------------------------------------------------------------
    | Trapped Exception: bsh.EvalError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' : Typed variable declaration : Error in method invocation: Method getBodyText() not found in class'com.itko.lisa.gui.XMLResultsCache' : at Line: 2 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' : LASTRESPONSE .getBodyText ( )
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' at line number 2
    | Trapped Message: javax.script.ScriptException: bsh.EvalError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' : Typed variable declaration : Error in method invocation: Method getBodyText() not found in class'com.itko.lisa.gui.XMLResultsCache' : at Line: 2 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' : LASTRESPONSE .getBodyText ( )
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' at line number 2
    ----------------------------------------------------------------------------
    STACK TRACE
    javax.script.ScriptException: bsh.EvalError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' : Typed variable declaration : Error in method invocation: Method getBodyText() not found in class'com.itko.lisa.gui.XMLResultsCache' : at Line: 2 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' : LASTRESPONSE .getBodyText ( )
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=LASTRESPONSE.getBod . . . '' at line number 2
    at bsh.BshScriptEngine.evalSource(BshScriptEngine.java:102)
    at bsh.BshScriptEngine.eval(BshScriptEngine.java:61)
    at javax.script.AbstractScriptEngine.eval(Unknown Source)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:669)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:426)
    at com.itko.lisa.test.UserScriptNode._execute(UserScriptNode.java:202)
    at com.itko.lisa.test.UserScriptNode.execute(UserScriptNode.java:172)
    at com.itko.lisa.test.TestNode.executeNode(TestNode.java:981)
    at com.itko.lisa.test.TestCase.execute(TestCase.java:1280)
    at com.itko.lisa.test.TestCase.execute(TestCase.java:1195)
    at com.itko.lisa.test.TestCase.executeNextNode(TestCase.java:1180)
    at com.itko.lisa.editor.WalkThruPanel.prepAndExecNode(WalkThruPanel.java:1048)
    at com.itko.lisa.editor.WalkThruPanel.access$900(WalkThruPanel.java:68)
    at com.itko.lisa.editor.WalkThruPanel$10.doCallback(WalkThruPanel.java:961)
    at com.itko.util.swing.panels.ProcessingDialog$2.run(ProcessingDialog.java:194)
    at java.lang.Thread.run(Unknown Source)
    ============================================================================

     

     

     

    getting error in getbodytext menthod and i checked its present in lisa-virtualize.jar in class com.itko.lisa.vse.stateful.model.Response.

     

    can somebody please let me know whats the issue?



  • 2.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 09:21 AM

    Which DevTest version are you using?



  • 3.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 09:55 AM

    Devtest 8.4



  • 4.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 09:27 AM

    Try using the following in place of LASTRESPONSE:  

    String b = lisa_vse_response.get(0).getBodyText();

    lisa_vse_response is a list of TransientResponse objects set by the VSI so you need to get the first (or 2nd or 3rd, etc.) response from the list, then ask the response to get the body. Alternatively, you might try LASTRESPONSE.get(0).getBodyText(), but this idiom might be a little more confusing. This script logic needs to occur after the response has been selected. Doing it prior will not work.



  • 5.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 09:54 AM

    Hi,

    i did the changes suggested by you. getting below error

     

     

    ============================================================================
    | Error in Script
    ============================================================================
    | Step: Execute script (JSR-223)
    ----------------------------------------------------------------------------
    | Message: bsh.EvalError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' : Typed variable declaration : Attempt to resolve method: get() on undefined variable or class name: lisa_vse_response : at Line: 2 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' : lisa_vse_response .get ( 0 )
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' at line number 2
    ----------------------------------------------------------------------------
    | Trapped Exception: bsh.EvalError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' : Typed variable declaration : Attempt to resolve method: get() on undefined variable or class name: lisa_vse_response : at Line: 2 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' : lisa_vse_response .get ( 0 )
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' at line number 2
    | Trapped Message: javax.script.ScriptException: bsh.EvalError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' : Typed variable declaration : Attempt to resolve method: get() on undefined variable or class name: lisa_vse_response : at Line: 2 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' : lisa_vse_response .get ( 0 )
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' at line number 2
    ----------------------------------------------------------------------------
    STACK TRACE
    javax.script.ScriptException: bsh.EvalError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' : Typed variable declaration : Attempt to resolve method: get() on undefined variable or class name: lisa_vse_response : at Line: 2 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' : lisa_vse_response .get ( 0 )
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=lisa_vse_response.g . . . '' at line number 2
    at bsh.BshScriptEngine.evalSource(BshScriptEngine.java:102)
    at bsh.BshScriptEngine.eval(BshScriptEngine.java:61)
    at javax.script.AbstractScriptEngine.eval(Unknown Source)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:669)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:426)
    at com.itko.lisa.test.UserScriptNode._execute(UserScriptNode.java:202)
    at com.itko.lisa.test.UserScriptNode.execute(UserScriptNode.java:172)
    at com.itko.lisa.test.TestNode.executeNode(TestNode.java:981)
    at com.itko.lisa.test.TestCase.execute(TestCase.java:1280)
    at com.itko.lisa.test.TestCase.execute(TestCase.java:1195)
    at com.itko.lisa.test.TestCase.executeNextNode(TestCase.java:1180)
    at com.itko.lisa.editor.WalkThruPanel.prepAndExecNode(WalkThruPanel.java:1048)
    at com.itko.lisa.editor.WalkThruPanel.access$900(WalkThruPanel.java:68)
    at com.itko.lisa.editor.WalkThruPanel$10.doCallback(WalkThruPanel.java:961)
    at com.itko.util.swing.panels.ProcessingDialog$2.run(ProcessingDialog.java:194)
    at java.lang.Thread.run(Unknown Source)
    ============================================================================



  • 6.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 09:42 AM

    In addition to Joel's response: Please see migrating-the-sdk/response-updatesmigrating-the-sdk/request-updates and body-carrier-updates:

    See class BodyCarrier for details about setting and accessing the Response body. In class BodyCarrier, method getBodyText() is deprecated since 6.0.7 and should be replaced by getBodyAsString(). In DevTest 10.1 classes com.itko.lisa.vse.stateful.model.response and com.itko.lisa.vse.stateful.model.request do not have a method getBodyText() (anymore).

    We may need to update the scripting guide accordingly. 

    (Edited)



  • 7.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 10:18 AM

    "Attempt to resolve method: get() on undefined variable or class name: lisa_vse_response"...  

    Which Step in the VSM did you implement the script?  Is it possible to upload a copy or picture of the VSM and point out where the script is that is accessing the response body. 

    And, if possible, please upload the entire script if it contains more than just the 3 lines, above.



  • 8.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 10:29 AM

     



  • 9.  Re: error in Beanshell Scripting in devtest

    Broadcom Employee
    Posted Nov 14, 2017 10:38 AM

    Please refer to the following

     

    https://docops.ca.com/devtest-solutions/10-1/en/using/using-ca-application-test/using-devtest-workstation-with-ca-application-test/building-test-cases/properties/common-properties-and-environment-variables

     

    LASTRESPONSE has the value returned by your Test step. 

     

    What are you trying to do with this value?

     

     



  • 10.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 10:41 AM

    i am trying to get response body of my last step in a property then i will try to find some text in that. but i am getting error in getbodytext() method.



  • 11.  Re: error in Beanshell Scripting in devtest

    Broadcom Employee
    Posted Nov 14, 2017 10:50 AM

    You cannot call getbodytext() on LASTRESPONSE here. 

     

    Each step in a test case has a response that is associated with it. When that step runs, the response is automatically saved in two properties: LASTRESPONSE and lisa.<StepName>.rsp

     

    If you only need to save the result to another property you can use a filter as documented below

     

    https://docops.ca.com/devtest-solutions/10-1/en/reference/filter-descriptions/utility-filters/save-property-from-one-key-to-another



  • 12.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 11:11 AM

    but if i want only body of response how can i do that. i was new to scripting so just wanted to try it. actually my problem statement is i want to check if particular tag is present in body or not if its not present then flow 1 should be executed if its present then flow 2 should be executed. i am i got stuck in getting response only once response is there in one property then i can try any searchstring function from already present lisa classes. and i want to acheive this through scripting.



  • 13.  Re: error in Beanshell Scripting in devtest

    Broadcom Employee
    Posted Nov 14, 2017 11:18 AM

    Please refer to the following

     

    https://docops.ca.com/devtest-solutions/8-4/en/using/using-ca-application-test/using-the-workstation-and-console-with-ca-application-test/building-test-cases/assertions

     

    An assertion is a DevTest code element that runs after a step and all its filters have run. An assertion verifies that the results from the step match expectations.

    The result of an assertion is a Boolean value (true or false).

    The outcome can determine whether the test step passes or fails, and also determines the next step to run in the test case. An assertion is used to change the test case workflow dynamically by introducing conditional logic (branching) into the workflow. An assertion operates very much like an "if" conditional block in programming.

    Following contains XML specific assertions

    https://docops.ca.com/devtest-solutions/8-4/en/reference/assertion-descriptions/xml-assertions/



  • 14.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 11:21 AM

    cant above be achieved through scripting?



  • 15.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 11:22 AM

    I believe you are bit confused with regard to usage.  lisa_vse_request and lisa_vse_response objects are created inside a Virtual Service having a Listen -> VSI Selection -> Responder step flow.  Your example is attempting to implement a Test Case flow as though it were a virtual service.

    As Prem_Bairoliya pointed out, review Test Case creation and the use of properties.

    Your LASTRESPONSE from the execution of the REST call contains an XML document:

    Add a Filter and XPath the value from within the XML Document you are looking to extract. In this example, there is no such lisa_vse_response object available for you to use.



  • 16.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 11:39 AM

    is there any other way i can achieve it through scripting.i would like to take up and solve this usng scripting as i am new to devtest scripting. is there any way i can save response of previous step in some property and then do a seach operation on it?



  • 17.  Re: error in Beanshell Scripting in devtest
    Best Answer

    Posted Nov 14, 2017 11:52 AM

    You can add a Scripted Assertion that accesses LASTRESPONSE or use a JSR-223 step.  

    In your example, LASTRESPONSE is a reference to the response that has been stored as a result of the REST step execution; therefore, String b = testExec.getStateValue("LASTRESPONSE"); returns a String representation of the XML document.

     

    If the script needs to implement its own XPath logic, you need to add behavior (and import the necessary classes) to convert the string into a DOM object. You also have the standard string manipulation functions (b.indexOf(), b.substring(), etc.) available.

     

    As mentioned, Filters provide an OOTB method for accessing the DOM objects. Similarly, once data is 'filtered', it can be accessed in a script by calling testExec.getStateValue( "filterName" ). where 'filterName' is the name of the filter that was created.



  • 18.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 12:57 PM

    when i did below changes 

     

    String  b=testExec.getStateValue("LASTRESPONSE");

    getting error annot cast com.itko.lisa.gui.XMLResultsCache to java.lang.String

     

    Target exception: java.lang.ClassCastException: Cannot cast com.itko.lisa.gui.XMLResultsCache to java.lang.String
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.response; String b=testExec.getStateVa . . . '' at line number 2
    at bsh.BshScriptEngine.evalSource(BshScriptEngine.java:97)
    at bsh.BshScriptEngine.eval(BshScriptEngine.java:61)
    at javax.script.AbstractScriptEngine.eval(Unknown Source)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:669)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:426)
    at com.itko.lisa.test.UserScriptNode._execute(UserScriptNode.java:202)
    at com.itko.lisa.test.UserScriptNode.execute(UserScriptNode.java:172)
    at com.itko.lisa.test.TestNode.executeNode(TestNode.java:981)
    at com.itko.lisa.test.TestCase.execute(TestCase.java:1280)
    at com.itko.lisa.test.TestCase.execute(TestCase.java:1195)
    at com.itko.lisa.test.TestCase.executeNextNode(TestCase.java:1180)
    at com.itko.lisa.editor.WalkThruPanel.prepAndExecNode(WalkThruPanel.java:1048)
    at com.itko.lisa.editor.WalkThruPanel.access$900(WalkThruPanel.java:68)
    at com.itko.lisa.editor.WalkThruPanel$10.doCallback(WalkThruPanel.java:961)
    at com.itko.util.swing.panels.ProcessingDialog$2.run(ProcessingDialog.java:194)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassCastException: Cannot cast com.itko.lisa.gui.XMLResultsCache to java.lang.String
    at bsh.Types.castError(Types.java:524)
    at bsh.Types.castError(Types.java:512)
    at bsh.Types.castObject(Types.java:502)
    at bsh.Types.castObject(Types.java:310)
    at bsh.Variable.setValue(Variable.java:83)
    at bsh.Variable.<init>(Variable.java:49)
    at bsh.NameSpace.setTypedVariable(NameSpace.java:703)
    at bsh.ExternalNameSpace.setTypedVariable(ExternalNameSpace.java:174)
    at bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:87)
    at bsh.Interpreter.eval(Interpreter.java:664)
    at bsh.Interpreter.eval(Interpreter.java:758)
    at bsh.Interpreter.eval(Interpreter.java:747)
    at bsh.BshScriptEngine.evalSource(BshScriptEngine.java:89)
    ... 15 more
    ============================================================================



  • 19.  Re: error in Beanshell Scripting in devtest

    Broadcom Employee
    Posted Nov 14, 2017 01:02 PM

    Try

     

    String  b=testExec.getStateValue("LASTRESPONSE").toString();



  • 20.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 01:06 PM

    Thanks it worked.



  • 21.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 01:15 PM

    can you please help me with class name in which i can get string functions.



  • 22.  Re: error in Beanshell Scripting in devtest

    Posted Nov 14, 2017 01:52 PM

    String referenced above is java.lang.String not a proprietary DevTest class.

    String (Java Platform SE 8 )  <- if running a Java 8 version of DevTest

    String (Java Platform SE 7 )  <- if running a Java 7 version of DevTest