Categories
Programming

Cypress E2E testing best practices + gotchas

Cypress E2E testing best practices + gotchas

Cypress is a new testing framework that we just moved to, so it is common to have growing pains and learning the ins and outs as we work with it more. Here are some common practices that we found to prevent any flakiness in the tests.

  • Always clean up any entities in the test suite before and after the test suite

E.g in the `beforeEach() or before()`

before(() => {

   cy.serviceVersion(‘DELETE’, ‘service-version-new’).then(() => {

     cy.servicePackage(‘DELETE’, ‘service-package-new’)

   })

 })

Also make sure to delete it in the `afterEach() or after()` as well.

afterEach(() => {

   cy.serviceVersion(‘DELETE’, ‘service-version-new’).then(() => {

     cy.servicePackage(‘DELETE’, ‘service-package-new’)

   })

 })

This will minimize any lingering entities and make the tests more independent and deterministic

  • Make sure the element you are selecting has a unique selector

Cypress will error if it finds multiple elements selected, so make sure that the element that you are selecting has a unique data-testid or selector so that it doesn’t potentially return more than one value.

       cy.get(‘[data-testid=”i-am-very-unique”]’).click()

  • Make sure you assert on elements that are unique to the page you are testing

This goes in hand with point 2, but the Cypress test environment is very eager so as soon as it finds an element matching the selector, it will act on it, even if it hasn’t navigated to the proper page yet – it will attempt to assert it on the current page if there is an matching element. So make sure the selector is unique to the page you are testing.

     cy.get(‘[data-testid=”i-only-exist-on-the-page-you-want-to-test”]’).click()

  • Be careful of using `.contains()` – it may not work the way you expect it to

In cypress, .contains() will yield a DOM element which you can chain other commands to, but it will operate under the scope of that selected DOM element. This can cause undesired assertions if we are inadvertently chaining a lot of assertions. If we just want to make a straight assertion on the DOM, we should use .should(‘contain’) instead

cy.get(‘foo’).contains(‘bar’) // everything chained after this will operate under the foo element that contained bar

cy.get(‘foo’).should(‘contain’, ‘bar’) // a straight assertion that doesn’t yield unexpected side effects when chaining

  • Try to assert on an element on the page before navigating or clicking somewhere

In Cypress, it will eagerly try to click or navigate as soon as it finds the element on the page before data is fully rendered – this leads to unexpected consequences. Instead of a .wait we can also assert on something in the page which will cause Cypress to wait until that element loads before the next step. 

cy.get(‘[data-testid=”Plugin-card”] .empty-state-content’).should(‘exist’) // this causes Cypress to wait until the KEmptyState has loaded which was a result of an XHR request returning an empty dataset. This ensures that Cypress waited for that request to finish before moving to next step.

cy.get(‘[data-testid=”entity-button”]’).click() // and then click on the entity button. Had we not done the previous assertion, Cypress would have clicked this immediately and any props or params that we got from any XHR requests would not have been set correctly.

 

Categories
Business Tech

Ideas and Improvements for Apple’s Siri

Siri, the personal digital assistant
Siri, the personal digital assistant

Recently I’ve been quite interested in Apple’s Siri, ever since I received my iPhone 4S and watching the WWDC Developer’s conference. At first I thought it was just a novelty, but upon seeing the upcoming features in iOS 6, my interest has been heightened. I’m interested in machine learning, AI, UI design, and aggregating content, as I’ve tried to do before. See: History of speech AI and How Siri works.

Concept and Functionality
Siri is essentially a search / AI engine thats powered by the voice.

The original Siri application relied upon a number of partners, including:
OpenTable, Gayot, CitySearch, BooRah, Yelp, Yahoo Local, ReserveTravel, Localeze for restaurant and business questions and actions;
Eventful, StubHub, and LiveKick for events and concert information;
MovieTickets, Rotten Tomatoes, and the New York Times for movie information and reviews;
Bing Answers and Wolfram Alpha for factual question answering;
Bing, Yahoo, and Google for web search.
Apple integrated it with default iOS functionality, such as contacts, calendars and text messages. It also supports search from Google, Bing, Yahoo, Wolfram Alpha and Wikipedia. Siri also works with Google Maps and Yelp! search in the United States only.

Context specific service searching
What I would like to see is Siri searching certain services depending on what keywords are being said. For example, if I say a query such as “What is the Earth’s circumference?” or “What is the area of a circle?” then it should direct me to Wolfram Alpha because Siri recognizes these queries as mathematical questions.
Asking about the news should take me to the New York Times, CNN or maybe show me some blog posts, tweets, etc.
Famous persons or companies can take me to their wikipedia page, twitter page, facebook page and website.
If I asked a knowledge question such as “Who was Rene Descartes?” or “Who invented basketball?” then it should direct me to a Wikipedia article or Yahoo/Bing Answers page. Location queries should pull up Google (or soon Apple) Maps.
Programming questions should take me to StackOverflow or some related forums.
Food queries will pull up Yelp and OpenTable to make a reservation and read reviews.
Asking about travel should pull up Priceline/Expedia/Kayak and TripAdvisor.
Movie based queries should take me to Fandango and Rottentomatoes, concert tickets to TicketMaster and event tickets to EventBrite.
Job related queries can take me to LinkedIn or Glassdoor.
In short, Siri should link all these Web 2.0 companies (that all have an API) together. Its the ultimate content aggregator.

update:
I think it would be better if you said the name of the service that you want Siri to pull from if there’s multiple sources.
So for example, if I were to say “Java, Wikipedia” – then Siri would pull from Wikipedia. But “Java, Google” would just do a google search,
and “Java, Stack Overflow” would search stack overflow for the keyword. This would make it easy and intuitive for Siri to bring up the correct response.

Integration with web and OS X
How about having Siri come to Macs and PCs? How about having Siri be accessible as a web application running on the cloud? Almost all computers come with microphones, so why not? I see the advantage of making Siri exclusive to iOS but soon I think Apple should make it widely available.

Text input
The main problem with me using Siri is that it looks awkward when I’m talking to it in a public place, or using it at a meeting can be distracting. So Siri should take text input to fix that. I’m sure some Java parser in the backend is translating natural speech into strings anyway, so it shouldn’t be too hard to make a text based option.

Expanding services
Siri should be able to integrate with countless Web 2.0 services. And it should be locale specific. If I’m in China, then Siri should integrate with Baidu, QQ, Sina, etc. If I’m looking for a deal, then Siri can pull up Groupon. Point is, Siri can be the glue that ties all these services together. And maybe pulling up Youtube when I’m looking for a funny cat video or something. Perhaps Siri can learn from users in general, in context, and know what to say in response depending on what other users have said to it (a la Cleverbot).

Social integration and Ad delivery
Siri now provides Apple with the same data as Facebook. It can learn about a user’s behavior, their location, their preferences, their hobbies, etc. Anything about a user can be learnt through Siri, so now Apple can do anything and everything that Facebook can do, including delivering targeted ads. If Siri knows you’ve been going to mexican restaurants lately, maybe it can show you an ad from another mexican restaurant nearby. Or maybe suggest you to buy Star Wars from iTunes or Netflix if you’ve been watching sci-fi movies lately. Or maybe show some ads from ESPN if you’ve been watching or asking about sports alot. Or direct me to the iTunes store for Usher’s new album or concert if I’ve been listening to a lot of his songs. Since Siri knows everything about you, it can be the ultimate ad and content delivery vessel.

Better Multilingual support and Translation
Siri will integrate with over 15 languages with iOS 6, so I would like to see quality recognition for different dialects and accents, particularly for Asian languages. And this would probably require a lot of native speakers and language specialists, but I think if Siri can do this well, then it could also be the best translator (by storing speech patterns and tonal variance). We all know Google Translate sucks for Asian languages, so I’m sure Apple can do it better!

Now that Siri is multilingual and will integrate with car manufacturers soon (potentially replacing navigation), I see lots of potential with it in the future. It has the possibility of overshadowing Google or Facebook, since it both searches by voice, and retains user information. So these are just some of my ideas for future improvements from Siri, but where it goes remains to be seen.