https://cypress.io logo
Variable Issue
p

powerful-camera-26936

05/25/2023, 2:35 AM
Hello! I'm quite new to Cypress, but I'm working hard to learn! I'm trying to build out tests for APIs, but am getting stuck at getting the authorize token into the test from my custom command. I am able to see it in the alias I created, but the variable on the actual test says it isn't defined. Would someone please show me what's wrong? I'm so interested in learning and am frustrated at getting stuck on this! I covered up some confidential things in the screenshots, but I don't think it will hinder people from seeing the code . The first screenshot is the custom command and the second is the test.

https://cdn.discordapp.com/attachments/1111120414022832158/1111122195641553016/image.png

https://cdn.discordapp.com/attachments/1111120414022832158/1111122195951923261/image.png

d

dry-portugal-25841

05/25/2023, 7:49 AM
token is local variable, so it will not be accessible outside of command. you should try to use environment variables https://docs.cypress.io/guides/guides/environment-variables
p

powerful-camera-26936

05/26/2023, 2:27 AM
Thank you! I will try this out!
h

happy-tiger-9701

05/26/2023, 9:15 AM
You need defined token as global variable using setter and getter in test https://docs.cypress.io/api/commands/task#Save-a-variable-across-non-same-origin-URL-visits
g

gray-kilobyte-89541

05/26/2023, 10:44 AM
Use cypress-data-session plug-in to init that token and access it via context property or Alia’s