Showing posts with label Maven setup. Show all posts
Showing posts with label Maven setup. Show all posts

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!

Sunday, August 14, 2022

Environment setup FOR JAVA Automation Testing

Before starting the JAVA basic for automation testing, we need to do first Environment setup for automation testing as below:

  •     JDK (java development kit)
  •     Eclipse IDE
  •     Maven (build/dependency management tool)

follow the instructions as per the environment set up for Java/JDK, Eclipse IDE and Maven

Tools, Versions & URLs:

Java SE Development Kit 8u291

https://www.oracle.com/uk/java/technologies/javase/javase-jdk8-downloads.html 

Eclipse IDE for Java Developers - Version 2020-12 (4.18)

https://www.eclipse.org/downloads/packages/release/2020-12/r 

Apache Maven 3.8.1

https://maven.apache.org/download.cgi

Configure Java:

Environment variable is essentially run third party tool like Maven. 

In order to set the variable, right click on system, go to properties, Advance system settings, Environment variable, New system variable there mention variable name (java_home) and value (jdk path). 

Maven Setup:

Maven is open source tool and developed by Apache. Download the Maven package, extract it. We need to add bin folder with the Maven command and in relation to the path. for that right click on system, go to properties, Advance system settings, Go to environment variable, New variable system, there mention variable name (MVN_Home) and value (apache path) and configure the path. 

Once your Maven setup is done, make sure to close all the open existing command prompt and open the new instance and run the below query in command prompt:

 >mvn -version (provides you the maven and java version version details)

Maven_Project
 >echo %MVN_HOME% (Provides details that it pointing to correct directory)

Eclipse Setup:

Download the eclipse package, extract it and there we are able to see eclipse icon, double click and it will open eclipse.

In Eclipse, in latest version we can see the Maven is inbuild, however if you do not find it then go to google and enter 'maven eclipse plugin' and go to eclipse.org site and there go to download version. Copy the laetst version link and paste it on Eclipse available software pop up screen (include the Maven Integration for Eclipse) and install. In order to check that the Maven installation successful, select New project and there you will see the Maven option.