Javier Villanueva
Reacticon 2018
Javier Villanueva
@jahvi
http://jahvi.com
javier@medialounge.co.uk
kateandtoms.com
E2E
UNIT
INTEGRATION
Increasing scope
More confidence
Faster
Better isolation
Fast, easy and reliable testing for anything that runs in a browser
Time Travel
Debuggability
Real time reloads
Automatic waiting
beforeEach(() => {
cy.viewport(1440, 900);
cy.visit('https://demo.vuestorefront.io');
cy.get('[data-testid="accountButton"]').click();
});
describe('Login', () => {
it('displays error message on invalid credentials', () => {
cy.get('input[name="email"]').type('test@email.com');
cy.get('input[name="password"]').type('test{enter}');
cy.get('[data-testid="notificationMessage"]').should(
'contain',
'You did not sign in correctly or your account is temporarily disabled'
);
});
it('displays success message on valid credentials', () => {
cy.get('input[name="email"]').type('test@email.co.uk');
cy.get('input[name="password"]').type('Letmein!{enter}');
cy.get('[data-testid="notificationMessage"]').should(
'contain',
'You are logged in'
);
});
});
describe('Quick buy', () => {
it('can add product to cart from category page', () => {
cy.get('.products .item')
.first()
.find('.size .swatch-option')
.first()
.click();
cy.get('.products .item')
.first()
.find('.color .swatch-option')
.first()
.click();
cy.get('.products .item')
.first()
.find('button[type="submit"]')
.click({ force: true });
cy.get('.message-success')
.invoke('text')
.should('match', /You added (.*) to your shopping cart/);
});
});
describe('Quick buy', () => {
it('can add product to cart from category page', () => {
cy.server();
cy.route('POST', '**/checkout/cart/add/**').as('addToCart');
cy.wait('@addToCart');
// Run assertions
});
});
Repeatable
Independent
<button id="main" class="btn btn-large" data-testid="submitBtn">Submit</button>
cy.get('button').click();
cy.get('.btn.btn-large').click();
cy.get('#main').click();
cy.contains('Submit').click();
cy.get('[data-testid="submitBtn"]').click();
beforeEach(() => {
cy.setCookie('theme', 'a');
});
describe('Button', () => {
it('can be clicked', () => {
cy.get('[data-testid="buttonA"]').click();
});
});
beforeEach(() => {
cy.setCookie('theme', 'b');
});
describe('Button', () => {
it('can be clicked', () => {
cy.get('[data-testid="buttonB"]').click();
});
});
beforeEach(() => {
cy.visit('https://demo.deity.io/customer/account/login');
cy.login('test@email.co.uk', 'testtest123!').then(() =>
cy.visit('https://demo.deity.io/customer/account')
);
});
describe('Login', () => {
it('can log user in', () => {
cy.get('.icon-logout').should('exist');
cy.get('.col-md-9 > h1').should('contain', 'My Dashboard');
});
});
beforeEach(() => {
cy.visit('https://demo.deity.io/customer/account/login');
cy.get('input[name="email"]').type('test@email.co.uk');
cy.get('input[name="password"]').type('testtest123!{enter}');
});
describe('Login', () => {
it('can log user in', () => {
cy.get('.icon-logout').should('exist');
cy.get('.col-md-9 > h1').should('contain', 'My Dashboard');
});
});
Cypress.Commands.add('login', (email, password) => {
return cy.request('POST', 'https://demo.deity.io/api/customer/sign-in', {
email,
password
});
});
describe('Search', () => {
it('shows empty message when no results', () => {
cy.get('.right-icons > [data-testid="openSearchPanel"]').click();
cy.focused().type('product does not exist');
cy.get('[data-testid="searchPanel"]').should(
'contain',
'No results were found'
);
});
});
describe('Search', () => {
it('shows empty message when no results', () => {
cy.server();
cy.route(
'POST',
'**/api/catalog/vue_storefront_catalog/product/_search?**',
{
took: 0,
timed_out: false,
_shards: { total: 5, successful: 5, skipped: 0, failed: 0 },
hits: { total: 0, max_score: null, hits: [] }
}
);
cy.get('.right-icons > [data-testid="openSearchPanel"]').click();
cy.focused().type('yoga');
cy.get('[data-testid="searchPanel"]').should(
'contain',
'No results were found'
);
});
});
Focus on real tests, tools like Cypress will help make this easier.