DevExpress / testcafe

A Node.js tool to automate end-to-end web testing.
https://testcafe.io
MIT License
9.82k stars 672 forks source link

SyntaxError: Unexpected token u in JSON at position 0 in JSON.parse when using native automation #7588

Closed ayemelyanenko-chegg closed 1 year ago

ayemelyanenko-chegg commented 1 year ago

What is your Scenario?

Hello, I am running a test which visits a page, gets a logging event from a request and then parses the request body

What is the Current behavior?

When running the test with native automation, it fails with

SyntaxError: Unexpected token u in JSON at position 0

 70 |async function verifyRioEventv2(
         71 |  expected: RioEventVerify
         72 |): Promise<void> {
         73 |  let foundEvents: event[] = await Promise.all(
         74 |    rioLogger.requests.map(async (r: any) => {
       > 75 |      const body = JSON.parse(r.request.body);
         76 |
         77 |      for (const subevent of body) {
         78 |        if (isSubset(subevent, expected.locator)) return
      subevent;
         79 |
         80 |        continue;

What is the Expected behavior?

The test should pass because it passes without native automation

What is your public website URL? (or attach your complete example)

https://www.citationmachine.net/

What is your TestCafe test code?

import { ClientFunction, RequestLogger, t } from 'testcafe';
import { expect } from 'chai';
import { merge } from 'lodash';
const isSubset = require('is-subset');

export interface event {
  cloud_events_version?: string;
  content_type?: string;
  data?: any;
  event_id?: string;
  event_time?: string;
  event_type: string;
  event_type_version?: string;
  extensions?: {
    client_common?: {
      adobe_mcid?: string;
      analytics_module_version?: string;
      auth_state?: string;
      base_event_type?: string;
      base_event_version?: string;
      browser_name?: string;
      browser_version?: string;
      chegg_session_id?: number;
      chegg_visitor_id?: string;
      chegg_uuid?: string;
      platform?: string;
      referral_url?: string;
      test_cells?: [];
      url?: string;
      user_subscription_status?: [];
      view?: {
        experience?: string;
        property?: string;
        feature?: string;
        view_name?: string;
        view_version?: string;
      };
      view_previous?: {
        experience?: string;
        property?: string;
        view_name?: string;
        view_version?: string;
      };
    };
  };
  source: string;
}

const server = process?.env?.CHEGG_ENV?.toUpperCase();

const rioUrl =
  server == 'PROD' || server == 'STAGE'
    ? 'https://analytics.chegg.com/rio-service-web/rest/rio-events/batch'
    : 'https://analytics.test.cheggnet.com/rio-service-web/rest/rio-events/batch';

const rioLogger = RequestLogger(
  { url: rioUrl, method: 'POST' },
  {
    logRequestHeaders: true,
    logRequestBody: true,
    stringifyRequestBody: true,
  }
);

interface RioEventVerify {
  locator: Object;
  event: Object;
}

async function verifyRioEventv2(
  expected: RioEventVerify
): Promise<void> {
  let foundEvents: event[] = await Promise.all(
    rioLogger.requests.map(async (r: any) => {
      console.log(r.request.body);
      const body = JSON.parse(r.request.body);

      for (const subevent of body) {
        if (isSubset(subevent, expected.locator)) return subevent;

        continue;
      }
    })
  );

  foundEvents = foundEvents.filter((event) => event !== undefined);

  await t
    .expect(foundEvents.length)
    .gt(
      0,
      `Rio event was not found! With the following locator ${JSON.stringify(
        expected.locator
      )}`
    );

  const firstEvent: any = foundEvents[0];
  const expectedEvent: any = merge({}, firstEvent, expected.event);
  await expect(firstEvent).to.deep.include(
    expectedEvent,
    ` Locator: ${JSON.stringify(expected.locator)}`
  );
}

const homepageNavbarComponentViewEvent: RioEventVerify = {
  event: {
    data: {
      component_view: {
        component: {
          name: 'check out our demo page',
          text: 'Check out our demo page',
          type: 'link',
        },
      },
    },
    event_type: 'net.citationmachine.clickstream_component_view',
    event_type_version: '3',
  },
  locator: {
    data: {
      component_view: {
        component: {
          name: 'check out our demo page',
          text: 'Check out our demo page',
          type: 'link',
        },
      },
    },
  },
};

fixture('Event test')
    .page('https://www.citationmachine.net/');

test.meta({
        testID: 't-0001',
        testType: 'Smoke',
        testRailCaseId: '00000',
        priority: 'p1',
      })
      .requestHooks(rioLogger)('Event test', async t => {
   await t.wait(20000);
   await verifyRioEventv2(homepageNavbarComponentViewEvent);
});

Your complete configuration file

No response

Your complete test report

`Running tests in:
 - Chrome 111.0.0.0 / Ventura 13

 Event test
 ✖ Event test

   1) SyntaxError: Unexpected token u in JSON at position 0

      Browser: Chrome 111.0.0.0 / macOS 10.15.7

         70 |async function verifyRioEventv2(
         71 |  expected: RioEventVerify
         72 |): Promise<void> {
         73 |  let foundEvents: event[] = await Promise.all(
         74 |    rioLogger.requests.map(async (r: any) => {
       > 75 |      const body = JSON.parse(r.request.body);
         76 |
         77 |      for (const subevent of body) {
         78 |        if (isSubset(subevent, expected.locator)) return
      subevent;
         79 |
         80 |        continue;

         at <anonymous>

   (/Users/ayemelyanenko/nextjs-nodejs-example/my-app/tests/sampleEventTest.ts:75:25)
         at <anonymous>

   (/Users/ayemelyanenko/nextjs-nodejs-example/my-app/tests/sampleEventTest.ts:8:71)
         at __awaiter

   (/Users/ayemelyanenko/nextjs-nodejs-example/my-app/tests/sampleEventTest.ts:4:12)
         at <anonymous>

   (/Users/ayemelyanenko/nextjs-nodejs-example/my-app/tests/sampleEventTest.ts:74:45)
         at <anonymous>
      (/Users/ayemelyanenko/nextjs-nodejs-example/my-app/tests/sampleEventTest.ts:74:24)

 1/1 failed (6s)`

Screenshots

Screenshot 2023-04-06 at 10 05 21 AM

Steps to Reproduce

  1. Clone https://github.com/ayemelyanenko-chegg/nextjs-nodejs-example

  2. CD into nextjs-nodejs-example/

  3. cd into my-app

  4. npm install

  5. Export variable in terminal by doing: export CHEGG_ENV=prod

  6. Run with: npx testcafe chrome tests/sampleEventTest.ts --native-automation and test fails. Note the unicode body response in attached screenshot

  7. Run with: npx testcafe chrome tests/sampleEventTest.ts and test passes

TestCafe version

2.5.0

Node.js version

18.15.0

Command-line arguments

npx testcafe chrome tests/sampleEventTest.ts --native-automation

Browser name(s) and version(s)

No response

Platform(s) and version(s)

No response

Other

No response

miherlosev commented 1 year ago

Hi @ayemelyanenko-chegg,

I ran the shared example without the --experimental-proxyless flag, and it also failed. image

Please fix the example.

ayemelyanenko-chegg commented 1 year ago

@miherlosev Hello, thanks for getting back to me. I updated the example, please pull in latest for the repo and then re-run and let me know if it worked for you. If it didn't, please post a screenshot if possible, perhaps you are getting blocked from running automation on the production page. If that is the case, then I'll look for another example if possible. Thanks.

miherlosev commented 1 year ago

I ran the updated example and got the same error as in the https://github.com/DevExpress/testcafe/issues/7588#issuecomment-1484631323. Please, fix it.

ayemelyanenko-chegg commented 1 year ago

Apologies for this @miherlosev. I did manage to get a testcafe log and the comparison between request outputs from running the test locally, perhaps this will help? If not, I will keep looking for ways to have a working sample

export DEBUG="testcafe:*,hammerhead:*" && npx testcafe chrome tests/sampleEventTest.ts --experimental-proxyless -e 2>testcafe.log

https://upload.disroot.org/r/XMYDW6mG#oJwFbCENsp2mgyMoTgu5+XnJKthAwH2WXD4TBSJZ9NQ=

Regular mode console.log(r.request);

{ timestamp: 1680555528136, url: 'https://analytics.chegg.com/rio-service-web/rest/rio-events/batch', method: 'post', headers: { host: 'analytics.chegg.com', connection: 'keep-alive', 'content-length': 4500, accept: 'application/json, text/plain, */*', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'content-type': 'application/json', origin: 'https://www.citationmachine.net', referer: 'https://www.citationmachine.net/', 'accept-encoding': 'gzip, deflate', 'accept-language': 'en-US,en;q=0.9', cookie: 'CVID=5aca262f-e7ce-40ed-9cd3-b60d94b006eb; CSID=1680555525870' }, body: '[{"data":{"component_view":{"component":{"name":"check out our demo page","type":"link","text":"Check out our demo page"}}},"event_id":"28a378f4-5fd8-432c-86d1-9b3b3109158f","event_time":"2023-04-03T20:58:46.173Z","cloud_events_version":"0.1","event_type":"net.citationmachine.clickstream_component_view","source":"/web/citationmachine/ucf","event_type_version":"3","content_type":"application/json","extensions":{"client_common":{"uvn":"31abd50280e3c6ed4f59e03b498f1712642b3e037ced90.62830028","usprivacy":"1YNY","url":"https://www.citationmachine.net/","platform":"desktop","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36","auth_state":"logged_out","test_cells":[{"layer":"al_cell","cell":"av-release-58-control"}],"referral_url":"http://10.0.0.246:59618/browser/idle/dF5nf7-","browser_name":"Chrome","browser_version":"111.0.0.0","os":"macOS","os_ver":"10.15.7","analytics_module_version":"9.6.43","user_subscription_status":[{"product":"citation_machine_plus","status":"non_member"}],"base_event_type":"web","base_event_version":"4","view":{"property":"wt","experience":"cm","view_name":"homepage"},"window_dimensions":{"px_width":1200,"px_height":749},"browser_language":"en-US","session_replay_link":"","client_cookie_consent":",SPD_BG,trg,snc,fnc,prf,","browser_tracking":null,"global_privacy_control":null,"chegg_visitor_id":"5aca262f-e7ce-40ed-9cd3-b60d94b006eb","chegg_session_id":1680555525532}}},{"data":{},"event_id":"9ecb5198-4fd8-4c0e-a7ba-dda256dee687","event_time":"2023-04-03T20:58:46.186Z","cloud_events_version":"0.1","event_type":"net.citationmachine.clickstream_view","source":"/web/citationmachine/ucf","event_type_version":"3","content_type":"application/json","extensions":{"client_common":{"uvn":"31abd50280e3c6ed4f59e03b498f1712642b3e037ced90.62830028","usprivacy":"1YNY","url":"https://www.citationmachine.net/","platform":"desktop","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36","auth_state":"logged_out","test_cells":[{"layer":"al_cell","cell":"av-release-58-control"}],"referral_url":"http://10.0.0.246:59618/browser/idle/dF5nf7-","browser_name":"Chrome","browser_version":"111.0.0.0","os":"macOS","os_ver":"10.15.7","analytics_module_version":"9.6.43","user_subscription_status":[{"product":"citation_machine_plus","status":"non_member"}],"base_event_type":"web","base_event_version":"4","view":{"property":"wt","experience":"cm","view_name":"homepage"},"window_dimensions":{"px_width":1200,"px_height":749},"browser_language":"en-US","session_replay_link":"","client_cookie_consent":",SPD_BG,trg,snc,fnc,prf,","browser_tracking":null,"global_privacy_control":null,"chegg_visitor_id":"5aca262f-e7ce-40ed-9cd3-b60d94b006eb","chegg_session_id":1680555525532}}},{"data":{"allocation_id":"31abd50280e3c6ed4f59e03b498f1712642b3e037ced90.62830028","allocation_id_type":"chegg_uvn","project_id":"17022043465","experiment_id":"22180830219","variant_id":"22164360162"},"event_id":"e6bd3ef2-247b-477b-a499-353ba6402713","event_time":"2023-04-03T20:58:47.764Z","cloud_events_version":"0.1","event_type":"net.citationmachine.optimizely_allocation","source":"/web/citationmachine/ucf","event_type_version":"3","content_type":"application/json","extensions":{"client_common":{"uvn":"31abd50280e3c6ed4f59e03b498f1712642b3e037ced90.62830028","usprivacy":"1YNY","url":"https://www.citationmachine.net/","platform":"desktop","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36","auth_state":"logged_out","test_cells":[{"layer":"al_cell","cell":"av-release-58-control"}],"experiments":[{"experiment_id":"22180830219","variant_id":"22164360162"}],"referral_url":"http://10.0.0.246:59618/browser/idle/dF5nf7-","browser_name":"Chrome","browser_version":"111.0.0.0","os":"macOS","os_ver":"10.15.7","analytics_module_version":"9.6.43","user_subscription_status":[{"product":"citation_machine_plus","status":"non_member"}],"base_event_type":"web","base_event_version":"4","view":{"property":"wt","experience":"cm","view_name":"homepage"},"window_dimensions":{"px_width":1200,"px_height":749},"browser_language":"en-US","session_replay_link":"","client_cookie_consent":",SPD_BG,trg,snc,fnc,prf,","browser_tracking":null,"global_privacy_control":null,"chegg_visitor_id":"5aca262f-e7ce-40ed-9cd3-b60d94b006eb","chegg_session_id":1680555525532}}}]' }

Experimental proxyless console.log(r.request). Please notice the strange body output, it seems like it's in unicode for some reason?

{ timestamp: 1680555616497, url: 'https://analytics.chegg.com/rio-service-web/rest/rio-events/batch', method: 'post', headers: { accept: 'application/json, text/plain, */*', 'content-type': 'application/json', cookie: 'CVID=9a341ace-1c1d-4170-9fcd-8f91fdccd7b5; CSID=1680555615601', origin: 'https://www.citationmachine.net', referer: 'https://www.citationmachine.net/', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'sec-ch-ua': '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"' }, body: 'u�Zr���w����{\x07(��\'z{gjg���$��h��^��Z��r��b�K^�Сy�(��.�צ���y�ޞ��w}�ߎ^k���_����k���M��gZkW\x1Cy�ޞ��g��m�ӏ��=��My�~\x19rZ.w��z{l����*\'�W�z{\x7F�*^��\\�֭���i�b��%��,�����(��\'z{\x7F�\'����q��y�܊֭���i�b���q��z{\x7F�*^����*\'��\'����ܩy�i�\'\x1A�*\'�;(��mz{"�{\x1C�\'���(�j\'�������w�������^�V��n8սz�f�{���~�s~��}���)�+�s-X5���\x1Bm����\f\x1C�֭���i�b��޷�ej��g^�Kh��\x1E���z{L�8�����Ɯ�{h�\x12\'��LiÒ_]?ן�\x02�eyg�*+\x7F�~���0�b��rJ\x02��&{�u�M4I�ڮ/�߽�j�a��Z��h�\x07������-�ǥ��Z��ڗ�\x1E�W\x1E�V�o�!y�>�o��{k�Z�}��j_�Xm�����\x1A�\x1A,��|��ۮ�,z��jg���&y���ǫ����*\'�]t�J,��\x0EJ�?����^{jv��؜���v�^����*\'��7�ǫ�˛���ب��-j۬��\x1D��\\�֭�����!�w��[��֭��������m�\x1E��ޞ��ʗ�y�ڱ�޽������*\'���\x9A����\x0B^Ɨ����yɯ��?��������\x1E�)ݣ\x0F݊g��*\'��\x7F�\'m�]�Ҝ\x7F�蠆������ǫ�V���y��Q+\x1E�Ȩ��ަV��X���bz{\x7Fr�$��ܢ{\x1E�ԏ\x0F�F��,���r��n�0�������)���e�Z\x1Bj_�+�s/ܢ{k�Y�V�����ȝ��}׽��]��}��m��}���9��z�x�w!z\b?�+"�����ZߍZq�sW~�^����w�\x1F�W�q�{o�!z\b?��,�����z�Ny�y�O]j֞����\'ts��}Ǿ�ʹ��v��翽\x7F���]���\x1E����ئ{m6��8�M��]4מxٗ%��\x7Fz�����z�"�}5z����r��޵ȭjب�f��)�rX���ky���\'����q��y�܊֭���i�b���q��z{\x7F�*^����*\'��\'����ܩy�i�\'\x1A�*\'�;(��mz{"�{\x1C�\'���(�j\'�������w�������^�V��n8սz�f�{���~�s~��}���)�+�s-X5���\x1Bm����\f\x1C�֭���i�b��޷�ej��g^�Kh��\x1E���z{L�8�����Ɯ�{h�\x12\'��LiÒ_]?ן�\x02�eyg�*+\x7F�~���0�b��rJ\x02��&{�u�M4I�ڮ/�߽�j�a��Z��h�\x07������-�ǥ��Z��ڗ�\x1E�W\x1E�V�o�!y�>�o��{k�Z�}��j_�Xm�����\x1A�\x1A,��|��ۮ�,z��jg���&y���ǫ����*\'�]t�J,��\x0EJ�?����^{jv��؜���v�^����*\'��7�ǫ�˛���ب��-j۬��\x1D��\\�֭�����!�w��[��֭��������m�\x1E��ޞ��ʗ�y�ڱ�޽������*\'���\x9A����\x0B^Ɨ����yɯ��?��������\x1E�)ݣ\x0F݊g��*\'��\x7F�\'m�]�Ҝ\x7F�蠆������ǫ�V���y��Q+\x1E�Ȩ��ަV��X���bz{\x7Fr�$��ܢ{\x1E�ԏ\x0F�F��,���r��n�0�������)���e�Z\x1Bj_�+�s/ܢ{k�Y�V�����ȝ��}׽��]��}��m��}���9��z�x�w!z\b?�+"�����ZߍZq�sW~�^����w�\x1F�W�q�{o�!z\b?��,�����z�Ny�y�O]j֚�Z\x1Cjب���ۿ7۝�wמ{�;۾�{�Z����V�덛��^�������}��֥��\x1A�*\'�\'\x7F�*^r\x17��������-�\'u�M�Ӎ�뗱������\'v�_4�}6��ڮ&�����m��޴�o\x1E����\'^��8���wG{�����z\x7FϽם��n|wO^����ئ{m6��8�M��]4׭}՗%��\x7Fz�����z�"�}5z����r��޵ȭjب�f��)ޢ�b�,ޗ/ږZ\x1Cjب��.�ǿ���r+Z�*\'��!�w���޽���ܩ{�ޮȨ�w(�ק��r�橦X�jب���w���슉�rX���ܢi����ۿ7۝�wמ{�;۾�{�Z����V�덛��^�������}��۬���i̵�.�Xm��?�\f0r+Z�\'��!�w�z�镫���z�-���z�ځ��2��V��C\x1Ar)���H�ץ1�\x0EI}t�^\x7F�\n' + 'i��l�����ߢ�L��G�y�(\n' + "\x1A����]4�&�j���~�髭��-jץ�\b\x1Ew�.�׬��\x1E�[%k'�j�zY\zYZm����$���r����^Ɨ��g��DZ������'v�_4�}6��ڮ&�����m��޴�o+y����\x7F��a��\x7F�Z\x1CjXh��}��\x7Fn�0������\n" + "\x1A���\x0B\x1E��ޮȨ�]u�M(�f�9,����My��ڗ+br��ۥ{�ޮȨ�޸��\x1E��.n�+��b�\x7F쵫n���v�-r+Z�'�f��)���n��Z��'�\x7F�zfޭ��{��z{\x7F�^���jǿz����ޮȨ���{\n" + k����,-{\x1A^�'�q�&�'��v�z\x1A&z��{\b�v�?v)��Ȩ��q�\b��\x1Dv�Jq�\x17��\x1B{��\x0B\x1E��Z�\x0B����D�z�"�\x7F�z�Z��b�G%�����(�'�r��z{R<?�\x1A��w\x1F��k}���ǫ���rH��{��\thm�\x7F���i̿r����g�YZv���g"w�y�^���v׽��M����<�}�=�܅� �����+�'}k~5iǾ��]��{��_q߼\x7F�_u�\x1D��\\�� �Ǭ�*'�'u��9瞵�= } testcafe:errors processTestFnError: SyntaxError: Unexpected token u in JSON at position 0`

aleks-pro commented 1 year ago

Hello @ayemelyanenko-chegg ,

Thank you for sharing the log with us. We researched it, but we could not find the cause of this behavior. If possible, please send us a working example with which we can reproduce the issue. It will help us improve the "Native Automation" (aka "Proxyless") mode.

ayemelyanenko-chegg commented 1 year ago

Hello @aleks-pro. Apologies for my inability to provide a working example.

I believe I have missed a step in the instructions and the example should hopefully now work. I also added a log output to show the problem if you manage to get the example to work.

Please follow the steps in the steps to reproduce section again.

Thanks.

aleks-pro commented 1 year ago

Hi,

It works now. Thank you. I reproduced the issue. We will look into it and update this thread once we have any results.

Klaster1 commented 1 year ago

I'm getting the same malformed body from logger with NA enabled, any updates on this? If it helps, I tried decoding with everything from here, but no luck.

AlexKamaev commented 1 year ago

We fixed this issue but did not publish the new release yet. Please try installing TestCafe from the latest master branch and check if this helps.