{"__v":0,"_id":"5697f9a81c4dc823005426ab","category":{"__v":50,"_id":"5697efe48d2a770d00d2fd16","pages":["5697f9021c4dc823005426a7","5697f9a7d231880d00676f88","5697f9a72cf4060d004eaa98","5697f9a71c4dc823005426a9","5697f9a82cf4060d004eaa99","5697f9a82cf4060d004eaa9a","5697f9a81c4dc823005426aa","5697f9a869393517000c8451","5697f9a73503e40d0061f4e1","5697f9a869393517000c8452","5697f9a92cf4060d004eaa9b","5697f9a88560a60d00e2c398","5697f9a81c4dc823005426ab","5697fe158560a60d00e2c3a6","5697fe15d231880d00676f8e","56995ac8cb127f0d003cc165","56995ac854d7db17000ee9e5","56995ac8113a901900ef8f78","56995ac8113a901900ef8f77","56a0ff2a8b2e6f0d0018e94b","56ab8b29c159aa0d0070de3d","56ab8b290b9e0b0d00616230","56ab8b294e8ba20d006a2377","56ab8b2935138c0d005c944e","56ab8b2af25f160d00e17ef8","56ab8b624e4b730d009eb09e","56ab8b62f25f160d00e17ef9","56ab8b62d4432d1900eed1b0","56ab8b62678b58170031ebb2","56ab8b62678b58170031ebb1","56ab8bf435138c0d005c9450","56ab8bf44e4b730d009eb0a1","56ab8bf435138c0d005c9451","56ab8bf5f9757e0d007c65e7","56ab8bf58beeff0d003b809a","56ab8c3ef25f160d00e17efc","56ab8c3f4e8ba20d006a237a","56ab8c3f8beeff0d003b809b","56ab8c3f4e4b730d009eb0a2","56ab8c3f0b9e0b0d00616234","56ab8c704e4b730d009eb0a3","56ab8c709327b30d00f7c1f1","56ab8c714e8ba20d006a237c","56ab8c71f9757e0d007c65e9","56ab8c71f9757e0d007c65ea","56ab8ce8678b58170031ebbd","56ab8ce78beeff0d003b809d","56ab8ce70b9e0b0d00616237","56ab8ce84e4b730d009eb0a4","56ab8ce84e8ba20d006a237d"],"project":"568fde81b700ce0d002f4b43","version":"568fde82b700ce0d002f4b46","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-14T18:58:44.771Z","from_sync":false,"order":3,"slug":"cypress-api","title":"Cypress API"},"parentDoc":null,"project":"568fde81b700ce0d002f4b43","user":"568fffce769f210d0013258f","version":{"__v":23,"_id":"568fde82b700ce0d002f4b46","project":"568fde81b700ce0d002f4b43","createdAt":"2016-01-08T16:06:26.373Z","releaseDate":"2016-01-08T16:06:26.373Z","categories":["568fde82b700ce0d002f4b47","568ff0e504440a1700e4cbbd","569002f19ebef90d0087289d","569004f4769f210d00132599","5690056d9ebef90d008728a0","569005d394c5030d0028813a","5690067804440a1700e4cbe2","569137eb3c4f510d00ec9b92","56913815e56a790d008dbfe3","569138ba3c4f510d00ec9b93","5691392f3c4f510d00ec9b94","56913bbe72f2810d007e4cb0","56933b8d6ebadc0d005b71d2","56933b8d6ebadc0d005b71d3","569564facaa32519009c41e6","5696a319b6d61f0d00acfb40","5696a319a857080d0082e8e8","5697efe43503e40d0061f4d1","5697efe48d2a770d00d2fd16","569e9597ffccd10d00a05c59","56a7a1523d33bc2100793d5c","56a7a32ecf6d771700baeee8","56b8b0f7ddeb231700e69825"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":false,"codename":"bar","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-14T19:40:24.977Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"`Cypress.env` allows you to `get` and `set` your environment variables.\n\nThis document covers the API for consuming your environment variables *in your tests*. The [Environment Variable](https://on.cypress.io/guides/environment-variables) guide explains the 4 ways you can set them *outside of your tests*.\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"[Read about environment variables first.](https://on.cypress.io/guides/environment-variables)\",\n  \"title\": \"New to Cypress?\"\n}\n[/block]\n\n# [Cypress.env()](#section-no-arguments-usage)\n\nReturns all of your environment variables as an object literal.\n\n***\n\n# [Cypress.env( *key* )](#section-key-usage)\n\nReturns the value of a single environment variable by its key.\n\n***\n\n# [Cypress.env( *key*, *value* )](#section-key-value-usage)\n\nSets an environment variable for a specific key.\n\n***\n\n# [Cypress.env( *object* )](#section-object-usage)\n\nSets multiple environment variables.\n\n***\n\n# No Arguments Usage\n\n## Get all environment variables.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// cypress.json\\n{\\n  \\\"env\\\": {\\n    \\\"foo\\\": \\\"bar\\\",\\n    \\\"baz\\\": \\\"quux\\\"\\n  }\\n}\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"Cypress.env() // => {foo: \\\"bar\\\", baz: \\\"quux\\\"}\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Key Usage\n\n## Return just a single environment variable value.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// cypress.json\\n{\\n  \\\"env\\\": {\\n    \\\"foo\\\": \\\"bar\\\",\\n    \\\"baz\\\": \\\"quux\\\"\\n  }\\n}\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"Cypress.env(\\\"foo\\\") // => bar\\nCypress.env(\\\"baz\\\") // => quux\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Key Value Usage\n\n## Cypress allows you to change the values of your environment variables from within your tests.\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"Any value you change will be permanently changed for the remainder of your tests.\"\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// cypress.json\\n{\\n  \\\"env\\\": {\\n    \\\"foo\\\": \\\"bar\\\",\\n    \\\"baz\\\": \\\"quux\\\"\\n  }\\n}\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"Cypress.env(\\\"host\\\", \\\"http://server.dev.local\\\")\\n\\nCypress.env(\\\"host\\\") // => http://server.dev.local\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Object Usage\n\n## You can set multiple values by passing an object literal.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// cypress.json\\n{\\n  \\\"env\\\": {\\n    \\\"foo\\\": \\\"bar\\\",\\n    \\\"baz\\\": \\\"quux\\\"\\n  }\\n}\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"Cypress.env({\\n  host: \\\"http://server.dev.local\\\",\\n  foo: \\\"foo\\\"\\n})\\n\\nCypress.env() // => {foo: \\\"foo\\\", baz: \\\"quux\\\", host: \\\"http://server.dev.local\\\"}\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Notes\n\n## Why use `Cypress.env` instead of `cy.env`?\n\nAs a rule of thumb anything you call from `Cypress` affects global state. Anything you call from `cy` affects local state.\n\nMethods on `cy` are local and specific to a single test. Side effects from `cy` methods are restored between each test. We chose to use `Cypress` because changes to your environment variables take effect for the remainder of **ALL** tests.\n\n***\n\n## Why would I ever need to use environment variables?\n\nThe [Environment Variables](https://on.cypress.io/guides/environment-variables) guide explains common use cases.\n\n***\n\n## Can I pass in environment variables from the command line?\n\nYes. You can do that and much more.\n\nThe [Environment Variables](https://on.cypress.io/guides/environment-variables) guide explains the 4 ways you can set environment variables for your tests.","excerpt":"get and set environment variables","slug":"env","type":"basic","title":"env"}

env

get and set environment variables

`Cypress.env` allows you to `get` and `set` your environment variables. This document covers the API for consuming your environment variables *in your tests*. The [Environment Variable](https://on.cypress.io/guides/environment-variables) guide explains the 4 ways you can set them *outside of your tests*. [block:callout] { "type": "info", "body": "[Read about environment variables first.](https://on.cypress.io/guides/environment-variables)", "title": "New to Cypress?" } [/block] # [Cypress.env()](#section-no-arguments-usage) Returns all of your environment variables as an object literal. *** # [Cypress.env( *key* )](#section-key-usage) Returns the value of a single environment variable by its key. *** # [Cypress.env( *key*, *value* )](#section-key-value-usage) Sets an environment variable for a specific key. *** # [Cypress.env( *object* )](#section-object-usage) Sets multiple environment variables. *** # No Arguments Usage ## Get all environment variables. [block:code] { "codes": [ { "code": "// cypress.json\n{\n \"env\": {\n \"foo\": \"bar\",\n \"baz\": \"quux\"\n }\n}\n", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "Cypress.env() // => {foo: \"bar\", baz: \"quux\"}\n", "language": "javascript" } ] } [/block] *** # Key Usage ## Return just a single environment variable value. [block:code] { "codes": [ { "code": "// cypress.json\n{\n \"env\": {\n \"foo\": \"bar\",\n \"baz\": \"quux\"\n }\n}\n", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "Cypress.env(\"foo\") // => bar\nCypress.env(\"baz\") // => quux\n", "language": "javascript" } ] } [/block] *** # Key Value Usage ## Cypress allows you to change the values of your environment variables from within your tests. [block:callout] { "type": "warning", "body": "Any value you change will be permanently changed for the remainder of your tests." } [/block] [block:code] { "codes": [ { "code": "// cypress.json\n{\n \"env\": {\n \"foo\": \"bar\",\n \"baz\": \"quux\"\n }\n}\n", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "Cypress.env(\"host\", \"http://server.dev.local\")\n\nCypress.env(\"host\") // => http://server.dev.local\n", "language": "javascript" } ] } [/block] *** # Object Usage ## You can set multiple values by passing an object literal. [block:code] { "codes": [ { "code": "// cypress.json\n{\n \"env\": {\n \"foo\": \"bar\",\n \"baz\": \"quux\"\n }\n}\n", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "Cypress.env({\n host: \"http://server.dev.local\",\n foo: \"foo\"\n})\n\nCypress.env() // => {foo: \"foo\", baz: \"quux\", host: \"http://server.dev.local\"}\n", "language": "javascript" } ] } [/block] *** # Notes ## Why use `Cypress.env` instead of `cy.env`? As a rule of thumb anything you call from `Cypress` affects global state. Anything you call from `cy` affects local state. Methods on `cy` are local and specific to a single test. Side effects from `cy` methods are restored between each test. We chose to use `Cypress` because changes to your environment variables take effect for the remainder of **ALL** tests. *** ## Why would I ever need to use environment variables? The [Environment Variables](https://on.cypress.io/guides/environment-variables) guide explains common use cases. *** ## Can I pass in environment variables from the command line? Yes. You can do that and much more. The [Environment Variables](https://on.cypress.io/guides/environment-variables) guide explains the 4 ways you can set environment variables for your tests.