Cypress' clicking feature does not automatically scroll into view

A new website has been constructed using a combination of Vue/Nuxt and includes SSR, with links located in the footer.

During testing, it was discovered that although the link is present and visible, when attempting to click on it, the error message click() indicates that the link is not in the view.

Subsequent investigation revealed that after the failed attempt to click the link, it can be manually scrolled into view.

What factors might lead to the link not being automatically scrolled into view?

        it('About us page', () => {
            cy.visit('https://test.fooddocs.ee');
            cy.url().should('include', 'https://test.fooddocs.ee');
            cy.get('footer.page-footer').within(() => {
                cy.get('a')
                    .contains('About us')
                    .should('be.visible')
                    .should('have.attr', 'href', '/about')
                    .click();
            });
            cy.url().should('include', siteUrl + '/about');
            cy.get('main h1').should('contain', 'Our team');
        });

https://i.sstatic.net/FyHdV.png

Answer №1

  1. Click method simulates user behavior, but consider using trigger(click) for a different approach. In this case, trigger(click) pertains to an Element Click handled by the DOM Element. Check out more information here: https://docs.cypress.io/api/commands/trigger.html#Syntax

  2. Another workaround to explore is adjusting the click position in click() beyond the default center setting. For instance, try using click(left). Refer to Cypress documentation for more details: https://docs.cypress.io/api/commands/click.html#Arguments

Answer №2

Upon seeing the error message on your screenshot saying "center of this element is hidden," it indicates that Cypress can locate the element but encounters an issue when trying to execute the "click" action. Utilizing {force:true} or .scrollTo('bottom') may not necessarily resolve the issue. One potential solution for the test scenario could be simulating the click by directly visiting the underlying URL. It is essential to investigate further why the center of the element is not visible. Please refer below for more details.

.should('have.attr', 'href', '/about')
            //.click();
            .invoke('attr','href')
                .then((url)=>{
                    cy.visit(siteUrl + url);
                });

https://i.sstatic.net/Fd8Gl.png

Although it seems like {force:true} might be effective, it does not fully accomplish the task, resulting in the URL not being redirected (nor the page getting reloaded) as intended. The following image shows the unsuccessful test.

https://i.sstatic.net/TzvUx.png

In addition, while scrolling to the bottom may appear to be successful, we only observe the page scroll after the test has already failed. Refer to the image below for illustration.

https://i.sstatic.net/0lxvk.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a custom countdown using Jquery and Javascript that integrates with MySQL and PHP to display the datetime in the format Y-m-d

Hey there, I'm looking to create a countdown script using dates in the format (Y-m-d H:m:s). The goal is to retrieve the current_datetime and expire_datetime in this format and incorporate them into some JavaScript or jQuery plugin to display a countd ...

Is there a discrepancy in performance when running a function on an individual element versus a group of elements within jQuery?

Imagine having the choice between applying a function to an individual DOM element or a list of them: For Individual Elements: $('#element1').click(function () { $(this).hide(); return false; }); $('#element2').click(functi ...

Restrict access to PHP scripts to only be allowed through AJAX requests

Within my content management system (CMS), I have a specific page that fetches various mini-interfaces from PHP files located in an /ajax directory using AJAX. I am wondering if there is a way to restrict access to these files solely through AJAX requests ...

Tips for sorting data by date in Angular 4

Seeking guidance on how to filter records in a table based on the date column. The current filtering code is generating an error message: ERROR TypeError: Cannot read property 'indexOf' of null temp = [{ "firstName":"Ravi", "date":"2019-04-0 ...

Incorporate an image into your webpage with the Fetch API by specifying the image link - JavaScript

I've been attempting to retrieve an image using the imageLink provided by the backend server. fetchImage(imageLink) { let result; const url = `https://company.com/internal/document/download?ID=${imageLink}`; const proxyurl = 'https:/ ...

The specified type does not meet the constraint as it lacks the required index signature

I'm currently working on refactoring a TypeScript project that utilizes React Hooks. While I have some knowledge of TypeScript, I am still more of a beginner than an expert. My main goal is to create reusable code for this project through the use of ...

Utilize JSON text importing for template literals in Node.js

When it comes to my node js projects, I usually opt for using a text.json file and requiring it rather than hardcoding static text directly into my code. Here's an example: JSON file { "greet": "Hello world" } var text = require('./text.json ...

How can you define a variable within the .config service in Angular JS?

Here is the code snippet I have been working on: spa.factory("linkFactory", function() { var linkFactoryObject = {}; linkFactoryObject.currentLink = "home"; return linkFactoryObject; }); This piece of code essentially serves as a global varia ...

Why isn't this code performing well when trying to alter the styling of DOM elements?

JavaScript is causing major issues and is performing poorly. Why is this code not working properly?? (this is a back to top button) function checkScrollTop(){ if (document.body.scrollTop > 300) { document.getElementById("backToTop").style.dis ...

The Model Viewer module is unable to load the three-dimensional model

Attempting to incorporate a 3D model into my website using the modelviewer library, but encountering difficulties in loading the file as shown below: Just to note, I am utilizing github pages with a custom domain from godaddy which may be contributing to ...

Tips for replicating input fields that fetch information via ajax requests

My current code fetches data using AJAX from the database and allows for adding an unlimited number of text fields: <div class="input" id="itemRows"> <div class="clone"> <style> .txtHint { width:95 ...

Tips for dynamically loading a different component based on route parameters with the VueJS Router

I'm searching for the best way to dynamically load a different VueJS component based on the presence of props in a route. For instance, navigating to /users would display the users list page, while going to /users/1 will take you to the user ...

Get around operator precedence in JavaScript

Imagine having a string like this: '1 + 2 + 3 * 4' Can you solve it sequentially from left to right in order to obtain a total of 24, instead of 15? It's important to note that the string is unknown beforehand, so it could be as simple as ...

Ways to prompt a specific text value to generate varied responses

Whenever I try to input the letter "h", I expect a specific value in return but for some reason, it's not working as intended. Despite my best efforts to troubleshoot the issue, I have been unsuccessful in finding a solution. It's frustrating bec ...

Utilize Lodash to dynamically filter data based on nested filter conditions

To enhance my product data filtering capabilities, I aim to implement multiple dynamic filter conditions. The initial format of my raw data is structured as follows: [{ ..., "_source": { ..., "categories": [ "home", "office" ], ..., ...

Intermittent Cloudfront Content Delivery Network (CDN) disruptions (monitoring) - Implementing CDN Fail

Over the past two months, I've been dealing with sporadic issues involving Amazon Cloudfront. These failures occur 2-3 times a week, where the page will load from my web server but assets from the CDN linger in pending status for minutes at a time. It ...

What is the process for deleting an event in Vue?

My Vue instance currently includes the following code: async mounted () { document.addEventListener('paste', this.onPasteEvent) }, beforeDestroy () { document.removeEventListener('paste', this.onPasteEvent) }, methods: { onPasteEv ...

What are the steps to make a basic slider with jQuery without using plugins?

<script> const animateImages = function(){ $("#slider").animate({"left":"-=1775px"},10000,function(){ $("#slider").animate({"left":"0px"},10000); animateImages(); }); }; animateImages(); </script> I incor ...

Using Jquery's getJson method, we can easily fetch and retrieve the appropriate JSON string by

I have the following code snippet at the beginning of the JSON url: if(isset($_GET['template']) && $_GET['template']=="blue") { $inifile_path="ctr/subthemes/blue/"; } else { $inifile_path="ctr/subthemes/fresh-n-clean/"; } Addi ...

What could be causing the lack of functionality for my button click in my JavaScript and HTML setup?

Currently, I am attempting to implement a functionality where I have two buttons at the top of my page. One button displays "French" by default, and when I click on the "English" button, it should replace the text with "French" using show and hide methods. ...