webdriverio/webdriverio

[🐛 Bug]: mock.respond with fetchResponse set to false still fetches the response from the server

Open

#14.090 aberto em 20 de jan. de 2025

Ver no GitHub
 (6 comments) (2 reactions) (0 assignees)JavaScript (6.029 stars) (1.793 forks)batch import
Bug 🐛Needs Investigationhelp wanted

Description

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

9.5.3

Node.js Version

9.2.0

Mode

WDIO Testrunner

Which capabilities are you using?

{
   browserName: "chrome",
}

What happened?

I wanted to create a test that does not connect to the actual backend server while testing with a mock api.

https://webdriver.io/docs/mocksandspies/#mocking-api-requests shows this code example:

mock.respond({ ... }, {
    // do not call the actual backend
    fetchResponse: false
})

I tried this with the following code but it still sent the request to the backend - and while it mocks the request successfully it still waits for the request.

    const mock = await browser.mock("http://localhost:1234");
    mock.respond("Hello", { fetchResponse: false });
    await browser.url("http://localhost:1234");

In the code this parameter shows up here https://github.com/webdriverio/webdriverio/blob/69bfb390d182bcda57eed684b95502875c7ec3f2/packages/webdriverio/src/commands/mock/respond.ts#L52

but I don't see it in the actual implementation https://github.com/search?q=repo%3Awebdriverio%2Fwebdriverio+fetchResponse&type=code especially not in https://github.com/webdriverio/webdriverio/tree/main/packages/webdriverio/src/utils/interception

I'm not sure if there is something I'm missing, is the documentation outdated or is this an actual bug?

What is your expected behavior?

Don't fetch the response from the remote server that is mocked

How to reproduce the bug.

open a terminal and execute python3 -m http.server 1234 or nc -l 1234 that acts as a "http server" to easily see that there is a request.

Then execute this test:

import { describe, it } from "mocha";
import { browser } from "@wdio/globals";

describe("Test", () => {
  it("should not fetch response from target server", async () => {
    const mock = await browser.mock("http://localhost:1234");
    mock.respond("Hello", { fetchResponse: false });
    await browser.url("http://localhost:1234");
  });
});

Relevant log output

$ python3 -m http.server 1234
Serving HTTP on 0.0.0.0 port 1234 (http://0.0.0.0:1234/) ...
127.0.0.1 - - [20/Jan/2025 11:57:07] "GET / HTTP/1.1" 200 -

-----------

bidi:RECV ◂ {"channel":"/3/nochan/bidi","id":9,"method":"session.subscribe","params":{"events":["network.beforeRequestSent","network.responseStarted"]}}
debug:queue Processing event: session.subscribe
bidi:SEND ▸ { "type": "success", "id": 9, "result": {}, "channel": "/3/nochan/bidi" }
bidi:RECV ◂ {"channel":"/3/nochan/bidi","id":10,"method":"network.addIntercept","params":{"phases":["beforeRequestSent","responseStarted"],"urlPatterns":[{"hostname":"localhost","pathname":"\\*","port":"1234","protocol":"http","type":"pattern"}]}}
debug:info Toggle Network Fetch (true) true Network (true) false
cdp:SEND ▸ { "id": 26, "method": "Fetch.enable", "params": { "patterns": [ { "urlPattern": "*", "requestStage": "Request" }, { "urlPattern": "*", "requestStage": "Response" } ], "handleAuthRequests": false }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
cdp:RECV ◂ { "id": 26, "result": {}, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
debug:queue Processing event: network.addIntercept
bidi:SEND ▸ { "type": "success", "id": 10, "result": { "intercept": "f7bc7b4e-bde5-46be-96db-f9f4373c0527" }, "channel": "/3/nochan/bidi" }
bidi:RECV ◂ {"channel":"/3/nochan/bidi","id":11,"method":"browsingContext.navigate","params":{"context":"1C80CA47C339281F64918C4C302C2DA1","url":"http://localhost:1234/","wait":"complete"}}
cdp:SEND ▸ { "id": 27, "method": "Page.navigate", "params": { "url": "http://localhost:1234/", "frameId": "1C80CA47C339281F64918C4C302C2DA1" }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
cdp:RECV ◂ { "method": "Page.frameStartedLoading", "params": { "frameId": "1C80CA47C339281F64918C4C302C2DA1" }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
debug:queue Processing event: browsingContext.navigationStarted
bidi:SEND ▸ { "type": "event", "method": "browsingContext.navigationStarted", "params": { "context": "1C80CA47C339281F64918C4C302C2DA1", "navigation": "15ee314b-d054-4bc7-be20-5d4c27f593c7", "timestamp": 1737370278268, "url": "http://localhost:1234/" }, "channel": "/3/nochan/bidi" }
cdp:RECV ◂ { "method": "Network.requestWillBeSent", "params": { "requestId": "65480CAC05BFD79D0CAD9AF73D6AEBC9", "loaderId": "65480CAC05BFD79D0CAD9AF73D6AEBC9", "documentURL": "http://localhost:1234/", "request": { "url": "http://localhost:1234/", "method": "GET", "headers": { "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36", "sec-ch-ua": "\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Linux\"" }, "mixedContentType": "none", "initialPriority": "VeryHigh", "referrerPolicy": "strict-origin-when-cross-origin", "isSameSite": true }, "timestamp": 185340.641866, "wallTime": 1737370278.268734, "initiator": { "type": "other" }, "redirectHasExtraInfo": false, "type": "Document", "frameId": "1C80CA47C339281F64918C4C302C2DA1", "hasUserGesture": false }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
cdp:RECV ◂ { "method": "Fetch.requestPaused", "params": { "requestId": "interception-job-1.0", "request": { "url": "http://localhost:1234/", "method": "GET", "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36", "sec-ch-ua": "\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Linux\"" }, "initialPriority": "VeryHigh", "referrerPolicy": "strict-origin-when-cross-origin" }, "frameId": "1C80CA47C339281F64918C4C302C2DA1", "resourceType": "Document", "networkId": "65480CAC05BFD79D0CAD9AF73D6AEBC9" }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
debug:queue Processing event: network.beforeRequestSent
bidi:SEND ▸ { "method": "network.beforeRequestSent", "params": { "context": "1C80CA47C339281F64918C4C302C2DA1", "navigation": "15ee314b-d054-4bc7-be20-5d4c27f593c7", "redirectCount": 0, "request": { "request": "65480CAC05BFD79D0CAD9AF73D6AEBC9", "url": "http://localhost:1234/", "method": "GET", "headers": [ { "name": "Upgrade-Insecure-Requests", "value": { "type": "string", "value": "1" } }, { "name": "User-Agent", "value": { "type": "string", "value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36" } }, { "name": "sec-ch-ua", "value": { "type": "string", "value": "\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"" } }, { "name": "sec-ch-ua-mobile", "value": { "type": "string", "value": "?0" } }, { "name": "sec-ch-ua-platform", "value": { "type": "string", "value": "\"Linux\"" } } ], "cookies": [], "headersSize": 273, "bodySize": 0, "timings": { "timeOrigin": 0, "requestTime": 0, "redirectStart": 0, "redirectEnd": 0, "fetchStart": 0, "dnsStart": 0, "dnsEnd": 0, "connectStart": 0, "connectEnd": 0, "tlsStart": 0, "requestStart": 0, "responseStart": 0, "responseEnd": 0 }, "goog:resourceType": "Document" }, "timestamp": 1737370278269, "isBlocked": true, "intercepts": [ "f7bc7b4e-bde5-46be-96db-f9f4373c0527" ], "initiator": { "type": "other" } }, "type": "event", "channel": "/3/nochan/bidi" }
bidi:RECV ◂ {"channel":"/3/nochan/bidi","id":12,"method":"network.continueRequest","params":{"request":"65480CAC05BFD79D0CAD9AF73D6AEBC9"}}
cdp:SEND ▸ { "id": 28, "method": "Fetch.continueRequest", "params": { "requestId": "interception-job-1.0" }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
cdp:RECV ◂ { "id": 28, "result": {}, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
debug:queue Processing event: network.continueRequest
bidi:SEND ▸ { "type": "success", "id": 12, "result": {}, "channel": "/3/nochan/bidi" }
cdp:RECV ◂ { "method": "Network.requestWillBeSentExtraInfo", "params": { "requestId": "65480CAC05BFD79D0CAD9AF73D6AEBC9", "associatedCookies": [], "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Accept-Encoding": "gzip, deflate, br, zstd", "Accept-Language": "en-US,en;q=0.9", "Connection": "keep-alive", "Host": "localhost:1234", "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-Site": "none", "Sec-Fetch-User": "?1", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36", "sec-ch-ua": "\"Not A(Brand\";v=\"8\", \"Chromium\";v=\"132\", \"Google Chrome\";v=\"132\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Linux\"" }, "connectTiming": { "requestTime": 185340.649696 }, "siteHasCookieInOtherPartition": false }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
cdp:RECV ◂ { "method": "Page.lifecycleEvent", "params": { "frameId": "1C80CA47C339281F64918C4C302C2DA1", "loaderId": "801EDDBEA08C689CD80D5EEC6C8BCB01", "name": "networkAlmostIdle", "timestamp": 185340.215739 }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }
cdp:RECV ◂ { "method": "Page.lifecycleEvent", "params": { "frameId": "1C80CA47C339281F64918C4C302C2DA1", "loaderId": "801EDDBEA08C689CD80D5EEC6C8BCB01", "name": "networkIdle", "timestamp": 185340.215739 }, "sessionId": "ECEFEAED5110E9EDA2A2C562C1657FF2" }

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

Guia do colaborador