In Cypress, set a token in localStorage before test

Here’s an example of adding a command cy.login() that you can use in any Cypress test, or put in a beforeEach hook. Cypress.Commands.add(‘login’, () => { cy.request({ method: ‘POST’, url: ‘http://localhost:3000/api/users/login’, body: { user: { email: ‘jake@jake.jake’, password: ‘jakejake’, } } }) .then((resp) => { window.localStorage.setItem(‘jwt’, resp.body.user.token) }) }) Then in your test: beforeEach(() => … Read more

How to test file inputs with Cypress?

it(‘Testing picture uploading’, () => { cy.fixture(‘testPicture.png’).then(fileContent => { cy.get(‘input[type=”file”]’).attachFile({ fileContent: fileContent.toString(), fileName: ‘testPicture.png’, mimeType: ‘image/png’ }); }); }); Use cypress file upload package: https://www.npmjs.com/package/cypress-file-upload Note: testPicture.png must be in fixture folder of cypress

Cypress get href attribute

The code below should do what you’re trying to achieve. There is also an entire recipe with suggestions on how to test links that open in new tabs. it(‘Advertise link should refer to Contact page’, () => { cy.get(‘div.footer-nav > ul > li:nth-child(2) > a’) .should(‘have.attr’, ‘href’).and(‘include’, ‘contact’) .then((href) => { cy.visit(href) }) }) I … Read more

How to check if element exists using Cypress.io

I’ll just add that if you decide to do if condition by checking the .length property of cy.find command, you need to respect the asynchronous nature of cypress. Example: Following condition evaluates as false despite appDrawerOpener button exists if (cy.find(“button[data-cy=appDrawerOpener]”).length > 0) //evaluates as false But this one evaluates as true because $body variable is … Read more

Cypress causing type errors in jest assertions

I ran into this problem yesterday. It seems that what you are saying is correct, cypress and jest both declares types for expect. In this case the cypress declaration seem to be the one that is picked up. Here’s an issue from the cypress repo regarding this: https://github.com/cypress-io/cypress/issues/1603 The solution mentioned in there worked for … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)