Pass random values in every execution is really important. when the application has the duplicate validation. So In every execution, we have to pass a unique value in the input fields.
We can write a c# program to generate random values in a String, Number or any other format according to the requirements In this example, I am going to explain to you how to pass random values in the input field using selenium C#.
During the automation, we face this problem regularly. So how can pass a new string or number in an input field in selenium using c#? Let’s understand the problem first with a real-time example.
For example, I am automating a registration form where I need to pass a “UserName”. Which accepts only a unique value. If you pass the same value then it will show a message “Record already exists”. In this case, your test case is executed only once. When you run it the second time you will face the issue “Record already exists”.
So, the perfect solution to this problem is to pass unique value in every execution. Now let’s see how to generate a random string value in selenium c#.
How to Generate Random string in C#
// Generate a random string with a given size public static string RandomString(int size, bool lowerCase) { StringBuilder builder = new StringBuilder(); Random random = new Random(); char ch; for (int i = 0; i < size; i++) { ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))); builder.Append(ch); } if (lowerCase) return builder.ToString().ToLower(); return builder.ToString(); }
In this code, RandomString(int, bool) is a custom method. it will generate a unique string value in every call.It will take two parameters 1) Length of the spring 2) true/false to covert this in Upper and lower case and Return a random string on every calling request.
Example: String s1 = RandomString(4, true);
How to pass String value in input field using selenium c#.
When you call this method like “RandomString(4, true)” it will return a string of size 4 in lower case and if you call this like “RandomString(5, False)” it will return a string of size 5 in uppercase letter.
//Call the method String s1 = RandomString(4, true); //Pass value to an element element.sendkeys(s1);
By this, you can create only String value. Now Let’s in how can create random value of integer type.
How to Generate Random Number in C#
public static int RandomInt(int from, int to) { Random random = new Random(); int n = random.Next(from, to); return n; }
This method will take two input parameter 1) for the starting number 2) for the last number and it will return an int value between the start number and end number.
How to pass Number or integer value in input field using selenium c#.
//Call the method int number = RandomInt(100,999); //Pass value to an element element.sendkeys(number);
When you call this Method RandomInt(100,999) then it will return a random number of 3 digits between 100 to 999.
How to Generate Random Double value in C#
public static double RandomDouble() { Random random = new Random(); double n = random.NextDouble(); Console.WriteLine(n); return n; }
Here, We are using NextDouble() it will return a random number of double type between 0.0 to 1.0. So when you call RandomDouble() it will return a random number of double type.
How to pass Number of double type value in input field using selenium c#.
//Call the method double number = RandomDouble(); //Pass value to an element element.sendkeys(number);
These all are the most common requirement to pass a random string in the input field using selenium C#. We can use according to the requirement.