Generating a Random string in C# for Selenium

Sometimes we may come across situations where we may need to generate some random string in C# for using it in Selenium. Be it Names, or City or email addresses or say Purchase Order or Reference number, we can make use of the below code to generate random strings in C# for any input length

E.g

if you want to generate a random string with 6 or 9 characters both can be done via the below code

       public static string RandomGenerate(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrsqtuvwxyz";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

 

The above code when called in any of Page object files or testcases, this will generate a Random characters with lenght that is passed via method..

You may also like...