Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. module JSHelpers
  2. module JQueryUI
  3.  
  4. # type inside an autocomplete field and pick a value.
  5. #
  6. # @param [String] field selector of the autocomplete field
  7. # @param [Hash] options details about what to search and what to select
  8. # @param options [String] :with the text to type in the autocomplete field
  9. # @param options [String] :select the text value of the autocomplete resuilt you
  10. # want to pick. If this option is not present, the first result will be picked.
  11. #
  12. # @example Auto-select first result
  13. # autocomplete('product_search', with: 'example')
  14. #
  15. # @example Select specific result
  16. # autocomplete('product_search', with: 'example', select: 'example product 12')
  17. #
  18. # @return nil
  19. #
  20. def autocomplete(field, options = {})
  21. fill_in(field, with: options.delete(:with))
  22.  
  23. expect(page).not_to have_selector 'html.loading'
  24. page.execute_script(%{ $('##{field}').trigger('focus') })
  25. page.execute_script(%{ $('##{field}').trigger('keydown') })
  26.  
  27. if text = options.delete(:select)
  28. selector = %{ ul.ui-autocomplete li.ui-menu-item a:contains('#{text}') }
  29. else
  30. selector = %{ ul.ui-autocomplete li.ui-menu-item:first-child a }
  31. end
  32.  
  33. page.should have_selector('ul.ui-autocomplete li.ui-menu-item a')
  34. page.execute_script(%{ $("#{selector}").trigger('mouseenter').click() })
  35. end
  36.  
  37. # find a datetimepicker field and set the datetime
  38. #
  39. # @param [DateTime] datetime datetime to set to
  40. # @param [Hash] options options for setting the value
  41. # @param options [String] :from the id of the field to set datetime on
  42. #
  43. # @example
  44. # datetimepick(2.days.from_now, from: 'event_start_datetime')
  45. #
  46. # @return nil
  47. #
  48. def datetimepick(datetime, options = {})
  49. set_datetimepicker_value(datetime, "##{options.delete(:from)}", 'datetimepicker')
  50. end
  51.  
  52. # find a datepicker field and set the date
  53. #
  54. # @param [Date] date date to set to
  55. # @param [Hash] options options for setting the value
  56. # @param options [String] :from the id of the field to set date on
  57. #
  58. # @example
  59. # datepick(Date.tomorrow, from: 'event_start_date')
  60. #
  61. # @return nil
  62. #
  63. def datepick(date, options = {})
  64. set_datetimepicker_value(date, "##{options.delete(:from)}", 'datepicker')
  65. end
  66.  
  67. # find a timepicker field and set the time
  68. #
  69. # @param [Time] time time to set to
  70. # @param [Hash] options options for setting the value
  71. # @param options [String] :from the id of the field to set time on
  72. #
  73. # @example
  74. # timepick(12.hours.from_now, from: 'event_start_time')
  75. #
  76. # @return nil
  77. #
  78. def timepick(time, options = {})
  79. set_datetimepicker_value(time, "##{options.delete(:from)}", 'timepicker')
  80. end
  81.  
  82. private
  83.  
  84. def set_datetimepicker_value(datetime, selector, widget)
  85. js_date_object = "new Date(#{datetime.to_i * 1000})"
  86. page.execute_script(%{ $("#{selector}").#{widget}("setDate", #{js_date_object})})
  87. end
  88. end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement