Features

Generative AI and Automated Testing

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

The world of software development is evolving and automated testing has become an integral part of delivering high quality software. In order to achieve more efficient testing processes, the importance of test data cannot be overstated.

Having diverse and realistic test data is crucial, for uncovering bugs, validating functionality and ensuring quality of the website or app you are testing.

This is where Generative AI comes in. Generative AI is a groundbreaking technology that revolutionizes how we generate test data, for example for Selenium or Appium-based automated tests.

In this article we will delve into the power of Generative AI, its role in Selenium & Appium testing and the advantages it brings.

Why use Generative AI for Test Data generation?

Generative AI, which is a subset of artificial intelligence (AI), gives machines the ability to fabricate data that looks like real-world data. By analyzing patterns in existing datasets, Generative AI algorithms generate data points that maintain the underlying characteristics of the original dataset. Because of these capabilities, Generative AI can create diverse and realistic data sets, which covers a wide range of scenarios and edge cases that are difficult to replicate manually.

Selenium and Generative AI

While Selenium excels at emulating user interactions, it often relies on predefined datasets for testing. This means it might not fully capture the complexity of real-world scenarios, for example with testing payment methods such as credit cards, or filling in user credentials such as a street address.

Generative Artificial Intelligence, or Generative AI, can help with generating random but valid looking data to be used in your automated Selenium tests.

One popular solution to generate fake test data is to use a library called Faker, which generates realistic data for tests.

To use generative AI with your tests, you can use various libraries in your test automation code. Below are some examples:

IBM Generative AI SDK

Using IBM Generative AI SDK, you can generate fake data by providing a prompt to the SDK, for example:

const address = await model.call(
  'Generate a valid address for a house located in California, United States',
)
// next, use the 'address' in your test automation code to for example fill in a form

Google AI Generative Language

Another popular solution, from Google, is @google-ai/generativelanguage (PaLM API). You can generate valid looking test data, such as a firstName and lastName with this SDK:

const { TextServiceClient } =
  require("@google-ai/generativelanguage").v1beta2;

const { GoogleAuth } = require("google-auth-library");

const MODEL_NAME = "models/text-bison-001";
const API_KEY = process.env.API_KEY;

const client = new TextServiceClient({
  authClient: new GoogleAuth().fromAPIKey(API_KEY),
});

function getUserDetails() {
	return new Promise((resolve) => {
		const prompt = "Generate a first name and a last name for a person living in Canada";

		client
		  .generateText({
		    model: MODEL_NAME,
		    prompt: {
		      text: prompt,
		    },
		  })
		  .then((result) => {
		    const { firstName, lastName } = result.split(' ')
		    resolve({
		    	firstName,
		    	lastName
		    })
		  });
		}
	})
}


const webdriver = require('selenium-webdriver');
const capabilities = {
 'platform' : 'WIN10',
 'browserName' : 'chrome',
 'version' : 'latest',
 'name': 'NodeJS Sample Test'
}
async function runTest () {
  const { firstName, lastName } = await getUserDetails() // fetch new fake generative AI test data

  let driver = new webdriver.Builder()
    .usingServer('https://hub.testingbot.com/wd/hub')
    .withCapabilities(capabilities)
    .build();
  await driver.get("https://www.google.com/ncr");
  const inputField = await driver.findElement(webdriver.By.name("q"));
  // Search google with the generate firstName and lastName

  await inputField.sendKeys(firstName + ' ' + lastName, webdriver.Key.ENTER);
  await driver.quit();
}
runTest();

The example above will start a new Selenium test session on TestingBot, navigate to Google, and enter a name generated by Google AI's Generative Language in the Google search input box. The test will then quit (close the browser).

Generate testdata with intellinode

This NodeJS SDK allows you to use various AI models, such as OpenAI, Cohere, LLaMa v2, Google PaLM, and others. For example, to use with OpenAI's ChatGPT, you can use the example below, after installing intellinode with npm i intellinode:

const { Chatbot, ChatGPTInput } = require('intellinode');

// set the api key for OpenAI

const chatbot = new Chatbot(apiKey);

const input = new ChatGPTInput('You are helping with creating valid looking data');
input.addUserMessage('Generate a realistic looking name and home address for a person living in the US');

const testdata = await chatbot.chat(input);
let driver = new webdriver.Builder()
    .usingServer('https://hub.testingbot.com/wd/hub')
    .withCapabilities(capabilities)
    .build();
  await driver.get("https://www.google.com/ncr");
  const inputField = await driver.findElement(webdriver.By.name("q"));
  // Search google with the generate firstName and lastName

  await inputField.sendKeys(testData, webdriver.Key.ENTER);
  await driver.quit();

The example above starts a Selenium session and enters data generated with ChatGPT, using a prompt that asks the Chatbot to create realistic looking test data.

The synergy between Generative AI and Selenium Automated Tests is a paradigm shift in software testing. It opens the door to improved testing coverage, enhanced accuracy, and faster testing cycles.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews
TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

Migrate from Appium 1.x to Appium 2.x

Learn how to migrate from Appium 1 to the new Appium 2.

Read more
Testing Tailwind CSS webpages

An introduction in running automated tests against websites built with Tailwind CSS.

Read more
Automate native iOS Apps with XCUITest

Looking to automate native iOS apps? Read our XCUITest tutorial on how to use code for iOS automation.

Read more
Using Touch Actions with Appium

Find out how to use Touch Actions with Appium.

Read more

How to Inspect Element using UIAutomatorViewer

Find our more about UIAutomatorViewer and how it can help with your automated Android tests.

Read more
Dark Mode Testing with Appium

Perform dark mode automated testing on iOS and Android with Appium.

Read more