2

In essence, I have a situation where when the user types in to an HTML text input, an element is dynamically added to a the page. No network activity is involved in adding the element.

I want to run my tests -that include checking this behaviour -on as wide a variety of browsers as possible and will use a cloud service e.g. saucelabs.com.

I'd like to test that the number of dynamically added elements is correct. My test so far:

text_input.send_keys('a') 
#send_keys('a') should make the browser immediately add an li to ul.new-elements
added_elements = browser.find_elements_by_css_selector('ul.new-elements li')
assert(len(added_elements), expected_num_of_list_items)

This is working reliably on my local machine, but from what I can see it assumes that the browser will always complete the DOM manipulation before Python/Webdriver gets to the find_elements_by_ call. Am I right in thinking this a dangerous assumption?

If so, do I need an explicit wait? Perhaps something like:

text_input.send_keys('a')
try:
    WebDriverWait(browser, 30).until(
        lambda b: len(b.find_elements_by_css_selector('ul.new-elements li')) == expected_num_of_list_items,
        'Timed out waiting for LI to be added.'
    )
except TimeoutException:
    fail('Timed out waiting for LI to be added.') #2 fail messages?

(My understanding is that implicit_wait is not relevant here).

KnewB
  • 353
  • 4
  • 16

2 Answers2

2

Your thinking is correct. As soon as you start using explicit waits, you should forget implicit waits. An excellent explanation of why you should not mix the two types is available here.

Community
  • 1
  • 1
Louis
  • 146,715
  • 28
  • 274
  • 320
  • I posted a [related question](http://stackoverflow.com/questions/20268396/mixing-implicit-and-explicit-waits) – KnewB Nov 28 '13 at 14:22
1

Yes you are correct in making that assumption.

In my understanding for the specific condition that you are asserting an explicit wait would be required. For other scenarios that are more commonly used you could use "expected conditions".

Read more here

Amey
  • 8,470
  • 9
  • 44
  • 63