Top 10 cypress programming interview questions with answers

In this article I am going to explain top 10 cypress programming interview questions with answers. These are the mostly asked interview questions for the different companies

Question 1: Write a Cypress test for a login page

Answer:

describe(‘Login Test’, () => {
it(‘should login successfully’, () => {
cy.visit(‘/login’);

cy.get('#username').type('testuser');
cy.get('#password').type('password123');

cy.get('#login').click();

cy.url().should('include', '/dashboard');
cy.contains('Welcome').should('be.visible');

});
});

Question 2: How do you select an element and enter text?

Answer:

cy.get(‘#username’).type(‘Bhaskar’);
cy.get(‘#email’).type(‘test@example.com’);

Question 3: Write code to select a value from a dropdown

Answer:

cy.get(‘#country’)
.select(‘India’);

cy.get(‘#country’)
.should(‘have.value’, ‘IN’);

Question 4: Write a test to verify multiple products

Answer:

cy.get(‘.product’).should(‘have.length’, 5);

cy.get(‘.product’)
.each(($product) => {
cy.wrap($product)
.should(‘be.visible’);
});

Question 5: Write Cypress code to intercept an API

Answer:

cy.intercept(‘GET’, ‘/api/products’).as(‘getProducts’);

cy.visit(‘/products’);

cy.wait(‘@getProducts’)
.its(‘response.statusCode’)
.should(‘eq’, 200);

Question 6: Write code to validate an API response

Answer:

Get Request:

cy.request(‘GET’, ‘/api/users’)
.then((response) => {
expect(response.status).to.eq(200);
expect(response.body).to.have.property(‘users’);
});

Post Request:

cy.request(‘POST’, ‘/api/login’, {
username: ‘testuser’,
password: ‘password123’
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.token).to.exist;
});

Question 7: Write a custom Cypress command

Answer:

Command.js

Cypress.Commands.add(‘login’, (username, password) => {
cy.get(‘#username’).type(username);
cy.get(‘#password’).type(password);
cy.get(‘#login’).click();
});

Usage in Test:

cy.visit(‘/login’);

cy.login(‘testuser’, ‘password123’);

cy.url().should(‘include’, ‘/dashboard’);

Question 8: Write code to verify table data

Answer:

cy.get(‘table tbody tr’).each(($row) => {
cy.wrap($row).within(() => {
cy.get(‘td’).eq(0).should(‘not.be.empty’);
cy.get(‘td’).eq(1).should(‘not.be.empty’);
});
});

Question 9: Write a Cypress test for a checkbox/radio button

Answer:

Validate Check the checkbox:

cy.get(‘#terms’)
.check()
.should(‘be.checked’);

Validate UnCheck the checkbox:

cy.get(‘#terms’)
.uncheck()
.should(‘not.be.checked’);

Question 10: Write a data-driven Cypress test

Answer:

const users = [
{ username: ‘user1’, password: ‘pass1’ },
{ username: ‘user2’, password: ‘pass2’ },
{ username: ‘user3’, password: ‘pass3’ }
];

users.forEach((user) => {

cy.visit('/login');
cy.get('#username').type(user.username);
cy.get('#password').type(user.password);
cy.get('#login').click();

cy.url().should('include', '/dashboard');

});
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Subscribe to our YouTube Channel: Testing Talks Latest
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox
Scroll to Top
0
Would love your thoughts, please comment.x
()
x