PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/rails/features/step_definitions/investigations_steps.rb

https://github.com/concord-consortium/rigse
Ruby | 360 lines | 269 code | 53 blank | 38 comment | 13 complexity | 1a03d984ca6adf0f231929081d2106ab MD5 | raw file
  1. Given /^the following empty investigations exist:$/ do |table|
  2. table.hashes.each do |hash|
  3. user = User.find_by_login hash['user']
  4. FactoryBot.create(:investigation, hash.merge('user' => user))
  5. end
  6. end
  7. Given /^the following linked investigations exist:$/ do |table|
  8. table.hashes.each do |hash|
  9. user = User.find_by_login hash['user']
  10. inv = FactoryBot.create(:investigation, hash.merge('user' => user))
  11. inv.activities << FactoryBot.create(:activity, :user => user)
  12. inv.activities[0].sections << FactoryBot.create(:section, :user => user)
  13. inv.activities[0].sections[0].pages << FactoryBot.create(:page, :user => user)
  14. open_response = FactoryBot.create(:open_response, :user => user)
  15. open_response.pages << inv.activities[0].sections[0].pages[0]
  16. inv.reload
  17. end
  18. end
  19. Given /^the following simple investigations exist:$/ do |investigation_table|
  20. investigation_table.hashes.each do |hash|
  21. user = User.where(:login => hash.delete('user')).first
  22. hash[:user_id] = user.id
  23. investigation = Investigation.create(hash)
  24. activity = Activity.create(hash)
  25. section = Section.create(hash)
  26. page = Page.create(hash)
  27. section.pages << page
  28. activity.sections << section
  29. investigation.activities << activity
  30. investigation.save
  31. end
  32. end
  33. Given /^the author "([^"]*)" created an investigation named "([^"]*)" with text and a open response question$/ do |author, name|
  34. user = User.where(:login => author).first
  35. hash = {:user_id => user.id, :name => name}
  36. investigation = Investigation.create(hash)
  37. activity = Activity.create(hash)
  38. section = Section.create(hash)
  39. page = Page.create(hash)
  40. section.pages << page
  41. activity.sections << section
  42. investigation.activities << activity
  43. page.add_embeddable(Embeddable::OpenResponse.create(hash))
  44. page.save
  45. investigation.save
  46. end
  47. #Table: | investigation | activity | activity_teacher_only | section | page | multiple_choices |
  48. Given /^the following investigations with multiple choices exist:$/ do |investigation_table|
  49. investigation_table.hashes.each do |hash|
  50. investigation = Investigation.where(name: hash['investigation']).first_or_create
  51. investigation.user = FactoryBot.create(:user)
  52. investigation.save
  53. # ITSISU requires descriptions on activities
  54. activity = Activity.where(name: hash['activity']).first_or_create(:description => hash['activity'])
  55. if hash['activity_teacher_only']
  56. # Create a teacher only activity if specified
  57. activity.teacher_only = (hash['activity_teacher_only'] == 'true')
  58. activity.save
  59. end
  60. section = Section.where(name: hash['section']).first_or_create
  61. page = Page.where(name: hash['page']).first_or_create
  62. mcs = hash['multiple_choices'].split(",").map{ |q| Embeddable::MultipleChoice.find_by_prompt(q.strip) }
  63. mcs.each do |q|
  64. q.pages << page
  65. end
  66. imgqs = hash['image_questions'].split(",").map{ |q| Embeddable::ImageQuestion.find_by_prompt(q.strip) }
  67. imgqs.each do |q|
  68. q.pages << page
  69. end
  70. page.save
  71. section.pages << page
  72. activity.sections << section
  73. investigation.activities << activity
  74. end
  75. end
  76. Given /^the following classes exist:$/ do |table|
  77. table.hashes.each do |hash|
  78. if hash['teacher']
  79. user = User.find_by_login hash['teacher']
  80. teacher = user.portal_teacher
  81. else
  82. teacher = FactoryBot.create(:teacher)
  83. end
  84. hash.merge!('teacher' => teacher)
  85. FactoryBot.create(:portal_clazz, hash)
  86. end
  87. end
  88. Given /^the investigation "([^"]*)" is published$/ do |investigation_name|
  89. investigation = Investigation.find_by_name investigation_name
  90. investigation.publish
  91. investigation.save
  92. end
  93. Given /^the investigation "([^"]*)" shows scores$/ do |investigation_name|
  94. investigation = Investigation.find_by_name investigation_name
  95. investigation.show_score = true
  96. investigation.save
  97. end
  98. When /^I sort investigations by "([^"]*)"$/ do |sort_str|
  99. visit "/investigations?sort_order=#{sort_str}"
  100. end
  101. When /sort order .*should be "([^"]*)"/ do |sort_str|
  102. expect(page).to have_selector('select[name="[sort_order]"]')
  103. expect(page).to have_selector("option[value='#{sort_str}'][selected='selected']")
  104. end
  105. When /^I drag the investigation "([^"]*)" to "([^"]*)"$/ do |investigation_name, to|
  106. investigation = Investigation.find_by_name investigation_name
  107. selector_id = "#investigation_#{investigation.id}"
  108. selector = find(selector_id)
  109. drop = find(to)
  110. # NP 2011-09 see support/drag_and_drop.rb
  111. # TODO: When Selenium issue ( http://bit.ly/q9LHR4 ) closes
  112. # use the actual dragging code which we replaced
  113. #
  114. # selector.drag_to(drop)
  115. fake_drop(selector_id,to)
  116. end
  117. Then /^I should not see the "([^"]*)" checkbox in the list filter$/ do |arg1|
  118. expect(page).not_to have_selector("input[name='#{arg1}'][type='checkbox']")
  119. end
  120. When /^I show offerings count on the investigations page$/ do
  121. visit "/investigations?include_usage_count=true"
  122. end
  123. When /^I remove the investigation "([^"]*)" from the class "([^"]*)"$/ do |investigation_name, class_name|
  124. clazz = Portal::Clazz.find_by_name(class_name)
  125. investigation = Investigation.find_by_name(investigation_name)
  126. offering = Portal::Offering.where(
  127. :runnable_type => investigation.class.name,
  128. :runnable_id => investigation.id,
  129. :clazz_id => clazz.id
  130. ).first
  131. visit "/portal/classes/#{clazz.id}/remove_offering?offering_id=#{offering.id}"
  132. end
  133. When /^I follow "(.*)" on the (.*) "(.*)" from the class "(.*)"$/ do |button_name, model_name, obj_name, class_name|
  134. the_class = model_name.gsub(/\s/, '_').singularize.classify.constantize
  135. clazz = Portal::Clazz.find_by_name(class_name)
  136. obj = the_class.find_by_name(obj_name)
  137. offering = Portal::Offering.where(
  138. :runnable_type => obj.class.name,
  139. :runnable_id => obj.id,
  140. :clazz_id => clazz.id
  141. ).first
  142. selector = "#portal__offering_#{offering.id}"
  143. with_scope(first(:id, selector)) do
  144. first(:link, button_name).click
  145. end
  146. end
  147. When /^a student has performed work on the investigation "([^"]*)" for the class "([^"]*)"$/ do |investigation_name, class_name|
  148. clazz = Portal::Clazz.find_by_name(class_name)
  149. investigation = Investigation.find_by_name(investigation_name)
  150. offering = Portal::Offering.where(
  151. :runnable_type => investigation.class.name,
  152. :runnable_id => investigation.id,
  153. :clazz_id => clazz.id
  154. ).first
  155. FactoryBot.create(:full_portal_learner, :offering => offering)
  156. end
  157. When /^I open the accordion for the offering for investigation "([^"]*)" for the class "([^"]*)"$/ do |investigation_name, class_name|
  158. clazz = Portal::Clazz.find_by_name(class_name)
  159. investigation = Investigation.find_by_name(investigation_name)
  160. offering = Portal::Offering.where(
  161. :runnable_type => investigation.class.name,
  162. :runnable_id => investigation.id,
  163. :clazz_id => clazz.id
  164. ).first
  165. selector = "#_toggle_portal__offering_#{offering.id}"
  166. find(selector).click
  167. end
  168. When /^I drag the investigation "([^"]*)" in the class "(.*)" to "([^"]*)"$/ do |investigation_name, class_name, to|
  169. clazz = Portal::Clazz.find_by_name(class_name)
  170. investigation = Investigation.find_by_name(investigation_name)
  171. offering = Portal::Offering.where(
  172. :runnable_type => investigation.class.name,
  173. :runnable_id => investigation.id,
  174. :clazz_id => clazz.id
  175. ).first
  176. selector = "#portal__offering_#{offering.id}"
  177. # NP 2011-09 see support/drag_and_drop.rb
  178. # TODO: When Selenium issue ( http://bit.ly/q9LHR4 ) closes
  179. # use the actual dragging code which we replaced
  180. #
  181. # find(selector).drag_to(find(to))
  182. fake_drop(selector,to)
  183. end
  184. Then /^the investigation "([^"]*)" in the class "(.*)" should be active$/ do |investigation_name, class_name|
  185. clazz = Portal::Clazz.find_by_name(class_name)
  186. investigation = Investigation.find_by_name(investigation_name)
  187. offering = Portal::Offering.where(
  188. :runnable_type => investigation.class.name,
  189. :runnable_id => investigation.id,
  190. :clazz_id => clazz.id
  191. ).first
  192. expect(offering).to be_active
  193. end
  194. #Then /^"([^\"]*)" should have ([0-9]+) "([^\"]*)"$/ do |selector1, count, selector2|
  195. #within(selector1) do |content|
  196. #content.should have_selector(selector2, :count => count.to_i)
  197. #end
  198. #end
  199. Then /^There should be (\d+) (?:investigations|assignables) displayed$/ do |count|
  200. within("#offering_list") do
  201. expect(page.all(".runnable").size).to eq(count.to_i)
  202. end
  203. end
  204. Then /^"([^"]*)" should not be displayed in the (?:investigations|assignables) list$/ do |not_expected|
  205. within("#offering_list") do
  206. expect(page).to have_no_content(not_expected)
  207. end
  208. end
  209. Then /^the following should (not )?be displayed in the (?:investigations|assignables) list:$/ do |nomatch, table|
  210. within('#assignable_list') do
  211. table.hashes.each do |hash|
  212. if nomatch == "not "
  213. expect(page).to have_no_content(hash[:name])
  214. else
  215. expect(page).to have_content(hash[:name])
  216. end
  217. end
  218. end
  219. end
  220. When /^I click on the next page of results$/ do
  221. within('.pagination') do
  222. click_link('Next')
  223. end
  224. end
  225. When /^I browse public investigations$/ do
  226. visit "/investigations"
  227. end
  228. When /^I browse draft investigations$/ do
  229. visit "/investigations?include_drafts=true"
  230. end
  231. Then /^"([^"]*)" should be displayed in the investigations list$/ do |expected|
  232. within("#offering_list") do
  233. expect(page).to have_content(expected)
  234. end
  235. end
  236. When /^I enter "([^"]*)" in the search box$/ do |search_string|
  237. within('#investigation_search_form') do
  238. fill_in 'name', :with => search_string
  239. end
  240. end
  241. Then /^every investigation should contain "([^"]*)"$/ do |expected|
  242. within("#offering_list") do
  243. page.all(".runnable").each do |piece|
  244. expect(piece).to have_content(expected)
  245. end
  246. end
  247. end
  248. # This step doesnt work, left for reference.
  249. #When /^I wait for the search spinner to be hidden/ do
  250. ## wait for the spinner to show up, and then to go away again
  251. ## This is sooo lame, but I coulnd't get the other techniques (below)
  252. ## to work, and was running out of time
  253. ##page.wait_until { (page.evaluate_script("$('search_spinner').visible()") == false) }
  254. #page.has_css?("#offering_list", :visible =>true)
  255. #page.has_css?("#search_spinner",:visible =>true)
  256. #page.has_css?("#search_spinner",:visible =>false)
  257. #end
  258. #
  259. When /^I wait for all pending requests to complete/ do
  260. # kind of a hack until the automatic junk in capybara starts to work
  261. begin
  262. page.wait_until { true == page.evaluate_script("PendingRequests > 0;")}
  263. rescue Capybara::TimeoutError
  264. puts "PendingRequests was zero"
  265. end
  266. page.wait_until(5) { true == page.evaluate_script("PendingRequests == 0;")}
  267. end
  268. Then /^the investigation "([^"]*)" should have been created$/ do |inv_name|
  269. investigation = Investigation.find_by_name inv_name
  270. expect(investigation).to be
  271. end
  272. Then /^the investigation "([^"]*)" should have an offerings count of (\d+)$/ do |inv_name, count|
  273. investigation = Investigation.find_by_name inv_name
  274. expect(investigation.offerings_count).to eq(count.to_i)
  275. end
  276. def show_actions_menu
  277. # this requires a javascript enabled driver
  278. # this simulates roughly what happens when the mouse is moved over the plus icon
  279. # this first part is what happens in the onmouseover event on the gear icon
  280. # it is necessary to call first because it positions the menu relative to the gear icon
  281. # it also adds listeners to make the menu show up, but we aren't using them since we
  282. # aren't really moving the mouse.
  283. page.execute_script("dropdown_for('button_actions_menu','actions_menu')")
  284. # now that the menu is positioned we can just manually show it
  285. page.execute_script("$('actions_menu').show()")
  286. end
  287. When /^I duplicate the investigation$/ do
  288. show_actions_menu
  289. click_link("duplicate")
  290. page.execute_script("$('actions_menu').hide()")
  291. # need to verify that the duplication is complete so there are not lingering database interactions
  292. expect(page).to have_content('Copied')
  293. end
  294. Then /^I cannot duplicate the investigation$/ do
  295. show_actions_menu
  296. expect(page).to have_no_content('duplicate')
  297. end
  298. When /^(?:|I )create investigations "(.+)" before "(.+)" by date$/ do |investigation_name1, investigation_name2|
  299. created_at = Date.today
  300. ['investigation_name1', 'investigation_name2'].each do |investigation|
  301. inv = Investigation.where(name: investigation).first_or_create
  302. created_at = created_at - 1
  303. inv.created_at = created_at
  304. inv.updated_at = created_at
  305. inv.save!
  306. end
  307. end