Created
July 29, 2020 22:07
Cypress Tips/Tricks - Environment variables and test fixtures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Can import these utility functions from another file | |
const getTestEnv = () => Cypress.env("testEnv") || "staging"; | |
const getApiHost = () => Cypress.env("apiHost") || "https://staging.api.com"; | |
context("Some Page Test", () => { | |
const testFixtures = { | |
testing: { | |
username: "testingUsername", | |
password: "testingPassword", | |
meta: { | |
// Other metadata for testing environment | |
domainName: "testingDomainName" | |
}, | |
}, | |
staging: { | |
username: "stagingUsername", | |
password: "stagingPassword", | |
meta: { | |
// Other metadata for staging environment | |
domainName: "stagingDomainName | |
}, | |
}, | |
}; | |
const testFixture = testFixture[getTestEnv()]; | |
before(() => { | |
const { username, password } = testFixture; | |
cy.login(username, password).then((token) => { | |
cy.setCookie("auth_token", token); | |
// In a Cypress task plugin or a cy.request() call that needs to know the API host, auth token, | |
// and potentially other metadata, we pass them through | |
return cy.task("makeRequestToBackend", { | |
token, | |
apiHost: getApiHost(), | |
domain: testFixture.meta.domain, | |
}).then((response) => { | |
// ...do stuff based on the result | |
}); | |
}); | |
}); | |
beforeEach(() => { | |
// Keep the user logged in by preserving the auth token cookie | |
Cypress.Cookies.preserveOnce("auth_token"); | |
// The "baseUrl" config value affects our page object's `cy.visit("/some/page")` calls inside the open() function | |
// by setting a "baseUrl" like "https://staging.app.com" before appending the path i.e. "https://staging.app.com/some/page" | |
SomePage.open(); | |
}); | |
it("should be able to do something on the page", () => { | |
SomePage.someButton.click(); | |
SomePage.someModal.should("be.visible"); | |
// More actions/assertions on the page | |
// We may need to use testFixture.meta.<someProperty> to help us out here | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment