---
# System prepended metadata

title: Cypress example

---

# Cypress example

```javascript
// The command to get VForm by its specific name
Cypress.Commands.add("getVForm", (vformName: string, timeout = 1000 * 30) => {
    return cy.get(`div[data-vf="${vformName}"]`, { timeout: timeout });
});

// wait for the ajax event
Cypress.Commands.add('firingAjaxEvent', { prevSubject: 'element' }, (subject, action, onAjaxReturned, numberOfAjaxToWait = 1, debugKey = '') => {
    const ajaxEventKey = `ajax-${debugKey}-${(new Date()).toISOString()}`;
    cy.intercept({
        url: '**/CS.aspx**',
        method: 'POST',
        times: numberOfAjaxToWait,
    }).as(ajaxEventKey).then(() => {
        action(cy.wrap(subject)).then(() => {
            for (let i = 0; i < numberOfAjaxToWait; i++) {
                cy.wait(`@${ajaxEventKey}`); // We want the waits to be fire and forget
            }

            cy.wait(1000); // Wait for setup JS to run

            if (onAjaxReturned) {
                onAjaxReturned();
            }
        });
    });
});

// Get the first VForm with the given VForm name
Cypress.Commands.add("getControl", { prevSubject: 'element' }, (subject, controlName: string, timeout = 1000 * 30) => getChildInElement(subject, `div[id$=":${controlName}"]`, timeout));

// Get the first VForm with the given VForm name
Cypress.Commands.add("getButton", { prevSubject: 'element' }, (subject, controlName: string, timeout = 1000 * 30) => getChildInElement(subject, `div.Button[id$=":${controlName}"] a`, timeout));

// Get the first VForm with the given VForm name
Cypress.Commands.add("getInput", { prevSubject: 'element' }, (subject, controlName: string, timeout = 1000 * 30) => getChildInElement(subject, `input[id$=":${controlName}"]`, timeout));

// Helper tries resolving the inconsistent click behavior
Cypress.Commands.add('mouseClick', { prevSubject: 'element' }, (subject) => cy.wrap(subject).trigger('mouseover').click());


it('Impersonate', () => {
    cy.visit(`CS.aspx?VP3=CMS3&VF=Home`, {
        timeout: 1000 * 60 * 2
    }).then(() => {
        cy.wait(1000);
        cy.getVForm('PageHeader_OverFullscreenSlideshow_VForm').should('be.visible');
        cy.getVForm('PageHeader_OverFullscreenSlideshow_VForm').getControl('WelcomeLbl').click();
        cy.get("a[aria-label='Impersonate Another User']").firingAjaxEvent((button) => button.mouseClick(), () => {
            cy.wait(1000)
            cy.getVForm('ContactImpersonateWidget_VForm')
                .getInput('ImpersonateAC')
                .type('FIRSTNAME.LASTNAME', { force: true });
            // Select option in auto completion 
            cy.get('.c2.ColClass',{timeout:1000 * 60 * 2})
                .contains('FIRSTNAME.LASTNAME').firingAjaxEvent((button) => button.mouseClick(), () => {
                    // Assert
                    cy.get("a[aria-label='Stop impersonating']").firingAjaxEvent((button) => button.mouseClick(), () => {
                        //Assert
                    });
                });
        });
    });
});
```