Service Virtualization

  • 1.  unable to compare variable values in java script step

    Posted Mar 16, 2018 12:33 AM

    Steps:
    ====
    #1. add a JavaScript object and place the following code

     

    testExec.setStateValue("ExpectedID","123");

     

    testExec.setStateValue("ExpectedESIGN","false");

     

    #2. save the below json in notepad in c drive
    {
        "eSign": false,
        "id": "123"
    }
    #3. Add Read a File(Disk,URL or Classpath) Step (continuation with #1) and browser the above file path
       - save as RESPONSEJSON

     

    #4.  Add Java Script Step and add following code:

     


    import java.io.*;
    import java.util.*;
    import java.lang.*;
    import java.lang.Integer;

     

    strActualID = testExec.getStateValue("ActualID");
    strExpectedID = testExec.getStateValue("ExpectedESIGN");

     

    strActualESIGN = testExec.getStateValue("ActualESIGN");
    strExpectedESIGN =  testExec.getStateValue("ExpectedID");
     

     

    if(!strExpectedESIGN.equals(strActualESIGN)){
         testExec.setStateValue("Result","Failed");
      }
    else{
         testExec.setStateValue("Result","Passed");
      }
      
    if(!strActualID.equals(strExpectedID)){
         testExec.setStateValue("Result1","Failed");
      }
    else{
         testExec.setStateValue("Result1","Passed");
      }

     


    #5. Add a Output log Message step: and se the following:
    ActualID = {{ActualID}}
    ActualESIGN = {{ActualESIGN}}

     

    Expected esign = {{ExpectedESIGN}}
    Expected Id = {{ExpectedID}}

     


      {{Result}}

     
      {{Result1}}

     

    #6. Run the script, see that {{Result}} & {{Result1}} displays as Failed.

     

    instead of Passed.
    Can you [please me how to solve this..

     


    the output displays as
    ActualID = 123
    ActualESIGN = false

     

     

     

     

     

    Expected esign = false
    Expected Id = 123
      Failed

     

      Failed



  • 2.  Re: unable to compare variable values in java script step

    Broadcom Employee
    Posted Mar 16, 2018 05:15 AM

    It looks like you're explicitly trying to set string values in your testExec.getStateValue statements. However, getStateValue will generate variables of the same type as the property, and your JSON is setting eSign to a boolean. Comparing the boolean false to the string "false" will always fail.

     

    What happens if you change your getStateValue to this:

     

    strExpectedID = testExec.getStateString("ExpectedESIGN", "defaultExpected");

    strActualESIGN = testExec.getStateString("ActualESIGN", "defaultActual");

     

    or what about:

     

    strExpectedID = String.valueOf(testExec.getStateValue("ActualESIGN"));



  • 3.  Re: unable to compare variable values in java script step

    Posted Mar 16, 2018 08:11 AM

    #1.  when we use getStateString - I m getting error as :

            Error in method invocation: Method getStateString(java.lang.String) not found in class com.itko.lisa.test.TestExec

    #2.  I m getting the same error as above when we use:

    String.valueOf(testExec.getStateValue("ActialESIGN")



  • 4.  Re: unable to compare variable values in java script step

    Broadcom Employee
    Posted Mar 16, 2018 08:57 AM

    #1: getStateString takes two parameters. You're only giving it one. My example included "defaultExpected" to use as a default value if the retrieved string is null.

     

    #2: I don't understand how you're getting a getStateString error when you're running a getStateValue command. Can you please check the command and the error?



  • 5.  Re: unable to compare variable values in java script step

    Posted Mar 16, 2018 10:50 AM

    +1 to Rick.Brown comments. 

    JSON having "eSign": false converts to boolean not a string having a value "false".



  • 6.  Re: unable to compare variable values in java script step

    Posted Mar 17, 2018 04:06 AM

    Thanks Joel for sharing the script. I ran the test and i m getting following error:

     

     Message:     TypeError: null has no such function "localeCompare" in <eval> at line number 20
    ----------------------------------------------------------------------------
    | Trapped Exception: TypeError: null has no such function "localeCompare" in <eval> at line number 20
    | Trapped Message:   javax.script.ScriptException: TypeError: null has no such function "localeCompare" in <eval> at line number 20..

     

    Could you please check at your end. thanks..



  • 7.  Re: unable to compare variable values in java script step

    Posted Mar 17, 2018 09:21 AM

    Yes, I noticed that I had a typo so I removed the script.  Sorry.



  • 8.  Re: unable to compare variable values in java script step

    Posted Mar 17, 2018 04:01 AM

    Thanks for the reply Rick.. I did the following changes as you recommended. Please find my observations as below:

     

    #1. changing testExec.setStateValue to testExec.setStringValue

    instead of using :

    testExec.setStateValue("ExpectedID","123");

    testExec.setStateValue("ExpectedESIGN","false");

     

    used as below:

    strExpectedID = testExec.getStateString("ExpectedID","123");
    strExpectedESIGN = testExec.getStateString("ExpectedESIGN","false1");

     

    ran the test and i see that its working fine as Expected.

     

    later, i modified the script to : strExpectedID = testExec.getStateString("ExpectedID","1234"); or

    testExec.setStateValue("ExpectedESIGN","true");

     

    saved the script and ran the test. it gave a exception at compare results step in the following code

    if(!strExpectedESIGN.equals(strActualESIGN)){
         testExec.setStateValue("Result","Failed");
      }
    else{
         testExec.setStateValue("Result","Passed");
      }

     

    | Message:     Sourced file: inline evaluation of: ``  import java.io.*;  import java.util.*;  import java.lang.*;  import java.lang. . . . '' : Undefined argument: strExpectedESIGN  : at Line: 11 : in file: inline evaluation of: ``  import java.io.*;  import java.util.*;  import java.lang.*;  import java.lang. . . . '' : ( strExpectedESIGN )

     

    suppose if just modified by removing  "!" and re-adding "!" at if(!strExpectedESIGN.equals(strActualESIGN) and save the script and run the test then it works fine.

     

    so every change we do at getStateString value, we have to modify the script in compare java step ad run the script. then only its working.