Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何在api测试中传递RESTAPI调用的会话id?_Java_Rest_Servlets_Selenium Webdriver_Web Api Testing - Fatal编程技术网

Java:如何在api测试中传递RESTAPI调用的会话id?

Java:如何在api测试中传递RESTAPI调用的会话id?,java,rest,servlets,selenium-webdriver,web-api-testing,Java,Rest,Servlets,Selenium Webdriver,Web Api Testing,对于UI自动化测试,我需要进行RESTAPI调用,以获得响应。我正在通过request.setheader将会话id传递到标头中,但收到的响应如下: {"result":{"success":false,"httpCode":200,"errorCode":"INVALID_SESSION_ID","errorMessage":"Session ID not provided... Kindly Login !!"}} 我已经尝试过显示会话id,但它似乎很好,我不明白的是,为什么它没有被传递到

对于UI自动化测试,我需要进行RESTAPI调用,以获得响应。我正在通过request.setheader将会话id传递到标头中,但收到的响应如下:

{"result":{"success":false,"httpCode":200,"errorCode":"INVALID_SESSION_ID","errorMessage":"Session ID not provided... Kindly Login !!"}}
我已经尝试过显示会话id,但它似乎很好,我不明白的是,为什么它没有被传递到API。代码如下:

    HttpClient httpClient = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet(getUrl);

            SessionId SessionId = ((FirefoxDriver)driver).getSessionId();
             request.setHeader("Cookie", SessionId.toString());
             HttpResponse response = httpClient.execute(request);

               InputStream ips  = response.getEntity().getContent();
               BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
               if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK)
               {
                   throw new Exception(response.getStatusLine().getReasonPhrase());
               }
               StringBuilder sb = new StringBuilder();
               String s;
               while(true )
               {
                   s = buf.readLine();
                   if(s==null || s.length()==0)
                       break;
                   sb.append(s);

               }
               buf.close();
               ips.close();
               System.out.println("After parsing: "+sb.toString());
输出: 解析后:强文本

{"result":{"success":false,"httpCode":200,"errorCode":"INVALID_SESSION_ID","errorMessage":"Session ID not provided... Kindly Login !!"}}
以下是解决方案: 已更改,
setHeader(“Cookie”,SessionId.toString()); 到 setHeader(“sessionId”,sessionId.toString()); API团队提供的API文档中提到了这一点。
然后,会话ID作为前一个API调用的输出提供,需要在后续API调用中传递。我对此一无所知,因为我是API测试新手。希望我的回答能帮助类似情况的人

对于UI测试,您不能执行api调用。嘲笑他们。UI测试不能依赖网络。对于UI测试中的少数测试用例,我需要api的响应。这就是功能测试的基本原因。