Adlerqa

In today’s software development world, having a strong and reliable set of tests is crucial. As apps get updated and changed, even small tweaks can sometimes break tests, which can be a real hassle to fix. One smart way to tackle this is by adding self-healing features to your testing framework. In this blog, we’ll show you how using OpenAI’s API can help your tests automatically adjust to changes, keeping them accurate and dependable.

What is Self-Healing in Testing?

Self-healing in testing means your test suite can automatically adjust to changes in the application it’s testing. This could be things like changes in the user interface or updates to element locators. With self-healing features, your tests can handle these changes without needing you to manually update them. This helps cut down on maintenance work and keeps your tests more stable.

How OpenAI API Can Enhance Self-Healing

OpenAI’s API offers powerful language models that can be utilised to improve your testing framework’s self-healing abilities. Here’s a step-by-step guide on how to integrate

 
OpenAI API for self-healing:

Install CodeceptJS with Playwright
Start by setting up your testing framework with CodeceptJS and Playwright. These tools facilitate browser automation and test scripting.

npm install codeceptjs playwright

Install and Configure .env Dependency
Use .env to manage sensitive information such as API keys securely. This keeps credentials out of your codebase and reduces security risks.
npm install dotenv

  1. Why Install .env: It ensures that sensitive information is stored securely and kept separate from the source code.

 
Configure CodeceptJS

Set up your codecept.conf.js file to include Playwright as the helper and configure other settings for your test environment.


const { setHeadlessWhen, setCommonPlugins } = require(‘@codeceptjs/configure’);

 

// turn on headless mode when running with HEADLESS=true environment variable

// export HEADLESS=true && npx codeceptjs run

setHeadlessWhen(process.env.HEADLESS);

// enable all common plugins https://github.com/codeceptjs/configure#setcommonplugins

setCommonPlugins();

/** @type {CodeceptJS.MainConfig} */

exports.config = {

 tests: ‘./tests/*_test.js’,

 output: ‘./output’,

 helpers: {

   Playwright: {

     browser: ‘chromium’,

     url: ‘http://localhost’,

     show: true

   }

 },

 include: {

   I: ‘./steps_file.js’ },

 name: ‘codeceptjs-openai’

}

 

Write a Test to Invoke OpenAI API
Create a test case that utilises OpenAI’s API to dynamically adapt to changes. For instance, use the API to analyse and suggest modifications for failing test cases.

const { I } = inject();

 

const OpenAI = require(‘openai’);

require(‘dotenv’).config(); // Load environment variables from .env file

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

Feature(‘OpenAI Integration’);

Scenario(‘Test OpenAI API’, async ({ I }) => {

 try {

   // Define messages for the OpenAI API

   const messages = [

     { role: ‘user’, content: ‘Hello, how are you?’ }

   ];

   // Send a request to the OpenAI API

   const completion = await openai.chat.completions.create({

     model: “gpt-3.5-turbo”,

     messages,

   });

   // Get the response content

   const responseContent = completion?.choices[0]?.message?.content;

   // Print the response to the console

   I.say(`Response from OpenAI: ${responseContent.trim()}`);

 } catch (error) {

   I.say(`Error: ${error.message}`);

 }

});

 

2. In this test:

  •  Prompt: A prompt is sent to the OpenAI API asking for suggestions on how to improve locator strategies or adapt to UI changes.
  • API Key: Ensure that your API key is secured using .env.
 
  

Run Your Tests
Execute your tests to validate that the integration works correctly and that self-healing capabilities are functioning as expected.
npx codeceptjs run

  
 

Benefits of Using OpenAI API for Self-Healing

  • Automated Adaptation: Self-healing allows your tests to automatically adjust to minor changes, reducing the need for manual test updates.

  • Reduced Maintenance: By integrating AI suggestions, you can minimise the effort required to keep your tests up-to-date.

  • Enhanced Reliability: Tests become more resilient to changes, leading to fewer false positives and more reliable test results.

Conclusion

Adding OpenAI API to your testing framework can greatly improve its ability to automatically adapt and fix itself. This makes your testing process smoother and keeps your test suite strong and reliable, even when the application changes. By following the steps we covered, you can use AI to keep your tests top-notch and make your development work more efficient.