Forum Discussion

mfoster711's avatar
mfoster711
Regular Contributor
10 years ago

Random value in Combobox/Select field Keyword Test

In my keyword test, I need to select a random value in a combobox. I don't care what value, but a value must be entered to complete the form. How do you random pick a value?

6 Replies

  • flightvan's avatar
    flightvan
    New Contributor
    Hi Mark,

    in your case I would prefer to click on the first item in combobox object, like this: comboboxObj.ClickItem(0). Otherwise, instead of zero, you can use random integer value from items count range by writing code snippet in language that you selected.
  • murugans1011's avatar
    murugans1011
    Regular Contributor
    As Ivan suggested if u want to click random item. then u can get the max no.of combobox items and generate random integer to click random item something lik this



    [VBScript]



                max=combobox.wItemCount-1   'gets the total no.of items

                min=0

                Randomize

                rndNo=(Int((max-min+1)*Rnd+min))





                combobox.ClickItem(rndNo)  'Clicks the random item

             
  • mfoster711's avatar
    mfoster711
    Regular Contributor
    Ivan or Murugan, how do I do either of those from a Keyword Test?



    I don't think the data generator wizard will help. In my case, I have a web form I am trying to fill out. There are 3 fields for Year, Make and Model of a vehicle. These 3 fields are combobox fields (Selects, whatever you call them) and I simply need to pick any value available in them. 
    • boroop's avatar
      boroop
      Occasional Contributor

      Building on 

       

       

       

       

       

       

      ComboBoxSelection.png

       

      • boroop's avatar
        boroop
        Occasional Contributor

        The better way to do this (in my opinion) is to setup a function that you can call many times. I pass in a set of valid combobox selections, separated by commas, and let the function choose one of them at random and then return it to me.

         

         

        function ChooseARandomStringValue(StringValues)
        {
          // Split the passed in StringValues and put them into an array
          var IndividualStrings = StringValues.split(",");
          // Get the arrays length
          HowMany = IndividualStrings.length;
        
          // Pick a random number between 1 - <<<the number of passed in values>>>
          i = Math["round"](Math["random"]()*(HowMany-1)+1)
        
          // The random string is found in the array (zero offset) 
          WhichString = IndividualStrings[i-1];
        
          // Return the single value from the list
          return WhichString;
        }

         

         

        Call it like this: ChooseARandomStringValue("One,Two,Three,Four,Five")

         

        The ComboBox selection command then uses the "Last Operation Result" to choose the proper value (One, Two, Three, Four or Five)(which is passed back as the WhichString return value).

         

        Back in the TestComplete UI, you can drag and drop the script into your keyword test and choose your function from the popup. Set the values to be passed in and let it run.

         

        Congratulations! You just invented the wheel! Now reuse it, don't reinvent it each time. :smileywink: