Slot testing helps check parts of a web page that can change or hold different content. It makes sure these parts work well and show the right things.
Slot testing in Cypress
cy.get('slot-selector').should('contain.text', 'expected text')
Use a proper CSS selector to find the slot or its container.
Assertions check if the slot content matches what you expect.
cy.get('my-component slot[name="header"]').should('contain.text', 'Welcome')
cy.get('slot').should('exist')
cy.get('my-component slot').then(slot => { expect(slot).to.have.length(1) })
This test visits a page with a component 'my-card' that uses slots. It checks the 'title' slot has 'Card Title' text and the 'subtitle' slot shows fallback text 'Default subtitle' if no content is provided.
describe('Slot testing example', () => { beforeEach(() => { cy.visit('/slot-test-page') }) it('checks slot content is correct', () => { cy.get('my-card slot[name="title"]').should('contain.text', 'Card Title') }) it('checks fallback content in slot', () => { cy.get('my-card slot[name="subtitle"]').should('contain.text', 'Default subtitle') }) })
Slots are placeholders inside web components where content can be inserted.
Always use clear selectors to target slots or their containers.
Check both filled slots and fallback content to cover all cases.
Slot testing ensures dynamic content areas in components work correctly.
Use Cypress commands to find slots and check their content.
Test both presence and correctness of slot content and fallback.
Practice
Solution
Step 1: Understand slot testing concept
Slot testing focuses on dynamic content areas called slots inside components.Step 2: Identify Cypress role in slot testing
Cypress commands are used to find slots and verify their content correctness.Final Answer:
To verify dynamic content inside component slots renders correctly -> Option DQuick Check:
Slot testing = Verify dynamic slot content [OK]
- Confusing slot testing with network or database testing
- Ignoring fallback content in slots
- Testing only static elements instead of slots
Solution
Step 1: Understand slot selector syntax
Named slots use theslottag with anameattribute, so selector isslot[name="header"].Step 2: Match Cypress command to selector
Onlycy.get('slot[name="header"]')correctly targets the named slot.Final Answer:
cy.get('slot[name="header"]') -> Option BQuick Check:
Named slot selector = slot[name="..."] [OK]
- Using class or id selectors instead of slot attribute
- Confusing slot tag with div or other elements
- Missing quotes around attribute value
cy.get('slot[name="footer"]').should('contain.text', 'Contact us')What will happen if the slot named 'footer' contains the text 'Contact us today'?
Solution
Step 1: Understand the assertion used
The assertionshould('contain.text', 'Contact us')checks if the slot text includes the substring 'Contact us'.Step 2: Analyze the slot content
The slot text is 'Contact us today', which contains 'Contact us' as a substring.Final Answer:
Test passes because 'Contact us' is part of the slot text -> Option AQuick Check:
contain.text checks substring presence [OK]
- Assuming contain.text requires exact match
- Confusing contain.text with equals assertion
- Ignoring slot visibility requirement
cy.get('slot[name="main"]').should('have.text', 'Welcome')But the test fails even though the slot shows 'Welcome to our site'. What is the likely problem?
Solution
Step 1: Understand 'have.text' assertion
'have.text' checks for exact text equality, not substring.Step 2: Compare expected and actual slot text
Expected 'Welcome' does not exactly match 'Welcome to our site', so assertion fails.Final Answer:
Using 'have.text' requires exact text match, so it fails -> Option CQuick Check:
have.text = exact match, contain.text = substring [OK]
- Confusing have.text with contain.text
- Assuming slot selector is wrong without checking
- Ignoring visibility but test fails due to text mismatch
Solution
Step 1: Select default slot correctly
The default slot has no name attribute, so selector isslot:not([name]).Step 2: Select named slot 'sidebar'
Named slot selector isslot[name="sidebar"].Step 3: Verify fallback and named slot content
Usecontain.textto check fallback 'No content' and sidebar 'Menu'.Final Answer:
cy.get('slot:not([name])').should('contain.text', 'No content'); cy.get('slot[name="sidebar"]').should('contain.text', 'Menu') -> Option AQuick Check:
Default slot = slot:not([name]), named slot = slot[name="..."] [OK]
- Using slot[name="default"] which does not exist
- Using class or id selectors on slot elements
- Not distinguishing default slot from named slots
Ready to prove it?
Pass the test and this pattern moves unknown
