I see the point of waiting for subtree modifications, however I don't like 
`when_dom_changes` because it's inherently flaky - in the `SearchPage` example, 
the DOM changes could very well be finished before the test has had a chance to 
call `autocomplete_results`, and you'd see a timeout even though the app 
behaves correctly.

This is why WebDriver has `StaleElementReferenceErrors` (and one of the reaons 
why I use watir-webdriver with `Watir.always_locate = false`).

To avoid this problem, the waiting should happen in the method that also 
triggers the search results:

```ruby
class SearchPage
  def search_for(term)
    old_results = autocomplete_element.wd
    
    search_field.set(term)
    
    # wait for the DOM change
    Wait.until do
      begin
        old_results.enabled? # could be any call - just need to force a 
staleness check in webdriver
        false
      rescue Selenium::WebDriver::Error::StaleElementReferenceError => ex
        true
      end
    end
  end
  
  def autocomplete_results
    autocomplete_element.as.map(&:text)
  end
  
  private

  def search_field
    browser.text_field(:id => "search_field") 
  end
  
  def autocomplete_element
    browser.div(:id => "autocomplete")
  end
end
```

As the verbosity of the example shows, it's not easy with Watir currently. In 
part this is because of Watir's lazy locating of elements, but here we need to 
get a reference to the element that will change, before it goes stale. One hack 
would be to do something like:

```ruby
  old_results = autocomplete_element
  old_results.exist? # with Watir.always_locate = false, the element reference 
is now cached - which is definitely not obvious to a normal user

  search_field.set(term)

  wait_until { old_results.stale? } # new method: Element#stale? - but it can 
only work when Watir.always_locate = false, and the element is cached / 
previously located`
```
I would support a change that makes this easier, but I don't think 
`.when_dom_changes` does the job correctly, since you can't set it up before 
triggering the action that performs the modification. 


---
Reply to this email directly or view it on GitHub:
https://github.com/watir/watir-webdriver/issues/193#issuecomment-18463730
_______________________________________________
Wtr-development mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-development

Reply via email to