0

I am working through a book repository, specifically chapter 5 of Test-Driven Development, which can be found at this repository: https://github.com/hjwp/book-example/tree/chapter_05 . When I attempt to run the functional tests, which create a simple POST form, submit some input, and then check to see whether the input has been rendered, I get the following error:

======================================================================
FAIL: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "book-example-chapter_05/functional_tests.py", line 45, in test_can_start_a_list_and_retrieve_it_later
    self.check_for_row_in_list_table('1: Buy peacock feathers')
  File "book-example-chapter_05/functional_tests.py", line 18, in check_for_row_in_list_table
    self.assertIn(row_text, [row.text for row in rows])
AssertionError: '1: Buy peacock feathers' not found in ['']

---------------------------------------------------------------------

It seems from a discussion on the mailing list that this might be due to the browser not receiving the 'ENTER' key. When I change the line to inputbox.send_keys('Buy peacock feathers\n'), to directly send a new line character, I get the following error:

======================================================================
ERROR: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "book-example-chapter_05/functional_tests.py", line 44, in test_can_start_a_list_and_retrieve_it_later
    inputbox.send_keys(Keys.ENTER)
  File "/Users/r/virtualenvs/r/bin/python3venv/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 322, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
  File "/Users/r/virtualenvs/r/bin/python3venv/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 448, in _execute
    return self._parent.execute(command, params)
  File "/Users/r/virtualenvs/r/bin/python3venv/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 196, in execute
    self.error_handler.check_response(response)
  File "/Users/r/virtualenvs/r/bin/python3venv/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
    at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9348)
    at Utils.getElementAt (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/fxdriver@googlecode.com/components/command-processor.js:8942)
    at fxdriver.preconditions.visible (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/fxdriver@googlecode.com/components/command-processor.js:9980)
    at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/fxdriver@googlecode.com/components/command-processor.js:12626)
    at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/fxdriver@googlecode.com/components/command-processor.js:12643)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/fxdriver@googlecode.com/components/command-processor.js:623)

----------------------------------------------------------------------
Ran 1 test in 5.511s

so that seems to suggest that there is an error with the DOM tree, and the next element it tries to find (one with an id equal to 'id_list_table') cannot be found.

Why is this the case? How can I have Selenium test the addition of a list item and its existence in the table I've created?

orange1
  • 2,871
  • 3
  • 32
  • 58

4 Answers4

2

Turns out that the selenium runserver command is unique to each app -- I was running the functional tests from the NEW (correct) repository, but I hadn't restarted my server with the NEW repository, I was using a server that was started using my OLD repository. Restarted the server and it now works as expected.

orange1
  • 2,871
  • 3
  • 32
  • 58
1

I don't think you want to use \n. If you want to send the enter key via selenium you would do:

 inputbox.send_keys('Buy peacock feathers')
 inputbox.send_keys(Keys.ENTER)

\n is a linefeed. This basically means it ends the current line. Check out this stackoverflow answer that explains in-depth.

Community
  • 1
  • 1
cssko
  • 3,027
  • 1
  • 18
  • 21
  • Thanks for the comment. Yes, I have tried using the syntax that you recommended - that is actually the original syntax in the repository. Unfortunately, that leads to the first error message in my original question. – orange1 Aug 15 '15 at 18:40
  • Are you using the code directly from the github repo? – cssko Aug 15 '15 at 19:12
  • Yup. I'm using exactly the code that is in the chapter 5 branch. Without any modifications, it gives me the first error message posted. I've tried running it both in python 2 and python 3, though it is intended to be run in python 3. – orange1 Aug 15 '15 at 19:18
  • It works fine with 2.7 for me. Try `inputbox.send_keys(u'\ue007')` in place of `inputbox.send_keys(Keys.ENTER)`? Also, what does your list_items table look like? – cssko Aug 15 '15 at 19:36
  • @orange1 actually the first error is not an error. It's your test assertion failing. – e4c5 Aug 16 '15 at 03:03
1

I ran into this problem too. I missed the name=item_text on the input of the home.html.

I'm not sure why I forgot that in the view, request.POST.get('item_text', '') looks for an input with that name.

Carl Brubaker
  • 1,602
  • 11
  • 26
0

I've seen this kind of problem before, and to work around it I usually first try a submit and then send a return key.

This is an extract from my custom wrapping class. elem would be your input element.

    try:
        self.debug("Trying regular submit")
        elem.submit()
    except:
        self.debug("Regular submit failed, sending RETURN key")
        elem.send_keys(Keys.RETURN)
Maresh
  • 4,644
  • 25
  • 30
  • +1 for using submit() method, this fixed a problem for me where Keys.ENTER was adding \ue007 to the output. What I don't see, is why you would put an exception handler around it, particularly with a catchall handler when catching an actual type would illuminate the reasoning a bit. – Robert Boehne May 15 '17 at 14:59