Increamenting value

Ask general questions here.
sianl
Posts: 1
Joined: Tue Oct 20, 2020 4:18 am

Increamenting value

Post by sianl » Tue Oct 20, 2020 4:29 am

I have been asked to use ranorex to create many users in one of our applications, I do not have database access.

The first name field needs to have an incremental value at the end ie "FirstNameX"

Is there a way to code this value?

My back up plan is to use a time stamp but i'm not use if this field can handle a long value.

Thank you

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 6360
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Increamenting value

Post by odklizec » Wed Oct 21, 2020 8:03 am

Hi,

Of course, you can do this with Ranorex, but not without using some code. I'm personally using something like this...

In case you need something more random, use DateTime...

Code: Select all

    using System.Linq;
    /// <summary>
    /// Random string generator (using DateTime)
    /// </summary>
    /// <param name="dateFormat">date time format string</param>
    /// <param name="rndStringLabel">string label</param>
    /// <returns>cur. date time string</returns>
    [UserCodeMethod]
    public static string RandomString(string dateFormat, string rndStringLabel)
    {
        //dateFormat e.g. "ddMMyyyyHHmmss"
        string random = System.DateTime.Now.ToString(dateFormat);
        return rndStringLabel + random;
    } 
Or this, using random number...

Code: Select all

    using System.Linq;
    /// <summary>
    /// Random string generator (using LINQ)
    /// </summary>
    /// <param name="length">length of generated random string</param>
    /// <returns>random number</returns>
    [UserCodeMethod]
    public static string RandomString(int length, string rndStringLabel)
    {
        const string chars = "0123456789";
        var random = new Random();
        return rndStringLabel + new string(Enumerable.Repeat(chars, length)
        .Select(s => s[random.Next(s.Length)]).ToArray());
    }
Simply create a recording variable, which will be used for input purposes. Then create User Code action, in which you need to fill the variable with random number. The above suggested methods can be placed either directly in user code or even better, you can create a user code collection, which will be then usable in entire solution. Then you can fill the variable with current datetime string...

Code: Select all

rndAssetString = SystemLib.RandomString("ddMMyyyyHHmmss", "FirstName");
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration