Cypress can’t find an element that actually exists

During my testing inside of SAP Hybris I’ve met an issue with the component I believed that nothing can go wrong. Namely it is a simple component containing the Description for the item (promotion in this case).
Here is my initial cypress command:

cy.get('.ye-input-text.ye-com_hybris_cockpitng_editor_defaulttext.z-textbox')
    .should('have.value', 'All users are able to try a ZYN can as a free sample (or free gift). Limited to 1 per registered user');

With this command I’ve received the following issue:

Now, as much I understand it, it suggests that there are multiple elements on the page containing the same class name (which is lets say true), but in that case I would expect for example the choose multiple elements error or the one suggesting to use the .eq() command or so. Although from the error message I understand that Cypress went through all 9 elements it found with the given class name but none contained the value requested.

Here is the HTML of the element I’m referring to:

<div id="p41H029" class="yw-loceditor-row ye-validation-none z-div">
<span id="p41H129" style="display:none;" class="yw-loceditor-row-locale z-label" title="English">en</span>
<div id="p41H229" class="yw-loceditor-row-editor z-div"><div id="p41H329" class="z-div">
<input id="p41H429" class="ye-input-text ye-com_hybris_cockpitng_editor_defaulttext z-textbox" value="All users are able to try a ZYN can as a free sample (or free gift). Limited to 1 per registered user" type="text" aria-disabled="false" aria-readonly="false"></div></div></div>

Another Cypress command I’ve tried out is this one (but it didn’t work):

cy.contains('All users are able to try a ZYN can as a free sample (or free gift). Limited to 1 per registered user')
  .parents('.yw-loceditor-row.z-div') // Go up to the parent row
  .find('input') // Then find the input
  .should('have.value', 'All users are able to try a ZYN can as a free sample (or free gift). Limited to 1 per registered user');
                });
            });

The short answer is, be a bit more specific in the selection of the one <input> that has this value.

You can see in the message

assert expected [ <input#p41H429.ye-input-text.ye-com_hybris_cockpitng_editor_defaulttext.z-textbox>, 8 more… ]

which means there are nine elements selected in total.

If I run a similar test, with three inputs and the value I want is on the second input, it fails when comparing the value of the first input.

<input 
  class="ye-input-text ye-com_hybris_cockpitng_editor_defaulttext z-textbox"
  value="Some promotion"
/>
<input
  class="ye-input-text ye-com_hybris_cockpitng_editor_defaulttext z-textbox"
  value="My specific promotion"
/>
<input
  class="ye-input-text ye-com_hybris_cockpitng_editor_defaulttext z-textbox"
  value="Some other promotion"
/>
cy.get('.ye-input-text.ye-com_hybris_cockpitng_editor_defaulttext.z-textbox')
  .should('have.value', 'My specific promotion');

If I make the selection more specific, by using the [value="..."] selector, so only one element is selected – it passes.

cy.get(`.ye-input-text[value="My specific promotion"]`)
  .should('have.value', 'My specific promotion');

You can also use other selectors such as eq(1) to pick just one <input> element to test.


What’s the rule?

When you use .should('have.value', ...) following a cy.get() that returns multiple elements, it’s equivalent to

cy.get(...)                     // multiple elements selected
  .invoke('val')                // extract value of only the first element 
  .should('eq', 'some value')   // test the value

So only the first element gets tested, the rest are ignored.
The above is the “standard” way to test a value of an input.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật