Page 1 of 1

How to use random data for account registration

Posted: Thu Jan 08, 2015 10:42 am
by lars
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

Re: How to use random data for account registration

Posted: Thu Jan 08, 2015 2:33 pm
by krstcs
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";

Re: How to use random data for account registration

Posted: Fri Jan 09, 2015 9:40 am
by lars
Thanks for your feedback and help. Got it! :)