microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
65.85k stars 3.58k forks source link

[Question] Is there any way to enable http cache when using route ? #7220

Open Syuking opened 3 years ago

Syuking commented 3 years ago

image

Chromium will cache js & css files ,but the cache is disabling when using route action So,Is there any way to enable http cache when using route ? Cause i hope to reduce the number of requests

aslushnikov commented 3 years ago

Cause i hope to reduce the number of requests

@Syuking Does it hit performance of the page loading in your tests?

Syuking commented 3 years ago

Cause i hope to reduce the number of requests

@Syuking Does it hit performance of the page loading in your tests?

maybe, i just want to try for this

rigwild commented 3 years ago

It would be nice if it was possible to mock responses on some routes while keeping non-matched routes in cache.

Use case: replace the content of some files/scripts loaded by a website with local versions, without slowing down the navigation by having to http load everything else everytime.

As a workaround I use a MITM proxy but that is not ideal.

oren-sarid commented 2 years ago

I need this too!

OKing0 commented 2 years ago

I need this too!

LeMoussel commented 2 years ago

As I detail it in #10414, it's possible to enable/disable http cache with Network.setCacheDisabled from Chrome DevTools Protocol. But I find it not very readable. So for more readability, just like the API Puppeteer: page.setCacheEnabled we could have this API in Playwright.

MLWeber commented 2 years ago

I would like this, too. I want to use page.route() to abort requests to images and other unnecessary resources, but it defeats the purpose if I then lose the ability to cache scripts etc.

Is there a technical reason why using route disables the http cache?

Pk-T commented 2 years ago

Any update on this? My usecase is also the same i want to mock some files but at the same time check if cache was hit for others on reload.

coader commented 2 years ago

also need this

Rumatoid commented 2 years ago

I really need this feature

Rumatoid commented 2 years ago

I found this._client.send('Network.setCacheDisabled', { cacheDisabled: true }), in files, that disable cache after enabling routing I have just comment this line and build playwright, and cache enabled

Sorry for my English

coader commented 2 years ago

I found this._client.send('Network.setCacheDisabled', { cacheDisabled: true }), in files, that disable cache after enabling routing I have just comment this line and build playwright, and cache enabled

Sorry for my English

is any way to config this without rebuild?

dgtlmoon commented 2 years ago

https://github.com/microsoft/playwright/commit/aabdac83804bfb28fba0a93a5d4f61ce17920f41 related to this change?

roy-k commented 2 years ago

Is there any new? I really need this too. As a service, it will be more effective~

pavliy commented 1 year ago

Would be happy to have this option back as well.

lifeart commented 1 year ago

+1

thiagodangelo commented 1 year ago

I also need this feature.

andobolocco commented 1 year ago

+1

lagunovsky commented 1 year ago

I also need this feature.

SwannG commented 1 year ago

+1

lagunovsky commented 1 year ago

Workaround

// package.json

"scripts": {
+   "prepare": "node prepare.js",
}
// prepare.js

import path from 'path'
import {readFileSync, writeFileSync} from 'fs'

const modules = path.resolve(`node_modules`)
const destination = path.join(modules, 'playwright-core', 'lib', 'server', 'chromium', 'crNetworkManager.js')
const buffer = readFileSync(destination)
writeFileSync(destination, buffer.toString().replace('cacheDisabled: true', 'cacheDisabled: false'))
xmarek commented 1 year ago

+1

josephwynn-sc commented 1 year ago

I'm sorely missing the Page.setCacheEnabled API too. We want to use Playwright to run automated tests for our analytics script, and there are cases where the testing code is much simpler when it can explicitly enable or disable the cache within a single test case.

hgezim commented 1 year ago

Seems that using page.context().on('response', handler) does not affect caching (requests are still cached). Can someone confirm this?

zomchak-code commented 1 year ago

@aslushnikov What kind of feedback do you need for this? I also need this feature to reduce network traffic and not warm up the planet)

sjoerd222888 commented 1 year ago

I also need this feature to make it more efficient, faster! And of course I want to reduce network traffic and not contribute to warming up the planet

OliverJAsh commented 1 year ago

Is there a reason why Playwright disables the cache when using page.route?

zhzehong commented 10 months ago

need this

achsal2 commented 10 months ago

+1

jcarlsonautomatiq commented 9 months ago

It definitely seems like something a route should allow when you are using playwright for integration testing. Keeping it alive before a resolver bot gets it.

tonybruess commented 5 months ago

It would be great to know why the cache is disabled when using route() at the very least

mr-anton-t commented 4 months ago

+1

vitalets commented 2 weeks ago

I've made a package that allows to cache responses on the filesystem and modify them in tests. Can be used as a workaround for now.