Support Corner: Creating Email Address Test Data With the Ranorex API

Feb 29, 2024 | Product Insights, Test Automation Insights

Ranorex is a powerful no-code tool that can automate testing for web, mobile, and desktop applications. In addition to the powerful no-code recording, you can utilize the Ranorex API to create code modules in C# or VB.NET. That’s beneficial because it can increase the effectiveness of your testing suite.

In this blog, we will share some code you can use. In many applications, you will need to input an email address into an input text field utilizing code with the Ranorex API in order to interact with the input box and generate random data to test with. Testing with random data increases the effectiveness of the test suite by improving the potential of finding issues with the input form. 

How It’s Done

To start we will create a user code collection. A user code collection is defined as a collection of user code methods that belong together logically. All user code collections together make up the user code library. You could also add the code inside of a code module but using the code collection will allow it to be reused more easily across different recording modules.

Inside the user code collection, we will add the following code methods.  You can easily add methods to a user code collection by right-clicking inside the main class and selecting “Insert new user code method…” 

 

The following method takes in two parameters — one to set the length of the string we want returned, and the second is the format of the string. Below the first method are the two helper methods that will help us generate the random strings. For the case “email,” it will make a random email address in the format “[email protected].” For the case “name” it will just return random text. 

[UserCodeMethod]

public static string RandomGenerator(int lengthOfString, string generatorType)
        {
            switch(generatorType)
            {
                case "email":
                    return string.Format("{0}@gmail.com", GenerateRandomAlphabetString(lengthOfString));
                break;

                case "name":
                    return GenerateRandomAlphabetString(lengthOfString);
                break;

                default:
                    return GenerateRandomAlphabetString(lengthOfString);
                break;
            }
        }

public static string GenerateRandomAlphabetString(int length)
    {
            string allowedChars = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var rnd = SeedRandom();
            char[] chars = new char[length];
            for (int i = 0; i < length; i++)
            {
                chars[i] = allowedChars[rnd.Next(allowedChars.Length)];
            }
            return new string(chars);
     }
 private static Random SeedRandom()
        {
            return new Random(Guid.NewGuid().GetHashCode());
        }

After adding the methods to the user code collection inside a recording module, we can utilize the methods. For example, in this screenshot, we have a few input boxes so we can create some random data for them.  

To use the methods we just created in the user code collection, we can go to a recording module and Add New Action -> User Code -> Select from library. You should see the RandomGenerator method we just added.

You can use that method to create random names and emails now. For example, here is how to create and input a random name and a random email into the name and email form shown above. Each time the test suite is run, there will be a random name and email generated, improving your testing effectiveness.

 

Next Steps

Overall, the Ranorex API allows you to create powerful scripts to complement the no-code recording tools of Ranorex Studio. Using C# to generate random data to test input fields is just one of the many use cases that can be created with it. Ready to bring your testing to the next level? Start your free trial of Ranorex today and try our robust QA testing automation tools including the Ranorex API for yourself.

 

 

Related Posts:

5 Software Quality Metrics That Matter

5 Software Quality Metrics That Matter

Which software quality metrics matter most? That’s the question we all need to ask. If your company is dedicated to developing high-quality software, then you need a definition for what “high-quality” actually looks like. This means understanding different aspects of...

The Ins and Outs of Pairwise Testing

The Ins and Outs of Pairwise Testing

Software testing typically involves taking user requirements and stories to create test cases that provide a desired level of coverage. Many of these tests contain a certain level of redundancy. Traditional testing methods can lead to a lot of wasted time and extend...