Forum Discussion

DCat1223's avatar
DCat1223
Frequent Contributor
5 years ago

Save first few digits of a string as a variable

Good morning.  I have a Keyword test, using VBScript.  As part of this test, I have to have the script enter the base pay for a worker being hired.  The UI actually lists a Pay Range in a field that is valid for the position.  The Range displays as "12.00 - 15.00 /Hour" for example.  I can use the string as my variable by pulling the contentText of the object and setting that to my variable to save for use later, but that pulls the whole string.  What I really want is just the "12.00" portion of the string, and use that as my variable to be entered as the new hire's hourly wage in a step in my test.  Ideally take the text that displays to the left of the "-" character.

 

My screenshot includes what I am currently using to get the whole string.  I'm guessing I need to use the Code Expereeion mode of the Set Variable Value, but have no idea how to code this.  Again, this is a VBScript Keyword Test.  

 

Any help would be very much appreciated.  

  

  • Well, first of all, your "Mode" should be Onscreen Object Property, not object, since contentText is a property

    Secondly, this is why you should learn some scripting coding because this is alot easier to write a simple script procedure to do the parsing.

     

    What I see is that you need to get the first part of the string up to the first "space" character because I'm assuming that the base pay won't always be 5 characters long... might be longer, might be shorter... so you need your code to grab a variable length.

     

    Here's how I'd write it in JAvaScript code.  You SHOULD be able to adapt this to VBScript as I'm using TestComplete objects/methods rather than native code.  

     

    function getBasePay(basePayString) {
        aqString.ListSeparator = " ";
        result = aqString.GetListItem(basePayString, 0);
    aqString.ListSeparator = "|"; }

     

    If you want to write it as a single line expression, to use in a keyword test, that can be done, too, but it's a bit more convoluted.

     

    aqString.SubString(<your property>, 0, aqString.Find(<your property>, " ") + 1)
  • DCat1223's avatar
    DCat1223
    5 years ago

    Thank you so much tristaanogre  and hkim5 .  Between the two of you I was able to write a basic script that worked!  tristaanogre , you are correct, I do need to learn to write script.  I have known this for a long time.  Just need to do it.  Thank you both again.  

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Well, first of all, your "Mode" should be Onscreen Object Property, not object, since contentText is a property

    Secondly, this is why you should learn some scripting coding because this is alot easier to write a simple script procedure to do the parsing.

     

    What I see is that you need to get the first part of the string up to the first "space" character because I'm assuming that the base pay won't always be 5 characters long... might be longer, might be shorter... so you need your code to grab a variable length.

     

    Here's how I'd write it in JAvaScript code.  You SHOULD be able to adapt this to VBScript as I'm using TestComplete objects/methods rather than native code.  

     

    function getBasePay(basePayString) {
        aqString.ListSeparator = " ";
        result = aqString.GetListItem(basePayString, 0);
    aqString.ListSeparator = "|"; }

     

    If you want to write it as a single line expression, to use in a keyword test, that can be done, too, but it's a bit more convoluted.

     

    aqString.SubString(<your property>, 0, aqString.Find(<your property>, " ") + 1)