CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

I.seeTraffic on post data traffic unexpectedly passes

Open ngraf opened this issue 2 years ago • 6 comments

What are you trying to achieve?

Test a POST request with I.seeTraffic (Playwright Helper) with an intentional fail.

For example at https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST the existing POST request to https://developer.mozilla.org/pong/get with payload

{
   keywords: [],
   pongs: ["top", "side"
}

I use this test code:

Scenario('DEMO', async ({I}) => {
    I.amOnPage('https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST');
    await I.seeTraffic({
        url: 'https://developer.mozilla.org/pong/get', 
        name: 'POST Test', 
        timeout: 3,
        requestPostData: {
            keywords: [],
            pongs: ["side", "top"],
            doesNotExist: true
        }
    })
})

What do you get instead?

Test passes unexpectedly.

Details

  • CodeceptJS version: 3.5.6
  • NodeJS Version: 20.6.1
  • Operating System: MacOS latest version
  • Playwright: 1.38.1

ngraf avatar Oct 11 '23 11:10 ngraf

I think the reason is that I.seeTraffic returns true too early. A fix would be to turn this code in seeTraffic step of Playwright helper:

  for (let i = 0; i <= timeout * 2; i++) {
      const found = this._isInTraffic(url, parameters);
      if (found) {
        return true;
      }
      await new Promise((done) => {
        setTimeout(done, 1000);
      });
    }

into this code:

    for (let i = 0; i <= timeout * 2; i++) {
      const found = this._isInTraffic(url, parameters);
      if (found) {
        if (!requestPostData) {
          // We are done! Traffic matches URL and parameters.
          return true
        } else {
          // We are not done yet! We have to check post data as well.
          break;
        }
      }
      await new Promise((done) => {
        setTimeout(done, 1000);
      });
    }

ngraf avatar Oct 11 '23 11:10 ngraf

I have the impression that after the fix the test of the post data is also broken. I think it is because of this not logical code in Playwright helper:

      if (typeof information.requestPostData === 'object') {
        information.requestPostData = JSON.parse(information.requestPostData);
      }

Shouldn't it be !== 'object?

ngraf avatar Oct 11 '23 12:10 ngraf

I have the impression that after the fix the test of the post data is also broken. I think it is because of this not logical code in Playwright helper:

      if (typeof information.requestPostData === 'object') {
        information.requestPostData = JSON.parse(information.requestPostData);
      }

Shouldn't it be !== 'object?

https://github.com/codeceptjs/CodeceptJS/pull/3834/files#diff-5dc6e0a9028f00d161e530bc3b734e92d0497c131429e67b07e87a9224e4ecdaR2733

I recalled that this condition is to fix the missing data.

kobenguyent avatar Oct 11 '23 12:10 kobenguyent

CodeceptJS v3.5.6 #StandWithUkraine
Using test root "/Users/t/Downloads/highlight_error_sample"

My --
  ✖ DEMO in 692ms

-- FAILURES:

  1) My
       DEMO:
     Failure in test automation. You use "I.seeTraffic", but "I.startRecordingTraffic" was never called before.
      at Playwright.seeTraffic (node_modules/codeceptjs/lib/helper/Playwright.js:2985:13)
      at Step.run (node_modules/codeceptjs/lib/step.js:122:47)
      at /Users/t/Downloads/highlight_error_sample/node_modules/codeceptjs/lib/actor.js:135:23
  
  Scenario Steps:
  - I.seeTraffic({"url":"https://developer.mozilla.org/pong/get","name":"POST Test","timeout":3,"requestPostData":{"keywords":[],"pongs":["side","top"],"doesNotExist":true}}) at Test.<anonymous> (./regression_test.js:13:13)
  - I.amOnPage("https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST") at Test.<anonymous> (./regression_test.js:12:7)
  
  Artifacts:
  - screenshot: /Users/t/Downloads/highlight_error_sample/output/DEMO.failed.png
  - video: /Users/t/Downloads/highlight_error_sample/output/videos/d40bbf6e-f314-424f-9811-8986a8e86ac2_DEMO.failed.webm


  FAIL  | 0 passed, 1 failed   // 2s

I got failed test not passed one 🤔

kobenguyent avatar Oct 11 '23 14:10 kobenguyent

I got failed test not passed one 🤔

You have to execute I.startRecordingTraffic of course first. I forgot to add this in the code snippet because I implicitly expected it.

ngraf avatar Oct 13 '23 09:10 ngraf