Automation Test
Vanna  

Use AI to generate code Automation

I. AI Chat Models

Among these, Claude is particularly effective for coding tasks. However, it does require a subscription, which may be a drawback for those who have not yet enrolled.

II. How to Use Specific “Prompts” to Generate Test Automation Code

1. Identify the Testing Framework: Choose the framework you plan to use, such as Selenium, JUnit, TestNG, Cypress, etc.

2. Define the Testing Scenario: Clearly outline what you want to test. For example, you might want to test a login functionality or a shopping cart feature.

3. Write the Prompt: Craft a prompt that includes the following elements:

  • Context: Briefly describe the application you are testing.

  • Specific Action: Clearly state the action you want to automate.

  • Expected Result: Describe what you expect to happen after the action is performed.
Example Prompt: “Generate Selenium test automation code to verify that a user can log into a website using the username ‘testuser’ and password ‘password123’, and ensure that the homepage is displayed after a successful login.”

4. Utilize AI Tools: Use AI-based code generation tools or platforms that can accept your prompt and generate code based on the input. Tools like OpenAI’s Codex or GitHub Copilot can be helpful.

5. Review and Modify Generated Code: Once the code is generated, review it for accuracy and completeness. Make any necessary adjustments to ensure it fits your application’s architecture and standards.

6. Run and Validate: Execute the generated test code in your testing environment to ensure it behaves as expected.

7. Iterate: If the generated code does not work as intended, refine your prompt for clarity or detail, and regenerate the code.

Example: Generating Code for a Login Page Using Selenium with Java and TestNG in a Maven Project

1. Maven Dependencies

First, ensure you have the necessary dependencies in your pom.xml. Add the following dependencies for Selenium and TestNG:

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.x.x</version> <!-- Use the latest version -->
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.x.x</version> <!-- Use the latest version -->
        <scope>test</scope>
    </dependency>
</dependencies>

2. LoginTest.java

Create a new Java class for your test case, for example, LoginTest.java:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class LoginTest {
    
    private WebDriver driver;

    @BeforeClass
    public void setUp() {
        // Set up the WebDriver (Make sure ChromeDriver is installed)
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.get("https://example.com/login"); // Replace with your login page URL
    }

    @Test
    public void testLogin() {
        // Locate username and password fields
        WebElement usernameField = driver.findElement(By.id("username")); // Replace with your username field locator
        WebElement passwordField = driver.findElement(By.id("password")); // Replace with your password field locator

        // Input credentials
        usernameField.sendKeys("testuser"); // Replace with test username
        passwordField.sendKeys("password123"); // Replace with test password

        // Click login button
        WebElement loginButton = driver.findElement(By.id("loginButton")); // Replace with your login button locator
        loginButton.click();

        // Assert that the login was successful
        String expectedUrl = "https://example.com/home"; // Replace with the expected URL after login
        String actualUrl = driver.getCurrentUrl();
        assert actualUrl.equals(expectedUrl) : "Login failed, expected URL: " + expectedUrl + ", but got: " + actualUrl;
    }

    @AfterClass
    public void tearDown() {
        // Close the browser
        if (driver != null) {
            driver.quit();
        }
    }
}

3. Running the Test

To run the test, you can use a Maven command in your terminal:

mvn test

Notes:

  • Make sure you have ChromeDriver set up for the Chrome browser or use a different WebDriver based on the browser you want to test with.
  • Replace By.id("username")By.id("password"), and By.id("loginButton") with the appropriate locators for your specific login page.
  • Adjust the test data (testuserpassword123, etc.) with real credentials for your testing purposes, if applicable.
  • Ensure your WebDriverManager dependency is included in your Maven dependencies to automatically manage driver binaries. If not, you may need to download the appropriate drivers separately.

This example outlines the basic structure for automating a login test with Selenium in Java using TestNG.

Leave A Comment