The question is as given in the title, ie, to access element whose parent is hidden. The problem is that, as per the cypress.io docs :
An element is considered hidden if:
- Its width or height is 0.
- Its CSS property (or ancestors) is visibility: hidden.
- Its CSS property (or ancestors) is display: none.
- Its CSS property is position: fixed and it’s offscreen or covered up.
But the code that I am working with requires me to click on an element whose parent is hidden, while the element itself is visible.
So each time I try to click on the element, it throws up an error reading :
CypressError: Timed out retrying: expected '< mdc-select-item#mdc-select-item-4.mdc-list-item>' to be 'visible'
This element '< mdc-select-item#mdc-select-item-4.mdc-list-item>' is not visible because its parent '< mdc-select-menu.mdc-simple-menu.mdc-select__menu>' has CSS property: 'display: none'
The element I am working with is a dropdown item
, which is written in pug
. The element is a component defined in angular-mdc-web, which uses the mdc-select
for the dropdown menu and mdc-select-item
for its elements (items) which is what I have to access.
A sample code of similar structure :
//pug
mdc-select(placeholder="installation type"
'[closeOnScroll]'="true")
mdc-select-item(value="false") ITEM1
mdc-select-item(value="true") ITEM2
In the above, ITEM1
is the element I have to access. This I do in cypress.io
as follows :
//cypress.io
// click on the dropdown menu to show the dropdown (items)
cy.get("mdc-select").contains("installation type").click();
// try to access ITEM1
cy.get('mdc-select-item').contains("ITEM1").should('be.visible').click();
Have tried with {force:true}
to force the item click, but no luck. Have tried to select the items using {enter}
keypress on the parent mdc-select
, but again no luck as it throws :
CypressError: cy.type() can only be called on textarea or :text. Your subject is a: < mdc-select-label class="mdc-select__selected-text">Select ...< /mdc-select-label>
Also tried using the select
command, but its not possible because the Cypress engine is not able to identify the element as a select
element (because its not, inner workings are different). It throws :
CypressError: cy.select() can only be called on a . Your subject is a: < mdc-select-label class="mdc-select__selected-text">Select ...< /mdc-select-label>
The problem is that the mdc-select-menu
that is the parent for the mdc-select-item
has a property of display:none
by some internal computations upon opening of the drop-down items.
This property is overwritten to display:flex
, but this does not help.
All out of ideas. This works in Selenium
, but does not with cypress.io
. Any clue what might be a possible hack for the situation other than shifting to other frameworks, or changing the UI code?
To expand a bit the answer of BTL, if anyone faced an error -
Property 'isVisible' does not exist on type 'Chainable<JQuery<HTMLElement>>
inTypescript
, following is what I added at the top ofcommands.ts
in cypress to get away with it -And may be replacing
expect(isVisible(subject[0])).to.be.true
withassert.True(isVisible(subject[0]));
if you see any chai assertion error with expect and don't want to import it - as in Josef Biehler answer..After much nashing-of-teeth, I think I have an answer.
I think the root cause is that
mdc-select-item
hasdisplay:flex
, which allows it to exceed the bounds of it's parents (strictly speaking, this feels like the wrong application of display flex, if I remember the tutorial correctly, however...).Cypress does a lot of parent checking when determining visibilty, see visibility.coffee,
But, when using
.should('be.visible')
, we are stuck with parent properties failing child visibility check, even though we can actually see the child.We need an alternate test.
The work-around
Ref jquery.js, this is one definition for visibility of the element itself (ignoring parent properties).
so we might use that as the basis for an alternative.
Footnote - Use of 'then' (or 'each')
The way you normally use assertion in cypress is via command chains, which basically wraps the elements being tested and handles things like retry and waiting for DOM changes.
However, in this case we have a contradiction between the standard visibility assertion
.should('be.visible')
and the framework used to build the page, so we usethen(fn)
(ref) to get access to the unwrapped DOM. We can then apply our own version of the visibility test using stand jasmine expect syntax.It turns out you can also use a function with
.should(fn)
, this works as wellUsing
should
instead ofthen
makes no difference in the visibility test, but note theshould
version can retry the function multiple times, so it can't be used withclick
test (for example).From the docs,
You can also solve the problem by extending chai assertions, but the documentation for this isn't extensive, so potentially it's more work.
I came across this topic but was not able to run your example. So I tried a bit and my final solution is this. maybe someone other also needs this. Please note that I use typescript.
First: Define a custom command
Please note the TODO. In my case I was targeting a button which has two border boxes. The first with height and width 0. So i must select the second one. Please adjust this to your needs.
Second: Use it
For convenience and reusability I had to mix the answer of Richard Matsen and Josef Biehler.
Define the command
You can now chain it from contains
From the docs, Cypress select syntax, the syntax is
You may need the
{force: true}
as well. See here select_spec.coffee for examples of their own tests, e.g