browser_test_helper.rb 49 KB

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