Bird
Raised Fist0
Cypresstesting~20 mins

Test configuration per environment in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Environment Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of environment-specific baseUrl in Cypress
Given the following Cypress configuration files for two environments, what will be the baseUrl used when running tests with the command cypress run --env configFile=staging?
Cypress
/* cypress.config.js */
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      const envConfig = require(`./cypress.env.${config.env.configFile}.json`)
      return {...config, ...envConfig}
    },
    baseUrl: 'http://localhost:3000'
  }
})

/* cypress.env.staging.json */
{
  "baseUrl": "https://staging.example.com"
}

/* cypress.env.production.json */
{
  "baseUrl": "https://www.example.com"
}
Ahttps://staging.example.com
Bhttp://localhost:3000
Chttps://www.example.com
Dundefined
Attempts:
2 left
💡 Hint
Check how the configFile environment variable affects the loaded JSON file.
❓ assertion
intermediate
1:30remaining
Correct assertion for environment-specific API endpoint
You want to assert that the API endpoint URL used in your Cypress test matches the environment-specific value. Which assertion correctly verifies that the API URL is set to the production endpoint when running with --env configFile=production?
Cypress
const apiUrl = Cypress.env('apiUrl')

// Production endpoint is https://api.example.com
Aassert.equal(apiUrl, 'http://localhost:3000')
Bexpect(apiUrl).to.equal('https://api.example.com')
Cexpect(apiUrl).to.be.undefined
Dassert.isTrue(apiUrl === 'https://staging.example.com')
Attempts:
2 left
💡 Hint
Use the correct assertion style and check the expected URL for production.
🔧 Debug
advanced
2:30remaining
Debug failing environment variable in Cypress test
A test fails because the environment variable apiKey is undefined when running tests with --env configFile=qa. The cypress.env.qa.json file contains { "apiKey": "12345" }. What is the most likely cause?
Cypress
/* cypress.config.js */
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      const envConfig = require(`./cypress.env.${config.env.configFile}.json`)
      return {...config, ...envConfig}
    }
  }
})
AThe apiKey variable is not accessed via Cypress.env('apiKey')
BThe cypress.env.qa.json file is missing the apiKey property
CThe test command did not include --env configFile=qa
DThe setupNodeEvents function does not merge envConfig correctly
Attempts:
2 left
💡 Hint
Check the test run command and environment variable usage.
🧠 Conceptual
advanced
2:00remaining
Best practice for managing sensitive environment variables in Cypress
Which approach is the safest and most recommended way to manage sensitive environment variables like API keys across different environments in Cypress?
AUse OS environment variables and load them in cypress.config.js without committing keys to version control
BStore sensitive keys directly in cypress.env.{env}.json files committed to version control
CHardcode sensitive keys in test files for easy access
DInclude sensitive keys in the baseUrl property in cypress.config.js
Attempts:
2 left
💡 Hint
Think about security and version control best practices.
❓ framework
expert
3:00remaining
Configuring Cypress to run tests with multiple environment configurations sequentially
You want to run your Cypress tests sequentially for 'development', 'staging', and 'production' environments, each with its own config file (cypress.env.development.json, etc.). Which approach correctly sets this up in a Node.js script?
Cypress
const { exec } = require('child_process')

// Fill in the missing code to run tests sequentially for each environment
A['development', 'staging', 'production'].map(env => exec(`cypress run --env configFile=${env}`))
B
async function run() {
  for (const env of ['development', 'staging', 'production']) {
    await exec(`cypress run --env configFile=${env}`)
  }
}
run()
C['development', 'staging', 'production'].forEach(env => exec(`cypress run --env configFile=${env}`))
D
const runEnv = env => new Promise((res, rej) => {
  exec(`cypress run --env configFile=${env}`, (err) => err ? rej(err) : res())
})

async function run() {
  for (const env of ['development', 'staging', 'production']) {
    await runEnv(env)
  }
}
run()
Attempts:
2 left
💡 Hint
Consider how to wait for each exec command to finish before starting the next.

Practice

(1/5)
1. What is the main purpose of setting baseUrl in Cypress configuration for different environments?
easy
A. To change the test framework version automatically
B. To run tests against different websites without changing test code
C. To enable debugging mode in tests
D. To set the timeout for all tests

Solution

  1. Step 1: Understand baseUrl role

    baseUrl defines the main website address for tests to run against.
  2. Step 2: Relate baseUrl to environment

    Setting different baseUrl values per environment lets tests run on different sites without changing test code.
  3. Final Answer:

    To run tests against different websites without changing test code -> Option B
  4. Quick Check:

    baseUrl changes target site = A [OK]
Hint: baseUrl points tests to the right website per environment [OK]
Common Mistakes:
  • Confusing baseUrl with test timeout
  • Thinking baseUrl changes test framework version
  • Assuming baseUrl enables debugging
2. Which of the following is the correct way to access an environment variable named apiKey inside a Cypress test?
easy
A. const key = Cypress.env('apiKey')
B. const key = Cypress.get('apiKey')
C. const key = Cypress.config('apiKey')
D. const key = Cypress.variable('apiKey')

Solution

  1. Step 1: Recall method to get env variables

    Cypress provides Cypress.env() to access environment variables.
  2. Step 2: Match method with variable name

    Using Cypress.env('apiKey') correctly retrieves the apiKey value.
  3. Final Answer:

    const key = Cypress.env('apiKey') -> Option A
  4. Quick Check:

    Cypress.env() gets env vars = C [OK]
Hint: Use Cypress.env('varName') to get environment variables [OK]
Common Mistakes:
  • Using Cypress.config() instead of Cypress.env()
  • Trying Cypress.get() which does not exist
  • Using Cypress.variable() which is invalid
3. Given this cypress.config.js snippet:
export default defineConfig({
  env: {
    username: 'user1',
    password: 'pass1'
  }
})

What will console.log(Cypress.env('username')) print during a test?
medium
A. null
B. 'username'
C. 'user1'
D. undefined

Solution

  1. Step 1: Check env object in config

    The config sets env.username to 'user1'.
  2. Step 2: Understand Cypress.env() behavior

    Cypress.env('username') returns the value of the username key from env, which is 'user1'.
  3. Final Answer:

    'user1' -> Option C
  4. Quick Check:

    Env username value = 'user1' [OK]
Hint: Cypress.env('key') returns the value set in config env [OK]
Common Mistakes:
  • Expecting the key name instead of its value
  • Assuming undefined if not set in test code
  • Confusing Cypress.env() with Cypress.config()
4. You wrote this in cypress.config.js:
env: {
  apiUrl: 'https://api.example.com'
}

But inside your test, Cypress.env('apiUrl') returns undefined. What is the most likely cause?
medium
A. The env object is not inside the exported config
B. You forgot to export the config object
C. You must use process.env instead
D. Cypress.env() only works for baseUrl

Solution

  1. Step 1: Check config structure

    The env object must be inside the exported config object for Cypress to read it.
  2. Step 2: Identify common mistake

    If env is outside or not properly nested, Cypress.env() returns undefined.
  3. Final Answer:

    The env object is not inside the exported config -> Option A
  4. Quick Check:

    env must be inside exported config = B [OK]
Hint: Always nest env inside exported config object [OK]
Common Mistakes:
  • Not exporting the config at all
  • Using process.env instead of Cypress.env
  • Thinking Cypress.env only works for baseUrl
5. You want to run tests on two environments: staging and production. You set baseUrl and env.apiKey differently for each in cypress.config.js. How can you run tests on staging without changing the config file each time?
hard
A. Use cy.visit() with full URL every time
B. Manually edit cypress.config.js before each run
C. Set baseUrl and env inside each test file
D. Use command line flag like cypress run --env configFile=staging and load config accordingly

Solution

  1. Step 1: Understand environment config switching

    You can pass environment variables or flags via command line to select config variants.
  2. Step 2: Use command line flag to select config

    Using --env configFile=staging lets you load staging config without editing files.
  3. Final Answer:

    Use command line flag like cypress run --env configFile=staging and load config accordingly -> Option D
  4. Quick Check:

    Command line env flags switch configs = A [OK]
Hint: Use CLI flags to pick environment config dynamically [OK]
Common Mistakes:
  • Editing config file manually each time
  • Hardcoding URLs inside tests
  • Ignoring CLI environment options
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard