I’m working on automating up a client’s AJAXy application right now and managed to get myself very confused frustrated yesterday over retrieving a value that was updated through a JS event.

For context, there are a bunch of fields that can have numbers entered into them and on each key press a running total is updated via Javascript and replaced in the DOM.

Because the field that gets updated is an input one, the content of it is stored in the value attribute.

This is a RoR application, so I’m using the selenium-client gem. To get an attribute named ‘value’ using it you would do the following.

<pre lang="ruby">@selenium.get_attribute "//input[@id='currency_count_total_value']@value"

And that works fine — until it is updated through Javascript. On the page the field will be updated but that snippet will always return the original value since the page hasn’t actually been updated — just the DOM.

So how do you get the updated value? Well, after an embarrassing length of time the magic is to use the get_value method.

<pre lang="ruby">@selenium.get_value "currency_count_total_value"

Now, in theory they should do the same darn thing. Reality is different. Yesterday evening would seem to imply that get_attribute works on the most recently updated html retrieved from the server whereas get_value works directly on the DOM.