0

I'm trying to write a Capybara test which tests multi select using ctrl on Windows and command key on MacOS.

I found a solution here which works only if I use Selenium as my driver. Testing jQuery Selectable capybara or selenium (ctrl + click)

However, the solution does not work for Poltergeist driver. Does Poltergeist currently have support to perform Ctrl + Click?

1 Answer 1

0

No Poltergeist doesn't. If you have to use Poltergeist for the test then you're probably going to have to use execute_script to build and fire the event from JS. Note that only applies if it's a JS widget. If it's actually an HTML select with the multiple attribute you should just be able to select each option separately and it will select multiples.

6
  • Thanks Thomas! I am aware of this solution as well: stackoverflow.com/questions/8474103/… Although it proposes using KeyboardEvent.keyCode which according to MDN is deprecated and not really recommended. I'm trying to select 'li' HTML elements. The issue is that we intentionally made it so that the user needs to be holding down either 'Ctrl' or 'Shift' in order to do multi select. Dec 27, 2017 at 18:59
  • @SpartaSixZero send_keys releases the key before returning, so it won't be down for the click that follows. From your description I assume you need to issue a mouse click event with the correct keyboard modifiers set in that event. If your code also implements a way to do the multiple select via keyboard then you could probably do that with send_keys - rubydoc.info/gems/capybara/Capybara/Node/… - in a cross driver manner Dec 27, 2017 at 19:34
  • Unfortunately, our requirements states that we need a combination of keydown with the left mouse click to make multi select. So I don't think send_keys will help here since send_keys only takes keys and not mouse clicks. I doubt we're going to make multi select possible with just key presses, it's not worth the effort just to make a Capybara test pass. Dec 27, 2017 at 19:59
  • @SpartaSixZero Okay - so passing the desired element through execute_script, using getBoundingClientRect to find the position, and building/firing a click event with the correct key modifier set is probably your best bet Dec 27, 2017 at 20:03
  • 1
    @SpartaSixZero Rather than putting the selector directly in the JS I'd recommend passing the element from ruby, that way you can turn the whole thing into a reusable method shiftclick_script = "var e = $.Event('click', { shiftKey: true}); $(arguments[0]).trigger(e);" page.execute_script(shiftclick_script, my_element) where my_element = page.find('mySelector') Dec 27, 2017 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.