TestProject Forum
Powered by leading experts in the test automation community

Rest api request works locally but not on testproject platform

i tried making request RESTfulAPIClient addon ,and java.net.HttpURLConnection but both doesn’t seem to work when running on the test project site
i recieve the follwong error -Failed retrieving results from wrapper

code when using the built in addon

        GetAction getAction;
        ExecutionResult executionresult;
        getAction = RESTfulAPIClient.getAction(ip,"","","","",false);
        executionresult = helper.executeProxy(getAction);
      BooleanCheck.ReporterCheck(helper.getReporter(),executionresult==ExecutionResult.PASSED,"");
        BooleanCheck.ReporterCheck(helper.getReporter(),true,getAction.response);
        //JSONObject myResponse = new JSONObject(getAction.response);
        System.out.println("result after Reading JSON Response");

code when using HttpURLConnection
URL obj = new URL(ip);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod(“GET”);
//add request header
Thread.sleep(3000);
con.setRequestProperty(“User-Agent”, “Mozilla/5.0”);
int responseCode = con.getResponseCode();
System.out.println("\nSending ‘GET’ request to URL : " + ip);
System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        //print in String
        BooleanCheck.ReporterCheck(helper.getReporter(),true," "+response.toString());

Hi @ermiastadese. Thanks for reaching out.

Few moments that is not clear to me :

What is the usage of BooleanCheck.ReporterCheck?
What is the version of the Agent that you are using?
How exactly you uploaded this code back to platform : as Coded test or you created an Add on?

May I ask you to send the code package with that part where you using the REST API addon to see, what can cause the issue?

Here’s my code snippet for example :

Code example
@Test(
        name = "Example",
        version = "1.0"
)
public class Example implements WebTest {
    @Parameter(
            description = "Auto generated application URL parameter",
            defaultValue = "https://google.com",
            direction = ParameterDirection.INPUT
    )
      public String ApplicationURL;


    @Parameter(description = "Responce", direction = ParameterDirection.OUTPUT)
    private String resp;

public ExecutionResult execute(WebTestHelper helper) throws FailureException {
    WebDriver driver = helper.getDriver();
    driver.setTimeout(15000);
    TestReporter report = helper.getReporter();
    boolean booleanResult;
    ExecutionResult executionresult;
    GetAction getAction;

    getAction = RESTfulAPIClient.getAction("https://google.com", "", "", "200", "", false);
    executionresult = helper.executeProxy(getAction);
    report.step("GET 'https://google.com'?'[NONE]'", executionresult == ExecutionResult.PASSED, TakeScreenshotConditionType.Failure);
    resp = getAction.response;
    report.step(resp);

    return ExecutionResult.PASSED;
}
}

More example on our official GitHub page: https://github.com/testproject-io/java-sdk-examples

Also please be sure that you correctly uploading this test as Coded test :

It should implement WebTest interface
It should have @Test annotation before classname (this one less necessary but also important)
Make sure that your test returns Executionresult.

Using the Gradle you need to go to Gradle tasks > build > Jar.
Using Maven: Tasks > build > Package

Example

Kindly looking forward to hear back from you.

hi @Vladyslav thanks for the response

  • the BooleanCheck.ReporterCheck is custom report built in testproject report
    it work with other test
    the agent version is 0.61.10
    but i dont implement webtest interface because my test are for android device
    my full code
Summary

package Tools;

import io.BooleanCheck;

import io.testproject.java.annotations.v2.Test;
import io.testproject.java.sdk.v2.enums.ExecutionResult;
import io.testproject.java.sdk.v2.exceptions.FailureException;
import io.testproject.java.sdk.v2.tests.AndroidTest;
import io.testproject.java.sdk.v2.tests.helpers.AndroidTestHelper;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

@Test(
name = “api”,
version = “1.0”
)

public class Runtest implements AndroidTest {
String ip=“https://jsonplaceholder.typicode.com/posts/1”;
public ExecutionResult execute (AndroidTestHelper helper) throws FailureException {
/*
try {
GetAction getAction;
ExecutionResult executionresult;

        getAction = RESTfulAPIClient.getAction(ip,"","","","",false);
        executionresult = helper.executeProxy(getAction);
        BooleanCheck.ReporterCheck(helper.getReporter(),executionresult==ExecutionResult.PASSED,"");
        BooleanCheck.ReporterCheck(helper.getReporter(),true,getAction.response);
        //JSONObject myResponse = new JSONObject(getAction.response);
        System.out.println("result after Reading JSON Response");
    }catch (Exception e){

        BooleanCheck.ReporterCheck(helper.getReporter(),true,e.getMessage());

    }*/
    try {
        //192.168.42.235
        URL obj = new URL(ip);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        //add request header
        Thread.sleep(3000);

        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + ip);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        //print in String
        BooleanCheck.ReporterCheck(helper.getReporter(),true," "+response.toString());

    }catch (Exception e){

        BooleanCheck.ReporterCheck(helper.getReporter(),true,e.getMessage());

    }


    //BooleanCheck.ReporterCheck(helper.getReporter(),true,       "ac1- "+myResponse.getString("title"));


  /*  try {
        URL obj = new URL(ip);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        //add request header
        Thread.sleep(3000);

        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + ip);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        //print in String
        BooleanCheck.ReporterCheck(helper.getReporter() ,true ,response.toString());
        //System.out.println(response.toString());
        //Read JSON response and print
        JSONObject myResponse = new JSONObject(response.toString());
        System.out.println("result after Reading JSON Response");
    } catch (Exception e) {
        e.printStackTrace();
    }*/
    return ExecutionResult.PASSED;


}

}

Hi @ermiastadese .
I have tried to run your code, but since i have no BooleanCheck method i had to comment it while trying to run.
In end the code was executed without any errors or excecptions.
I suspect that this method for reports that you are using might not work properly when you uploading it to the plarform.
Reporter class should be inititated once per execution method (once per test) and it will not work like a generic for all the tests.
Hope this information will be helpful for you.

If this will not resolve the issue, i would like to have your code in .zip file (which is not yet compiled) , so i’ll be able to test it