Midscene supports caching the planning steps and matched DOM element information of AI to reduce the call of AI models and improve the execution efficiency.
Effect
After enabling the cache, the execution time is significantly reduced, for example, from 39s to 13s.
There are two key points to use caching:
MIDSCENE_CACHE=1
in the environment variable.cacheId
to specify the cache file name. It's automatically set in Playwright and Yaml mode, but if you are using javascript SDK, you should set it manually.In playwright mode, you can use the MIDSCENE_CACHE=1
environment variable to enable caching.
The cacheId
will be automatically set to the test file name.
Enable caching by setting the MIDSCENE_CACHE=1
environment variable.
And also, you should set the cacheId
to specify the cache identifier.
Enable caching by setting the MIDSCENE_CACHE=1
environment variable.
The cacheId
will be automatically set to the yaml filename.
These two types of content will be cached:
The results of aiQuery
and aiAssert
will never be cached. You can always use them to verify whether the AI's task is as expected.
Cache will only be hit when the following conditions are met:
cacheId
When using cache for locate element tasks, the following conditions are also required:
If cache is not hit, Midscene will call AI model again and the result in cache file will be updated.
You should commit the cache file to the repository (usually in the ./midscene_run/cache
directory). And also, check the cache-hit conditions.
No. Caching is not a tool for ensuring long-term script stability. We have noticed many scenarios where the cache may miss when the page changes, such as when the element position changes slightly or the DOM structure changes. AI services are still needed to reevaluate the task when the cache miss occurs.
You can remove the cache file in the cache
directory, or edit the contents in the cache file.