Testing greasemonkey/tampermonkey userscripts #28107
-
My userscript injects a new span into matched websites and I would like to use cypress to run tests if the userscript runs correctly. cy.visit('https://www.example.org')
cy.get("head").then(head => {
const script = document.createElement("script");
script.type = "text/javascript";
script.src = "../../../my.user.js";
head.appendChild(script);
})
}) It claims "head" does not have |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Wouldn't it be easier to just install TamperMonkey in the Cypress browser and just let it do it's thing with the script? |
Beta Was this translation helpful? Give feedback.
-
I have found a working solid solution. With a little bit of metadata the userscript can work as a browser extension. {
"manifest_version": 3,
"name": "my userscript",
"version": "1.0",
"content_scripts": [
{
"js": ["my.userscript.js"],
"matches": [
"https://www.amazon.com/*"
]
}
]
} Then loading this in the cypress plugin.js /// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************
// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
// eslint-disable-next-line import/no-extraneous-dependencies
const path = require('path')
const DIST_PATH = path.resolve(path.join(__dirname, '..', '..'))
/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
// Load userscript as webextension for E2E testing
// WARNING: headless mode only work for Electron and Firefox browser
// Chrome must be run with --headed option.
on('before:browser:launch', async (_, launchOptions) => {
launchOptions.extensions.push(DIST_PATH)
return launchOptions
})
} |
Beta Was this translation helpful? Give feedback.
I have found a working solid solution. With a little bit of metadata the userscript can work as a browser extension.
Then loading this in the cypress plugin.js