title: Cypress: Mocha Automation Framework: Custom Command : Custom Command not running not running in test
body: <p>I created a custom command that would automate the process of of adding an item to shopping cart.  I ran rand the code in the test PRIOR to add it to the custom command and it worked perfectly.  I add the code 'add to shopping cart' code and created the custom command called <code>AddToCart</code>.  I added the command to my test spec file and tried to run the test.</p>
  <p>Although, the raw automation code is correct, because I created and ran it in the test successfully in the test spec file, it won't run it won't perform as a  custom command, so the test hangs and won't add the item to the shopping cart.   You can watch it <a href="https://www.dropbox.com/s/86qrxdmysvblmj5/custom%20command%20problem.mp4?dl=0" rel="nofollow noreferrer">here</a></p>
  <p>I I've been looking at this code and I'm utterly confused what I might have gone wrong.  I'd love another pair of eyes to take a look at it.</p>
  <p>thanks in advance.</p>
  <p>I'm totally confused how this could happen</p>
  <p>Test spec with custom command</p>
  <pre><code>&gt; /// &lt;reference types=&quot;Cypress&quot; /&gt;
  &gt;  
  &gt;      describe('My test feature',function() {
  &gt;  
  &gt;     beforeEach(function(){
  &gt;        cy.fixture('example').then(function(data){ 
  &gt;  
  &gt;              this.data=data 
  &gt;  
  &gt;             })
  &gt;  
  &gt;     })
  &gt;  
  &gt;     
  &gt;  
  &gt;     it('my first test scenario', function(){
  &gt;    
  &gt;         cy.visit('https://rahulshettyacademy.com/angularpractice/')
  &gt;         cy.get('input[name=&quot;name&quot;]:nth-child(2)').type(this.data.name)
  &gt;         cy.get('select').select(this.data.gender)
  &gt;  
  &gt;         cy.get('input[name=&quot;name&quot;]:nth-child(1)').should('have.value',this.data.name
  &gt; )  
  &gt;         cy.get('input[name=&quot;name&quot;]:nth-child(2)').should('have.attr', 'minlength','2')
  &gt;         cy.get('input[id=inlineRadio3]').should('be.disabled')
  &gt;  
  &gt;         //custom command
  &gt;         cy.get('a[href=&quot;/angularpractice/shop&quot;]').click()
  &gt;         cy.AddToCart('blackberry')
  &gt;       
  &gt;  
  &gt;    
  &gt;      })//end of test case 
  &gt;    
  &gt;  
  &gt;  
  &gt;      })//end of describe
  </code></pre>
  <p>The custom Command</p>
  <pre><code>&gt; // *********************************************** // This example
  &gt; commands.js shows you how to // create various custom commands and
  &gt; overwrite // existing commands. // // For more comprehensive examples
  &gt; of custom // commands please read more here: //
  &gt; https://on.cypress.io/custom-commands //
  &gt; *********************************************** // // // -- This is a parent command -- // Cypress.Commands.add('login', (email, password)
  &gt; =&gt; { ... }) //   Cypress.Commands.add(&quot;AddToCart&quot;, (productName) =&gt; { 
  &gt;  
  &gt;     cy.get('h4.card-title').each(($e1, index, $list) =&gt;{//this go the the array of items for sale using an array
  &gt;  
  &gt;         if($e1.text().includes(productName))//find the product in the script
  &gt;       
  &gt;         {
  &gt;               cy.get('button.btn.btn-info').eq(index).click()//when you have found the product 'add to cart' button
  &gt;     
  &gt;         }
  &gt;     })   })
  &gt;     // // -- This is a child command -- // Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject,
  &gt; options) =&gt; { ... }) // // // -- This is a dual command -- //
  &gt; Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject,
  &gt; options) =&gt; { ... }) // // // -- This will overwrite an existing
  &gt; command -- // Cypress.Commands.overwrite('visit', (originalFn, url,
  &gt; options) =&gt; { ... })
  </code></pre>