Here's the structure of my Angular application:
MyApplication
apps
- app1
- src
- app
- assets
- environments //*****USE THIS FILES ****
- app1-e2e
- src
- fixtures
- integration
- plugins
- supports
I'm using Cypress for the e2e tests. For instance, I want to use the base URL, so that it depends on the environment (local, dev, ...):
it('should redirect to release', () => {
cy.get('[data-testid="my-link"]').click();
cy.url().should('include', 'http://localhost.homedepot.com:4200/');
});
if it's the development machine, it will be localhost.homedepot.com:4200. However, if the dev machine it will be myapplication-np.mycompany.com, and so forth.
We already have all this information in the environment files. Should I duplicate them in the e2e folder or is there a way to read them from the environment files.
I came up with a way to quickly switch my environment when I need to, hope it helps.
To use it, add these lines to your
cypress/plugins/index.jsNow you need to set your target environment and configure your environments like this:
In your
cypress.json:You can also set an environment in your
cypress.env.jsonwhich should be in your.gitignoreso it won't be pushed to your remote repo.cypress.env.json:Finally, all you need to do is change
targetincypress.jsonorcypress.env.jsonto quickly switch between your environments. Remember that any value set incypress.env.jsonoverwrite the ones set incypress.json.And then in your code, you would use it like this:
Edit #1
If your environment config already exists somewhere:
Have a look at how to set environment values from
cypress/plugins/index.jshere. They are showing how to get your.env, but you might also be able to read a.configfile.Edit #2:
I was able to read my
root/src/web.configfromcypress/plugins/index.jsusingfsandxml-js: