Forum Discussion

JBSoccerbrit's avatar
JBSoccerbrit
Contributor
9 years ago
Solved

Parsing a string and counting characters.

String Example: type1=12345678901234567890111&type2=3.0.00&type3=1234567811

From reading the user guide it looks like I can use the aqString.ListSeparator to define the & as use the delimiter and then I could the aqString.GetLength to validate the length of the string.

I'd like to do this using a keyword test if possible also.

However what I would like to do is something like the following, 

Validate type1 is 23 characters long after the equals symbol 
Validate type2 is 6 characters long after the equals symbol
Validate type3 is 10 characters long after the equal symbol

My issue here is that I don't want to count the characters that make up the title, such as type3= would add 6 addational characters to my validation. Is there a way for me to work around this?

Thanks,

  • Helen's reply got me thinking... Could we just use the powers of JavaScript directly?

     

    function test()
    {
      var str = "type1=12345678901234567890111&type2=3.0.00&type3=1234567811";
      var strarr = str.split('&');
      for (var i = 0; i < strarr.length; i++) {
        var typearr = strarr[i].split('=');
        Log.Message(strarr[i]);
        switch (typearr[0]) {
        case 'type1':
          Log.Message("TYPE1: length = " + typearr[1].length);
          break;
        case 'type2':
          Log.Message("TYPE2: length = " + typearr[1].length);
          break;
        case 'type3':
          Log.Message("TYPE3: length = " + typearr[1].length);
          break;
        default:
          Log.Message("unrecognized type");
          break;
        }
      }
    }

    produces

     

    testresult.PNG

19 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    If type1 is supposed to be 23 chars, then char 31 had better be "t"

    in a similar fashion

    char 44 should also be "t"

     

    That would be the simplest way to check.  If you want the test to be more flexible, then you can parse the string and look for "type1" and then count some number of chars after that, then repeat that for each substring.

     

    You can do any of the aqString functions in a keyword test using Run Code Snippet.

     

    If you need help with either of these approaches, just ask!

     

    • JBSoccerbrit's avatar
      JBSoccerbrit
      Contributor

      Hi Marsha_R I'm note sure I under the 

      "If type1 is supposed to be 23 chars, then char 31 had better be "t"

      in a similar fashion

      char 44 should also be "t""

      Part of your post.

      type1=12345678901234567890111 - if were to count each numeric as an individual id have 23 total numerics if that makes sense. So I want to count everything after the equal for type1.

      I believe this is what I need to do  "That would be the simplest way to check.  If you want the test to be more flexible, then you can parse the string and look for "type1" and then count some number of chars after that, then repeat that for each substring."

      Thanks again, can you explain this part further please and thank you again.

      • Marsha_R's avatar
        Marsha_R
        Champion Level 3

        Will you always be counting to 23 for type 1, or do you want to make it flexible and work for any length?

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    If you have the Desktop module, you can use the .NET HttpUtility.ParseQueryString method to convert a query string into a name/value collection. Then you can get the parameter values as CollectionObj.Item(param_name). Note that to be able to use ParseQueryString, you need to add the System.Web assembly from the GAC to Tools > Current Project Properties > CLR Bridge.

     

    Assuming you're using JScript, here's a sample function that shows how to use ParseQueryString and how to check the parameter length. You can call this function from your keyword tests using the Run Script Routine operation.

     

    // JScript

    /*
    Usage example:

    var str = "type1=12345678901234567890111&type2=3.0.00&type3=1234567811";
    CheckQueryStringParameterLength(str, "type1", 23);
    */
    function CheckQueryStringParameterLength(QueryString, ParamName, ExpectedLength) { var qs = dotNET.System_Web.HttpUtility.ParseQueryString(QueryString); var strValue = qs.Item(ParamName).OleValue;
    var length = strValue.length; if (length == ExpectedLength) Log.Checkpoint( aqString.Format("Parameter \"%s\" has the correct length (%d).", ParamName, ExpectedLength), strValue ); else { Log.Error( aqString.Format("Parameter \"%s\" is %d characters long, but should be %d characters.", ParamName, length, ExpectedLength), strValue ); } }
      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        JBSoccerbrit wrote:

        Hi HKosova sorry but what does "GAC" mean? 


        GAC = Global Assembly Cache.

         

        When you add .NET DLLs to your TestComplete project (in the project properties > CLR Bridge), there're options to Browse Files (add from the disk) and Browse GAC. To use the example I posted you need to add the System.Web assembly via Browse GAC.