Samkayoki
Samkayoki

Reputation: 89

Cypress test fails but works when tested manually

I am trying to test the ability to retrieve results from a database. When running a test that logs in, navigates to the search tab and searches a word, I am getting a 401. If I go to the website, use the same log in information and do the exact same steps, it works perfectly. Here's the last few steps of the test:

cy.contains('Search').click(); //open tab
cy.contains('Search by').click();
cy.contains('Name').click(); 
cy.get('.search-field').type('Jane');
cy.get('.search-btn').click();

There's a drop down menu for what you want to search by, a textfield for the search word and a search button. I can't share all of the code but I can see from the video that logging in and all the following steps are performed as supposed to. What kind of things can cause a cypress test to return different results as opposed to manually performing the action? I added wait(2000)'s in between the steps but it made no difference.

Upvotes: 0

Views: 718

Answers (2)

Samkayoki
Samkayoki

Reputation: 89

Cypress deletes localStorage in between tests. In this case that deleted the authorization token which is why I was getting the 401. I installed this package: https://www.npmjs.com/package/cypress-localstorage-commands

After the log in I use

cy.saveLocalStorage();

And before making search

cy.restoreLocalStorage();

Upvotes: 0

Put that sock away
Put that sock away

Reputation: 162

Maybe there is an event that needs to be triggered before the page is able to search.

Take a look at the element you are typing into in the devtools, under the Event Listeners tab.

For example, the StackOverflow search box has an event for s-popover:show listed there - if testing that you would .trigger('s-popover:show') to fire that event and display the instruction tooltip.

So try something like this

cy.get('.search-field')
  .type('Jane')
  .trigger('change')  // or .trigger('input')

Upvotes: 2

Related Questions