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.

 

 

Get a free trial of Ranorex Studio and streamline your automated testing tools experience.

Start your intelligent testing journey with a free DesignWise trial today.

Related Posts:

Test Design: What Is It and Why Is It Important?

Test Design: What Is It and Why Is It Important?

In software development, the quality of your tests often determines the quality of your application. However, testing can be as complex as the software itself. Poorly designed tests can leave defects undetected, leading to security vulnerabilities, performance issues,...

Ranorex Introduces Subscription Licensing

Ranorex Introduces Subscription Licensing

Software testing needs are evolving, and so are we. After listening to customer feedback, we’re excited to introduce subscription licensing for Ranorex Studio and DesignWise. This new option complements our perpetual licenses, offering teams a flexible, scalable, and...