Back to Test Automation
2026-04-067 min read

Specifying an Alternative Config File (Test Automation)

Learn Specifying an Alternative Config File (Test Automation) step by step with clear examples and exercises.

Title: Specifying an Alternative Config File (Test Automation) for Selenium, Cypress, and Playwright using JavaScript


Why This Matters

In test automation, configuration files play a crucial role in setting up the environment, defining test parameters, and controlling the behavior of testing tools like Selenium, Cypress, and Playwright. An alternative config file can help you customize your tests to meet specific project requirements, avoid conflicts with other projects, or simply streamline your workflow by separating configuration from code.


Prerequisites

To follow this guide, you should have a basic understanding of:

  1. JavaScript programming language
  2. Test automation concepts
  3. Familiarity with Selenium, Cypress, or Playwright for test automation
  4. Understanding of how to write and run tests using these tools
  5. Knowledge of the project's directory structure and file organization
  6. Familiarity with environment variables and their usage in command-line interfaces
  7. Understanding of Git version control system (for managing configuration files)
  8. Basic understanding of Node.js and npm (for Selenium and Playwright examples)
  9. Familiarity with Cypress CLI and its project structure (for Cypress example)
  10. Understanding of how to use a text editor or Integrated Development Environment (IDE) for editing configuration files

Core Concept

Selenium

Selenium allows you to specify an alternative config file by providing a selenium-config.json or selenium2-config.json file in the project root directory. This file can contain various configurations like browser type, browser version, test suite path, and more.

{
"selenium": {
"start_maximized": true,
"browser": "chrome",
"version": "latest"
},
"test_suite": "./tests"
}

In your test script, you can use the webdriver.WebDriver constructor to create a new instance of the WebDriver and pass the path to the custom config file as an argument:

const { Builder } = require('selenium-webdriver');

async function main() {
const configFile = './selenium-config.json';
const driver = await new Builder().withConfig(configFile).build();
// Your test code here...
}

main();

Cypress

Cypress uses a cypress.json file to store configuration options. You can specify an alternative config file by setting the CYPRESS_CONFIG environment variable before running your tests:

export CYPRESS_CONFIG=custom-config.json
npm run cypress:open

In your custom config file, you can define various configurations like test runner, test files, and more.

{
"testFiles": "integration/**/*.js",
"baseUrl": "http://your-app.com",
"reporter": "mochawesome",
"reporterOptions": {
"reportDir": "./cypress/reports/mocha",
"overwrite": false,
"html": true
}
}

Playwright

Playwright uses a playwright.config.js file to store configuration options. You can specify an alternative config file by providing the path to your custom config file when initializing Playwright:

const { chromium, launch } = require('playwright');

async function main() {
const configPath = './custom-config.js';
const browser = await launch({ config: configPath });
// Your test code here...
}

main().catch(console.error).finally(() => browser.close());

In your custom config file, you can define various configurations like browser type, browser version, test suite path, and more.

module.exports = {
launchOptions: {
headless: false,
slowMo: 100,
args: ['--start-maximized']
},
timeout: 30 * 1000,
// Other options...
}

Worked Example

Let's create a simple test suite using Selenium, Cypress, and Playwright with custom config files.

Selenium

Create a selenium-config.json file in your project root directory:

{
"selenium": {
"start_maximized": true,
"browser": "chrome",
"version": "latest"
},
"test_suite": "./tests"
}

Create a package.json file (if you don't have one) and add the following dependencies:

{
"name": "my-test-project",
"version": "1.0.0",
"description": "",
"main": "tests/sampleTest.js",
"scripts": {
"test": "node tests/sampleTest.js"
},
"dependencies": {
"selenium-webdriver": "^8.14.0"
}
}

Create a tests/sampleTest.js file with your test code:

const { Builder, By, Key } = require('selenium-webdriver');

async function main() {
const configFile = './selenium-config.json';
const driver = await new Builder().withConfig(configFile).build();
await driver.get('http://your-app.com');
// Your test code here...
}

main();

Run your tests:

npm install
npm test

Cypress

Create a cypress.json file in your project root directory:

{
"testFiles": "integration/**/*.js",
"baseUrl": "http://your-app.com",
"reporter": "mochawesome",
"reporterOptions": {
"reportDir": "./cypress/reports/mocha",
"overwrite": false,
"html": true
}
}

Create an integration/sampleTest.js file with your test code:

describe('Sample Test', () => {
it('Visits the app homepage', () => {
cy.visit('/');
// Your test code here...
});
});

Run your tests:

npm install cypress
npm run cypress:open

Playwright

Create a playwright.config.js file in your project root directory:

module.exports = {
launchOptions: {
headless: false,
slowMo: 100,
args: ['--start-maximized']
},
timeout: 30 * 1000,
// Other options...
}

Create a test.js file with your test code:

const { chromium } = require('playwright');

async function main() {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('http://your-app.com');
// Your test code here...
}

main().catch(console.error).finally(() => browser.close());

Run your tests:

npm install playwright
node playwright.config.js test.js

Common Mistakes

  1. Forgetting to set the CYPRESS_CONFIG environment variable when using Cypress
  2. Not providing a valid path to the custom config file in Selenium or Playwright
  3. Using an incorrect naming convention for the custom config file (e.g., selenium-config.js instead of selenium-config.json)
  4. Failing to update the test suite path in the custom config file if it changes
  5. Not closing the browser after running tests with Playwright, causing potential issues with subsequent test runs

Practice Questions

  1. How can you specify a different browser for Selenium using a custom config file?
  2. What is the purpose of the reporter option in a Cypress configuration file?
  3. How can you run multiple tests at once with Playwright using a custom config file?
  4. If your project requires testing on Firefox and Chrome, how would you set up separate configurations for each browser using Selenium or Playwright?
  5. What are some best practices to follow when creating a custom config file for test automation projects?

FAQ

How can I specify a custom config file for Selenium when using Java instead of JavaScript?

A: Create a selenium2-config.xml or selenium2-config.properties file in your project root directory and use the appropriate WebDriver constructor to load it (e.g., new RemoteWebDriver(DesiredCapabilities.firefox()) for Firefox).

What happens if you don't provide a custom config file for Cypress and run your tests?

A: Cypress will use its default configuration, which may not meet your project requirements. You might encounter issues with test runner, browser selection, or reporting.

Can you create a custom Playwright configuration to run tests on multiple browsers at once?

A: Yes, you can use the launch() method with an array of browser types to launch multiple browsers simultaneously (e.g., const browsers = await launch({ headless: false, args: ['--start-maximized'], chromium, firefox });).

How can I debug issues related to my custom config files in Selenium, Cypress, or Playwright?

A: You can use print statements, console logs, and test assertions to verify that the correct configurations are being loaded and applied during test execution. Additionally, you can check the browser's developer tools network tab for any errors related to configuration loading.

What are some best practices when creating a custom config file for test automation projects?

A: Some best practices include using clear and descriptive naming conventions, separating environment-specific configurations, documenting your config files, and following a consistent structure across all config files.

How can I handle different configurations for different environments (e.g., development, staging, production) using a custom config file?

A: You can use environment variables to dynamically load the correct configuration based on the current environment. For example, you could set an ENV variable and adjust your custom config file accordingly.

What are some common performance optimization techniques for test automation projects?

A: Some performance optimization techniques include using headless browsers, disabling browser logs, reducing test suite size, optimizing test data, and implementing parallel test execution. Additionally, you can use tools like Selenium Grid or Sauce Labs to run your tests on multiple platforms without needing physical machines.

How do I ensure that my tests are robust and reliable across various browser versions and operating systems?

A: You should test your automation suite on a variety of browsers, versions, and operating systems to ensure compatibility and reliability. Additionally, you can use tools like BrowserStack or Sauce Labs to run your tests on multiple platforms without needing physical machines.

How can I integrate continuous integration (CI) tools like Jenkins or CircleCI with Selenium, Cypress, or Playwright to run automated tests on every code commit?

A: You can use plugins or integrations provided by these CI tools to easily set up test automation for your projects. For example, with Jenkins, you can install the Selenium Plugin to execute Selenium tests, and with CircleCI, you can configure your circle.yml file to run Cypress or Playwright tests.

What are some strategies for maintaining and updating test automation projects over time?

A: Some strategies include regularly reviewing and refactoring your test suite, keeping up-to-date with browser updates and testing tool releases, documenting changes and improvements, and collaborating with team members to ensure consistency and maintainability. Additionally, you can use tools like GitHub Actions or GitLab CI/CD to automate the testing process further.

Specifying an Alternative Config File (Test Automation) | Test Automation | XQA Learn