如果我想提取JSON响应值并避免在开始时使用静态变量,如何在重新介绍中使用hashmap?

发布于 2025-01-22 19:30:38 字数 1278 浏览 0 评论 0原文

现在,我使用JSON路径函数来获取JSON响应值,例如PlaceID,ProfileId,然后将其声明为静态,因为我需要在clickprofile()和clickplace()函数中使用这些变量。展望未来,我将拥有很多值,例如PlaceID,ProfileId,ContentId等,我想避免在开始时声明为静态字符串的值。取而代之的是,我想在此处使用地图概念,并想从Framework实用程序中使用它。 

我是哈希图的新手。有人可以在这里帮助我在这里实施地图概念吗?

public static String placeid;

public static String profileid;

 public void extractplaceId()
 {
  placeid = getJsonPath("results.place_id");
  System.out.println(placeid);
 }

 public void extractpleId()
 {
  profileid = getJsonPath("results.profile_id");
  System.out.println(profileid);
 }

 public Response clickProfile() throws IOException
 {
  config.setProperty("APIendPoints.properties");
  PROFILE_URL = config.getConfig().getProperty("CLICK_PROFILE") + profileid + ".json";
  response = requestSpec.when().get(PROFILE_URL);
  return response;
 }

 public Response clickPlace() throws IOException
 {
  config.setProperty("APIendPoints.properties");
  PLACE_URL = config.getConfig().getProperty("CLICK_PLACE") + placeid + ".json";
  response = requestSpec.when().get(PLACE_URL);
  return response;
 }

框架实用程序:

 public String getJsonPath(String key) 
 {
  String resp = response.asString();
  JsonPath js = new JsonPath(resp);
  return js.get(key).toString();
 }

Right now I am using the JSON path function to get the JSON response values like placeid, profileid and then am declaring that as static, because I need to use those variables in clickProfile() and clickPlace() functions. Going forward I will have a lot of values like placeid, profileid, contentid etc and I want to avoid those declaring as static String at the beginning. Instead I want to use the Maps concept here going forward and want to use it from framework Utility. 

I am new to HashMaps. Can someone help me here in implementing the Maps concept here?

public static String placeid;

public static String profileid;

 public void extractplaceId()
 {
  placeid = getJsonPath("results.place_id");
  System.out.println(placeid);
 }

 public void extractpleId()
 {
  profileid = getJsonPath("results.profile_id");
  System.out.println(profileid);
 }

 public Response clickProfile() throws IOException
 {
  config.setProperty("APIendPoints.properties");
  PROFILE_URL = config.getConfig().getProperty("CLICK_PROFILE") + profileid + ".json";
  response = requestSpec.when().get(PROFILE_URL);
  return response;
 }

 public Response clickPlace() throws IOException
 {
  config.setProperty("APIendPoints.properties");
  PLACE_URL = config.getConfig().getProperty("CLICK_PLACE") + placeid + ".json";
  response = requestSpec.when().get(PLACE_URL);
  return response;
 }

Framework Utility:

 public String getJsonPath(String key) 
 {
  String resp = response.asString();
  JsonPath js = new JsonPath(resp);
  return js.get(key).toString();
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2025-01-29 19:30:38

我认为您想要一包所有变量,以后要保存和检索。我下面的POC仅适用于单线线程,不确定多个线程。

  1. 一个添加/获取/编辑/删除变量的类。它包裹了一张地图以实现困难。
public class EnvironmentVariable {
    private static final Map<String, Object> variables  = new HashMap<>();

    public static void add(String key, Object value) {
        variables.put(key, value);
    }

    public static Object get(String key) {
        return variables.get(key);
    }

    public static void edit(String key, Object value) {
        variables.put(key, value);
    }

    public static Object remove(String key) {
        return variables.remove(key);
    }
}
  1. extractutil类提供功能提取extractAndSave
public class ExtractUtils {

    public static Object extractFrom(Response res, String query){
        return res.jsonPath().get(query);
    }

    public static void extractAndSave(Response res, String query, String key) {
        Object value = res.jsonPath().get(query);
        EnvironmentVariable.add(key, value);
    }
}
  1. 测试客户端,
public class TestClient {

    public Response test() {
        return given().get("https://auto-test-challenges.herokuapp.com/challenge3restassured");
    }

    @Test
    void name() {
        Response res = test();
        ExtractUtils.extractAndSave(res, "data.key1.number", "key1_number");
        given().log().all().get("https://postman-echo.com/get?a=" + EnvironmentVariable.get("key1_number"));
    }

    @Test
    void name2() {
        Response res = test();
        int key2_number = (Integer) ExtractUtils.extractFrom(res, "data.key2.number");
        EnvironmentVariable.add("key2_number", key2_number);
        given().log().all().get("https://postman-echo.com/get?a=" + EnvironmentVariable.get("key2_number"));
    }
}

因为我希望我的地图可以保存任何对象,因此我使用map&lt; string,object&gt;。它需要铸造以从地图中获取值,例如int key2_number =(integer)extractutils.extractfrom(res,“ data.key.2.number”);

I assume that you want something like a pack of all variables that you want to save and retrieve later. My POC below just work correctly for single thread, not sure about multiple threads.

  1. A class to add/get/edit/remove variables. It wraps a Map to do the trick.
public class EnvironmentVariable {
    private static final Map<String, Object> variables  = new HashMap<>();

    public static void add(String key, Object value) {
        variables.put(key, value);
    }

    public static Object get(String key) {
        return variables.get(key);
    }

    public static void edit(String key, Object value) {
        variables.put(key, value);
    }

    public static Object remove(String key) {
        return variables.remove(key);
    }
}
  1. ExtractUtil class to provide functions extract or extractAndSave
public class ExtractUtils {

    public static Object extractFrom(Response res, String query){
        return res.jsonPath().get(query);
    }

    public static void extractAndSave(Response res, String query, String key) {
        Object value = res.jsonPath().get(query);
        EnvironmentVariable.add(key, value);
    }
}
  1. Test client
public class TestClient {

    public Response test() {
        return given().get("https://auto-test-challenges.herokuapp.com/challenge3restassured");
    }

    @Test
    void name() {
        Response res = test();
        ExtractUtils.extractAndSave(res, "data.key1.number", "key1_number");
        given().log().all().get("https://postman-echo.com/get?a=" + EnvironmentVariable.get("key1_number"));
    }

    @Test
    void name2() {
        Response res = test();
        int key2_number = (Integer) ExtractUtils.extractFrom(res, "data.key2.number");
        EnvironmentVariable.add("key2_number", key2_number);
        given().log().all().get("https://postman-echo.com/get?a=" + EnvironmentVariable.get("key2_number"));
    }
}

Because I want my map can save any object, so that I use Map<String,Object>. It requires casting to get value from the map, like int key2_number = (Integer) ExtractUtils.extractFrom(res, "data.key2.number");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文