Ask a Question

Save first few digits of a string as a variable

SOLVED
DCat1223
Frequent Contributor

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.  

  

3 REPLIES 3
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)

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

adding on to what @tristaanogre mentioned,

he is using TestComplete's built in aqString function with its methods, which you can browse here:

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqstring/methods.html

For example, you can see the GetListItem method, and if you scroll down you can see an example code snippet in various languages, including VBscript.

Best,
Justin Kim
DCat1223
Frequent Contributor

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.  

cancel
Showing results for 
Search instead for 
Did you mean: