I am struggling with some problems in Cypress when i want to use a variable outside of a method. Let's explain the environment , i got two files, one .spec which is the main for running tests and one .js which is the class where I'm building my methods.
class createCan{
getRefferal = () => {
return cy
.request('someURL')
.its('body')
.then((response) => {
var randomNumber = Math.floor(Math.random() * response.length);
let referral = response[randomNumber].name;
return referral;
});
};
export default new createClass();
If I want to access referral in my .spec file it says that referral it's undefined/empty.
let referral;
describe("'Create Candidate' tests", () => {
beforeEach(() => {
loginPage.login(loginPage.testUsername);
createClass.getRefferal();
cy.log(referral);
});
The result of cy.log it's empty :( What should i change to the code so I can be abble to work with value of referral get by that API.