How to access the value of baseURL in Cypress

JavascriptGlobal VariablesCypress

Javascript Problem Overview


I'm just starting to learn JavaScript and have been using Cypress to automate some regression tests. The test I writing currently is meant to verify link's text and href in a header and footer.

The issue I am having is that these tests need to be run across various environments and I cannot seem to access the baseUrl property set in the cypress.json in order to set the domain in my assertion.

In the script that follows it is the line cy.get("a").should("have.attr", "href", baseUrl + footerLink.link):

    it.only("translates the content info section", () => {
      cy.wrap(orbitData).each(service => {
        cy.visit(service.name);
        cy.get("#orb-contentinfo > div > ul > li").each(($li, index) => {
          let footerLink = service.links[index]
          cy.wrap($li).should("have.text", footerLink.linkText)
          .within(($li) => {
            cy.get("a").should("have.attr", "href", baseUrl + footerLink.link)
          });
        });
      });
    });

So far I have tried a number of things, I'm a bit embarrassed to list them all, I'm new to this so they're probably insane and definitely just guesses; amongst them were Cypress.env('CYPRESS_baseUrl') and Cypress.baseUrl. Each time it just comes back as undefined.

Or, if I'm attacking this in completely the wrong way any guidance on a better way would be appreciated. I'd be grateful for any help or guidance, thanks.

Javascript Solutions


Solution 1 - Javascript

You can use the Cypress.config() command.

To get baseUrl value use Cypress.config().baseUrl or Cypress.config('baseUrl').

Solution 2 - Javascript

cy.visit('/') will visit the baseUrl if you have it defined in your cypress.json. (Source)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid BoydellView Question on Stackoverflow
Solution 1 - JavascriptDiogo RochaView Answer on Stackoverflow
Solution 2 - JavascriptJakob Jan KammingaView Answer on Stackoverflow