AppiumTestDistribution / appium-interceptor-plugin

Appium 2.0 plugin to mock api calls for android apps
27 stars 11 forks source link

Java Mock Example not working #25

Closed anuj3011 closed 5 months ago

anuj3011 commented 6 months ago
`             JSONObject customObject = new JSONObject();
                customObject.put("url","/api/users?.*");
                customObject.put("responseBody","{\n" +
                        "  \"page\": 2,\n" +
                        "  \"per_page\": 6,\n" +
                        "  \"total\": 12,\n" +
                        "  \"total_pages\": 2,\n" +
                        "  \"data\": [\n" +
                        "    {\n" +
                        "      \"id\": 7,\n" +
                        "      \"email\": \"michael.test@reqres.in\",\n" +
                        "      \"first_name\": \"Michael\",\n" +
                        "      \"last_name\": \"Lawson\",\n" +
                        "      \"avatar\": \"https://reqres.in/img/faces/7-image.jpg\"\n" +
                        "    }\n" +
                        "  ]\n" +
                        "}");
                JSONObject parentJson = new JSONObject();
                parentJson.put("config", customObject);
                ((JavascriptExecutor)DriverManager.getDriver()).executeScript("interceptor: addMock",new JSONObject(parentJson));
`

Using the exact same java code provided in the example, I'm getting java.lang.IllegalArgumentException: Argument is of an illegal type: org.json.JSONObject at org.openqa.selenium.remote.internal.WebElementToJsonConverter.apply(WebElementToJsonConverter.java:83)

sudharsan-selvaraj commented 6 months ago

Im unable to reproduce the issue on my end. If this is blocking, you can try the below example

Map<String, String> config = new HashMap();
config.put("url", "/api/users?.*");
config.put("responseBody","{\n" +
                        "  \"page\": 2,\n" +
                        "  \"per_page\": 6,\n" +
                        "  \"total\": 12,\n" +
                        "  \"total_pages\": 2,\n" +
                        "  \"data\": [\n" +
                        "    {\n" +
                        "      \"id\": 7,\n" +
                        "      \"email\": \"michael.test@reqres.in\",\n" +
                        "      \"first_name\": \"Michael\",\n" +
                        "      \"last_name\": \"Lawson\",\n" +
                        "      \"avatar\": \"https://reqres.in/img/faces/7-image.jpg\"\n" +
                        "    }\n" +
                        "  ]\n" +
                        "}");
((JavascriptExecutor)DriverManager.getDriver()).executeScript("interceptor: addMock",config);
PrakashBalraj94 commented 6 months ago

Hi @sudharsan-selvaraj, While using the hapmap I'm getting the below error.

Could you please help on this issue.

Error: org.openqa.selenium. InvalidArgumentException: The following required parameter is missing: ["config"]

Known required parameters are: ["config"]

You have provided none

sudharsan-selvaraj commented 6 months ago

@PrakashBalraj94 Can you try with the below example?

Map<String, String> config = new HashMap();
config.put("url", "/api/users?.*");
config.put("responseBody","{\n" +
                        "  \"page\": 2,\n" +
                        "  \"per_page\": 6,\n" +
                        "  \"total\": 12,\n" +
                        "  \"total_pages\": 2,\n" +
                        "  \"data\": [\n" +
                        "    {\n" +
                        "      \"id\": 7,\n" +
                        "      \"email\": \"michael.test@reqres.in\",\n" +
                        "      \"first_name\": \"Michael\",\n" +
                        "      \"last_name\": \"Lawson\",\n" +
                        "      \"avatar\": \"https://reqres.in/img/faces/7-image.jpg\"\n" +
                        "    }\n" +
                        "  ]\n" +
                        "}");
 Map<String,Map<String, String>> args = new HashMap("config", config);                       
((JavascriptExecutor)DriverManager.getDriver()).executeScript("interceptor: addMock", args);
PrakashBalraj94 commented 5 months ago

@sudharsan-selvaraj Thanks for your response!

The above works find after adding the below line, Map<String, String> config = new HashMap(); config.put("url", "/api/users?.*"); config.put("responseBody","{\n" + " \"page\": 2,\n" + " \"per_page\": 6,\n" + " \"total\": 12,\n" + " \"total_pages\": 2,\n" + " \"data\": [\n" + " {\n" + " \"id\": 7,\n" + " \"email\": \"michael.test@reqres.in\",\n" + " \"first_name\": \"Michael\",\n" + " \"last_name\": \"Lawson\",\n" + " \"avatar\": \"https://reqres.in/img/faces/7-image.jpg\"\n" + " }\n" + " ]\n" + "}"); Map<String,Map<String, String>> args = new HashMap();
// Added below line to the existing code args.put("config", config);
((JavascriptExecutor)DriverManager.getDriver()).executeScript("interceptor: addMock", args);