Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How to Transform Expected Value to Regular Expression

LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited December 2016 in SOAtest
Hello!

I'm using a test that after execution, provides an element with the following value (used for a posterior test assertion): "VALUE1 VALUE2 VALUE3"

Then, I'm asserting a set of fields that contains: "VALUE1", "VALUE2", "VALUE3" , respectively.

I would like to transform the first value (stored as a DataTable Parameter) to a regular expression string, like "VALUE1|VALUE2|VALUE3".
Is there a way to perform this with SOATEST?


Thanks, and if you need more clarification please let me know.
Regards,

rodrigo.
Tagged:

Comments

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited December 2016
    Hi Rodrigo,

    I'm afraid I don't clearly understand what you are trying to do. Perhaps attaching your tst file would be more helpful for me to understand your setup. I also have some questions to try and clarify my understanding. When you say the value is stored as a "DataTable Parameter", do you mean it is inside of a Table Datasource? Also, how are you doing the assertions for the element? Do you somehow extract the three values individually in order to do your assertions of the individual values? How did you setup the assertion values ("VALUE1", "VALUE2", "VALUE3")?

    Again, perhaps attaching your tst file along with answers to these questions will give me a better idea on how to approach your testing needs.

    Thanks,
    Denard

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited December 2016
    Hello Denard, thanks for your quick reply!

    I'll try to clarify:
    I'm obtaining the first value from an xml response. Specifically, I'm retrieving the data from one element, which data is exactly: "VALUE1 VALUE2 VALUE3"
    It means that this is a white space separated set of three values.

    Then, from another call, I'm trying to assert that a certain element being returned, is either "VALUE1" or "VALUE2" or "VALUE3".
    In order to do this, I was thinking in use the "Regular Expression" Assertion.

    So my main question is how to transform the first obtained value -->"VALUE1 VALUE2 VALUE3" into something like "VALUE1|VALUE2|VALUE3", which is the format of regular expressions assertion to indicate that any of those values is expected.


    Hope this clarify a little,
    Thanks again!

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited December 2016
    Hi Rodrigo,

    Thanks for the detailed description. I believe I understand what you are trying to do now.

    So for instance you first receive a response that looks like this: (with other elements also in the response)

    <xml>
    ...
    <values>test tester testers</values>
    ...
    </xml>

    Then you make a new query wherein a separate response looks like this:

    <xml>
    ...
    <singlevalue>testers</singlevalue>
    ...
    </xml>


    And now what you want to do is check to make sure that the "<singlevalue>" element contains one of the strings from the "<values>" element.

    You have the right idea in using a regular expression assertion however I think there is something a little easier that you can do to create the regular expression rather than just transforming the values of the response.

    What I recommend is that you store the regular expression inside of a Test Suite Variable and then parameterize the regular expression against this test suite variable. I have attached a very simple tst file that shows you can achieve this. (i created the file in SOAtest 6.2 service pack 5 so you may need to update to that service pack in order to open the tst file: SOAtest > Check for Updates (assuming you already have SOAtest 6.2)).

    Here's a description of what i did in the tst file and you can easily move this over to your own implementation:

    1) I attached a Response SOAP Envelope --> XML Data Bank as an output to the test that receives the response containing the element with three values.
    -- Inside the Data Bank, I extract the value for the element and give it a data source column name (i.e. values).

    2) Next I added a Test Suite Variable to the root test suite folder that containes the tests I'm running.
    -- I double click the scenario node > Select the "Test Variables" tab > Click "Add" > Give the variable a name (ie "ValueRegExp") > Type: String > Select the "use local value" radio button > clear the value field so that it's empty. > Click OK.
    -- Save the changes.

    3) Next I added an Extension Tool Test, right after the Test that receives the response with three values in the element (it doesnt have to be immediately after, just sometime after this test).
    -- In the extension tool I wrote the following code:
    CODE
    from com.parasoft.api import *


    def createRegExp(input, context):
        
        values = context.getValue("Generated Data Source", "values")
        value = values.split(" ")
        return context.setValue("ValueRegExp", value[0] + "|" + value[1] + "|" + value[2])

    Note that I used the "Python" language and that I checked the box indicating that the "Return Value indicates success".

    What this code does is:
    a) it grabs the value you extracted in the XML Data Bank (values = context.getValue("Generated Data Source", "values"))
    b ) stores them in a "value" array by the java function call ( value = values.split(" ") )
    c) sets the value for the test suite variable with the value "value1|value2|value3" (return context.setValue("ValueRegExp", value[0] + "|" + value[1] + "|" + value[2]))

    4) Then in the test wherein I receive a response that contains a single value, I add an XML Assertor as ouput. In the configuration for a regular expression, I changed it to "parameterized" and selected the name of the test suite variable I selected.

    After saving all changes and running the test suite, you can see that the test passes since I receive a value that is one of the three values. You can change the value in the test to be anything else so you can see it fail as well.

    If you have any questions, please feel free to let me know. Otherwise, adapting these steps to your own test should allow you to do regular expression assertions with those three values.

    Thanks,
    Denard

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited December 2016
    Hello Denard, thanks a lot for your very detailed response!

    There's a little problem, my company is still using Soatest 5.5.3 as an standard, so I think that's why this is not working... Let me explain you a little more:


    I was able to reproduce almoust all steps you mentioned, except for this one:

    2) Next I added a Test Suite Variable to the root test suite folder that containes the tests I'm running.
    -- I double click the scenario node > Select the "Test Variables" tab > Click "Add" > Give the variable a name (ie "ValueRegExp") > Type: String > Select the "use local value" radio button > clear the value field so that it's empty. > Click OK.
    -- Save the changes.

    The "use local value" radio button is not present in Soatest 5.5.3. I've ignored this, and I'm receiving the following error message:

    Traceback (innermost last): File "<string>", line 8, in createRefExp AttributeError: setValue.



    Is there a way to achieve the same steps using SoaTest 5.5.3?
    Thanks in advance and Regards,

    Rodrigo.

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited December 2016
    Hi Rodrigo,

    Thanks for the information. Sorry for assuming you had the latest SOAtest. As for that error message, I believe it is because that function isn't defined for 5.5.3 (we've made a lot of changes to the method tool (i.e. we now call it the extension tool fyi )).

    Therefore, I've looked through the api for 5.5.3 (which I believe you can access through the help menu in SOAtest for more details) and I see some differences in functions available for use.

    Here's my suggestion to try out:

    First, we're going to have to replace my original line of code:

    CODE
    return context.setValue("ValueRegExp", value[0] + "|" + value[1] + "|" + value[2])

    with the following instead:

    CODEtsvariable = context.getValue("ValueRegExp")
    tsvariable.setValue(value[0] + "|" + value[1] + "|" + value[2])
    return True

    I always return True because the setValue function doesn't return something, so you may want to keep in mind that although the extension tool returns true, it doesn't necessarily means it successfully set the value. You'll have to check that yourself.

    I believe that should be all you have to change.

    Please let me know how this goes.


    -Denard

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited December 2016
    Dear Denard,
    After trying with no success to reproduce this in 5.5.3, I have some good news: Just installed Soatest 6.2 and will try to push forward to use this latest version as standard in the company, as it seems pretty stable and has a lot of very good features incorporated.

    So the first solution you proposed worked perfectly using SoaTest 6.2.

    Thanks a Lot,
    Rodrigo.

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Rodrigo,

    I'm glad it helped!


    Regards,
    Denard
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    If you want to create "VALUE1|VALUE2|VALUE3" from a text node with the content "VALUE1 VALUE2 VALUE3", you don't need to use scripting. Within the XML Data Bank, you can use an XPath that replaces each space with a "|". Create an extraction with this XPath:

    replace(/path/to/values/text(), " ", "|")
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Documentation on the "replace" function can be found here (and the page contains documentation on many other XPath functions):

    http://www.w3.org/TR/xquery-operators/#func-replace