Forum Discussion

Gene's avatar
Gene
New Contributor
9 years ago

I wan't to use SoupUI (Free Version) with JavaScript assertion.

Hello,

I wan't to use SoupUI (Free Version) with JavaScript assertion. 

And I found how to select this language as project language.

But I can't find information about how to us JavaScript in SoupUI project.

How to use Libraries?

How to convert response to string?

How to use test case name as property? 

Where I can find these kind of information?

7 Replies

  • Gurbakhsish's avatar
    Gurbakhsish
    Occasional Contributor

    After investing few hours on this topic, I am able to run few scripts in javascript, they are too basic scripts, but can be used to perform subtle actions.

     

    1. Script to run a test step at testcase tear down - 
     
    Prerequisites - have  a test step inside a testcase and disable it, following testcase teardown script will execute the test step 
     
    For me, Add a priority is a test step in the test case, which should return anything positive, and Delete Priority test step is a test step that will be executed at teat down.
     
    if( context.expand( "${Add a priority#Response#$.status}" ) > 400 ) {
        var s = testRunner.getTestCase().getTestStepByName("Delete Priority").run(testRunner, context).status;   
        if (s == "OK"){
            log.info("Deleted test data - Priority id - "+ context.expand( "${Add a priority#Response#$.data.id}" ));
        }
    }
     
    Here, the variable s will have the status of the run, and if it is not OK, you may like to add a throw statement, which will break the execution.
     
    2. Setup script for a testsuite
     
    To run a testcase at very beginning of a testsuite, which will create some test data. Add that test case to the test suite and disable it.
     
    runner.getTestSuite().getTestCaseByName("Setup - Add an article").run(context.properties, false);
     
    3. Assert a value from the response
     
    var total_count = context.expand( "${Retrieve cases#Response#$.total_count}" );
    var result = true;
    var entry_id,case_id;
    for (i = 0;i<total_count;i++){
    entry_id = context.expand( "${Retrieve cases#Response#$.data["+i+"].id}" );
    case_id = context.expand( "${Add a case#Response#$.data.id}" );
    if ( entry_id == case_id){
    result = false;break;
    }
    }
    if(result){throw "Assertion Failed"};
  • nmrao's avatar
    nmrao
    Champion Level 3
    Interesting. Haven't used or seen any questions around it in the forum. Is there any specific driver for you to opt javascript over groovy?
    • Gene's avatar
      Gene
      New Contributor

      I found few questions about it in the forum. But the questions remained without answers.
      I can't work with groovy because of my company's policy.
      JavaScript support is the decicive condition to choose the tool for automated tests...

      • nmrao's avatar
        nmrao
        Champion Level 3
        Can you pass the links that you found for the same?
  • mosak's avatar
    mosak
    Occasional Visitor

    Hi,

    did You find out how to add Javascript library to use it in Assertions?