This article is about Provide a
  • 12-Feb-2023
Lightrun Team
Author Lightrun Team
Share
This article is about Provide a

Provide a “Cypress” way to access textContent (and/or innerText) – .text() command in cypress-io cypress

Lightrun Team
Lightrun Team
12-Feb-2023

Explanation of the problem

Unfortunately, Cypress does not have a built-in method to add a .text() method similar to jQuery’s .text() method. However, you can achieve this behavior by adding a custom command in Cypress.

Here’s an example of how you could create a custom command that mimics jQuery’s .text() method:

Cypress.Commands.add('text', { prevSubject: 'element' }, (subject, options) => {
  if (options === 'innerText') {
    return subject[0].innerText;
  } else {
    return subject.text();
  }
});

With this custom command, you can now use cy.text() in your tests and pass the 'innerText' option to get the innerText value of an element:

cy.get('#my-element').text('innerText');

This custom command uses the prevSubject property to specify that the subject should be an element, and then it either returns the result of calling .text() on the subject or the innerText property of the first element in the subject array, depending on the value of the options argument.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Provide a “Cypress” way to access textContent (and/or innerText) – .text() command in cypress-io cypress

In Cypress, it is important to access the text content of elements accurately and reliably to avoid issues with false positives and formatting changes. When accessing the text content of an element, it is common to use the .text() method, however, this method may not always return the expected results.

One solution to this issue is to use the .invoke('text') method in conjunction with other methods such as .then() and .trim(). The .invoke('text') method allows you to access the textContent property of an element, which provides a straightforward representation of the text content. The .then() method allows you to process the result of the .invoke('text') method as a Promise, and the .trim() function is used to remove any whitespace from the beginning and end of the text content.

cy.get('#element').invoke('text').then((text) => {
  const processedText = text.trim();
  // use processedText for assertions
});

By using this combination of methods, you can ensure that the text content you access is accurate and not affected by formatting changes. This approach is especially useful if you want to avoid issues with false positives and ensure that your tests are not brittle.

Other popular problems with cypress-io cypress

Problem: Slow performance when running tests with a large number of elements

When running tests with a large number of elements, Cypress can experience slow performance due to the need to interact with each element individually. This can lead to long test execution times and decreased productivity.

Solution:

To address this issue, it is recommended to limit the number of elements that are interacted with in a single test. This can be achieved by breaking down tests into smaller, more focused tests that only interact with the necessary elements. Additionally, using the cy.get method to retrieve specific elements, rather than using more general methods like cy.find, can help to improve performance.

Problem: Tests that are brittle and prone to breaking with changes to the application

One of the biggest challenges with end-to-end testing is maintaining the stability of tests over time as the application changes. Tests that are overly reliant on specific element selectors, for example, are prone to breaking when the application changes and these selectors are no longer valid.

Solution:

To address this issue, it is important to write tests that are flexible and can adapt to changes in the application. This can be achieved by using data-driven testing techniques, which allow you to define the elements you are testing in a data file that can be easily updated. Additionally, it is important to write tests that are focused on testing the functionality of the application, rather than specific element selectors.

Problem: Debugging tests can be difficult and time-consuming

Debugging tests can be difficult and time-consuming, especially when tests are failing for complex or obscure reasons. This can be a major obstacle to efficient testing and can slow down development.

Solution:

To address this issue, Cypress provides powerful debugging tools that make it easier to understand why tests are failing. For example, the time-travel debugging feature allows you to step through the execution of your tests in real-time, observing the state of the application and the DOM at each step. Additionally, the real-time reloading feature allows you to make changes to your tests while they are running, which can greatly speed up the debugging process

A brief introduction to cypress-io cypress

Cypress is an end-to-end testing framework designed to make testing modern web applications simple and fast. It provides a streamlined, browser-based testing environment that allows developers to write, run, and debug tests directly in the browser. Cypress leverages the same environment that the application is running in, providing a more accurate and efficient testing experience.

Cypress utilizes a JavaScript API that interacts with the DOM and the browser directly, enabling it to access and manipulate elements and their behaviors in real-time. This API is designed to be easy to use, with a simple syntax that makes it possible to write tests quickly and effectively. Additionally, Cypress includes features such as automatic waiting, real-time reloading, and time-travel debugging, which make it easier for developers to write, run, and debug tests. These features, combined with the fast, browser-based testing environment, make Cypress a powerful tool for end-to-end testing.

Most popular use cases for cypress-io cypress

  1. End-to-end testing of web applications Cypress is a powerful tool for end-to-end testing of web applications. It provides an easy-to-use API for interacting with elements on a page, making it possible to test the full user experience, from navigation to the submission of forms. For example, you can use the following code to navigate to a page and fill out a form:
cy.visit('https://example.com')
cy.get('#form-username').type('my-username')
cy.get('#form-password').type('my-password')
cy.get('#form-submit').click()
  1. Real-time reloading and time-travel debugging Cypress also provides real-time reloading and time-travel debugging features, which can greatly speed up the development process. The real-time reloading feature allows you to make changes to your tests while they are running, which can save you time and effort by eliminating the need to constantly stop and restart your tests. The time-travel debugging feature allows you to step through the execution of your tests in real-time, observing the state of the application and the DOM at each step.
  2. Automated testing of web applications in a real browser environment Finally, Cypress provides a true end-to-end testing experience by executing tests directly in a real browser environment. This provides a more accurate testing experience, as tests run in the same environment as the users of your application, allowing you to catch problems that may not be evident in a simulated environment.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.