browser_test_helper.rb 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. ENV["RAILS_ENV"] = "test"
  2. require File.expand_path('../../config/environment', __FILE__)
  3. require 'selenium-webdriver'
  4. class TestCase < Test::Unit::TestCase
  5. def browser
  6. ENV['BROWSER'] || 'firefox'
  7. end
  8. def browser_support_cookies
  9. if browser =~ /(internet_explorer|ie)/i
  10. return false
  11. end
  12. return true
  13. end
  14. def browser_url
  15. ENV['BROWSER_URL'] || 'http://localhost:3000'
  16. end
  17. def browser_instance
  18. if !@browsers
  19. @browsers = {}
  20. end
  21. if !ENV['REMOTE_URL'] || ENV['REMOTE_URL'].empty?
  22. local_browser = Selenium::WebDriver.for( browser.to_sym )
  23. browser_instance_preferences(local_browser)
  24. @browsers[local_browser.hash] = local_browser
  25. return local_browser
  26. end
  27. caps = Selenium::WebDriver::Remote::Capabilities.send( browser )
  28. if ENV['BROWSER_OS']
  29. caps.platform = ENV['BROWSER_OS']
  30. end
  31. if ENV['BROWSER_VERSION']
  32. caps.version = ENV['BROWSER_VERSION']
  33. end
  34. local_browser = Selenium::WebDriver.for(
  35. :remote,
  36. :url => ENV['REMOTE_URL'],
  37. :desired_capabilities => caps,
  38. )
  39. browser_instance_preferences(local_browser)
  40. @browsers[local_browser.hash] = local_browser
  41. return local_browser
  42. end
  43. def browser_instance_close(local_browser)
  44. return if !@browsers[local_browser.hash]
  45. @browsers.delete( local_browser.hash )
  46. local_browser.quit
  47. end
  48. def browser_instance_preferences(local_browser)
  49. #local_browser.manage.window.resize_to(1024, 1024)
  50. if ENV['REMOTE_URL'] !~ /saucelabs/i
  51. if @browsers.size < 1
  52. local_browser.manage.window.move_to(0, 0)
  53. else
  54. local_browser.manage.window.move_to(1024, 0)
  55. end
  56. end
  57. local_browser.manage.timeouts.implicit_wait = 3 # seconds
  58. end
  59. def teardown
  60. return if !@browsers
  61. @browsers.each { |hash, local_browser|
  62. browser_instance_close(local_browser)
  63. }
  64. end
  65. =begin
  66. username = login(
  67. :browser => browser1,
  68. :username => 'someuser',
  69. :password => 'somepassword',
  70. :url => 'some url', # optional
  71. :remember_me => true, # optional
  72. )
  73. =end
  74. def login(params)
  75. instance = params[:browser] || @browser
  76. if params[:url]
  77. instance.get( params[:url] )
  78. end
  79. element = instance.find_elements( { :css => '#login input[name="username"]' } )[0]
  80. if !element
  81. raise "No login box found"
  82. end
  83. element.clear
  84. element.send_keys( params[:username] )
  85. element = instance.find_elements( { :css => '#login input[name="password"]' } )[0]
  86. element.clear
  87. element.send_keys( params[:password] )
  88. if params[:remember_me]
  89. instance.find_elements( { :css => '#login [name="remember_me"]' } )[0].click
  90. end
  91. instance.find_elements( { :css => '#login button' } )[0].click
  92. sleep 4
  93. login = instance.find_elements( { :css => '.user-menu .user a' } )[0].attribute('title')
  94. if login != params[:username]
  95. raise "login failed"
  96. end
  97. assert( true, "login ok" )
  98. login
  99. end
  100. =begin
  101. logout(
  102. :browser => browser1
  103. )
  104. =end
  105. def logout(params = {})
  106. instance = params[:browser] || @browser
  107. instance.find_elements( { :css => 'a[href="#current_user"]' } )[0].click
  108. sleep 0.1
  109. instance.find_elements( { :css => 'a[href="#logout"]' } )[0].click
  110. (1..6).each {|loop|
  111. login = instance.find_elements( { :css => '#login' } )[0]
  112. if login
  113. assert( true, "logout ok" )
  114. return
  115. end
  116. }
  117. raise "no login box found, seems logout was not successfully!"
  118. end
  119. =begin
  120. location(
  121. :browser => browser1,
  122. :url => 'http://someurl',
  123. )
  124. =end
  125. def location(params)
  126. instance = params[:browser] || @browser
  127. instance.get( params[:url] )
  128. end
  129. =begin
  130. location_check(
  131. :browser => browser1,
  132. :url => 'http://someurl',
  133. )
  134. =end
  135. def location_check(params)
  136. instance = params[:browser] || @browser
  137. if instance.current_url !~ /#{Regexp.quote(params[:url])}/
  138. raise "url #{instance.current_url} is not matching #{params[:url]}"
  139. end
  140. assert( true, "url #{instance.current_url} is matching #{params[:url]}" )
  141. end
  142. =begin
  143. reload(
  144. :browser => browser1,
  145. )
  146. =end
  147. def reload(params = {})
  148. instance = params[:browser] || @browser
  149. instance.navigate.refresh
  150. end
  151. =begin
  152. click(
  153. :browser => browser1,
  154. :css => '.some_class',
  155. )
  156. =end
  157. def click(params)
  158. instance = params[:browser] || @browser
  159. instance.find_elements( { :css => params[:css] } )[0].click
  160. sleep 0.5
  161. end
  162. =begin
  163. exists(
  164. :browser => browser1,
  165. :css => '.some_class',
  166. )
  167. =end
  168. def exists(params)
  169. instance = params[:browser] || @browser
  170. if !instance.find_elements( { :css => params[:css] } )[0]
  171. raise "#{params[:css]} dosn't exist, but should"
  172. end
  173. true
  174. end
  175. =begin
  176. exists_not(
  177. :browser => browser1,
  178. :css => '.some_class',
  179. )
  180. =end
  181. def exists_not(params)
  182. instance = params[:browser] || @browser
  183. if instance.find_elements( { :css => params[:css] } )[0]
  184. raise "#{params[:css]} exists but should not"
  185. end
  186. true
  187. end
  188. =begin
  189. set(
  190. :browser => browser1,
  191. :css => '.some_class',
  192. :value => true,
  193. :slow => false,
  194. :clear => true, # todo
  195. )
  196. =end
  197. def set(params)
  198. instance = params[:browser] || @browser
  199. element = instance.find_elements( { :css => params[:css] } )[0]
  200. element.clear
  201. if !params[:slow]
  202. element.send_keys( params[:value] )
  203. else
  204. element.send_keys( '' )
  205. keys = params[:value].to_s.split('')
  206. keys.each {|key|
  207. instance.action.send_keys(key).perform
  208. }
  209. end
  210. sleep 0.5
  211. end
  212. =begin
  213. select(
  214. :browser => browser1,
  215. :css => '.some_class',
  216. :value => 'Some Value',
  217. )
  218. =end
  219. def select(params)
  220. instance = params[:browser] || @browser
  221. element = instance.find_elements( { :css => params[:css] } )[0]
  222. dropdown = Selenium::WebDriver::Support::Select.new(element)
  223. dropdown.select_by(:text, params[:value])
  224. end
  225. =begin
  226. check(
  227. :browser => browser1,
  228. :css => '.some_class',
  229. )
  230. =end
  231. def check(params)
  232. instance = params[:browser] || @browser
  233. element = instance.find_elements( { :css => params[:css] } )[0]
  234. checked = element.attribute('checked')
  235. if !checked
  236. element.click
  237. end
  238. end
  239. =begin
  240. uncheck(
  241. :browser => browser1,
  242. :css => '.some_class',
  243. )
  244. =end
  245. def uncheck(params)
  246. instance = params[:browser] || @browser
  247. element = instance.find_elements( { :css => params[:css] } )[0]
  248. checked = element.attribute('checked')
  249. if checked
  250. element.click
  251. end
  252. end
  253. =begin
  254. sendkey(
  255. :browser => browser1,
  256. :value => :enter,
  257. )
  258. =end
  259. def sendkey(params)
  260. instance = params[:browser] || @browser
  261. if params[:value].class == Array
  262. params[:value].each {|key|
  263. instance.action.send_keys(key).perform
  264. }
  265. return
  266. end
  267. instance.action.send_keys(params[:value]).perform
  268. sleep 0.5
  269. end
  270. =begin
  271. match(
  272. :browser => browser1,
  273. :css => '#content .text-1',
  274. :value => 'some test for browser and some other for browser',
  275. :attribute => 'some_attribute', # match on attribute
  276. :should_not_match => true,
  277. :no_quote => false, # use regex
  278. )
  279. =end
  280. def match(params)
  281. instance = params[:browser] || @browser
  282. element = instance.find_elements( { :css => params[:css] } )[0]
  283. if params[:css] =~ /select/
  284. dropdown = Selenium::WebDriver::Support::Select.new(element)
  285. success = false
  286. if dropdown.selected_options
  287. dropdown.selected_options.each {|option|
  288. if option.text == params[:value]
  289. success = true
  290. end
  291. }
  292. end
  293. if params[:should_not_match]
  294. if success
  295. raise "should not match '#{params[:value]}' in select list, but is matching"
  296. end
  297. return true
  298. else
  299. if !success
  300. raise "not matching '#{params[:value]}' in select list"
  301. end
  302. return true
  303. end
  304. end
  305. # match pn attribute
  306. if params[:attribute]
  307. text = element.attribute( params[:attribute] )
  308. elsif params[:css] =~ /(input|textarea)/i
  309. text = element.attribute('value')
  310. else
  311. text = element.text
  312. end
  313. match = false
  314. if params[:no_quote]
  315. #puts "aaaa #{text}/#{params[:value]}"
  316. if text =~ /#{params[:value]}/i
  317. match = $1 || true
  318. end
  319. else
  320. if text =~ /#{Regexp.quote(params[:value])}/i
  321. match = true
  322. end
  323. end
  324. if match
  325. if params[:should_not_match]
  326. raise "matching '#{params[:value]}' in content '#{text}' but should not!"
  327. end
  328. else
  329. if !params[:should_not_match]
  330. raise "not matching '#{params[:value]}' in content '#{text}' but should!"
  331. end
  332. end
  333. sleep 0.8
  334. return match
  335. end
  336. =begin
  337. match_not(
  338. :browser => browser1,
  339. :css => '#content .text-1',
  340. :value => 'some test for browser and some other for browser',
  341. :attribute => 'some_attribute', # match on attribute
  342. :should_not_match => true,
  343. :no_quote => false, # use regex
  344. )
  345. =end
  346. def match_not(params)
  347. params[:should_not_match] = true
  348. match(params)
  349. end
  350. =begin
  351. cookie(
  352. :browser => browser1,
  353. :name => '^_zammad.+?',
  354. :value => '.+?',
  355. :expires => nil,
  356. )
  357. cookie(
  358. :browser => browser1,
  359. :name => '^_zammad.+?',
  360. :should_not_exist => true,
  361. )
  362. =end
  363. def cookie(params)
  364. instance = params[:browser] || @browser
  365. if !browser_support_cookies
  366. assert( true, "'#{params[:value]}' ups browser is not supporting reading cookies, go ahead")
  367. return true
  368. end
  369. cookies = instance.manage.all_cookies
  370. cookies.each {|cookie|
  371. #puts "CCC #{cookie.inspect}"
  372. # :name=>"_zammad_session_c25832f4de2", :value=>"adc31cd21615cb0a7ab269184ec8b76f", :path=>"/", :domain=>"localhost", :expires=>nil, :secure=>false}
  373. if cookie[:name] =~ /#{params[:name]}/i
  374. if params.has_key?( :value ) && cookie[:value].to_s =~ /#{params[:value]}/i
  375. assert( true, "matching value '#{params[:value]}' in cookie '#{cookie.to_s}'" )
  376. else
  377. raise "not matching value '#{params[:value]}' in cookie '#{cookie.to_s}'"
  378. end
  379. if params.has_key?( :expires ) && cookie[:expires].to_s =~ /#{params[:expires]}/i
  380. assert( true, "matching expires '#{params[:expires].inspect}' in cookie '#{cookie.to_s}'" )
  381. else
  382. raise "not matching expires '#{params[:expires]}' in cookie '#{cookie.to_s}'"
  383. end
  384. if params[:should_not_exist]
  385. raise "cookie with name '#{params[:name]}' should not exist, but exists '#{cookies.to_s}'"
  386. end
  387. return
  388. end
  389. }
  390. if params[:should_not_exist]
  391. assert( true, "cookie with name '#{params[:name]}' is not existing" )
  392. return
  393. end
  394. raise "not matching name '#{params[:name]}' in cookie '#{cookies.to_s}'"
  395. end
  396. =begin
  397. watch_for(
  398. :browser => browser1,
  399. :css => true,
  400. :value => 'some text',
  401. :timeout => '16', # in sec, default 16
  402. )
  403. =end
  404. def watch_for(params = {})
  405. instance = params[:browser] || @browser
  406. timeout = 16
  407. if params[:timeout]
  408. timeout = params[:timeout]
  409. end
  410. loops = (timeout).to_i
  411. text = ''
  412. (1..loops).each { |loop|
  413. element = instance.find_elements( { :css => params[:css] } )[0]
  414. if element #&& element.displayed?
  415. begin
  416. text = element.text
  417. if text =~ /#{params[:value]}/i
  418. assert( true, "'#{params[:value]}' found in '#{text}'" )
  419. sleep 0.5
  420. return true
  421. end
  422. rescue
  423. # just try again
  424. end
  425. end
  426. sleep 1
  427. }
  428. raise "'#{params[:value]}' found in '#{text}'"
  429. end
  430. =begin
  431. watch_for_disappear(
  432. :browser => browser1,
  433. :css => true,
  434. :timeout => '16', # in sec, default 16
  435. )
  436. =end
  437. def watch_for_disappear(params = {})
  438. instance = params[:browser] || @browser
  439. timeout = 16
  440. if action[:timeout]
  441. timeout = params[:timeout]
  442. end
  443. loops = (timeout).to_i
  444. text = ''
  445. (1..loops).each { |loop|
  446. element = instance.find_elements( { :css => params[:css] } )[0]
  447. if !element #|| element.displayed?
  448. assert( true, "not found" )
  449. sleep 1
  450. return true
  451. end
  452. sleep 1
  453. }
  454. raise "#{test[:css]}) still exsists"
  455. end
  456. =begin
  457. tasks_close_all(
  458. :browser => browser1,
  459. :discard_changes => true,
  460. )
  461. =end
  462. def tasks_close_all(params = {})
  463. instance = params[:browser] || @browser
  464. for i in 1..100
  465. sleep 1
  466. begin
  467. if instance.find_elements( { :css => '.navigation .tasks .task:first-child' } )[0]
  468. instance.mouse.move_to( instance.find_elements( { :css => '.navigation .tasks .task:first-child' } )[0] )
  469. sleep 0.2
  470. click_element = instance.find_elements( { :css => '.navigation .tasks .task:first-child .js-close' } )[0]
  471. if click_element
  472. sleep 0.1
  473. click_element.click
  474. # accept task close warning
  475. if params[:discard_changes]
  476. sleep 1
  477. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  478. end
  479. end
  480. else
  481. break
  482. end
  483. rescue
  484. # just try again
  485. end
  486. end
  487. sleep 1
  488. assert( true, "all tasks closed" )
  489. end
  490. =begin
  491. username = overview_create(
  492. :browser => browser1,
  493. :data => {
  494. :name => name,
  495. :link => name,
  496. :role => 'Agent',
  497. :prio => 1000,
  498. 'order::direction' => 'down',
  499. }
  500. )
  501. =end
  502. def overview_create(params)
  503. instance = params[:browser] || @browser
  504. data = params[:data]
  505. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  506. instance.find_elements( { :css => 'a[href="#manage/overviews"]' } )[0].click
  507. instance.find_elements( { :css => '#content a[data-type="new"]' } )[0].click
  508. sleep 2
  509. if data[:name]
  510. element = instance.find_elements( { :css => '.modal input[name=name]' } )[0]
  511. element.clear
  512. element.send_keys( data[:name] )
  513. end
  514. if data[:link]
  515. element = instance.find_elements( { :css => '.modal input[name=link]' } )[0]
  516. element.clear
  517. element.send_keys( data[:link] )
  518. end
  519. if data[:role]
  520. element = instance.find_elements( { :css => '.modal select[name="role_id"]' } )[0]
  521. dropdown = Selenium::WebDriver::Support::Select.new(element)
  522. dropdown.select_by( :text, data[:role])
  523. end
  524. if data[:prio]
  525. element = instance.find_elements( { :css => '.modal input[name=prio]' } )[0]
  526. element.clear
  527. element.send_keys( data[:prio] )
  528. end
  529. if data['order::direction']
  530. element = instance.find_elements( { :css => '.modal select[name="order::direction"]' } )[0]
  531. dropdown = Selenium::WebDriver::Support::Select.new(element)
  532. dropdown.select_by( :text, data['order::direction'])
  533. end
  534. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  535. (1..12).each {|loop|
  536. element = instance.find_elements( { :css => 'body' } )[0]
  537. text = element.text
  538. if text =~ /#{Regexp.quote(data[:name])}/
  539. assert( true, "overview created" )
  540. overview = {
  541. :name => name,
  542. }
  543. return overview
  544. end
  545. sleep 1
  546. }
  547. raise "overview creation failed"
  548. end
  549. =begin
  550. ticket = ticket_create(
  551. :browser => browser1,
  552. :data => {
  553. :customer => 'nico',
  554. :group => 'Users',
  555. :title => 'overview #1',
  556. :body => 'overview #1',
  557. },
  558. :do_not_submit => true,
  559. )
  560. returns (in case of submitted)
  561. {
  562. :id => 123,
  563. :number => '100001',
  564. }
  565. =end
  566. def ticket_create(params)
  567. instance = params[:browser] || @browser
  568. data = params[:data]
  569. instance.find_elements( { :css => 'a[href="#new"]' } )[0].click
  570. instance.find_elements( { :css => 'a[href="#ticket/create"]' } )[0].click
  571. element = instance.find_elements( { :css => '.active .newTicket' } )[0]
  572. if !element
  573. raise "no ticket create screen found!"
  574. end
  575. sleep 1
  576. # check count of agents, should be only 1 / - selection on init screen
  577. count = instance.find_elements( { :css => '.active .newTicket select[name="owner_id"] option' } ).count
  578. assert_equal( 1, count, 'check if owner selection is empty per default' )
  579. if data[:group]
  580. element = instance.find_elements( { :css => '.active .newTicket select[name="group_id"]' } )[0]
  581. dropdown = Selenium::WebDriver::Support::Select.new(element)
  582. dropdown.select_by( :text, data[:group])
  583. sleep 0.2
  584. end
  585. if data[:title]
  586. element = instance.find_elements( { :css => '.active .newTicket input[name="title"]' } )[0]
  587. element.clear
  588. element.send_keys( data[:title] )
  589. sleep 0.2
  590. end
  591. if data[:body]
  592. #instance.execute_script( '$(".active .newTicket div[data-name=body]").focus()' )
  593. sleep 0.5
  594. element = instance.find_elements( { :css => '.active .newTicket div[data-name=body]' } )[0]
  595. element.clear
  596. element.send_keys( data[:body] )
  597. end
  598. if data[:customer]
  599. element = instance.find_elements( { :css => '.active .newTicket input[name="customer_id_completion"]' } )[0]
  600. element.click
  601. element.clear
  602. element.send_keys( data[:customer] )
  603. sleep 4
  604. element.send_keys( :arrow_down )
  605. sleep 0.1
  606. instance.find_elements( { :css => '.active .newTicket .recipientList-entry.js-user.is-active' } )[0].click
  607. sleep 0.3
  608. end
  609. if params[:do_not_submit]
  610. assert( true, "ticket created without submit" )
  611. return
  612. end
  613. sleep 0.8
  614. #instance.execute_script( '$(".content.active .newTicket form").submit();' )
  615. instance.find_elements( { :css => '.active .newTicket button.submit' } )[0].click
  616. sleep 1
  617. (1..16).each {|loop|
  618. if instance.current_url =~ /#{Regexp.quote('#ticket/zoom/')}/
  619. assert( true, "ticket created" )
  620. sleep 0.5
  621. id = instance.current_url
  622. id.gsub!(//, )
  623. id.gsub!(/^.+?\/(\d+)$/, "\\1")
  624. number = instance.find_elements( { :css => '.active .page-header .ticket-number' } )[0].text
  625. ticket = {
  626. :id => id,
  627. :number => number,
  628. }
  629. sleep 1
  630. return ticket
  631. end
  632. sleep 0.5
  633. }
  634. raise "ticket creation failed, can't get zoom url"
  635. end
  636. =begin
  637. ticket_update(
  638. :browser => browser1,
  639. :data => {
  640. :title => '',
  641. :body => 'some body',
  642. :group => 'some group',
  643. :state => 'closed',
  644. },
  645. :do_not_submit => true,
  646. )
  647. =end
  648. def ticket_update(params)
  649. instance = params[:browser] || @browser
  650. data = params[:data]
  651. if data[:title]
  652. #element = instance.find_elements( { :css => '.content.active .page-header .ticket-title-update' } )[0]
  653. #element.clear
  654. #sleep 0.5
  655. #element = instance.find_elements( { :css => '.content.active .page-header .ticket-title-update' } )[0]
  656. #element.send_keys( data[:title] )
  657. #sleep 0.5
  658. #element.send_keys( :tab )
  659. instance.execute_script( '$(".content.active .page-header .ticket-title-update").focus()' )
  660. instance.execute_script( '$(".content.active .page-header .ticket-title-update").text("' + data[:title] + '")' )
  661. instance.execute_script( '$(".content.active .page-header .ticket-title-update").blur()' )
  662. instance.execute_script( '$(".content.active .page-header .ticket-title-update").trigger("blur")' )
  663. # {
  664. # :where => :instance2,
  665. # :execute => 'sendkey',
  666. # :css => '.content.active .page-header .ticket-title-update',
  667. # :value => 'TTT',
  668. # },
  669. # {
  670. # :where => :instance2,
  671. # :execute => 'sendkey',
  672. # :css => '.content.active .page-header .ticket-title-update',
  673. # :value => :tab,
  674. # },
  675. end
  676. if data[:body]
  677. #instance.execute_script( '$(".content.active div[data-name=body]").focus()' )
  678. sleep 0.5
  679. element = instance.find_elements( { :css => '.content.active div[data-name=body]' } )[0]
  680. element.clear
  681. element.send_keys( data[:body] )
  682. end
  683. if data[:group]
  684. element = instance.find_elements( { :css => '.active .sidebar select[name="group_id"]' } )[0]
  685. dropdown = Selenium::WebDriver::Support::Select.new(element)
  686. dropdown.select_by( :text, data[:group])
  687. sleep 0.2
  688. end
  689. if data[:state]
  690. element = instance.find_elements( { :css => '.active .sidebar select[name="state_id"]' } )[0]
  691. dropdown = Selenium::WebDriver::Support::Select.new(element)
  692. dropdown.select_by( :text, data[:state])
  693. sleep 0.2
  694. end
  695. found = nil
  696. (1..5).each {|loop|
  697. if !found
  698. begin
  699. text = instance.find_elements( { :css => '.content.active .js-reset' } )[0].text
  700. if text =~ /(Discard your unsaved changes.|Verwerfen der)/
  701. found = true
  702. end
  703. rescue
  704. # just try again
  705. end
  706. sleep 1
  707. end
  708. }
  709. if !found
  710. raise "no discard message found"
  711. end
  712. if params[:do_not_submit]
  713. assert( true, "(#{test[:name]}) ticket updated without submit" )
  714. return true
  715. end
  716. instance.find_elements( { :css => '.content.active button.js-submit' } )[0].click
  717. (1..10).each {|loop|
  718. text = instance.find_elements( { :css => '.content.active .js-reset' } )[0].text
  719. if !text || text.empty?
  720. return true
  721. end
  722. sleep 1
  723. }
  724. raise "unable to update ticket"
  725. end
  726. =begin
  727. ticket_open_by_overview(
  728. :browser => browser2,
  729. :number => ticket1[:number],
  730. :link => '#ticket/view/' + name,
  731. )
  732. =end
  733. def ticket_open_by_overview(params)
  734. instance = params[:browser] || @browser
  735. instance.find_elements( { :css => '#navigation li.overviews a' } )[0].click
  736. sleep 1
  737. instance.find_elements( { :css => ".content.active .sidebar a[href=\"#{params[:link]}\"]" } )[0].click
  738. sleep 1
  739. element = instance.find_elements( { :partial_link_text => params[:number] } )[0].click
  740. sleep 1
  741. number = instance.find_elements( { :css => '.active .page-header .ticket-number' } )[0].text
  742. if number !~ /#{params[:number]}/
  743. raise "unable to search/find ticket #{params[:number]}!"
  744. end
  745. assert( true, "ticket #{params[:number]} found" )
  746. end
  747. =begin
  748. ticket_open_by_search(
  749. :browser => browser2,
  750. :number => ticket1[:number],
  751. )
  752. =end
  753. def ticket_open_by_search(params)
  754. instance = params[:browser] || @browser
  755. # search by number
  756. element = instance.find_elements( { :css => '#global-search' } )[0]
  757. element.click
  758. element.clear
  759. element.send_keys( params[:number] )
  760. sleep 3
  761. # empty search box by x
  762. instance.find_elements( { :css => '.search .empty-search' } )[0].click
  763. sleep 0.5
  764. text = instance.find_elements( { :css => '#global-search' } )[0].attribute('value')
  765. if !text
  766. raise "#global-search is not empty!"
  767. end
  768. # search by number again
  769. element = instance.find_elements( { :css => '#global-search' } )[0]
  770. element.click
  771. element.clear
  772. element.send_keys( params[:number] )
  773. sleep 1
  774. # open ticket
  775. element = instance.find_element( { :partial_link_text => params[:number] } ).click
  776. number = instance.find_elements( { :css => '.active .page-header .ticket-number' } )[0].text
  777. if number !~ /#{params[:number]}/
  778. raise "unable to search/find ticket #{params[:number]}!"
  779. end
  780. end
  781. =begin
  782. overview_count = overview_counter(
  783. :browser => browser2,
  784. )
  785. returns
  786. {
  787. '#ticket/view/all_unassigned' => 42,
  788. }
  789. =end
  790. def overview_counter(params = {})
  791. instance = params[:browser] || @browser
  792. instance.find_elements( { :css => '#navigation li.overviews a' } )[0].click
  793. sleep 2
  794. overviews = {}
  795. instance.find_elements( { :css => '.content.active .sidebar a[href]' } ).each {|element|
  796. url = element.attribute('href')
  797. url.gsub!(/(http|https):\/\/.+?\/(.+?)$/, "\\2")
  798. overviews[url] = 0
  799. #puts url.inspect
  800. #puts element.inspect
  801. }
  802. overviews.each {|url, value|
  803. count = instance.find_elements( { :css => ".content.active .sidebar a[href=\"#{url}\"] .badge" } )[0].text
  804. overviews[url] = count.to_i
  805. }
  806. overviews
  807. end
  808. =begin
  809. organization_open_by_search(
  810. :browser => browser2,
  811. :value => 'some value',
  812. )
  813. =end
  814. def organization_open_by_search(params = {})
  815. instance = params[:browser] || @browser
  816. element = instance.find_elements( { :css => '#global-search' } )[0]
  817. element.click
  818. element.clear
  819. element.send_keys( params[:value] )
  820. sleep 3
  821. instance.find_elements( { :css => '.search .empty-search' } )[0].click
  822. sleep 0.5
  823. text = instance.find_elements( { :css => '#global-search' } )[0].attribute('value')
  824. if !text
  825. raise "#global-search is not empty!"
  826. end
  827. element = instance.find_elements( { :css => '#global-search' } )[0]
  828. element.click
  829. element.clear
  830. element.send_keys( params[:value] )
  831. sleep 2
  832. element = instance.find_element( { :partial_link_text => params[:value] } ).click
  833. name = instance.find_elements( { :css => '.active h1' } )[0].text
  834. if name !~ /#{params[:value]}/
  835. raise "unable to search/find org #{params[:value]}!"
  836. return
  837. end
  838. assert( true, "org #{params[:value]} found" )
  839. true
  840. end
  841. =begin
  842. user_open_by_search(
  843. :browser => browser2,
  844. :value => 'some value',
  845. )
  846. =end
  847. def user_open_by_search(params = {})
  848. instance = params[:browser] || @browser
  849. element = instance.find_elements( { :css => '#global-search' } )[0]
  850. element.click
  851. element.clear
  852. element.send_keys( params[:value] )
  853. sleep 3
  854. element = instance.find_element( { :partial_link_text => params[:value] } ).click
  855. name = instance.find_elements( { :css => '.active h1' } )[0].text
  856. if name !~ /#{params[:value]}/
  857. raise "unable to search/find user #{params[:value]}!"
  858. end
  859. assert( true, "user #{params[:term]} found" )
  860. true
  861. end
  862. =begin
  863. user_create(
  864. :browser => browser2,
  865. :data => {
  866. :login => 'some login' + random,
  867. :firstname => 'Manage Firstname' + random,
  868. :lastname => 'Manage Lastname' + random,
  869. :email => user_email,
  870. :password => 'some-pass',
  871. },
  872. )
  873. =end
  874. def user_create(params = {})
  875. instance = params[:browser] || @browser
  876. data = params[:data]
  877. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  878. instance.find_elements( { :css => 'a[href="#manage/users"]' } )[0].click
  879. sleep 2
  880. instance.find_elements( { :css => 'a[data-type="new"]' } )[0].click
  881. sleep 2
  882. element = instance.find_elements( { :css => '.modal input[name=firstname]' } )[0]
  883. element.clear
  884. element.send_keys( data[:firstname] )
  885. element = instance.find_elements( { :css => '.modal input[name=lastname]' } )[0]
  886. element.clear
  887. element.send_keys( data[:lastname] )
  888. element = instance.find_elements( { :css => '.modal input[name=email]' } )[0]
  889. element.clear
  890. element.send_keys( data[:email] )
  891. element = instance.find_elements( { :css => '.modal input[name=password]' } )[0]
  892. element.clear
  893. element.send_keys( data[:password] )
  894. element = instance.find_elements( { :css => '.modal input[name=password_confirm]' } )[0]
  895. element.clear
  896. element.send_keys( data[:password] )
  897. instance.find_elements( { :css => '.modal input[name="role_ids"][value="3"]' } )[0].click
  898. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  899. sleep 2
  900. set(
  901. :browser => instance,
  902. :css => '.content .js-search',
  903. :value => data[:email],
  904. )
  905. watch_for(
  906. :browser => instance,
  907. :css => 'body',
  908. :value => data[:lastname],
  909. )
  910. assert( true, "user created" )
  911. end
  912. =begin
  913. sla_create(
  914. :browser => browser2,
  915. :data => {
  916. :name => 'some sla' + random,
  917. :first_response_time => 61
  918. },
  919. )
  920. =end
  921. def sla_create(params = {})
  922. instance = params[:browser] || @browser
  923. data = params[:data]
  924. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  925. instance.find_elements( { :css => 'a[href="#manage/slas"]' } )[0].click
  926. sleep 2
  927. instance.find_elements( { :css => 'a[data-type="new"]' } )[0].click
  928. sleep 2
  929. element = instance.find_elements( { :css => '.modal input[name=name]' } )[0]
  930. element.clear
  931. element.send_keys( data[:name] )
  932. element = instance.find_elements( { :css => '.modal input[name=first_response_time]' } )[0]
  933. element.clear
  934. element.send_keys( data[:first_response_time] )
  935. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  936. (1..8).each {|loop|
  937. element = instance.find_elements( { :css => 'body' } )[0]
  938. text = element.text
  939. if text =~ /#{Regexp.quote(data[:name])}/
  940. assert( true, "sla created" )
  941. return true
  942. end
  943. sleep 1
  944. }
  945. raise "sla creation failed"
  946. end
  947. =begin
  948. text_module_create(
  949. :browser => browser2,
  950. :data => {
  951. :name => 'some sla' + random,
  952. :keywords => 'some keywords',
  953. :content => 'some content',
  954. },
  955. )
  956. =end
  957. def text_module_create(params = {})
  958. instance = params[:browser] || @browser
  959. data = params[:data]
  960. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  961. instance.find_elements( { :css => 'a[href="#manage/text_modules"]' } )[0].click
  962. sleep 2
  963. instance.find_elements( { :css => 'a[data-type="new"]' } )[0].click
  964. sleep 2
  965. element = instance.find_elements( { :css => '.modal input[name=name]' } )[0]
  966. element.clear
  967. element.send_keys( data[:name] )
  968. element = instance.find_elements( { :css => '.modal input[name=keywords]' } )[0]
  969. element.clear
  970. element.send_keys( data[:keywords] )
  971. element = instance.find_elements( { :css => '.modal textarea[name=content]' } )[0]
  972. element.clear
  973. element.send_keys( data[:content] )
  974. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  975. (1..8).each {|loop|
  976. element = instance.find_elements( { :css => 'body' } )[0]
  977. text = element.text
  978. if text =~ /#{Regexp.quote(data[:name])}/
  979. assert( true, "text module created" )
  980. return true
  981. end
  982. sleep 1
  983. }
  984. raise "text module creation failed"
  985. end
  986. # Add more helper methods to be used by all tests here...
  987. def browser_login(data)
  988. all_tests = [
  989. {
  990. :name => 'login',
  991. :instance => data[:instance] || browser_instance,
  992. :url => data[:url] || browser_url,
  993. :action => [
  994. {
  995. :execute => 'login',
  996. :username => data[:username] || 'nicole.braun@zammad.org',
  997. :password => data[:password] || 'test'
  998. },
  999. ],
  1000. },
  1001. ];
  1002. return all_tests
  1003. end
  1004. def browser_signle_test_with_login(tests, login = {})
  1005. all_tests = browser_login( login )
  1006. all_tests = all_tests.concat( tests )
  1007. browser_single_test(all_tests)
  1008. end
  1009. def browser_double_test(tests)
  1010. instance1 = browser_single_test( browser_login({
  1011. :instance => tests[0][:instance1],
  1012. :username => tests[0][:instance1_username],
  1013. :password => tests[0][:instance1_password],
  1014. :url => tests[0][:url],
  1015. }), true )
  1016. instance2 = browser_single_test( browser_login({
  1017. :instance => tests[0][:instance2],
  1018. :username => tests[0][:instance2_username],
  1019. :password => tests[0][:instance2_password],
  1020. :url => tests[0][:url],
  1021. }), true )
  1022. tests.each { |test|
  1023. if test[:action]
  1024. test[:action].each { |action|
  1025. # wait
  1026. if action[:execute] == 'wait'
  1027. sleep action[:value]
  1028. next
  1029. end
  1030. # ignore no browser defined action
  1031. next if !action[:where]
  1032. # set current browser
  1033. if action[:where] == :instance1
  1034. instance = instance1
  1035. else
  1036. instance = instance2
  1037. end
  1038. browser_element_action(test, action, instance)
  1039. }
  1040. end
  1041. }
  1042. browser_instance_close(instance1)
  1043. browser_instance_close(instance2)
  1044. end
  1045. def browser_single_test(tests, keep_connection = false)
  1046. instance = nil
  1047. @stack = nil
  1048. tests.each { |test|
  1049. if test[:instance]
  1050. instance = test[:instance]
  1051. end
  1052. if test[:url]
  1053. instance.get( test[:url] )
  1054. end
  1055. if test[:action]
  1056. test[:action].each { |action|
  1057. if action[:execute] == 'wait'
  1058. sleep action[:value]
  1059. next
  1060. end
  1061. browser_element_action(test, action, instance)
  1062. }
  1063. end
  1064. }
  1065. if keep_connection
  1066. return instance
  1067. end
  1068. browser_instance_close(instance)
  1069. end
  1070. def browser_element_action(test, action, instance)
  1071. puts "NOTICE #{Time.now.to_s}: " + action.inspect
  1072. if action[:execute] !~ /accept|dismiss/i
  1073. #cookies = instance.manage.all_cookies
  1074. #cookies.each {|cookie|
  1075. # puts " COOKIE " + cookie.to_s
  1076. #}
  1077. end
  1078. sleep 0.1
  1079. if action[:css]
  1080. if action[:css].match '###stack###'
  1081. action[:css].gsub! '###stack###', @stack
  1082. end
  1083. begin
  1084. if action[:range] == 'all'
  1085. element = instance.find_elements( { :css => action[:css] } )
  1086. else
  1087. element = instance.find_element( { :css => action[:css] } )
  1088. end
  1089. rescue
  1090. element = nil
  1091. end
  1092. if action[:result] == false
  1093. assert( !element, "(#{test[:name]}) Element with css '#{action[:css]}' exists" )
  1094. else
  1095. assert( element, "(#{test[:name]}) Element with css '#{action[:css]}' doesn't exist" )
  1096. end
  1097. elsif action[:element] == :url
  1098. if instance.current_url =~ /#{Regexp.quote(action[:result])}/
  1099. assert( true, "(#{test[:name]}) url #{instance.current_url} is matching #{action[:result]}" )
  1100. else
  1101. assert( false, "(#{test[:name]}) url #{instance.current_url} is not matching #{action[:result]}" )
  1102. end
  1103. elsif action[:element] == :title
  1104. title = instance.title
  1105. if title =~ /#{action[:value]}/i
  1106. assert( true, "(#{test[:name]}) matching '#{action[:value]}' in title '#{title}'" )
  1107. else
  1108. assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in title '#{title}'" )
  1109. end
  1110. return
  1111. elsif action[:element] == :alert
  1112. element = instance.switch_to.alert
  1113. elsif action[:execute] == 'login'
  1114. element = instance.find_elements( { :css => '#login input[name="username"]' } )[0]
  1115. if !element
  1116. assert( false, "(#{test[:name]}) no login box found!" )
  1117. return
  1118. end
  1119. element.clear
  1120. element.send_keys( action[:username] )
  1121. element = instance.find_elements( { :css => '#login input[name="password"]' } )[0]
  1122. element.clear
  1123. element.send_keys( action[:password] )
  1124. if action[:remember_me]
  1125. instance.find_elements( { :css => '#login [name="remember_me"]' } )[0].click
  1126. end
  1127. instance.find_elements( { :css => '#login button' } )[0].click
  1128. sleep 4
  1129. login = instance.find_elements( { :css => '.user-menu .user a' } )[0].attribute('title')
  1130. if login != action[:username]
  1131. assert( false, "(#{test[:name]}) login failed" )
  1132. return
  1133. end
  1134. assert( true, "(#{test[:name]}) login success" )
  1135. return
  1136. elsif action[:execute] == 'logout'
  1137. instance.find_elements( { :css => 'a[href="#current_user"]' } )[0].click
  1138. sleep 0.1
  1139. instance.find_elements( { :css => 'a[href="#logout"]' } )[0].click
  1140. (1..6).each {|loop|
  1141. login = instance.find_elements( { :css => '#login' } )[0]
  1142. if login
  1143. assert( true, "(#{test[:name]}) logout" )
  1144. return
  1145. end
  1146. }
  1147. assert( false, "(#{test[:name]}) no login box found!" )
  1148. return
  1149. elsif action[:execute] == 'watch_for'
  1150. timeout = 16
  1151. if action[:timeout]
  1152. timeout = action[:timeout]
  1153. end
  1154. loops = (timeout).to_i
  1155. text = ''
  1156. (1..loops).each { |loop|
  1157. element = instance.find_elements( { :css => action[:area] } )[0]
  1158. if element #&& element.displayed?
  1159. begin
  1160. text = element.text
  1161. if text =~ /#{action[:value]}/i
  1162. assert( true, "(#{test[:name]}) '#{action[:value]}' found in '#{text}'" )
  1163. return
  1164. end
  1165. rescue
  1166. # just try again
  1167. end
  1168. end
  1169. sleep 1
  1170. }
  1171. assert( false, "(#{test[:name]}) '#{action[:value]}' found in '#{text}'" )
  1172. return
  1173. elsif action[:execute] == 'watch_for_disappear'
  1174. timeout = 16
  1175. if action[:timeout]
  1176. timeout = action[:timeout]
  1177. end
  1178. loops = (timeout / 2).to_i
  1179. text = ''
  1180. (1..loops).each { |loop|
  1181. element = instance.find_elements( { :css => action[:area] } )[0]
  1182. if !element #|| element.displayed?
  1183. assert( true, "(#{test[:name]}) not found" )
  1184. sleep 0.2
  1185. return
  1186. end
  1187. sleep 2
  1188. }
  1189. assert( false, "(#{test[:name]} / #{test[:area]}) still exsists" )
  1190. return
  1191. # create user
  1192. elsif action[:execute] == 'create_user'
  1193. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  1194. instance.find_elements( { :css => 'a[href="#manage/users"]' } )[0].click
  1195. sleep 2
  1196. instance.find_elements( { :css => 'a[data-type="new"]' } )[0].click
  1197. sleep 2
  1198. #element = instance.find_element( { :css => '.modal input[name=login]' } )
  1199. #element.clear
  1200. #element.send_keys( action[:login] )
  1201. element = instance.find_elements( { :css => '.modal input[name=firstname]' } )[0]
  1202. element.clear
  1203. element.send_keys( action[:firstname] )
  1204. element = instance.find_elements( { :css => '.modal input[name=lastname]' } )[0]
  1205. element.clear
  1206. element.send_keys( action[:lastname] )
  1207. element = instance.find_elements( { :css => '.modal input[name=email]' } )[0]
  1208. element.clear
  1209. element.send_keys( action[:email] )
  1210. element = instance.find_elements( { :css => '.modal input[name=password]' } )[0]
  1211. element.clear
  1212. element.send_keys( action[:password] )
  1213. element = instance.find_elements( { :css => '.modal input[name=password_confirm]' } )[0]
  1214. element.clear
  1215. element.send_keys( action[:password] )
  1216. instance.find_elements( { :css => '.modal input[name="role_ids"][value="3"]' } )[0].click
  1217. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1218. (1..14).each {|loop|
  1219. element = instance.find_elements( { :css => 'body' } )[0]
  1220. text = element.text
  1221. if text =~ /#{Regexp.quote(action[:lastname])}/
  1222. assert( true, "(#{test[:name]}) user created" )
  1223. return
  1224. end
  1225. sleep 0.5
  1226. }
  1227. assert( true, "(#{test[:name]}) user creation failed" )
  1228. return
  1229. # create signature
  1230. elsif action[:execute] == 'create_signature'
  1231. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  1232. instance.find_elements( { :css => 'a[href="#channels/email"]' } )[0].click
  1233. instance.find_elements( { :css => 'a[href="#c-signature"]' } )[0].click
  1234. sleep 8
  1235. instance.find_elements( { :css => '#content #c-signature a[data-type="new"]' } )[0].click
  1236. sleep 2
  1237. element = instance.find_elements( { :css => '.modal input[name=name]' } )[0]
  1238. element.clear
  1239. element.send_keys( action[:name] )
  1240. element = instance.find_elements( { :css => '.modal textarea[name=body]' } )[0]
  1241. element.clear
  1242. element.send_keys( action[:body] )
  1243. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1244. (1..12).each {|loop|
  1245. element = instance.find_elements( { :css => 'body' } )[0]
  1246. text = element.text
  1247. if text =~ /#{Regexp.quote(action[:name])}/
  1248. assert( true, "(#{test[:name]}) signature created" )
  1249. return
  1250. end
  1251. sleep 1
  1252. }
  1253. assert( true, "(#{test[:name]}) signature creation failed" )
  1254. return
  1255. # create group
  1256. elsif action[:execute] == 'create_group'
  1257. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  1258. instance.find_elements( { :css => 'a[href="#manage/groups"]' } )[0].click
  1259. sleep 2
  1260. instance.find_elements( { :css => 'a[data-type="new"]' } )[0].click
  1261. sleep 2
  1262. element = instance.find_elements( { :css => '.modal input[name=name]' } )[0]
  1263. element.clear
  1264. element.send_keys( action[:name] )
  1265. element = instance.find_elements( { :css => '.modal select[name="email_address_id"]' } )[0]
  1266. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1267. dropdown.select_by( :index, 1 )
  1268. #dropdown.select_by( :text, action[:group])
  1269. if action[:signature]
  1270. element = instance.find_elements( { :css => '.modal select[name="signature_id"]' } )[0]
  1271. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1272. dropdown.select_by( :text, action[:signature])
  1273. end
  1274. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1275. (1..12).each {|loop|
  1276. element = instance.find_elements( { :css => 'body' } )[0]
  1277. text = element.text
  1278. if text =~ /#{Regexp.quote(action[:name])}/
  1279. assert( true, "(#{test[:name]}) group created" )
  1280. # add member
  1281. if action[:member]
  1282. action[:member].each {|login|
  1283. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  1284. instance.find_elements( { :css => 'a[href="#manage/users"]' } )[0].click
  1285. sleep 2
  1286. element = instance.find_elements( { :css => '#content [name="search"]' } )[0]
  1287. element.clear
  1288. element.send_keys( login )
  1289. sleep 2
  1290. #instance.find_elements( { :css => '#content table [data-id]' } )[0].click
  1291. instance.execute_script( '$("#content table [data-id] td").first().click()' )
  1292. sleep 2
  1293. #instance.find_elements( { :css => 'label:contains(" ' + action[:name] + '")' } )[0].click
  1294. instance.execute_script( '$(\'label:contains(" ' + action[:name] + '")\').first().click()' )
  1295. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1296. }
  1297. end
  1298. return
  1299. end
  1300. sleep 1
  1301. }
  1302. assert( true, "(#{test[:name]}) group creation failed" )
  1303. return
  1304. elsif action[:execute] == 'verify_task_attributes'
  1305. if action[:title]
  1306. text = instance.find_elements( { :css => '.tasks .active' } )[0].text.strip
  1307. assert_equal( action[:title], text )
  1308. end
  1309. return
  1310. elsif action[:execute] == 'verify_ticket_attributes'
  1311. if action[:title]
  1312. text = instance.find_elements( { :css => '.content.active .page-header .ticket-title-update' } )[0].text.strip
  1313. assert_equal( action[:title], text )
  1314. end
  1315. if action[:body]
  1316. text = instance.find_elements( { :css => '.content.active [data-name="body"]' } )[0].text.strip
  1317. assert_equal( action[:body], text )
  1318. end
  1319. return
  1320. elsif action[:execute] == 'set_ticket_attributes'
  1321. if action[:title]
  1322. #element = instance.find_elements( { :css => '.content.active .page-header .ticket-title-update' } )[0]
  1323. #element.clear
  1324. #sleep 0.5
  1325. #element = instance.find_elements( { :css => '.content.active .page-header .ticket-title-update' } )[0]
  1326. #element.send_keys( action[:title] )
  1327. #sleep 0.5
  1328. #element.send_keys( :tab )
  1329. instance.execute_script( '$(".content.active .page-header .ticket-title-update").focus()' )
  1330. instance.execute_script( '$(".content.active .page-header .ticket-title-update").text("' + action[:title] + '")' )
  1331. instance.execute_script( '$(".content.active .page-header .ticket-title-update").blur()' )
  1332. instance.execute_script( '$(".content.active .page-header .ticket-title-update").trigger("blur")' )
  1333. # {
  1334. # :where => :instance2,
  1335. # :execute => 'sendkey',
  1336. # :css => '.content.active .page-header .ticket-title-update',
  1337. # :value => 'TTT',
  1338. # },
  1339. # {
  1340. # :where => :instance2,
  1341. # :execute => 'sendkey',
  1342. # :css => '.content.active .page-header .ticket-title-update',
  1343. # :value => :tab,
  1344. # },
  1345. end
  1346. if action[:body]
  1347. #instance.execute_script( '$(".content.active div[data-name=body]").focus()' )
  1348. sleep 0.5
  1349. element = instance.find_elements( { :css => '.content.active div[data-name=body]' } )[0]
  1350. element.clear
  1351. element.send_keys( action[:body] )
  1352. end
  1353. return
  1354. # create ticket
  1355. elsif action[:execute] == 'create_ticket'
  1356. instance.find_elements( { :css => 'a[href="#new"]' } )[0].click
  1357. instance.find_elements( { :css => 'a[href="#ticket/create"]' } )[0].click
  1358. element = instance.find_elements( { :css => '.active .newTicket' } )[0]
  1359. if !element
  1360. assert( false, "(#{test[:name]}) no ticket create screen found!" )
  1361. return
  1362. end
  1363. sleep 2
  1364. # check count of agents, should be only 1 / - selection on init screen
  1365. count = instance.find_elements( { :css => '.active .newTicket select[name="owner_id"] option' } ).count
  1366. assert_equal( 1, count, 'check if owner selection is empty per default' )
  1367. if action[:group]
  1368. element = instance.find_elements( { :css => '.active .newTicket select[name="group_id"]' } )[0]
  1369. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1370. dropdown.select_by( :text, action[:group])
  1371. sleep 0.2
  1372. end
  1373. if action[:subject]
  1374. element = instance.find_elements( { :css => '.active .newTicket input[name="title"]' } )[0]
  1375. element.clear
  1376. element.send_keys( action[:subject] )
  1377. sleep 0.2
  1378. end
  1379. if action[:body]
  1380. #instance.execute_script( '$(".active .newTicket div[data-name=body]").focus()' )
  1381. sleep 0.5
  1382. element = instance.find_elements( { :css => '.active .newTicket div[data-name=body]' } )[0]
  1383. element.clear
  1384. element.send_keys( action[:body] )
  1385. end
  1386. if action[:customer] == nil
  1387. element = instance.find_elements( { :css => '.active .newTicket input[name="customer_id_completion"]' } )[0]
  1388. element.click
  1389. element.clear
  1390. element.send_keys( 'nico*' )
  1391. sleep 4
  1392. element.send_keys( :arrow_down )
  1393. sleep 0.1
  1394. instance.find_elements( { :css => '.active .newTicket .recipientList-entry.js-user.is-active' } )[0].click
  1395. sleep 0.3
  1396. end
  1397. if action[:do_not_submit]
  1398. assert( true, "(#{test[:name]}) ticket created without submit" )
  1399. return
  1400. end
  1401. sleep 0.8
  1402. #instance.execute_script( '$(".content.active .newTicket form").submit();' )
  1403. instance.find_elements( { :css => '.active .newTicket button.submit' } )[0].click
  1404. sleep 1
  1405. (1..16).each {|loop|
  1406. if instance.current_url =~ /#{Regexp.quote('#ticket/zoom/')}/
  1407. assert( true, "(#{test[:name]}) ticket created" )
  1408. sleep 1
  1409. return
  1410. end
  1411. sleep 0.5
  1412. }
  1413. assert( false, "(#{test[:name]}) ticket creation failed, can't get zoom url" )
  1414. return
  1415. # search ticket
  1416. elsif action[:execute] == 'search_ticket'
  1417. element = instance.find_elements( { :css => '#global-search' } )[0]
  1418. element.click
  1419. element.clear
  1420. action[:number].gsub! '###stack###', @stack
  1421. element.send_keys( action[:number] )
  1422. sleep 3
  1423. instance.find_elements( { :css => '.search .empty-search' } )[0].click
  1424. sleep 0.5
  1425. text = instance.find_elements( { :css => '#global-search' } )[0].attribute('value')
  1426. if !text
  1427. assert( false, "(#{test[:name]}) #global-search is not empty!" )
  1428. return
  1429. end
  1430. element = instance.find_elements( { :css => '#global-search' } )[0]
  1431. element.click
  1432. element.clear
  1433. action[:number].gsub! '###stack###', @stack
  1434. element.send_keys( action[:number] )
  1435. sleep 2
  1436. element = instance.find_element( { :partial_link_text => action[:number] } ).click
  1437. number = instance.find_elements( { :css => '.active .page-header .ticket-number' } )[0].text
  1438. if number !~ /#{action[:number]}/
  1439. assert( false, "(#{test[:name]}) unable to search/find ticket #{action[:number]}!" )
  1440. return
  1441. end
  1442. assert( true, "(#{test[:name]}) ticket #{action[:number]} found" )
  1443. return
  1444. # close all tasks
  1445. elsif action[:execute] == 'close_all_tasks'
  1446. for i in 1..100
  1447. sleep 1
  1448. if instance.find_elements( { :css => '.navigation .tasks .task:first-child' } )[0]
  1449. instance.mouse.move_to( instance.find_elements( { :css => '.navigation .tasks .task:first-child' } )[0] )
  1450. sleep 0.2
  1451. click_element = instance.find_elements( { :css => '.navigation .tasks .task:first-child .js-close' } )[0]
  1452. if click_element
  1453. sleep 0.1
  1454. click_element.click
  1455. # accept task close warning
  1456. if action[:discard_changes]
  1457. sleep 1
  1458. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1459. end
  1460. end
  1461. else
  1462. break
  1463. end
  1464. end
  1465. assert( true, "(#{test[:name]}) all tasks closed" )
  1466. return
  1467. elsif action[:execute] == 'navigate'
  1468. instance.navigate.to( action[:to] )
  1469. return
  1470. elsif action[:execute] == 'reload'
  1471. instance.navigate.refresh
  1472. return
  1473. elsif action[:execute] == 'js'
  1474. result = instance.execute_script( action[:value] )
  1475. elsif action[:execute] == 'sendkey'
  1476. if action[:value].class == Array
  1477. action[:value].each {|key|
  1478. instance.action.send_keys(key).perform
  1479. }
  1480. else
  1481. instance.action.send_keys(action[:value]).perform
  1482. #instance.action.send_keys(:enter).perform
  1483. end
  1484. # element.send_keys( action[:value] )
  1485. elsif action[:link]
  1486. if action[:link].match '###stack###'
  1487. action[:link].gsub! '###stack###', @stack
  1488. end
  1489. element = instance.find_element( { :partial_link_text => action[:link] } )
  1490. else
  1491. assert( false, "(#{test[:name]}) unknow selector for '#{action[:element]}'" )
  1492. end
  1493. if action[:execute] == 'setCheck'
  1494. checked = element.attribute('checked')
  1495. if !checked
  1496. element.click
  1497. end
  1498. elsif action[:execute] == 'setUncheck'
  1499. checked = element.attribute('checked')
  1500. if checked
  1501. element.click
  1502. end
  1503. elsif action[:execute] == 'set'
  1504. element.clear
  1505. if action[:value] == '###stack###'
  1506. element.send_keys( @stack )
  1507. else
  1508. if !action[:slow]
  1509. element.send_keys( action[:value] )
  1510. else
  1511. element.send_keys( '' )
  1512. keys = action[:value].to_s.split('')
  1513. keys.each {|key|
  1514. instance.action.send_keys(key).perform
  1515. }
  1516. end
  1517. sleep 0.3
  1518. end
  1519. elsif action[:execute] == 'select'
  1520. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1521. dropdown.select_by(:text, action[:value])
  1522. elsif action[:execute] == 'click'
  1523. if element.class == Array
  1524. element.each {|item|
  1525. item.click
  1526. }
  1527. else
  1528. element.click
  1529. end
  1530. elsif action[:execute] == 'accept'
  1531. element.accept
  1532. elsif action[:execute] == 'dismiss'
  1533. element.dismiss
  1534. elsif action[:execute] == 'send_key'
  1535. element.send_keys action[:value]
  1536. elsif action[:execute] == 'match'
  1537. if action[:css] =~ /select/
  1538. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1539. success = false
  1540. if dropdown.selected_options
  1541. dropdown.selected_options.each {|option|
  1542. if option.text == action[:value]
  1543. success = true
  1544. end
  1545. }
  1546. end
  1547. if action[:match_result]
  1548. if success
  1549. assert( true, "(#{test[:name]}) matching '#{action[:value]}' in select list" )
  1550. else
  1551. assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in select list" )
  1552. end
  1553. else
  1554. if success
  1555. assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in select list" )
  1556. else
  1557. assert( true, "(#{test[:name]}) matching '#{action[:value]}' in select list" )
  1558. end
  1559. end
  1560. else
  1561. if action[:attribute]
  1562. text = element.attribute( action[:attribute] )
  1563. elsif action[:css] =~ /(input|textarea)/i
  1564. text = element.attribute('value')
  1565. else
  1566. text = element.text
  1567. end
  1568. if action[:value] == '###stack###'
  1569. action[:value] = @stack
  1570. end
  1571. match = false
  1572. if action[:no_quote]
  1573. #puts "aaaa #{text}/#{action[:value]}"
  1574. if text =~ /#{action[:value]}/i
  1575. if $1
  1576. @stack = $1
  1577. end
  1578. match = $1 || true
  1579. end
  1580. else
  1581. if text =~ /#{Regexp.quote(action[:value])}/i
  1582. match = true
  1583. end
  1584. end
  1585. if match
  1586. if action[:match_result]
  1587. assert( true, "(#{test[:name]}) matching '#{action[:value]}' in content '#{text}'" )
  1588. else
  1589. assert( false, "(#{test[:name]}) matching '#{action[:value]}' in content '#{text}' but should not!" )
  1590. end
  1591. else
  1592. if !action[:match_result]
  1593. assert( true, "(#{test[:name]}) not matching '#{action[:value]}' in content '#{text}'" )
  1594. else
  1595. assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in content '#{text}' but should not!" )
  1596. end
  1597. end
  1598. end
  1599. elsif action[:execute] == 'check'
  1600. elsif action[:execute] == 'js'
  1601. elsif action[:execute] == 'sendkey'
  1602. else
  1603. assert( false, "(#{test[:name]}) unknow action '#{action[:execute]}'" )
  1604. end
  1605. end
  1606. end