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?

SpartaSixZero
  • 2,183
  • 5
  • 27
  • 46

1 Answers1

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.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Thanks Thomas! I am aware of this solution as well: https://stackoverflow.com/questions/8474103/is-there-a-way-to-send-key-presses-to-webkit-using-capybara 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. – SpartaSixZero Dec 27 '17 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` - http://www.rubydoc.info/gems/capybara/Capybara/Node/Element#send_keys-instance_method - in a cross driver manner – Thomas Walpole Dec 27 '17 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. – SpartaSixZero Dec 27 '17 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 – Thomas Walpole Dec 27 '17 at 20:03
  • Awesome! this works perfectly. Thanks keypress_script = "var e = $.Event('click', { shiftKey: true }); $('mySelector').trigger(e);" page.execute_script(keypress_script) – SpartaSixZero Dec 27 '17 at 21:08
  • 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')` – Thomas Walpole Dec 27 '17 at 21:12