Verify login functionality using environment variables for credentials
Preconditions (2)
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with a visible welcome message
describe('Login Test Using Environment Variables', () => { beforeEach(() => { cy.visit('https://example.com/login'); }); it('should login successfully using env variables', () => { const email = Cypress.env('USER_EMAIL'); const password = Cypress.env('USER_PASSWORD'); cy.get('#email').type(email); cy.get('#password').type(password, { log: false }); cy.get('#loginBtn').click(); cy.url().should('include', '/dashboard'); cy.get('#welcomeMessage').should('be.visible'); }); });
This test suite uses Cypress to automate login using environment variables for credentials.
The beforeEach hook opens the login page before each test.
Inside the test, Cypress.env() fetches the email and password securely without hardcoding them.
Selectors use id attributes for clarity and reliability.
The password input uses { log: false } to avoid showing sensitive data in logs.
Assertions check that the URL contains '/dashboard' and the welcome message is visible, confirming successful login.
Now add data-driven testing with 3 different sets of environment variables for login credentials
API_URL inside a Cypress test?Cypress.env('VAR_NAME') to access env variables.cypress.config.js snippet:export default defineConfig({
env: {
USERNAME: 'testUser',
PASSWORD: 'pass123'
}
})cy.log(Cypress.env('USERNAME')) print during test execution?const apiUrl = Cypress.env.API_URL; cy.visit(apiUrl);
TypeError: Cannot read property 'API_URL' of undefined. What is the problem?Cypress.env('API_URL') instead of Cypress.env.API_URL -> Option Dcypress.config.js. Which command line option correctly sets the environment variable API_URL to https://api.test.com?--env VAR_NAME=value to set env variables temporarily.cypress run runs tests in headless mode; --env sets env vars.Pass the test and this pattern moves unknown