如何使用Intern Leadfoot waitForDeletedByXpath函数及适用场景解析
How to Use Intern Leadfoot's
waitForDeletedByXpath Function & Its Practical Use Cases Let me clear up the common misunderstanding first: you’re right to be confused—this method does not wait for an element to become invisible; it waits for the element to be completely removed from the DOM tree. That’s a key distinction from elements that are just hidden (via display: none, visibility: hidden, or similar styles).
Basic Usage Example
Here’s how to implement it for your specific loading spinner scenario:
// Assume your search button and loading spinner have these XPaths return this.remote // Trigger the search action .findByXpath('//button[@id="search-submit"]') .click() .end() // Wait up to 5 seconds for the loading spinner to be deleted from DOM .waitForDeletedByXpath('//div[@class="loading-spinner"]', 5000) .then(() => { // Now the spinner is gone—safe to interact with the data grid return this.remote.findByXpath('//table[@id="results-grid"]'); }) .catch(err => { // Handle timeout or unexpected errors here console.error('Loading spinner did not get deleted in time:', err); throw err; });
- The second parameter (
5000) is an optional timeout in milliseconds; if omitted, it uses thewaitForTimeoutvalue from your Intern config. - The method will poll the DOM at intervals until the element is gone or the timeout hits.
Key Use Cases
This function shines in scenarios where you need to confirm an element is no longer part of the page structure entirely:
- Your loading spinner scenario: Perfect when the spinner is removed from the DOM once data loads (not just hidden). If your spinner only gets hidden (style change), use
waitForNotDisplayedByXpathinstead. - Modal/popup cleanup: After closing a modal, wait for it to be deleted from the DOM to avoid accidental interactions with leftover elements.
- Dynamic content removal: When deleting a list item or card, wait for the element to be fully removed before validating the updated list.
- SPA route transitions: In single-page apps, wait for old page elements to be deleted after routing to ensure the new page is fully rendered.
Quick Troubleshooting Tip for Your Scenario
If waitForDeletedByXpath isn’t working for your loading spinner, open your browser’s DevTools:
- Trigger the search action
- Watch the loading spinner’s DOM entry—does it get removed (
<div class="loading-spinner">disappears entirely) or just hidden (styles change, but the element stays in the DOM)?
If it’s just hidden, switch towaitForNotDisplayedByXpathinstead.
内容的提问来源于stack exchange,提问作者CodeBlooded
相关产品推荐
相关产品推荐

