CAPTCHA - I read it through OpenAI

CAPTCHA - I read it through OpenAI

This is 100% not an AI generated article.

What was planned?

To practice TestNG using TOOLSQA 's tutorials so, we don't forget the framework basics.

What actually is achieved?

When I was trying to launch Amazon site, it directly led me to the Captcha page and that's when the curiosity inside this automation tester, who is trying out OpenAI for anything and everything was poked.

I started exploring the OpenAI playground with an image of Captcha with me and tried to get the values out of it and it worked.

So I wanted to implement this now with Java.

I started calling openai through the RestAssured code.

There were 400 and 401's encountered and finally came out of them after resolving all the errors in the request.

And finally my code was able to read the CAPTCHA from the site using OpenAI.

Page tried for trying out my experiment: www.amazon.com

Created a OpenAIUtils.java

package org.example.AIIntegration;
import java.util.HashMap;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;


public class OpenAIUtils {
    private static final String OPENAI_API_KEY = "Your OpenAI Key";
    private static final String OPENAI_ENDPOINT = "https://api.openai.com/v1/chat/completions";

    public String getCaptchaValueFromOpenAI(String imageURL) throws Exception {
        String captchaValue="";
        String Prompt="Give only the captcha value from the image";
        try
        {
            captchaValue=CallOpenAI(Prompt,imageURL);
        }
        catch(Exception e){
        }
        finally {
            return captchaValue;
        }

    }



    public String CallOpenAI(String Prompt,String imageURLFromSRC)
    {
//        // Create the JSON request body
// Creating the JSON payload using JSONSimple objects
        JSONObject payload = new JSONObject();

        // Adding "model" key-value
        payload.put("model", "gpt-4o-mini");

        // Creating the "messages" array
        JSONArray messages = new JSONArray();

        // Creating the first message object
        JSONObject message = new JSONObject();
        message.put("role", "user");

        // Creating the "content" array
        JSONArray content = new JSONArray();

        // Adding the first content object (text)
        JSONObject textContent = new JSONObject();
        textContent.put("type", "text");
        textContent.put("text", Prompt);
        content.add(textContent);

        // Adding the second content object (image_url)
        JSONObject imageContent = new JSONObject();
        imageContent.put("type", "image_url");

        // Creating the image_url object
        JSONObject imageUrl = new JSONObject();
        imageUrl.put("url", imageURLFromSRC);
        imageContent.put("image_url", imageUrl);

        content.add(imageContent);

        // Adding the content array to the message object
        message.put("content", content);

        // Adding the message object to the messages array
        messages.add(message);

        // Adding the messages array to the payload
        payload.put("messages", messages);

        // Adding "max_tokens" key-value
        payload.put("max_tokens", 2000);
                Response response=RestAssured.given().
                header("Authorization","Bearer "+OPENAI_API_KEY).
                header("Content-Type","application/json").
                body(payload.toJSONString())
            .post(OPENAI_ENDPOINT);
        if (response.getStatusCode() == 200) {
            System.out.println(response.getBody().jsonPath().getString("choices[0].message.content"));
        } else {
            System.out.println("Error: " + response.getStatusCode() + " - " + response.getBody().asString());
        }
        return response.getBody().jsonPath().getString("choices[0].message.content");
    }

}        

Complete code is available in the testNg branch of my code base https://github.com/krishnapriya21/AutomationFrameworkUi/tree/testNg





CHARANJIT SINGH

Quality Assurance analyst | Automation testing | Manual Testing | API Testing | ISTQB Certified | Requirement Analysis | Selenium and Java | Agile Scrum | UAT Testing | JIRA | Micro soft Azure

8 个月

Thank you for sharing Shree Krishna Priya J

Saimanikandan V

Automation Test Engineer

8 个月

Interesting!

Shree Krishna Priya J

Mom | When I am away AI and automation work for me | Accessibility tester/enabler | Created Test Case Generator | Entwinning AI and Automation at FEFundinfo | ISTQB AI Testing Certified |

8 个月
回复

要查看或添加评论,请登录

Shree Krishna Priya J的更多文章

社区洞察

其他会员也浏览了