This post is a continuation of Advance Cypress Interview Questions – Part 2 for any QA Automation interview. 

19. What is after:run event in cypress?

after:run event is executed after all the test completed in cypress, This is very useful when you want to generate reports or collate/merge the reports. This after:run typically used in plugin files.

Example:

module.exports = (on, config) => {

on('after:run', async () => {

await exec("npx jrm ./cypress/reports/junitreport.xml ./cypress/reports/junit/*.xml");

});

}

20. What is cy.task() functions in Cypress?

cy.task() is the powerful command in cypress which helps to create some set of actions outside the scope of cypress. You can execute any node commands using the task. There are lot of things we can do with the cy.task() below is just one example.

Example:

Problem: cy.log() doesn’t work if you are running cypress CLI, we can’t use console.log() directly on our cypress framework to fix this issue we can take a help of task.

Inside plugin/index.js you can write some code like below.

on('task', {

log (message) {

console.log(message)

return null

}

})

And once you write the above code you can call this in your script like below.

describe('Demo', () => {

it('HTML DOM', () => {

       cy.task('log', 'This will be output to the terminal');

});

});

21. What is cy.exec() function in cypress?

cy.exec () is used to run the system command.

Example:

cy.exec('npm run build')

22. How can I read file in Cypress?

Cypress provides inbuild readfile capability cy.readFile() can be used for this purpose

Example:

Consider I have .json file like this

{

  "name":"somename"

}

I can read the above file in cypress like below

cy.readFile('data.json').then((d)=>{

cy.log(d.name); //prints somename

})

Cypress supports types if line including text, json, yaml, mp3 etc.

23. How can I write a file in cypress?

Cypress provides inbuild writefile capability cy.writeFile() can be used for this purpose

cy.writeFile('data.json', { fname: 'something', lname: 'example' })

after execution of above command data.json will have below entry:

{

"fname": "something",

"lname": "example"

}

24. How can I run the tests / test suites only on specific browser without doing configuration file Changes?

Cypress provides option to specify the browser name on test suites or testcase level.

There are two options:

  1. Using {browser: '<browser_name>'}
  2. Using Cypress.isBrowser('<browser_name>'))

Option 1: {browser:<browser_name>} option can be used to execute the test cases or testuites at browser level

Example: Run test suite if its only chrome

describe('Google Navigation', { browser: 'chrome' }, () => {

it('Google Search', () => {

//some code

})

})

Example: Run Test cases if its any browser except firefox

describe('Google Navigation', () => {

it('Google Search', { browser: '!firefox' }, () => {

//some code

})

})

Option 2: Using Cypress.isBrowser('<browser_name>'))

Example: Run if its Firefox

it('Example', () => {

if (Cypress.isBrowser('firefox')) {

    //Some code

  }

)}

Example: If its other than chrome

it('Example', () => {

  // true when running in Firefox, etc...

  if (Cypress.isBrowser('!chrome')) {

   //some code

  }

})

25. What are the reporters cypress supports?

By default, Cypress uses the spec reporter to output information to STDOUT. However since cypress built on mocha we can use all available mocha reporters including Junit, Teamcity etc.

26. What is retry in cypress? How can I change the behaviour?

Cypress automatically retries all the command which will query the DOM element for 4 seconds.

If we want to increase re-try timeout then we need to configure defaultCommandTimeout parameter either in cypress.json or should pass it in cypress CLI

Example:

cypress.json

{

"defaultCommandTimeout":30000,

}

Command line:

cypress run --config defaultCommandTimeout=10000

27. How can I run the tests in headless mode?

Cypress support headless mode, we can pass it as command line parameter

Example:

cypress run --headless --browser chrome

28. How can I debug in Cypress?

Cypress provides two way to debug the tests.

Using cy.pause()

Using .debug

Option 1: Using cy.pause

When we use the cy.pause()  to pause the execute, we can do intended action  manually and we can continue next

Example:

cy.visit('https://www.google.com')

cy.pause() //pauses the execution here

we can also use pause command by chaining off with other command

Example:

cy.get('nav').pause()

Option 2: Using .debug

To use this command we need to open the developer tools, This will set at debug point and it will pause the execution in that point

cy.visit('https://www.google.com')

cy.debug() //pauses the execution here

we can also use debug command by chaining off with other commands

cy.get('nav').debug ()


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}