Friday, May 23, 2025

Integrating Selenium with Java and OpenAI API – Intelligent Automation for QA Engineers

🧠 Integrate Selenium with Java and OpenAI API – Full Guide

This guide shows how to use Selenium for web automation with OpenAI’s Java SDK. Learn how to retrieve your API key, manage it securely, and write a test script that interacts with OpenAI and a web page.

🔑 How to Get Your OpenAI API Key

  1. Go to https://platform.openai.com/signup and sign up or log in.
  2. Once logged in, click on your profile (top-right) > API keys.
  3. Click Create new secret key and copy the key securely.
⚠️ Do not share your API key publicly. Store it safely using a .env file as explained below.

📦 Maven Project Setup

In your Java Maven project’s pom.xml, add these dependencies:

<dependencies>
  <!-- Selenium WebDriver -->
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.8.0</version>
  </dependency>

  <!-- OpenAI Java Client (Theo Kanning's library) -->
  <dependency>
    <groupId>com.theokanning.openai-gpt3-java</groupId>
    <artifactId>client</artifactId>
    <version>0.18.2</version>
  </dependency>

  <!-- dotenv-java to manage environment variables -->
  <dependency>
    <groupId>io.github.cdimascio</groupId>
    <artifactId>dotenv-java</artifactId>
    <version>5.2.2</version>
  </dependency>
</dependencies>

🔐 Step: Setup .env File

Create a file named .env in your project’s root folder with this content:

OPENAI_API_KEY=your_openai_api_key_here

Also add .env to your .gitignore file to keep your API key private.

💻 Real-World Java Example with OpenAI + Selenium

This example uses Selenium to open a flight search website and OpenAI to generate a user tip for filling out the form.

import io.github.cdimascio.dotenv.Dotenv;
import com.theokanning.openai.service.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionChoice;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FlightAutomationExample {
    public static void main(String[] args) {
        // Load environment variables
        Dotenv dotenv = Dotenv.load();
        String apiKey = dotenv.get("OPENAI_API_KEY");

        // Initialize OpenAI
        OpenAiService service = new OpenAiService(apiKey);

        // Use OpenAI to generate a flight booking tip
        CompletionRequest request = CompletionRequest.builder()
            .prompt("Give one important tip for booking flights online.")
            .model("text-davinci-003")
            .maxTokens(100)
            .build();

        CompletionChoice choice = service.createCompletion(request).getChoices().get(0);
        System.out.println("OpenAI Tip: " + choice.getText());

        // Launch browser with Selenium
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.example.com/flights");

        // You can automate actions here, e.g., filling a form, clicking search, etc.

        driver.quit();
    }
}

✅ Summary

  • 💡 Use OpenAI to generate smart content or logic in your automation tests.
  • 🧪 Use Selenium for browser testing or automation steps.
  • 🔐 Secure API keys using dotenv-java in your Maven project.

By combining these technologies, you can bring intelligence to your automation framework, such as generating test data, writing user prompts, or analyzing UI interactions.


If you found this helpful, share it with fellow QA engineers or developers!

No comments:

Post a Comment

Thanks for your comment..! Keep commenting for more and more updates. To get post updates subscribe Blog or become a follower of this blog. Thanks Again..!