在终端到终端的测试Capture服务器JSON响应 [英] Capture server JSON response in end-to-end test

查看:477
本文介绍了在终端到终端的测试Capture服务器JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个终端到终端的测试,模拟与量角器用户认证。用户感觉到在她的凭据并点击提交按钮。其结果,服务器返回在可用于其他的REST API调用JSON响应一个访问令牌。我想此令牌保存到一个文件中。

I'm writing an end-to-end test that simulates user authentication with Protractor. A user feels in her credentials and clicks a Submit button. As a result, the server returns an access token in a JSON response that can be used for other REST API calls. I'd like to save this token to a file.

有攻克一个GET请求的响应<一个类似的问题href=\"http://stackoverflow.com/questions/25137881/how-to-use-protractor-to-get-the-response-status-$c$c-and-response-text\">here,但我不知道它的发送另一个请求我点击按钮后,一个好主意。

There's a similar question on capturing a response of a GET request here, but I'm not sure it's a good idea to send another request after I click the button.

我怎样才能捕捉按钮后的反应点击?

How can I capture the response after a button click?

推荐答案

下面是我对如何捕捉HTTP响应的想法。的量角器的提供了一种方法 browser.addMockModule()文档) - 它是用来添加自定义的的模块到一个页面,通常用来嘲笑outcoming请求,并提供自定义响应。但我们并不需要模拟请求,这将是不够的,只是听任何从服务器来。它可以与的帮助下角HTTP的拦截 的。的拦截的用于捕捉的请求或响应,并修改它之前,任何需要在到达它的终点。我们可以使用它们来收集关于什么是来自服务器的信息储存,然后让反应前进没有变化。由于该自定义模块和规格测试将在同一页上运行,有关反应的信息可以存储在某种全局属性。然后,点击按钮时,将有可能注入自定义脚本的页面通过来获取所需的响应browser.executeScript()文档)。这里是源:

Here is my idea about how to catch HTTP responses. Protractor provides a method browser.addMockModule() (docs) - it is used to add custom Angular modules to a page, which are usually used to mock outcoming requests and provide custom response. But we do not need to mock requests, it would be enough to just listen for whatever comes from a server. It can be achieved with the help of Angular HTTP interceptors. Interceptors are used to catch a request or a response and modify it for whatever needs before in gets to it's endpoint. We can use them to collect information about what is coming from the server, store it, and then let response go forward without changes. Since this custom module and spec tests will run on the same page, information about responses can be stored in some global property. Then, when button is clicked, it would be possible to inject custom script to a page to retrieve required responses via browser.executeScript() (docs). Here is the source:

it('should intercept requests', function () {

    // Inject custom Angular module on a page
    // Script should be injected before you "browser.get()" the page
    browser.addMockModule('e2eHttp', function () {
        // Note: This function scope is not a Protractor environment

        angular
        .module('e2eHttp', [])
        .config(function ($httpProvider) {
            // add custom interceptor to all requests
            $httpProvider.interceptors.push('e2eHttpInterceptor');
        })
        .factory('e2eHttpInterceptor', function () {
            return {
                response: function (response) {
                    // name of the global property to store responses
                    var global = 'e2eHttpResponses';
                    // responses will be grouped by url
                    // but you can use data from "response.config" to adapt it
                    // it has a lot of info about response headers, method, etc
                    var url = response.config.url;

                    window[global] = window[global] || {};
                    window[global][url] = window[global.url] || [];
                    window[global][url].push(response); // store response

                    // proceed without changing response
                    return response;
                }
            };
        });
    });

    // Load the page
    browser.get('#/auth/login');

    $('#submit').click();

    // If we are sure that response has come, then extract it

    browser.executeScript(function () {
        // Note: This function scope is not a Protractor environment

        var global = 'e2eHttpResponses';
        var uri = 'api/auth/login.json';

        // extract array of stored responses for required uri
        var responses = (window[global] && window[global][uri]) || [];

        // return responses to spec
        return responses;

    }).then(function (responses) {
        // and finally, we are now able to get all information we need
        // about response, and in your case, save it to a file

        console.log(responses);

        var data = responses[0].data; // first response body as a string
    });


    // remove injected module not to break another specs
    browser.removeMockModule('e2eHttp');
});

您可以移动设置和注入调用一些实用的模块,因此测试规范是干净的。

You can move setup and injection calls to some utility modules, so test specs would be clean.

这篇关于在终端到终端的测试Capture服务器JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆