How to use random data for account registration

Mobile Testing, Android App Testing.
lars
Posts: 2
Joined: Thu Jan 08, 2015 10:28 am

How to use random data for account registration

Post by lars » Thu Jan 08, 2015 10:42 am

Hi there,

I'm creating test cases for a iOS/Android app with account registration. Currently I'm stuck at the point where I want to use random username and email addresses. Since we are using a staging backend, it's not necessary to have a valid username or email because the created accounts will not be used furthermore.

Can someone please give me (rather less coding skills) a hint, how to add some random characters to the key input.

Thanks in advance.

Lars

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to use random data for account registration

Post by krstcs » Thu Jan 08, 2015 2:33 pm

You could do something like the following. It will take the min and max lengths you want for the string, randomly generating the actual length and then randomly getting each character from the alphabet array. Finally it will capitalize the first letter, if you tell it to.

Code: Select all

public static string getRandomString(int minLength, int maxLength, bool initialCaps) {
    Random r = new Random();

    int length = r.Next(minLength, maxLength);

    char[] characters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        	
    StringBuilder sb = new StringBuilder();
        	
    for (int i = 0; i < length; i++) {
        sb.Append(characters[r.Next(26)]);
    }
        	
    if (initialCaps) return sb.ToString().Substring(0,1).ToUpper() + sb.ToString().Substring(1);

    return sb.ToString();
}
You could use it to make first and last names, email addresses, etc.

Code: Select all

string firstName = getRandomString(6,8,true);
string lastName = getRandomString(6,10,true);
string emailAddress = getRandomString(6,10,false) + "@" + getRandomString(6,10,false) + ".com";
Shortcuts usually aren't...

lars
Posts: 2
Joined: Thu Jan 08, 2015 10:28 am

Re: How to use random data for account registration

Post by lars » Fri Jan 09, 2015 9:40 am

Thanks for your feedback and help. Got it! :)