Utilities for parsing command-line arguments in the same way Cypress CLI processes them. This allows programmatic integration with existing CLI workflows and custom automation scripts.
Parses CLI arguments for cypress run commands and returns options that can be passed to cypress.run().
/**
* Parses the given array of string arguments to "cypress run" just like Cypress CLI does it
* @param args - Array of CLI arguments including the command
* @returns Promise resolving to parsed options for cypress.run()
*/
function parseRunArguments(args: string[]): Promise<Partial<CypressRunOptions>>;Usage Examples:
const cypress = require('cypress');
// Parse CLI arguments from process.argv
const args = process.argv; // e.g., ['node', 'script.js', 'cypress', 'run', '--browser', 'chrome', '--spec', '**/*.cy.js']
const options = await cypress.cli.parseRunArguments(args);
// options will be: { browser: 'chrome', spec: '**/*.cy.js' }
// Use parsed options with cypress.run()
const results = await cypress.run(options);// Parse specific command arguments
const cliArgs = ['cypress', 'run', '--browser', 'firefox', '--headed', '--record', '--key', 'abc123'];
const options = await cypress.cli.parseRunArguments(cliArgs);
// options will be: { browser: 'firefox', headed: true, record: true, key: 'abc123' }
const results = await cypress.run(options);The CLI parser recognizes all standard Cypress CLI arguments and maps them to the appropriate options:
// CLI arguments that map to CypressRunOptions
interface CliArgumentMapping {
'--browser': 'browser';
'--spec': 'spec';
'--config': 'config';
'--config-file': 'configFile';
'--env': 'env';
'--headed': 'headed';
'--headless': 'headless';
'--record': 'record';
'--key': 'key';
'--parallel': 'parallel';
'--group': 'group';
'--tag': 'tag';
'--ci-build-id': 'ciBuildId';
'--port': 'port';
'--project': 'project';
'--quiet': 'quiet';
'--reporter': 'reporter';
'--reporter-options': 'reporterOptions';
'--auto-cancel-after-failures': 'autoCancelAfterFailures';
'--runner-ui': 'runnerUi';
}Advanced Usage Examples:
// Integration with custom CLI tools
async function runCustomCypress(customArgs) {
// Transform custom arguments to Cypress CLI format
const cypressArgs = [
'cypress', 'run',
'--browser', customArgs.browser || 'electron',
'--spec', customArgs.testPattern || 'cypress/e2e/**/*.cy.js'
];
if (customArgs.headless === false) {
cypressArgs.push('--headed');
}
if (customArgs.record) {
cypressArgs.push('--record', '--key', customArgs.recordKey);
}
// Parse and run
const options = await cypress.cli.parseRunArguments(cypressArgs);
return await cypress.run(options);
}
// Usage in build scripts
const results = await runCustomCypress({
browser: 'chrome',
testPattern: 'cypress/e2e/critical/*.cy.js',
headless: true,
record: true,
recordKey: process.env.CYPRESS_RECORD_KEY
});// Parse complex CLI arguments
const complexArgs = [
'cypress', 'run',
'--browser', 'chrome',
'--config', 'baseUrl=http://localhost:4200,viewportWidth=1280,viewportHeight=720',
'--env', 'environment=staging,apiUrl=https://api.staging.com',
'--spec', 'cypress/e2e/auth/**/*.cy.js,cypress/e2e/dashboard/**/*.cy.js',
'--reporter', 'mochawesome',
'--reporter-options', 'reportDir=cypress/reports,overwrite=false,html=true,json=true',
'--record',
'--key', 'your-record-key',
'--parallel',
'--group', 'staging-tests',
'--tag', 'nightly,regression'
];
const parsedOptions = await cypress.cli.parseRunArguments(complexArgs);
/*
parsedOptions will contain:
{
browser: 'chrome',
config: {
baseUrl: 'http://localhost:4200',
viewportWidth: 1280,
viewportHeight: 720
},
env: {
environment: 'staging',
apiUrl: 'https://api.staging.com'
},
spec: 'cypress/e2e/auth/**/*.cy.js,cypress/e2e/dashboard/**/*.cy.js',
reporter: 'mochawesome',
reporterOptions: {
reportDir: 'cypress/reports',
overwrite: false,
html: true,
json: true
},
record: true,
key: 'your-record-key',
parallel: true,
group: 'staging-tests',
tag: 'nightly,regression'
}
*/
const results = await cypress.run(parsedOptions);The CLI parser is particularly useful for creating custom automation scripts that need to maintain compatibility with Cypress CLI arguments:
// automation-script.js
const cypress = require('cypress');
async function main() {
try {
// Get arguments from command line, removing the first two (node and script path)
const args = ['cypress', 'run', ...process.argv.slice(2)];
console.log('Parsing Cypress CLI arguments...');
const options = await cypress.cli.parseRunArguments(args);
// Add custom logic before running tests
console.log('Setting up test environment...');
// Custom setup logic here
console.log('Running Cypress tests...');
const results = await cypress.run(options);
// Custom logic after running tests
if (results.status === 'failed') {
console.error(`Tests failed: ${results.message}`);
process.exit(results.failures);
} else {
console.log(`All tests passed! ${results.totalPassed} tests completed successfully.`);
// Custom reporting or notifications
if (results.runUrl) {
console.log(`View results at: ${results.runUrl}`);
}
}
} catch (error) {
console.error('Error running tests:', error);
process.exit(1);
}
}
// Run if this script is executed directly
if (require.main === module) {
main();
}
// Usage: node automation-script.js --browser chrome --spec "cypress/e2e/**/*.cy.js" --record --key abc123The CLI parser helps integrate Cypress with various CI/CD systems while maintaining argument compatibility:
// ci-integration.js
const cypress = require('cypress');
async function runCypressInCI() {
// Build arguments based on CI environment
const args = ['cypress', 'run'];
// Add browser selection
args.push('--browser', process.env.CYPRESS_BROWSER || 'electron');
// Add recording if in CI
if (process.env.CI) {
args.push('--record');
if (process.env.CYPRESS_RECORD_KEY) {
args.push('--key', process.env.CYPRESS_RECORD_KEY);
}
// Add CI build ID for grouping
if (process.env.CI_BUILD_ID) {
args.push('--ci-build-id', process.env.CI_BUILD_ID);
}
// Enable parallel runs in CI
if (process.env.CYPRESS_PARALLEL === 'true') {
args.push('--parallel');
}
}
// Add custom config for CI environment
const ciConfig = [
'baseUrl=' + (process.env.BASE_URL || 'http://localhost:3000'),
'defaultCommandTimeout=10000',
'requestTimeout=8000'
].join(',');
args.push('--config', ciConfig);
// Parse and run
const options = await cypress.cli.parseRunArguments(args);
return await cypress.run(options);
}
// Export for use in other modules
module.exports = { runCypressInCI };This CLI integration capability allows for seamless integration between programmatic Cypress usage and existing command-line workflows, making it easy to migrate from CLI-based test execution to programmatic control while maintaining the same argument structure.