TestComplete has the ability to drive tests using a data source (Excel, CSV, datbase, etc), and there is a Data Loop option built in if you need to repeat the same steps for all the rows in a dataset.
However, there may be times when you don't need to loop through all the data in a dataset and just want to pick one at random. This is a guide showing an example using JScript. Similar functionality is available for other scripting languages as well, with slightly different syntax.
1.) Define your data
You can use a pre-defined data source for this, or TestComplete's built-in data generation wizard. In this case I'm using the data generation wizard to create 10,000 random names for me to pick from. This creates a Table variable, and that can be stored in a single Keyword test or with an entire Project.
2.) Create a second variable as an integer to store the random number
3.) In the test, use the Set Variable Value to assign that integer a random number between 1 and the total number of rows in that table. In my case 10,000. Use the Code Expression option for the mode.
This example works in JScript, if your Project is in another scripting language the syntax here would need to be adjusted.
4.) Use that integer variable to select a row of your table and return the value, that can be used in the test steps.
Related Forum Post:
Thanks for sharing such great information, Dan!
For python you can use this expression:
var = random.choice([i for i in range(x)]) # Will return the random mumber between 0 and x
@baxatob,I tried to implement your suggested Python code into a Keyword Test and I can't get it to work. Is it possible to import a library into a Keyword Test? I tried using the Run Code Snippet tool to add "import random" at the top of the script but I am getting exception errors. See screenshots for examples, if desired. I can create a script but my team has decided that the majority of our tests should be Keyword Tests, not Scripts, so they are easily debugged by everyone on the team. Thanks!
@SmartBearDan Great solution! This is exactly what I am looking for.
Note: Could be even better if this feature (select random row) be implemented in TC's Data Driven functionality, so that testers don't need to use scripting!
See screenshot to illustrate implementation:
I created a feature request. It can be found here. Please vote for it if you agree.
Is this method still valid with Version 12.20.935.7. Or is there a better method to use. Not able to get this to work.
Yes, the approach must work.
The more details you provide as for what does not work in your case, the more chances to get a piece of useful advice.
I tried implementing this method but was not successful. Got an error saying that "The table does not contain a column with the specified name". Is there anything needed to change for this to be applicable to v12.50.4142 x86. Or is there an alternative approach for this now?
What I would want done is to generate a 5-digit number with random value then use it in my tests. I prefer that a new number will be generated everytime the step is executed. Also, I prefer to not use scripts, unless there's no other way to do this.
I hope anyone can help me out.