question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cypress not able to locate element using cy.get

See original GitHub issue

Current behavior

In the below DOM we are trying to locate the heading using data-test-id but for some reason, it is not able to locate in the runtime but when search with the same selector is suggested selector it is able to uniquely identify. image image I am using cypress for other #tests where modal is used and I did not find this issue there but in this specific case it’s failing.

Desired behavior

Test code to reproduce

it.only(`Verify user is able to use welcome new member template`, () => {
		cy.getDataTestId(`template-link-post-composer`)
			.should(`be.visible`)
			.should(`contain.text`, `Use a template`)
			.click();
		cy.getDataTestId(`heading-template-pop-up`).should(`be.visible`).should(`contain.text`, `Use a template`);
		cy.getDataTestId(`button-close-template-pop-up`).should(`be.visible`).click();
		cy.getDataTestId(`template-link-post-composer`)
			.should(`be.visible`)
			.should(`contain.text`, `Use a template`)
			.click();
		cy.getDataTestId(`template-welcome-new-members`)
			.should(`be.visible`)
			.within(() => {
				cy.getDataTestId(`heading-template-type`)
					.should(`contain.text`, `Welcome New Members (weekly)`)
					.should(`be.visible`);
				cy.getDataTestId(`description-template`)
					.should(`contain.text`, `Copy this post and use this weekly to welcome the new members of your group`)
					.should(`be.visible`);
				cy.getDataTestId(`button-view-template`).should(`be.visible`).should(`contain.text`, `View`).click();
				cy.getDataTestId(`view-template-pop`).should(`be.visible`);
				cy.get('[data-test-id="view-template-heading"]').should(`be.visible`);
				cy.getDataTestId(`view-template-heading`).should(`be.visible`).should(`contain.text`, `View template`);
				cy.getDataTestId(`template-name-view-pop-up`)
					.should(`be.visible`)
					.should(`contain.text`, `Welcome New Members (weekly)`);
			});
	});

Versions

Cypress version - 6.2.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

23reactions
neil-r-bcommented, Jun 22, 2021

For others who stumble across this issue, as I was suffering from the same exact problem. The suggestion to use withinSubject:null from @myselfsrjn4u does work although to add more color from the docs this does:

Element to search for children in. If null, search begins from root-level DOM element

Which clued us in to the mistake we were doing in code - it is due to checking for the existence of the modal in a within() block. This has cypress searching the DOM from that point but the modal is outside of it. So an alternative to using withinSubject:null is to check for existence outside of the within() block.

18reactions
myselfsrjn4ucommented, Jan 26, 2021

@Ppkd2021 Can you try with this —> cy.get(‘[data-test-id=“view-templete-heading”]’,{withinSubject:null})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not able to find the element in cypress - Stack Overflow
no it does not work, it says - CypressError: cy.click() can only be called on a single element. Your subject contained 6 elements....
Read more >
find | Cypress Documentation
Get the descendent DOM elements of a specific selector. The querying behavior of this command matches exactly how .find() works in jQuery. Syntax...
Read more >
not - Cypress Documentation
Yields .not() yields the new DOM element(s) it found. .not() is a query, and it is safe to chain further commands. Examples. Selector....
Read more >
get | Cypress Documentation
The cy.get command always starts its search from the cy.root element. In most cases, it is the document element, unless used inside the ......
Read more >
Interacting with Elements - Cypress Documentation
Whenever Cypress cannot interact with an element, it could fail at any of the above steps. You will usually get an error explaining...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found