0
0
Cypresstesting~20 mins

Base URL configuration in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Base URL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the base URL used in this Cypress test?
Given the Cypress configuration and test code below, what is the base URL that the test will use when running cy.visit('/')?
Cypress
/// cypress.config.js
export default {
  e2e: {
    baseUrl: 'https://example.com',
  },
};

// test.spec.js
describe('Home page test', () => {
  it('Visits the base URL', () => {
    cy.visit('/');
  });
});
A/
Bhttps://example.com
Chttp://localhost:3000
Dhttps://example.com/
Attempts:
2 left
💡 Hint
Remember that cy.visit('/') appends '/' to the baseUrl configured in cypress.config.js.
assertion
intermediate
2:00remaining
Which assertion correctly verifies the current URL includes the base URL?
Assuming the baseUrl is set to 'https://myapp.test', which Cypress assertion correctly checks that the current URL starts with the base URL after visiting '/'?
Cypress
cy.visit('/');
// Which assertion below is correct?
Acy.url().should('contain', '/');
Bcy.url().should('eq', 'https://myapp.test/');
Ccy.url().should('include', 'https://myapp.test');
Dcy.url().should('not.include', 'https://myapp.test');
Attempts:
2 left
💡 Hint
Use an assertion that checks if the URL contains the base URL string.
🔧 Debug
advanced
2:00remaining
Why does cy.visit('/') fail with 'Invalid URL' error?
Given this Cypress config and test, why does cy.visit('/') throw an 'Invalid URL' error?
Cypress
/// cypress.config.js
export default {
  e2e: {
    // baseUrl is missing
  },
};

// test.spec.js
describe('Test without baseUrl', () => {
  it('Visits root path', () => {
    cy.visit('/');
  });
});
ABecause baseUrl is not set, cy.visit('/') cannot resolve a full URL and throws an error.
BBecause '/' is an invalid path for cy.visit, it must be a full URL.
CBecause the test file is missing import statements for Cypress.
DBecause cy.visit('/') requires a protocol prefix like 'http://' explicitly.
Attempts:
2 left
💡 Hint
Check if baseUrl is configured when using relative paths in cy.visit.
🧠 Conceptual
advanced
2:00remaining
What is the benefit of setting baseUrl in Cypress config?
Why is it recommended to set baseUrl in Cypress configuration instead of using full URLs in cy.visit calls?
AIt forces all tests to run only on localhost, improving speed.
BIt allows using relative paths in tests, making tests easier to maintain and environment switching simpler.
CIt disables network requests outside the baseUrl domain automatically.
DIt automatically retries failed tests on different URLs.
Attempts:
2 left
💡 Hint
Think about how baseUrl helps when running tests in different environments.
framework
expert
2:00remaining
How to override baseUrl for a single test in Cypress?
You have baseUrl set globally in cypress.config.js. How can you override baseUrl for only one test to visit a different domain?
Cypress
/// cypress.config.js
export default {
  e2e: {
    baseUrl: 'https://default.com',
  },
};

// test.spec.js
describe('Override baseUrl test', () => {
  it('Visits a different domain', () => {
    // What code below correctly overrides baseUrl for this test?
  });
});
Acy.visit('https://otherdomain.com/page');
Bcy.visit('/', { baseUrl: 'https://otherdomain.com' });
CCypress.config('baseUrl', 'https://otherdomain.com'); cy.visit('/page');
Dcy.visit('/page').baseUrl('https://otherdomain.com');
Attempts:
2 left
💡 Hint
Try using a full URL in cy.visit to override baseUrl temporarily.