Lädt...


🔧 Three optional styles for UI assertions in Cypress


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

.contains()

Often, I just want to scan over a UI in Cypress and make sure that a set of text labels are present. It's typically been my habit to style these sorts of tests like this:

cy.contains('element.class', 'Element text')
  .should('be.visible');    

I love the simplicity of cy.contains(), and the log output of this command will show what text was queried for, but not in the clearest, most visible way.

Cypress log output using cy.contains()

.should()

After a recent code review, I noticed a team-mate styling a similar query like this:

cy.get('element.class')
  .should('be.visible')
  .should('contain.text', 'Element text');

After a bit of thought, I realized that this would make for much better output in the Cypress runner log. Here's a screenshot where you can see that the expected text is now highlighted and easier to read. This makes debugging a much simpler process when we get feedback about what text the query was looking for.

Cypress log output showing an assertion with .should()

expect()

However, we can go even further and have the Cypress runner log tell us both which string was expected and what was actually found.

cy.get('element.class')
  .should('be.visible')
  .invoke('text')
  .then((text) => {
    expect(text).eq('Element text');
  });

As you can see, this provides the richest output to the log, showing both what was expected and what was found.

Cypress log output showing an assertion with expect()

This style is certainly more verbose, but the additional clarity in log output may make it worth your time. Having consistent style in a codebase can be valuable, but I think it's ok to mix all these various approaches together as long as your focus is on test effectiveness. Hopefully this article gets you thinking about how you might want clearer logging in certain situations, and how to change up your code style based on your situational needs.

addendum

Gleb Bahmutov talks about this in his video "Use Stronger Assertions":

You can see that he opts for the second style, using a .should() assertion to highlight just the expected text in the log. I think this is actually the best style for most use cases, but it can be helpful to lean on richer logging at times, especially when debugging a test as it's being written or modified.

...

🔧 Three optional styles for UI assertions in Cypress


📈 85 Punkte
🔧 Programmierung

🔧 Complete Cypress Tutorial: Learn Cypress From Scratch


📈 33.09 Punkte
🔧 Programmierung

🔧 Cypress vs. Sorry-Cypress - Decoding the Drama


📈 33.09 Punkte
🔧 Programmierung

🔧 Optional vs. Undefined: How To Check for Optional Properties


📈 30.33 Punkte
🔧 Programmierung

🔧 How Do I Make RegEx Optional? Specify Optional Pattern in Regular Expressions


📈 30.33 Punkte
🔧 Programmierung

🔧 Testing with the xUnit Framework - Theories and Assertions (3 of 12) | Automated Software Testing


📈 26.41 Punkte
🔧 Programmierung

🔧 Stop requiring only one assertion per unit test: Multiple assertions are fine


📈 26.41 Punkte
🔧 Programmierung

📰 Aduket - Straight-forward HTTP Client Testing, Assertions Included


📈 26.41 Punkte
📰 IT Security Nachrichten

🐧 Microsoft joins LOT Network, helping protect developers against patent assertions


📈 26.41 Punkte
🐧 Linux Tipps

🔧 Top 5 Reasons to Use Shouldly for Assertions in .NET


📈 26.41 Punkte
🔧 Programmierung

🔧 Using Zero-Width Assertions in Regular Expressions


📈 26.41 Punkte
🔧 Programmierung

🔧 Generate assertions using AI


📈 26.41 Punkte
🔧 Programmierung

🔧 Type Guards & Type Assertions


📈 26.41 Punkte
🔧 Programmierung

📰 Meet Spade: An AI Method for Automatically Synthesizing Assertions that Identify Bad LLM Outputs


📈 26.41 Punkte
🔧 AI Nachrichten

🔧 Playwright Tutorial for Beginners 9 - Assertions


📈 26.41 Punkte
🔧 Programmierung

🔧 Best practices with const assertions in TypeScript


📈 26.41 Punkte
🔧 Programmierung

🔧 Type Assertions, Trucks, and Aliens


📈 26.41 Punkte
🔧 Programmierung

🔧 Testing with the xUnit Framework - More Assertions (4 of 12) | Automated Software Testing


📈 26.41 Punkte
🔧 Programmierung

📰 Move over, Wordle: LinkedIn adds three daily puzzle games - competition optional


📈 23.9 Punkte
📰 IT Nachrichten

🔧 Flexbox button with 3 styles and variations


📈 18.15 Punkte
🔧 Programmierung

🔧 "Learning Styles: A Myth?"


📈 18.15 Punkte
🔧 Programmierung

🔧 Api Architecture Styles using GraphQL


📈 18.15 Punkte
🔧 Programmierung

🔧 Use Styles to Improve the UI (5 of 18) | Building Apps with XAML and .NET MAUI


📈 18.15 Punkte
🔧 Programmierung

📰 FOSS Weekly #23.14: Immutable Linux Distros, Gedit Tweaks, Color Styles in Linux Mint & More


📈 18.15 Punkte
🐧 Unix Server

🔧 How to Fix "Styles and JS Not Loading on Page Reload" in React When Using useParams()


📈 18.15 Punkte
🔧 Programmierung

🔧 Creating Global Styles for your Web App


📈 18.15 Punkte
🔧 Programmierung

🪟 Make professional-looking Word docs with themes and styles – here's how!


📈 18.15 Punkte
🪟 Windows Tipps

🔧 Exploring Different API Architecture Styles: GraphQL, gRPC, Sockets, and SOAP


📈 18.15 Punkte
🔧 Programmierung

📰 Cleanroom styles


📈 18.15 Punkte
🐧 Unix Server

🐧 Linux Mint 21.2 Simplifies Theming with ‘Cinnamon Styles’


📈 18.15 Punkte
🐧 Linux Tipps

🐧 ‘Dash to Dock’ Adds New Launcher Styles, Support for GNOME 3.28


📈 18.15 Punkte
🐧 Linux Tipps

🔧 The Power of Customized Signature Styles


📈 18.15 Punkte
🔧 Programmierung

📰 Harry Styles: Vermögen, Tattoos, Twitter und Schwester Gemma


📈 18.15 Punkte
🖥️ Betriebssysteme

matomo