Cypress Grep Plugin

Profile picture for user arilio666

Before moving deep into the grep plugin, we shall now know what grep is and what it usually does. Grep is an expert in filtering and searching a file with the specified keyword which was provided.

Meaning, out of the billionth lines in a file, if we want the word "Shazam," grep gets it for you. Grep gets all the words matching the specified.

That's enough of grep and now let us see how grep works in cypress.

  • If you got a spec file with 1000s of tests and want to run a specific one from the command line.
  • We can do this using grep.

Enough talking, and let us dive into the practical work.

1.)Installation

npm i -D @cypress/grep
  • Install grep using this.
const registerCypressGrep = require('@cypress/grep')
registerCypressGrep()
  • Paste this into the support/e2e.js file.
require('@cypress/grep/src/plugin')(config);
  • Paste this into the cypress.config.js file.

2.)Hands-On

  • Now consider this below spec file.
describe('Clear Command',()=>{
       
       it('Type Username And Password',()=>{
           cy.visit('https://www.programsbuzz.com/user/login')
           cy.get('form').within(()=>{
               cy.xpath("//input[@id='edit-name']").type('Rataalada').clear()
               cy.get('#edit-pass').clear().type('bat')
               
                    
          })
       })
           it('Within Command Test',()=>{
               cy.visit('http://zero.webappsecurity.com/login.html')
               cy.get('form').within(()=>{
                   cy.get("input[id='user_login']").type('bblabla')
                   cy.get("#user_password").type('mananana')
               })
           })
})
  • There is a requirement that only the second test alone should run.
  • Using grep, we can do this so quickly.
npx cypress run --env grep="Within Command Test" --spec cypress\e2e\Tests\grep.cy.js
  • Using this, we grep the test name and run it alone.
  • We can see that out of two tests, one has run and passed successfully, while the other is still pending.
  • Using the same way, we can run the other test step.

3.)Tags

  • Tags come into play when we want to select a unique test out of most of the common words.
  • We can only partially depend upon words and test names alone because the same names can be used across and grep gets it all commonly it matches.
describe('Clear Command',()=>{
       
       it('Type Username And Password',{tags:'@smoke'},()=>{
           cy.visit('https://www.programsbuzz.com/user/login')
           cy.get('form').within(()=>{
               cy.xpath("//input[@id='edit-name']").type('Rataalada').clear()
               cy.get('#edit-pass').clear().type('bat')
               
                    
          })
       })
           it('Within Command Test',{tags:'@zero'},()=>{
               cy.visit('http://zero.webappsecurity.com/login.html')
               cy.get('form').within(()=>{
                   cy.get("input[id='user_login']").type('bblabla')
                   cy.get("#user_password").type('mananana')
               })
           })
})
  • Here @smoke and @zero have been given for both tests.
npx cypress run --env grepTags="@zero" --spec cypress\e2e\Tests\grep.cy.js      
  • Using grepTags, we can run and run tags attached to tests.

Multiple Tags:

  • We can set multiple tags into grep and run it too.
npx cypress run --env grepTags=@zero+@smoke --spec cypress\e2e\Tests\grep.cy.js  

Conclusion:

This is how grep works, and plenty of tags and filter methods can be used, which will be covered in a separate article.