【问题标题】:How to use JSON result in selenium webdriver如何在 selenium webdriver 中使用 JSON 结果
【发布时间】:2018-06-20 07:10:15
【问题描述】:

我是自动化测试的初学者。在这里,我想从 json 中获取数据以供 selenium webdriver 使用。我编写了一个 java 类并硬编码了一些东西以获得结果作为尝试。并在控制台面板中获得答案喜欢

{
  "name": "john",
  "location": "jihu",
  "job": [
    {
      "manager": [
        {
          "collegues": [
            "ram",
              "ranma",
               ],
              }
}
}

(不完全是这个)。

现在我想将它用于一个使用 selenium webdriver 的特定应用程序。我该如何继续。请详细帮助我。在此先感谢。

【问题讨论】:

    标签: javascript java json selenium-webdriver


    【解决方案1】:

    从 Selenium 和后端比较测试的角度来看,我的回答相同。您需要使用 GSON、jackson 等一些库将 json 响应解析为 Java Pojo 对象,然后在要与 json 比较的字段上应用断言和用户界面。我想您需要先了解以下主题

    1) 序列化 - 反序列化 API 响应 2) GSON 或 Jackson 解析器 3) RestAssured(提供 API 测试框架) 4) JAVA pojo 的分类和使用。您可能需要它们以某种 Response 类的形式保存您的响应并比较对象。

    【讨论】:

    • 我用过这个,Gson gson = new Gson();字符串 myObjName=gson.toJson(myObj); System.out.println(myObjName);并得到了这个结果。在那之后我应该怎么做......你能告诉我任何完美的网站让我更好地澄清这个问题。
    • 您可以使用以下代码将您的响应映​​射到地图中
    • Map map = gson.fromJson(json.asString(), Map.class);现在您可以使用地图对象来提取您想要的任何节点,并使用断言将其与 UI 元素进行比较
    • 现在我刚刚创建了 json 文件并使用 system. getproperty()。现在它可以工作了。但是谢谢你的信息。我也试试这个。
    【解决方案2】:

    您可以使用这些在线转换器将 json 对象转换为 Java Pojo 类: http://json2java.azurewebsites.net/(我一般用这个)

    https://codebeautify.org/json-to-java-converter

    然后将它们放在一个包中或您希望的同一类中:

    你可以通过下面的例子更好地理解它:

    package com.demo.core;
    
    import com.google.gson.Gson;
    import io.restassured.RestAssured;
    import io.restassured.response.Response;
    
    public class Demo {
        public static void main(String r[]) {
            Response response1 = RestAssured.given().get("http://dummy.restapiexample.com/api/v1/employee/1"); // use your method to get the API response (I have used RestAssured here)
            String json = response1.asString(); // you can use your json response here and convert it into a String
            Gson gson = new Gson();
            Employee emp = gson.fromJson(json, Employee.class);  // The response is now captured in this Employee class
    
            System.out.println(emp.getEmployeeAge());
            System.out.println(emp.getEmployeeName());
            System.out.println(emp.getEmployeeSalary());
        }
    
    }
    
    class Employee {
        private String id;
        private String employee_name;
        private String employee_salary;
        private String employee_age;
        private String profile_image;
    
        public String getId() {
            return this.id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getEmployeeName() {
            return this.employee_name;
        }
    
        public void setEmployeeName(String employee_name) {
            this.employee_name = employee_name;
        }
    
        public String getEmployeeSalary() {
            return this.employee_salary;
        }
    
        public void setEmployeeSalary(String employee_salary) {
            this.employee_salary = employee_salary;
        }
    
        public String getEmployeeAge() {
            return this.employee_age;
        }
    
        public void setEmployeeAge(String employee_age) {
            this.employee_age = employee_age;
        }
    
        public String getProfileImage() {
            return this.profile_image;
        }
    
        public void setProfileImage(String profile_image) {
            this.profile_image = profile_image;
        }
    }
    

    在 Java 类中捕获响应后,您可以使用它们的 getter 方法轻松地断言值,就像我在代码中使用的那样来获取 Employee 类中存在的字段值。

    如果您仍有问题,请告诉我。 如果您的结果包含许多字段,最好将所有转换的 java 类转移到一个单独的包中以获得清晰的代码。

    【讨论】:

    • 感谢您的努力。我试试这个。
    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2017-05-28
    • 2012-10-14
    相关资源
    最近更新 更多