Or copy link
Copy link
Scrolling in a web application is a common requirement during automated testing, especially when elements are dynamically loaded or hidden within scrollable containers. This guide focuses on how to scroll in a Flutter web app using Cypress, leveraging a custom Cypress command and implementing detailed logic to ensure your target elements are scrolled into view effectively. Whether you are testing a dynamic user interface or verifying accessibility, mastering this technique will enhance your Cypress testing workflow.
Flutter web apps often utilize custom or non-standard scrolling implementations due to their framework-specific rendering. Identifying and interacting with elements hidden within scrollable areas requires more than default Cypress commands. With Cypress’s flexibility and a custom command, we can efficiently scroll through content until the desired element is visible. In this guide, you’ll learn step-by-step how to scroll in a Flutter web app using Cypress and apply it to test scenarios.
To start, open your Flutter web app in Google Chrome.
overflow-y: scroll
Navigate through the DOM structure to identify the specific scrollable container relevant to your app. In Flutter web apps, scrollable elements typically feature styles such as:
[style*="position: absolute; touch-action: none; overflow-y: scroll;"]
To enable efficient scrolling, we will define a Cypress command, scrollDownUntilVisible, which repeatedly scrolls through a container until the target element becomes visible. Below is the code for the custom command:
scrollDownUntilVisible
Cypress.Commands.add('scrollDownUntilVisible', (selector, maxScrolls = 15) => { let scrollCount = 0; function scrollAndCheck() { if (scrollCount >= maxScrolls) { throw new Error (`Element with selector '${selector}' not visible after ${maxScrolls} scroll attempts.`); } // Get the scrollable container cy.get('[style*="position: absolute; touch-action: none; overflow-y: scroll;"]').then(($scrollable) => { if ($scrollable.length === 0) { throw new Error('Scrollable container not found.'); } const scrollableElement = $scrollable[0]; scrollableElement.scrollTop += 1.2; // Increment scroll position cy.wait(500).then(() => { scrollCount += 1; // Use Cypress.$ to check if the element exists in the DOM const $target = Cypress.$(selector); if ($target.length > 0 && $target.is(':visible')) { // Element is found and visible cy.get(selector).scrollIntoView({ easing: 'linear' }); } else { // Element not yet visible, continue scrolling scrollAndCheck(); } }); }); } scrollAndCheck(); });
scrollTop
scrollIntoView
While scrolling down in a Flutter web app is crucial for navigating and interacting with elements, there are many scenarios where scrolling back to the top is just as important. For example, ensuring key UI elements like navigation menus or icons (e.g., a login icon) are visible at the top of the page after interactions lower on the page. This section will teach you how to implement and use a Cypress command for scrolling up in a Flutter web app.
Below is the scrollUpToTop custom command, which ensures the application scrolls up to the top of a container. It halts when the desired top-most element, such as the login icon, is visible or the scrollable area has reached its limit.
scrollUpToTop
Cypress.Commands.add('scrollUpToTop', (maxScrolls = 15) => { let scrollCount = 0; function scrollUp() { cy.get('[style*="position: absolute; touch-action: none; overflow-y: scroll;"]') .then(($scrollable) => { // Ensure the scrollable element exists if ($scrollable.length === 0) { throw new Error(`Scrollable element not found.`); } const scrollableElement = $scrollable[0]; // Stop scrolling if the login icon is visible cy.get('[aria-label="login-icon"]').then(($loginIcon) => { if ($loginIcon.is(':visible')) { cy.log('Scrolled to the top successfully. "login-icon" is visible.'); return; } // Stop if already at the top if (scrollableElement.scrollTop === 0) { cy.log('Scrolled to the top successfully.'); return; } // Check scroll limit if (scrollCount >= maxScrolls) { throw new Error(`Failed to scroll up to the top after ${maxScrolls} attempts.`); } // Scroll up by decreasing the scrollTop value scrollableElement.scrollTop = Math.max(0, scrollableElement.scrollTop - 100); scrollCount += 1; // Wait for the scroll to settle and then check again cy.wait(500).then(() => { scrollUp(); }); }); }); } scrollUp(); });
login-icon
scrollTop === 0
maxScrolls
cy.wait(500)
Here’s how you can use the scrollDownUntilVisible command in a practical test scenario:
describe('Accessories Section', () => { before(() => { // This will run before each test, ensuring the website is always launched with custom viewport cy.viewport(1500, 780); cy.visit('/'); cy.wait(3000); cy.get('.lds-roller', { timeout: 15000 }).should('not.be.visible'); cy.get('.lds-roller').invoke('css', 'display', 'none'); }); it('verify that the elements are displayed and scroll back up', () => { cy.wait(2000) // Perform actions that may scroll down the page cy.scrollDownUntilVisible('flt-semantics[aria-label="New arrivals"]'); cy.get('flt-semantics[aria-label="New arrivals"]').should('be.visible'); }); });
Here is an example of how to incorporate the scrollUpToTop command in a Cypress test case to verify UI functionality at the top of the scrollable container.
describe('Accessories Section', () => { before(() => { // This will run before each test, ensuring the website is always launched with custom viewport cy.viewport(1500, 780); cy.visit('/'); cy.wait(3000); cy.get('.lds-roller', { timeout: 15000 }).should('not.be.visible'); cy.get('.lds-roller').invoke('css', 'display', 'none'); }); it('verify that the elements are displayed and scroll back up', () => { cy.wait(2000) // Perform actions that may scroll down the page cy.scrollDownUntilVisible('flt-semantics[aria-label="New arrivals"]'); cy.get('flt-semantics[aria-label="New arrivals"]').should('be.visible'); // Scroll back up to the top cy.scrollUpToTop(); cy.get('flt-semantics[aria-label="login-icon"]').should('be.visible'); }); });
Testing Flutter web apps often requires tackling unique challenges, such as custom scrolling behaviors. By defining a tailored Cypress command, you can ensure your automation tests effectively interact with dynamically loaded content. This guide has demonstrated how to scroll in a Flutter web app using Cypress, combining robust logic, clear examples, and actionable tips. With this knowledge, you can confidently handle scroll-based scenarios in your Flutter web testing suite. By mastering these techniques, your Cypress tests will become more efficient and reliable, saving time and enhancing the quality of your applications.
Save my name, email, and website in this browser for the next time I comment.
All the QA News You Need!
Zero Spam, 100% Quality