AppiumTestDistribution / appium-reporter-plugin

Appium2 plugin to generate html report with screenshots.
Apache License 2.0
30 stars 12 forks source link

Can I use this reporter in Appium using Java? #123

Closed Stefankoff closed 1 year ago

Dileep17 commented 1 year ago

Sure you can.

For now hoping below steps would help

  1. Install plugin appium plugin install --source=npm appium-reporter-plugin
  2. Start appium with plugin appium --use-plugins=appium-reporter-plugin
  3. Refer to https://github.com/AppiumTestDistribution/appium-reporter-plugin/blob/main/test/demo/base.js. In java you need to call the api exposed by plugin (as these are custom bindings).
Stefankoff commented 1 year ago

Maybe you have some knowledge in Java how to that? I wrote some methods public static void setTestInfo(String sessionId, String testName, String testStatus, String error) throws IOException, InterruptedException {

` String url = "http://localhost:4723/session/" + sessionId + "/setTestInfo";

    var jsonBody = new HashMap<String,String>();
    jsonBody.put("testName", testName);
    jsonBody.put("testStatus", testStatus);
    jsonBody.put("error", error);

    var client = HttpClient.newHttpClient();
    var request = HttpRequest.newBuilder(URI.create(url))
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(String.valueOf(jsonBody)))
            .build();

    var response = client.send(request, HttpResponse.BodyHandlers.ofString());

}

public static String getReport() throws IOException, InterruptedException {

    String url = "http://localhost:4723/getReport";

    var client = HttpClient.newHttpClient();
    var request = HttpRequest.newBuilder(URI.create(url)).build();

    var response = client.send(request, HttpResponse.BodyHandlers.ofString());

    return String.valueOf(response);

}

public static void deleteReportData() throws IOException, InterruptedException {

    String url = "http://localhost:4723/deleteReportData";
    var client = HttpClient.newHttpClient();
    var request = HttpRequest.newBuilder(URI.create(url)).DELETE().build();

    var response = client.send(request, HttpResponse.BodyHandlers.ofString());
}

public static void createReportFile(String data, String fileName) throws IOException {
    FileWriter fileWriter = new FileWriter(HTML_REPORT_DIR + "/" + fileName + ".html");
    fileWriter.write(data);
    fileWriter.close();
}`

Could you please say what maybe wrong, and how to use this methods in test flow as in your example https://github.com/AppiumTestDistribution/appium-reporter-plugin/blob/main/test/demo/demo.spec.js 
Dileep17 commented 1 year ago

@Stefankoff identified a couple of improvements while coding a sample in java. Fixed them and released a new version.

Java project is added to tests folder appium-reporter-plugin/test/AppiumReportPluginDemo/src/test/java/SampleTest.java

I hope this helps!