browser_test_helper.rb 47 KB

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