How to fill in a datetime-local field with capybar

2019-09-15 15:52发布

问题:

I am using Cocoon to add records. IDs look like workshop_instance_sessions_attributes_1477654140_start_time . At the moment I am traversing the DOM and grabbing the dynamically generated IDs (this works fine) so I can use them to reference the input to fill in. I have tried many things (many that work in console) and still cannot seem to get the spec to work. Below are a selection of things I've tried (first is the variable which holds the id and @time is basically Time.now that increments as a block executes).

Works in console:

page.evaluate_script("document.getElementById('#{first}').value = '#{@time.strftime('%Y-%m-%eT%H:%M')}'")

page.evaluate_script("$('##{first}').val('#{@time.strftime("%Y-%m-%eT%H:%M")}')")

Doesn't work in console:

fill_in first, with: @time.strftime("%Y-%m-%eT%H:%M")

find("##{first}").set(@time.strftime("%Y-%m-%eT%H:%M"))

These are all already within a within block (a common issue I run into) that I know is working correct as the first variable is being correctly set.

As reference my html looks like this:

<input class="form-control" type="datetime-local" name="workshop_instance[sessions_attributes][1477655024][start_time]" id="workshop_instance_sessions_attributes_1477655024_start_time">

Any pointers on how to get this spec to work would be much appreciated.

回答1:

This is going to vary based on driver/browser used. Assuming you're using selenium-webdriver with chrome you need to set the keys that would be hit to enter in the control so to set december 13th 2000 @ 3:15 PM you would need to do

fill_in first, with: "12132000\t0315p"


回答2:

According to the Capybara release notes here:

Selenium driver supports Date/Time when filling in date/time/datetime-local inputs

So to answer your question:

fill_in first, with: Time.new(2018, 01, 02, 12)