Commit c81ef8d8 authored by Swathi Mashetty's avatar Swathi Mashetty

new repo

parents
error is java.lang.AssertionError: Page title is not correct expected [Tpoint Tech - Free Online Tutorials conveyed] but found [Tpoint Tech - Free Online Tutorials] and the method where the error is public void the_homepage_opens_verify_it() {\n String expectedTitle = \"Tpoint Tech - Free Online Tutorials conveyed\";\n String actualTitle = driver.getTitle();\n Assert.assertEquals(actualTitle, expectedTitle, \"Page title is not correct\");\n } how to resolve the above issue give one exact solution rather than multiple solutions
\ No newline at end of file
Copied from console
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>LLMRestAPI</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <!-- Specify the packaging type as jar -->
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!-- Surefire plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.11.0</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.11.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>3.0.21</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
</project>
package POJO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
// Main handler = new Main();
// String inputBody = handler.prompt("test message for Llama 3.2");
// System.out.println("----------->"+inputBody);
// Response response = given().log().all().contentType(ContentType.JSON)
// .body(inputBody).when().post("http://localhost:11434/api/generate");
// System.out.println(response.getBody().asString());
// response.then().statusCode(200);
}
// public String prompt(String string){
// PromptPOJO prompt = new PromptPOJO();
// prompt.setModel("llama3.2:latest");
// prompt.setStream(false);
// prompt.setPrompt(string);
// return prompt.toString();
// }
}
\ No newline at end of file
package POJO;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class PromptPOJO {
private boolean stream;
private String prompt;
private String model;
// public String getModel() {
// return model;
// }
//
// public void setModel(String model) {
// this.model = model;
// }
//
//
// public boolean isStream() {
// return stream;
// }
//
// public void setStream(boolean stream) {
// this.stream = stream;
// }
//
// public String getPrompt() {
// return prompt;
// }
//
// public void setPrompt(String prompt) {
// this.prompt = prompt;
// }
@Override
public String toString() {
return "{\"model\":\"" + model + "\", \"stream\":" + stream + ", \"prompt\":\"" + prompt + "\"}";
}
}
package CucumberOptions;
import org.testng.annotations.Test;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(features="src/test/resources/features",
glue = "StepDefinitions",
tags = "@errorLLM",
plugin= {"pretty", "html:target/cucumber-reports.html", "json:target/cucumber-reports.json"}
)
@Test
public class TestRunner extends AbstractTestNGCucumberTests {
}
package Resources;
import POJO.PromptPOJO;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import java.io.*;
public class TestData {
PromptPOJO prompt = new PromptPOJO();
public String prompt(String input){
prompt.setModel("llama3.2:latest");
prompt.setStream(false);
prompt.setPrompt(input);
return prompt.toString();
}
public String errorPrompt(String error, String file) throws IOException {
String content = formatErrorMessage(error);
System.out.println(content);
writeStringToFile(content, file);
prompt.setModel("llama3.2:latest");
prompt.setStream(false);
prompt.setPrompt(readFromFile(file));
return prompt.toString();
}
public void writeStringToFile(String content, String fileName) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(content);
writer.close();
System.out.println("Content written to file " +fileName);
}
public String readFromFile(String fileName) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
StringBuilder prompt = new StringBuilder();
String line;
while ((line= reader.readLine())!= null){
prompt.append(line).append("\n");
}
reader.close();
System.out.println("Read from file: " + fileName);
return prompt.toString().trim();
}
public static String formatErrorMessage(String errorMessage) {
String formattedString = errorMessage.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t");
return formattedString;
}
public static String getJsonPath(Response response, String key) {
String resp = response.asString();
JsonPath js = new JsonPath(resp);
return js.get(key).toString();
}
public static String convertStringToBulletPoints(String llmResponse) {
llmResponse = llmResponse.trim();
if (llmResponse.isEmpty()) {
return "No content to convert.";
}
String[] sentences = llmResponse.split("(?<=\\. )\\s*");
StringBuilder bulletPoints = new StringBuilder();
for (String sentence : sentences) {
String trimmedSentence = sentence.trim();
if (!trimmedSentence.isEmpty()) {
bulletPoints.append("• ").append(trimmedSentence.trim()).append("\n");
}
}
return bulletPoints.toString();
}
public static String convertToBulletPoints(String llmResponse) {
llmResponse = llmResponse.trim();
if (llmResponse.isEmpty()) {
return "No content to convert.";
}
StringBuilder bulletPoints = new StringBuilder();
StringBuilder sentence = new StringBuilder();
for (int i = 0; i < llmResponse.length(); i++) {
char currentChar = llmResponse.charAt(i);
sentence.append(currentChar);
if (currentChar == '.') {
String sentenceStr = sentence.toString().trim();
if (!sentenceStr.isEmpty()) {
bulletPoints.append("• ").append(sentenceStr).append("\n");
}
sentence.setLength(0);
}
}
String remainingSentence = sentence.toString().trim();
if (!remainingSentence.isEmpty()) {
bulletPoints.append("• ").append(remainingSentence).append("\n");
}
return bulletPoints.toString();
}
public static void main(String[] args) throws IOException {
}
}
package StepDefinitions;
import io.cucumber.java.After;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
public class ExceptionHandling {
private WebDriver driver;
@Given("the ecom website")
public void the_ecom_website() {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
driver.get("https://www.tpointtech.com/");
driver.manage().window().maximize();
}
@When("the homepage opens verify it")
public void the_homepage_opens_verify_it() {
String expectedTitle = "Tpoint Tech - Free Online Tutorials conveyed";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle, "Page title is not correct");
}
@Then("click on the course selenium")
public void click_on_the_course_selenium() {
WebElement selenium = driver.findElement(By.xpath("//ul[@class='nav']//a[text()=' Selenium']"));
selenium.click();
}
@Then("verify navigated to course page")
public void verify_navigated_to_course_page() {
String expectedUrl = "https://www.javatpoint.com/selenium-tutorial";
String currentUrl = driver.getCurrentUrl();
if (currentUrl.contains(expectedUrl)) {
System.out.println("Successfully navigated to the Selenium course page.");
} else {
System.out.println("Failed to navigate to the Selenium course page.");
}
}
@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
package StepDefinitions;
import Resources.TestData;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.restassured.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.testng.Assert;
import java.io.IOException;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.notNullValue;
@Slf4j
public class PromptCall {
private Response response;
TestData data = new TestData();
String requestBody;
@Given("LLM model body with {}")
public void llmModelBodyWith(String prompt) {
log.info("Creating request body with prompt: {}", prompt);
requestBody = data.prompt(prompt);
System.out.println(requestBody);
}
@When("user calls prompt API using POST method")
public void userCallsPromptAPIUsingPOSTMethod() {
log.info("Serialized request body: {}", requestBody);
System.out.println("----------> "+requestBody);
response = given().log().all().contentType("application/json")
.body(requestBody).when()
.post("http://localhost:11434/api/generate");
System.out.println(response.getBody().asString());
}
@Then("user verifies status code in response")
public void thenUserVerifiesStatusCode() {
response.then().statusCode(200);
}
@And("user verifies response attribute from response")
public void andUserVerifiesResponseAttribute() {
response.then().body("response", notNullValue());
String responseBody = response.getBody().asString();
Assert.assertNotNull(responseBody);
System.out.println(responseBody);
log.info("Response Body: {}", responseBody);
}
@Given("LLM model body from a file")
public void llm_model_body_from_a_file() throws IOException {
String error ="error is java.lang.AssertionError: Page title is not correct expected [Tpoint Tech - Free Online Tutorials conveyed] but found [Tpoint Tech - Free Online Tutorials] and the method where the error is public void the_homepage_opens_verify_it() {\n" +
" String expectedTitle = \"Tpoint Tech - Free Online Tutorials conveyed\";\n" +
" String actualTitle = driver.getTitle();\n" +
" Assert.assertEquals(actualTitle, expectedTitle, \"Page title is not correct\");\n" +
" } how to resolve the above issue give one perfect solution rather than multiple solutions";
String fileName = "ExceptionPrompt.txt";
requestBody = data.errorPrompt(error, fileName);
System.out.println(requestBody);
}
@Then("print the LLM response attribute in a formatted way")
public void print_the_llm_response_attribute_in_a_formatted_way() {
response.then().body("response", notNullValue());
response.getBody().asString();
String llmResponse = TestData.getJsonPath(response, "response");
String formattedResponse = TestData.convertStringToBulletPoints(llmResponse);
System.out.println(formattedResponse);
}
}
package StepDefinitions;
public class duplicatePrompt {
}
Feature: Create an exception in the code
Scenario: Validate LLM handing the exception
Given the ecom website
When the homepage opens verify it
Then click on the course selenium
And verify navigated to course page
Feature: Validating the LLM response with same prompt
Scenario: LLM validating a error from console
Given LLM model body from a file
When user calls prompt API using POST method
Then user verifies status code in response
And print the LLM response attribute in a formatted way
Feature: API testing with LLM model
Scenario Outline: Testing the LLM model API with different prompts
Given LLM model body with <prompt>
When user calls prompt API using POST method
Then user verifies status code in response
And user verifies response attribute from response
Examples:
| prompt |
| How to create a plugin in Intellij console |
# | how to test a LLM model |
# | Generate AI robot |
@errorLLM
Scenario: LLM validating a error from console
Given LLM model body from a file
When user calls prompt API using POST method
Then user verifies status code in response
And print the LLM response attribute in a formatted way
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment