Service Virtualization

 View Only
  • 1.  Substring script in beanshell

    Posted Mar 09, 2017 10:58 AM

    If a DevTest property is more than 20 in length, I want to keep only 20 chars or else as it is. How do I handle this in Beanshell script step?



  • 2.  Re: Substring script in beanshell
    Best Answer

    Broadcom Employee
    Posted Mar 09, 2017 11:05 AM

    You mean something like

    variableName = testExec.getStateString("propertyName", "");

    if(variableName.length() > 20) variableName = variableName.substring(0,19);

    testExec.setStateValue("propertyName", variableName);



  • 3.  Re: Substring script in beanshell

    Posted Mar 09, 2017 12:00 PM

    Correct. I was wondering what classes need to be called, etc.

    I am already capturing the value through Filters in DevTest 9.5.1. I need use the above logic, trim it & set to a different property.



  • 4.  Re: Substring script in beanshell

    Posted Mar 09, 2017 12:35 PM

    I believe Rick.Brown answered your question.  And, his code snippet is accurate As-Is.  

    Properties and values are stored as key/value pairs.  The "testExec" object (that keeps track of these properties) is exposed to your beanshell logic by DevTest so no import is required.

     

    What classes were you thinking that you need?  

     

    Replace Rick's use of "propertyName" in the getStateString method with the property name of your existing Filter.

    In the setStateString method replace "propertyName" with the name you want assigned to your new property and ensure variableName holds the value you want assigned.  This action will create a new key/value pair.

     

    Once assigned, you can access your new property using {{ }} notation or via testExec.getStateValue("yourNewProp");

    You will also see your new property in the Properties tab within ITR mode when you execute your test. 



  • 5.  Re: Substring script in beanshell

    Posted Mar 09, 2017 12:56 PM

    Thanks so much.