skip to Main Content

How to declare a variable (content) inside a function and pass it to another function?

This is the first function:

export function check_f_e_overview_account_details_status_modal_ok() {
  //CHECK THE STATUS OK BUTTON WORKS
  cy.get("[name = 'accountid']").type('GCS_M822')
  cy.get('.ng-scope > .glyphicons').click()
  //Click the Status edit link
  cy.get('.ng-binding > .ng-scope > .glyphicons').click()
  //Check what the value of the status is and select another status value
  cy.get('h3').contains("Financial Account Status Change").closest("form").find('span').eq(1).then((el) => {
        let content = el.text();
        if (content == "00 (Active)") {
           cy.get('.modal-body > :nth-child(2) > .col-sm-9 > .form-control').select('05 (Decline)')   
        } else //if (content == "05 (Decline)")
        {
        cy.get('.modal-body > :nth-child(2) > .col-sm-9 > .form-control').select('00 (Active)')
        } 
        return cy.wrap(content).as('MyCustomContent')
     });            
      
  cy.get('.modal-body > :nth-child(3) > .col-sm-9 > .form-control').type('No Good Reason')
  cy.get('.modal-body > :nth-child(4) > .col-sm-9 > .form-control').type('Testing Memo')
  
  
  cy.get('.btn-primary > .ng-scope').click()
  //check the update ok modal appears correctly
  cy.get('#updateSuccess').find(".modal-title").contains('Update OK').should('exist')
  cy.get('.modal-body').contains('Financial Account Status Change (General) was updated OK').should('exist')
  cy.get('.modal-footer').find('.btn').contains('OK').should('exist').scrollIntoView().click({force: true})
  

}

This is the second function:

export function check_f_e_overview_account_details_status_modal_isupdated() {
  //CHECK THE STATUS IS UPDATED
  cy.get("[name = 'accountid']").type('GCS_M822')
  cy.get('.ng-scope > .glyphicons').click()

  //Check the status has changed
  cy.get('.ng-binding > .ng-scope > .glyphicons').click()
  cy.get('@MyCustomContent').then(content=>{
  cy.get('.col-sm-9 > .form-control-static > .ng-scope > .ng-binding').contains(content).should('be.visible')
  //cy.get('.ng-binding').parents('.ng-scope').contains(content).should('be.visible')
  })
  }

This is how I am calling the functions so they execute:

it("Account Details Status OK button works",() =>{
 login();
 access_financial_enquiry();
 check_f_e_overview_account_details_status_modal_ok();
 //logout();
})

it("Account Details Status is updated",() =>{
 login();
 access_financial_enquiry();
 check_f_e_overview_account_details_status_modal_isupdated();
 //logout();
})

How do I pass the variable content between functions?

2

Answers


  1. You should use Aliases.

    But keep in mind that:

    All aliases are reset before each test

    So in your example, if you define a variable in the scenario Account Details Status OK button works, then it will not be defined in the other scenario Account Details Status is updated.

    Also, you should avoid to have a dependency of a scenario in another scenario.

    So you could try instead for example:

    it("Account Details Status OK button works",() =>{
     login();
     access_financial_enquiry();
     check_f_e_overview_account_details_status_modal_ok();
     //logout();
    })
    
    it("Account Details Status is updated",() =>{
     login();
     access_financial_enquiry();
     check_f_e_overview_account_details_status_modal_ok();
     check_f_e_overview_account_details_status_modal_isupdated();
     //logout();
    })
    
    Login or Signup to reply.
  2. Provided all the functions are in the same file, you could just use global variables, rather than variables declared within the scope of the function. That way, both functions would be able to read/write to the same variable. Not the most elegant solution, but it’ll do the trick.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search