AI-Assisted Test Case Generation for Web & API: A Practical Approach

AI-Assisted Test Case Generation for Web & API: A Practical Approach

Introduction

AI is transforming software testing by automating test case generation, ensuring better test coverage, and reducing manual effort. This newsletter explores how AI can assist in test case creation for web applications and APIs with real-world implementation examples using OpenAI.


AI-Powered Test Case Generation

?? Why Automate Test Case Generation???

Manual test case creation is often time-consuming, error-prone, and resource-intensive. AI helps overcome these challenges by:

  • ? Generating test cases faster and with greater accuracy
  • ? Enhancing test coverage by considering multiple scenarios
  • ? Standardizing test cases for better collaboration
  • ? Reducing dependency on domain expertise


How AI-Powered Test Case Generation Works

1?? Web Test Case Generation

AI can analyse a web application's structure and generate test cases based on UI elements.

?? Step 1: Web Scraping

Web scraping will provide us with the DOM structure information of the web page. We will store this and then pass it to the next process, which is analysing this stored DOM structure. (Source Code)

?? Step 2: Element Analysis

In this step, we are analyzing the elements that we got from our first step, and based on that, we will define what action to take on those elements.? (Source Code)

?? Step 3: Generating AI-Powered Test Cases

We pass the extracted UI elements to OpenAI's API to generate behavior-driven test cases:

const axios = require('axios');
async function generateBddTestCases(actions, apiKey) {
    const prompt = `
    Generate BDD test cases using Gherkin syntax for the following login page actions: ${actions.join(', ')}. Include test cases for:
    1. Functional Testing: Verify each function of the software application.
    2. Boundary Testing: Test boundaries between partitions.
    3. Equivalence Partitioning: Divide input data into valid and invalid partitions.
    4. Error Guessing: Anticipate errors based on experience.
    5. Performance Testing: Ensure the software performs well under expected workloads.
    6. Security Testing: Identify vulnerabilities in the system.
    `;
    const headers = {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`
    };
    const data = {
        model: 'gpt-3.5-turbo',
        prompt,
        max_tokens: 1000,
        n: 1,
        stop: ['\n'],
    };
    try {
        const response = await axios.post('https://api.openai.com/v1/completions', data, { headers });
        return response.data.choices[0].text.trim();
    } catch (error) {
        console.error('Error generating test cases:', error.response ? error.response.data : error.message);
        return null;
    }
}
module.exports = generateBddTestCases;        

?? Example AI-Generated Test Case for Web Login

Scenario: Successful login with valid credentials

Given the user is on the login page

When the user fills in the username field with “user123”

And the user fills in the password field with “password123”

And the user clicks the submit button

Then the user should be redirected to the dashboard

(Check out comple output here)


2?? API Test Case Generation

For API testing, AI generates test cases based on API request payloads and expected responses.

?? Step 1: Storing API Payload & Expected Response

We are going to use the POST API for this, and for POST API, we need payload. The payload and expected API response can be stored in a separate JSON file. The expected API response will be helpful for maximum test coverage.

?? Step 2: Generating AI-Powered API Test Cases

We will use the stored payload and expected response json files along with the API endpoint.

const fs = require('fs');
const axios = require('axios');

// Read JSON files
const readJson = (path) => JSON.parse(fs.readFileSync(path, 'utf8'));
const payload = readJson('path_of_payload.json');
const expectedResult = readJson('path_of_expected_result.json');

const apiKey = 'your_api_key';
const apiUrl = 'https://reqres.in/api/login';

const generateTestCases = async (retries = 3) => {
  const prompt = `Generate BDD test cases for API: ${apiUrl} \nPayload: ${JSON.stringify(payload)} \nExpected: ${JSON.stringify(expectedResult)}`;

  try {
    const { data } = await axios.post('https://api.openai.com/v1/completions', {
      model: 'gpt-3.5-turbo', prompt, max_tokens: 1000, n: 1, stop: ['\n']
    }, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } });

    const testCases = data.choices?.[0]?.text?.trim();
    if (testCases) fs.writeFileSync('apiTestCases.txt', testCases);
    console.log("BDD test cases saved.");
  } catch (err) {
    if (err.response?.status === 429 && retries) {
      console.log('Rate limit exceeded, retrying...');
      setTimeout(() => generateTestCases(retries - 1), 2000);
    } else console.error('Error:', err.response?.data || err.message);
  }
};

generateTestCases();        

?? Example AI-Generated Test Case for API Login

Scenario: Successful login with valid credentials Given the API endpoint is “https://reqres.in/api/login” When a POST request is made with payload:

“””
{
“email”: “[email protected]”,
“password”: “cityslicka”
}
“””
Then the response status should be 200
And the response should be:
“””
{
“token”: “QpwL5tke4Pnpja7X4”
}
“””        

(Check out complete output here)


?? Have you ever encountered unexpected AI-generated test cases? What was the outcome? ??

Key Benefits of AI-Driven Test Case Generation

  • ? Speed & Efficiency: Automates repetitive tasks, allowing faster test cycles.
  • ? Improved Accuracy: This reduces human errors in test case documentation.
  • ? Higher Test Coverage: Covers edge cases and security vulnerabilities.
  • ? Better Collaboration: Standardizes test cases across teams.


?? What features would you like to see in AI-driven test automation tools? ??

Challenges & Your Thoughts ??

While AI improves test case generation, some challenges remain:

  • ?? Handling dynamic UI elements effectively
  • ?? Fine-tuning AI models for complex test scenarios
  • ?? Ensuring security and performance aspects are fully covered

Do you have thoughts on this? Let’s chat in the comments! ??

If you found this newsletter interesting, don’t stop here! You can read our other newsletters for more expert insights on Software Testing and Test Automation. Stay ahead in the game—check them out now! ????

?? Until next time, keep testing and keep innovating! ???



要查看或添加评论,请登录

SpurQLabs | Next-Generation Software Testing & Test Automation Services的更多文章

社区洞察

其他会员也浏览了