0

When I run testcafe code, the output from this step is garbled.:

for (let i = simpleLogger.requests.length - 1; i > 0; i--) { let url_test = simpleLogger.requests[i].request.url; if (url_test.indexOf("/gt/validate/") !== -1) { let res_json = simpleLogger.requests[i].response.body console.log(res_json); break; } }

Then I turned on the browser debug mode and found that there was an extra piece of js code in the return content of each page request:

/*hammerhead|script|start*/if (typeof window !== 'undefined' && window){window['hammerhead|process-dom-method'] && window['hammerhead|process-dom-method']();if (window.__get$ && typeof __get$ === 'undefined') {var __get$Loc = window.__get$Loc,__set$Loc = window.__set$Loc,__set$ = window.__set$,__get$ = window.__get$,__call$ = window.__call$,__get$Eval = window.__get$Eval,__proc$Script = window.__proc$Script,__proc$Html = window.__proc$Html,__get$PostMessage = window.__get$PostMessage,__get$ProxyUrl = window.__get$ProxyUrl,__rest$Array = window.__rest$Array,__rest$Object = window.__rest$Object;}} else {var __get$Loc = function(l){return l},__set$Loc = function(l,v){return l = v},__set$ = function(o,p,v){return o[p] = v},__get$ = function(o,p){return o[p]},__call$ = function(o,p,a){return o[p].apply(o,a)},__get$Eval = function(e){return e},__proc$Script = function(s){return s},__proc$Html = function(h){return h},__get$PostMessage = function(w,p){return arguments.length===1?w.postMessage:p},__get$ProxyUrl = function(u,d){return u},__rest$Array = function(a,i){return Array.prototype.slice.call(a, i)},__rest$Object = function(o,p){var k=Object.keys(o),n={};for(var i=0;i<k.length;++i)if(p.indexOf(k[i])<0)n[k[i]]=o[k[i]];return n};}/*hammerhead|script|processing-header-end*/ geetest_1610940527001({"theme_version": "1.2.4", "clean": false, "so": 0, "mobile": true, "xpos": 0, "version": "6.0.9", "ypos": 37, "gt": "53eb2795e892363b5a7e6c0df0ed74af", "feedback": "http://www.geetest.com/contact#report", "id": "a738db9d4442c5fd0bcbe65865085aa96", "benchmark": false, "gct_path": "/static/js/gct.1.0.0.js", "type": "multilink", "template": "", "width": "100%", "hide_delay": 800, "fullpage": false, "api_server": "http://g1-server-staging.kjy.gtapp.xyz/", "challenge": "738db9d4442c5fd0bcbe65865085aa96g3", "height": 160, "s": "7a69353a", "https": false, "slice": "pictures/gt/d49a453dc/slice/8acfcf53c.png", "bg": "pictures/gt/d49a453dc/bg/8acfcf53c.jpg", "show_delay": 250, "logo": true, "i18n_labels": {"cancel": "\u53d6\u6d88", "loading": "\u52a0\u8f7d\u4e2d...", "fail": "\u8bf7\u6b63\u786e\u62fc\u5408\u56fe\u50cf", "refresh": "\u5237\u65b0\u9a8c\u8bc1", "close": "\u5173\u95ed\u9a8c\u8bc1", "error": "\u8bf7\u91cd\u8bd5", "feedback": "\u5e2e\u52a9\u53cd\u9988", "read_reversed": false, "tip": "\u8bf7\u5b8c\u6210\u4e0b\u65b9\u9a8c\u8bc1", "voice": "\u89c6\u89c9\u969c\u788d", "logo": "\u7531\u6781\u9a8c\u63d0\u4f9b\u6280\u672f\u652f\u6301", "slide": "\u62d6\u52a8\u6ed1\u5757\u5b8c\u6210\u62fc\u56fe", "success": "sec \u79d2\u7684\u901f\u5ea6\u8d85\u8fc7 score% \u7684\u7528\u6237", "forbidden": "\u602a\u7269\u5403\u4e86\u62fc\u56fe\uff0c\u8bf7\u91cd\u8bd5"}, "product": "embed", "c": [12, 58, 98, 36, 43, 95, 62, 15, 12], "link": "", "static_servers": ["static.geetest.com/", "dn-staticdown.qbox.me/"], "fullbg": "pictures/gt/d49a453dc/d49a453dc.jpg", "theme": "ant"}) /*hammerhead|script|end*/

How can I get what I want through simplelogger (except hammerhead) hope to give me an answer

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47

1 Answers1

1

Please take a look at the following articles: https://devexpress.github.io/testcafe/documentation/guides/advanced-guides/intercept-http-requests.html#log-http-requests

https://devexpress.github.io/testcafe/documentation/reference/test-api/requestlogger/constructor.html

You need to pass the 'logResponseBody: true' argument to the RequestLogger constructor as follows:

const headerLogger = RequestLogger(/testcafe/, {
    logResponseBody: true
});

In this case, the body will be of the Buffer type, so you need to convert it into string. Otherwise, you can use the stringifyResponseBody: true argument to convert it automatically.

Please also check if your response is compressed as gzip. In this case you need to unzip it self as described in the following threads:

How to get the json response of a RequestLogger

https://github.com/DevExpress/testcafe/issues/3243

Regarding the /*hammerhead|script|start*/if (typeof window !== 'undefined' part. This is code injected by TestCafe into a tested page. It does not interfere with your requests and is not logged.

Alex Kamaev
  • 6,198
  • 1
  • 14
  • 29