标签: rest-assured-jsonpath

通过RestAssured中的JsonPath访问匿名数组的元素

我从一个服务返回的JSON中有一个匿名数组,如:

[
  {"foo":1, "bar":2 , "baz":3 },
  {"foo":3, "bar":4 , "baz":5 }
]
Run Code Online (Sandbox Code Playgroud)

我如何访问bar元素,例如

expect().body("$[*].bar", hasItems(2,4)) 
Run Code Online (Sandbox Code Playgroud)

我尝试了一些我在这里找到的可能性以及StefanGössnerJsonPath页面上的一些可能性,但无论我尝试什么,我都会遇到异常.我的问题似乎直接来自试图访问该项目列表.

json rest-assured rest-assured-jsonpath

27
推荐指数
2
解决办法
2万
查看次数

如何使用java搜索/查找JSON

我从下面有一个下面的JSON字符串我想在JSON字符串中查找/搜索条件.

1).查找存在的键数.2).获取给定键的值(如果我们有数组)

{
"store": {
    "book": [
        {
            "category": "reference",
            "author": "Nigel Rees",
            "title": "Sayings of the Century",
            "price": 8.95
        },
        {
            "category": "fiction",
            "author": "Evelyn Waugh",
            "title": "Sword of Honour",
            "price": 12.99
        },
        {
            "category": "fiction",
            "author": "Herman Melville",
            "title": "Moby Dick",
            "isbn": "0-553-21311-3",
            "price": 8.99
        },
        {
            "category": "fiction",
            "author": "J. R. R. Tolkien",
            "title": "The Lord of the Rings",
            "isbn": "0-395-19395-8",
            "price": 22.99
        }
    ],
    "bicycle": {
        "color": "red",
        "price": 19.95
    }
},
 "expensive": 10
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找像Groovy …

java simplejson rest-assured rest-assured-jsonpath

8
推荐指数
1
解决办法
2万
查看次数

REST-Assured - 用于获取值列表的JsonPath

我正在使用REST-Assured来测试一些RESTful Web服务.这是我的JSON:

{
    "status":true,
    "responseData":{
        "orderLevelReasons":[
            {
                "reasons":[
                    {
                        "reasonId":"129cfea8-b022-4dc8-9811-222a324f46aa",
                        "reasonName":"COD Amount Mismatch"
                    },
                    {
                        "reasonId":"a881fd5c-626e-438c-8026-646aa2a19098",
                        "reasonName":"Gave wrong information"
                    },
                    {
                        "reasonId":"543d438a-88cc-487c-86e4-19eecefa9ca7",
                        "reasonName":"Late delivery"
                    },
                    {
                        "reasonId":"080cd7c1-7a37-48ad-9090-57286d93ea41",
                        "reasonName":"Parcel not received"
                    },
                    {
                        "reasonId":"5ca3d9b4-0fa2-49da-a534-a6f2e7eccc07",
                        "reasonName":"Staff did not inform about the parcel arrival"
                    }
                ],
                "issueName":"ISSUE TYPE 1",
                "issueId":"0c2c37a6-62b6-4c28-ab6c-566487d045bd",
                "hint":""
            },
            {
                "reasons":[
                    {
                        "reasonId":"129cfea8-b022-4dc8-9811-222a324f46aa",
                        "reasonName":"COD Amount Mismatch"
                    },
                    {
                        "reasonId":"14975b5d-23fb-4735-8082-2e02d6335788",
                        "reasonName":"Data issue"
                    },
                    {
                        "reasonId":"7e6e8446-3774-4589-9171-8e7ab0a7f73b",
                        "reasonName":"Delivery BOY did not inform before delivering"
                    },
                    {
                        "reasonId":"543d438a-88cc-487c-86e4-19eecefa9ca7",
                        "reasonName":"Late delivery"
                    },
                    {
                        "reasonId":"080cd7c1-7a37-48ad-9090-57286d93ea41",
                        "reasonName":"Parcel …
Run Code Online (Sandbox Code Playgroud)

java jsonpath rest-assured rest-assured-jsonpath

7
推荐指数
1
解决办法
2295
查看次数

org.apache.http.ConnectionClosedException:块编码消息正文的提早结束:预期关闭块

我正在尝试RestAssured并写了以下语句-

String URL = "http://XXXXXXXX";
Response result = given().
            header("Authorization","Basic xxxx").
            contentType("application/json").
            when().
            get(url);
JsonPath jp = new JsonPath(result.asString());
Run Code Online (Sandbox Code Playgroud)

关于上一个声明,我收到以下异常:

org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected

我的响应中返回的标头是:

Content-Type ? application/json; qs=1 Date ? Tue, 10 Nov 2015 02:58:47 GMT Transfer-Encoding ? chunked

任何人都可以指导我解决此异常,并在缺少任何内容或任何不正确的实现时指出我。

apache http chunked-encoding rest-assured rest-assured-jsonpath

6
推荐指数
2
解决办法
2万
查看次数

使用 Selenium 捕获网络 XHR 日志(带参数的请求/响应)

我尝试使用 Selenium webdriver 捕获通常显示 Request(MethodType, Headers, parameters) 和 Response 的 Network XHR 日志(chrome 浏览器),但我只能获取客户端发送到服务器的 api 请求(不带参数),而在搜索时我发现下面的代码,它只提供我 apis 请求:-

LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
          for (LogEntry entry : logEntries) {
            System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage())
}
Run Code Online (Sandbox Code Playgroud)

但我还想获得客户端(浏览器)发送到服务器的所有参数以及响应。*相同的功能如何适用于 Firefox。

提前致谢!!

javascript java selenium selenium-webdriver rest-assured-jsonpath

6
推荐指数
1
解决办法
7681
查看次数

导入 io.restassured.RestAssured 无法解析

嗨,我在使用放心 4.1.1 时无法解决错误。我的 Eclipse IDE 中的库。我在 pom.xml 文件中添加了放心库,但错误仍未解决。

我尝试从https://mvnrepository.com/artifact/io.rest-assured/rest-assured/4.1.1重新导入放心库 但仍然不起作用

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RestAssuredTutorial</groupId>
<artifactId>RestAssuredTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
  <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>4.1.1</version>
      <scope>test</scope>
  </dependency>
 <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json- 
 simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
 </dependency>

 </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

导入io无法解析

java dependencies pom.xml rest-assured rest-assured-jsonpath

6
推荐指数
1
解决办法
8339
查看次数

与 RestAssured 保持会话

放心如何设置会话属性?在我的应用程序代码中,我们有这样的东西

String userId= request.getSession().getAttribute("userid")

如何在这里将 userId 设置为会话属性(在放心的测试用例中)?

如何为所有请求(多个后续请求)维护相同的会话?

当我发送多个请求时,它会将每个请求视为新请求,并且会话从服务器端变得无效,我想在后续调用之间保持会话。

我尝试在 cookie 中设置 jsessionid 并在第二个请求中发送它,但是当我在服务器端调试时,它没有加载创建的会话,而是创建不同的会话,因此它不显示该属性当我第一次发送请求时,我已经在会话中设置了。

当我尝试使用直接 HttpClient 进行相同操作时,它可以工作,而与 RestAssured 相同,它不起作用。

使用 HttpClient 的代码是这样的

HttpClient httpClient = util.getHttpClient(); 
Run Code Online (Sandbox Code Playgroud)

//第一个请求

HttpResponse response=httpClient.execute(postRequest); 
Run Code Online (Sandbox Code Playgroud)

从响应中我提取了 jessionid 并将其设置在第二个请求中

HttpGet getRequest = new HttpGet(Client.endPointUrl);
 getRequest.addHeader("content-type", "application/json");
 getRequest.addHeader("accept", "application/json");
 getRequest.addHeader("Origin", Client.endPointUrl);
 getRequest.addHeader("Referer", Client.endPointUrl);
 getRequest.addHeader("Auth-Token", authToken);
 getRequest.addHeader("Set-Cookie", jsessionId);
Run Code Online (Sandbox Code Playgroud)

//设置我从响应中提取的jessionid后的第二个请求

HttpResponse eventsResponse = httpClient.execute(getRequest); 
Run Code Online (Sandbox Code Playgroud)

上面的代码工作得很好,我得到了预期的响应。一项观察是我使用相同的 httpClient 对象来调用这两个请求。

如果我使用 RestAssured 尝试相同的操作,它不起作用。

RestAssured.baseURI = "http://localhost:8080";
 Response response=RestAssured.given().header("Content-Type","application/json").
                     header("Origin","http://localhost:8080").
                     header("Referer","http://localhost:8080").
                     body("{"+  
                           "\"LoginFormUserInput\":{"+  
                            "\"username\":\"test\","+
                             "\"password\":\"password\""+
                          "}"+
                    "}")
                     .when().post("/sample/services/rest/validateLogin").then().extract().response();

 JsonPath js=Util.rawToJson(response);
 String sessionId=js.get("sessionID");
 System.out.println(sessionId);

 for …
Run Code Online (Sandbox Code Playgroud)

rest-assured rest-assured-jsonpath

5
推荐指数
1
解决办法
7962
查看次数

在 RestAssured 中关闭连接的理想方式?

我正在使用 RestAssured 进行 API 自动化。我们有一个庞大的应用程序,有如此多的 api 调用。那么,在正确收到响应后关闭连接的更好方法是什么?这样我就不会担心连接泄漏了。

下面是我的代码:

RestAssuredConfig restAssuredConfig = RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)).httpClient(HttpClientConfig.httpClientConfig().setParam("http.connection.timeout", 60000).setParam("http.socket.timeout", 300000).setParam("http.conn-manager.timeout", 300000));

RequestSpecification requestSpecification = RestAssured.given().config(restAssuredConfig).relaxedHTTPSValidation();
Run Code Online (Sandbox Code Playgroud)

我应该像下面这样指定 ConnectionConfig 吗?

RestAssured.config().connectionConfig(ConnectionConfig.connectionConfig().closeIdleConnectionsAfterEachResponse());
Run Code Online (Sandbox Code Playgroud)

我应该使用 closeIdleConnectionsAfterEachResponse() 方法吗?或 closeIdleConnectionsAfterEachResponseAfter(longidleTime, TimeUnit timeUnit) 方法?

请帮忙。提前致谢。

rest httpclient rest-assured apache-httpclient-4.x rest-assured-jsonpath

5
推荐指数
0
解决办法
2542
查看次数

如何使用 JSON 响应通过 Rest Assured 基于正文项进行断言?

如何使用放心的 .body() 方法在“描述”数组中断言我的属性。

例子:

 .body ("[0] .userType", equalTo (1)); // error 
Run Code Online (Sandbox Code Playgroud)

这是我当前想要断言的 JSON 数据:

{
"validSession": true,
"value": "xxx",
"description": [
    {
        "userType": 1,
        "userTypeDescription": "xxx",
        "uname": "xx",
        "distributorId": 1
    }
]}
Run Code Online (Sandbox Code Playgroud)

java rest rest-assured web-api-testing rest-assured-jsonpath

4
推荐指数
1
解决办法
2万
查看次数

请放心:如何以字符串形式返回 JSON 响应?(爪哇)

如何使用我的代码以字符串形式返回 JSON 响应?

目的:我想在getAccessToken()它从 json 响应正文获取 accessToken 后调用,并且需要将其作为 a 返回String以在其他方法中使用。

我试图获得的响应示例:

"accessToken" : "The ID I need from here"
Run Code Online (Sandbox Code Playgroud)

代码 :

private String apiAccessToken;

public JsonPath getAccessToken() {
    JsonPath jsonPath = given().header("X-API-KEY", Config.API_KEY).header("session", this.sessionID)
            .header("username", this.userNameId).queryParam("code", verifiedCode).log().all()
            .get(baseUri + basePath + "/vm1/verifyCode").then().log().all().extract().jsonPath();

    this.apiAccessToken = jsonPath.get("accessToken");
    return new JsonPath(apiAccessToken);
}
Run Code Online (Sandbox Code Playgroud)

[已添加 - 显示我如何使用下面评论中的解决方案]

我如何调用此方法的示例

public static String getToken(String key) {
    String res = given()
            .header("X-API-KEY", Config.API_KEY)
            .header("session", this.SessionId)
            .header("username", this.UserName)
            .queryParam("code", verifiedCode)
            .log().all()
            .get(baseUri …
Run Code Online (Sandbox Code Playgroud)

java rest-assured rest-assured-jsonpath

4
推荐指数
1
解决办法
2万
查看次数