browser_test_helper.rb 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160
  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. # currently console log not working for firefox
  18. # https://github.com/SeleniumHQ/selenium/issues/1161
  19. #browser_profile['loggingPref'] = { browser: :all }
  20. elsif browser == 'chrome'
  21. # profile are only working on remote selenium
  22. if ENV['REMOTE_URL']
  23. browser_profile = Selenium::WebDriver::Chrome::Profile.new
  24. browser_profile['intl.accept_languages'] = 'en'
  25. browser_profile['loggingPref'] = { browser: :all }
  26. end
  27. end
  28. browser_profile
  29. end
  30. def browser_support_cookies
  31. if browser =~ /(internet_explorer|ie)/i
  32. return false
  33. end
  34. true
  35. end
  36. def browser_url
  37. ENV['BROWSER_URL'] || 'http://localhost:3000'
  38. end
  39. def browser_instance
  40. if !@browsers
  41. @browsers = {}
  42. end
  43. if !ENV['REMOTE_URL'] || ENV['REMOTE_URL'].empty?
  44. local_browser = Selenium::WebDriver.for(browser.to_sym, profile: profile)
  45. @browsers[local_browser.hash] = local_browser
  46. browser_instance_preferences(local_browser)
  47. return local_browser
  48. end
  49. # avoid "Cannot read property 'get_Current' of undefined" issues
  50. (1..5).each {|count|
  51. begin
  52. local_browser = browser_instance_remote
  53. break
  54. rescue
  55. wait_until_ready = rand(9) + 5
  56. sleep wait_until_ready
  57. log('browser_instance', { rescure: true, count: count, sleep: wait_until_ready })
  58. end
  59. }
  60. local_browser
  61. end
  62. def browser_instance_remote
  63. caps = Selenium::WebDriver::Remote::Capabilities.send(browser)
  64. if ENV['BROWSER_OS']
  65. caps.platform = ENV['BROWSER_OS']
  66. end
  67. if ENV['BROWSER_VERSION']
  68. caps.version = ENV['BROWSER_VERSION']
  69. end
  70. local_browser = Selenium::WebDriver.for(
  71. :remote,
  72. url: ENV['REMOTE_URL'],
  73. desired_capabilities: caps,
  74. )
  75. @browsers[local_browser.hash] = local_browser
  76. browser_instance_preferences(local_browser)
  77. # upload files from remote dir
  78. local_browser.file_detector = lambda do |args|
  79. str = args.first.to_s
  80. str if File.file?(str)
  81. end
  82. local_browser
  83. end
  84. def browser_instance_close(local_browser)
  85. return if !@browsers[local_browser.hash]
  86. @browsers.delete(local_browser.hash)
  87. local_browser.quit
  88. end
  89. def browser_instance_preferences(local_browser)
  90. browser_width = ENV['BROWSER_WIDTH'] || 1024
  91. browser_height = ENV['BROWSER_HEIGHT'] || 800
  92. local_browser.manage.window.resize_to(browser_width, browser_height)
  93. if ENV['REMOTE_URL'] !~ /saucelabs|(grid|ci)\.(zammad\.org|znuny\.com)/i
  94. if @browsers.count == 1
  95. local_browser.manage.window.move_to(0, 0)
  96. else
  97. local_browser.manage.window.move_to(browser_width, 0)
  98. end
  99. end
  100. local_browser.manage.timeouts.implicit_wait = 3 # seconds
  101. end
  102. def teardown
  103. return if !@browsers
  104. @browsers.each { |_hash, local_browser|
  105. screenshot(browser: local_browser, comment: 'teardown')
  106. browser_instance_close(local_browser)
  107. }
  108. end
  109. def screenshot(params)
  110. instance = params[:browser] || @browser
  111. comment = params[:comment] || ''
  112. filename = "tmp/#{Time.zone.now.strftime('screenshot_%Y_%m_%d__%H_%M_%S')}_#{comment}_#{instance.hash}.png"
  113. log('screenshot', { filename: filename })
  114. instance.save_screenshot(filename)
  115. end
  116. =begin
  117. username = login(
  118. browser: browser1,
  119. username: 'someuser',
  120. password: 'somepassword',
  121. url: 'some url', # optional
  122. remember_me: true, # optional
  123. auto_wizard: false, # optional, in case of auto wizard, skip login
  124. success: false, #optional
  125. )
  126. =end
  127. def login(params)
  128. switch_window_focus(params)
  129. log('login', params)
  130. instance = params[:browser] || @browser
  131. if params[:url]
  132. instance.get(params[:url])
  133. end
  134. # submit logs anyway
  135. instance.execute_script('App.Track.force()')
  136. element = instance.find_elements(css: '#login input[name="username"]')[0]
  137. if !element
  138. if params[:auto_wizard]
  139. watch_for(
  140. browser: instance,
  141. css: 'body',
  142. value: 'auto wizard is enabled',
  143. timeout: 10,
  144. )
  145. location(url: "#{browser_url}/#getting_started/auto_wizard")
  146. sleep 10
  147. login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
  148. if login != params[:username]
  149. screenshot(browser: instance, comment: 'auto wizard login failed')
  150. raise 'auto wizard login failed'
  151. end
  152. assert(true, 'auto wizard login ok')
  153. clues_close(
  154. browser: instance,
  155. optional: true,
  156. )
  157. return
  158. end
  159. screenshot(browser: instance, comment: 'login_failed')
  160. raise 'No login box found'
  161. end
  162. screenshot(browser: instance, comment: 'login')
  163. element.clear
  164. element.send_keys(params[:username])
  165. element = instance.find_elements(css: '#login input[name="password"]')[0]
  166. element.clear
  167. element.send_keys(params[:password])
  168. if params[:remember_me]
  169. instance.find_elements(css: '#login .checkbox-replacement')[0].click
  170. end
  171. instance.find_elements(css: '#login button')[0].click
  172. sleep 4
  173. login_failed = false
  174. if !instance.find_elements(css: '.user-menu .user a')[0]
  175. login_failed = true
  176. else
  177. login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
  178. if login != params[:username]
  179. login_failed = true
  180. end
  181. end
  182. if login_failed
  183. if params[:success] == false
  184. assert(true, 'login not successfull, like wanted')
  185. return true
  186. end
  187. screenshot(browser: instance, comment: 'login_failed')
  188. raise 'login failed'
  189. end
  190. if params[:success] == false
  191. raise 'login successfull but should not'
  192. end
  193. clues_close(
  194. browser: instance,
  195. optional: true,
  196. )
  197. screenshot(browser: instance, comment: 'login_ok')
  198. assert(true, 'login ok')
  199. login
  200. end
  201. =begin
  202. logout(
  203. browser: browser1
  204. )
  205. =end
  206. def logout(params = {})
  207. switch_window_focus(params)
  208. log('logout', params)
  209. instance = params[:browser] || @browser
  210. click(
  211. browser: instance,
  212. css: 'a[href="#current_user"]',
  213. mute_log: true,
  214. )
  215. click(
  216. browser: instance,
  217. css: 'a[href="#logout"]',
  218. mute_log: true,
  219. )
  220. (1..6).each {
  221. sleep 1
  222. login = instance.find_elements(css: '#login')[0]
  223. next if !login
  224. screenshot(browser: instance, comment: 'logout_ok')
  225. assert(true, 'logout ok')
  226. return
  227. }
  228. screenshot(browser: instance, comment: 'logout_failed')
  229. raise 'no login box found, seems logout was not successfully!'
  230. end
  231. =begin
  232. clues_close(
  233. browser: browser1,
  234. optional: false,
  235. )
  236. =end
  237. def clues_close(params = {})
  238. switch_window_focus(params)
  239. log('clues_close', params)
  240. instance = params[:browser] || @browser
  241. clues = instance.find_elements(css: '.js-modal--clue .js-close')[0]
  242. if !params[:optional] && !clues
  243. screenshot(browser: instance, comment: 'no_clues')
  244. raise 'Unable to closes clues, no clues found!'
  245. end
  246. return if !clues
  247. instance.execute_script("$('.js-modal--clue .js-close').click()")
  248. assert(true, 'clues closed')
  249. sleep 1
  250. end
  251. =begin
  252. notify_close(
  253. browser: browser1,
  254. optional: false,
  255. )
  256. =end
  257. def notify_close(params = {})
  258. switch_window_focus(params)
  259. log('notify_close', params)
  260. instance = params[:browser] || @browser
  261. notify = instance.find_elements(css: '.noty_inline_layout_container.i-am-new')[0]
  262. if !params[:optional] && !notify
  263. screenshot(browser: instance, comment: 'no_notify')
  264. raise 'Unable to closes notify, no notify found!'
  265. end
  266. return if !notify
  267. notify.click
  268. assert(true, 'notify closed')
  269. sleep 1
  270. end
  271. =begin
  272. location(
  273. browser: browser1,
  274. url: 'http://someurl',
  275. )
  276. =end
  277. def location(params)
  278. switch_window_focus(params)
  279. log('location', params)
  280. instance = params[:browser] || @browser
  281. instance.get(params[:url])
  282. # check if reload was successfull
  283. if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
  284. instance.navigate.refresh
  285. end
  286. screenshot(browser: instance, comment: 'location')
  287. end
  288. =begin
  289. location_check(
  290. browser: browser1,
  291. url: 'http://someurl',
  292. )
  293. =end
  294. def location_check(params)
  295. switch_window_focus(params)
  296. log('location_check', params)
  297. instance = params[:browser] || @browser
  298. sleep 0.7
  299. current_url = instance.current_url
  300. if current_url !~ /#{Regexp.quote(params[:url])}/
  301. screenshot(browser: instance, comment: 'location_check_failed')
  302. raise "url #{current_url} is not matching #{params[:url]}"
  303. end
  304. assert(true, "url #{current_url} is matching #{params[:url]}")
  305. end
  306. =begin
  307. reload(
  308. browser: browser1,
  309. )
  310. =end
  311. def reload(params = {})
  312. switch_window_focus(params)
  313. log('reload', params)
  314. instance = params[:browser] || @browser
  315. screenshot(browser: instance, comment: 'reload_before')
  316. instance.navigate.refresh
  317. # check if reload was successfull
  318. if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
  319. instance.navigate.refresh
  320. end
  321. screenshot(browser: instance, comment: 'reload_after')
  322. end
  323. =begin
  324. click(
  325. browser: browser1,
  326. css: '.some_class',
  327. fast: false, # do not wait
  328. wait: 1, # wait 1 sec.
  329. )
  330. click(
  331. browser: browser1,
  332. text: '.partial_link_text',
  333. fast: false, # do not wait
  334. wait: 1, # wait 1 sec.
  335. )
  336. =end
  337. def click(params)
  338. switch_window_focus(params)
  339. log('click', params)
  340. instance = params[:browser] || @browser
  341. if params[:css]
  342. begin
  343. element = instance.find_elements(css: params[:css])[0]
  344. #if element
  345. # instance.mouse.move_to(element)
  346. #end
  347. element.click
  348. rescue => e
  349. sleep 0.5
  350. # just try again
  351. log('click', { rescure: true })
  352. element = instance.find_elements(css: params[:css])[0]
  353. #if element
  354. # instance.mouse.move_to(element)
  355. #end
  356. element.click
  357. end
  358. else
  359. sleep 0.5
  360. begin
  361. instance.find_elements(partial_link_text: params[:text])[0].click
  362. rescue => e
  363. sleep 0.5
  364. # just try again
  365. log('click', { rescure: true })
  366. instance.find_elements(partial_link_text: params[:text])[0].click
  367. end
  368. end
  369. sleep 0.2 if !params[:fast]
  370. sleep params[:wait] if params[:wait]
  371. end
  372. =begin
  373. scroll_to(
  374. browser: browser1,
  375. position: 'top', # botton
  376. css: '.some_class',
  377. )
  378. =end
  379. def scroll_to(params)
  380. switch_window_focus(params)
  381. log('scroll_to', params)
  382. instance = params[:browser] || @browser
  383. position = 'true'
  384. if params[:position] == 'botton'
  385. position = 'false'
  386. end
  387. execute(
  388. browser: instance,
  389. js: "\$('#{params[:css]}').get(0).scrollIntoView(#{position})",
  390. mute_log: params[:mute_log]
  391. )
  392. sleep 0.3
  393. end
  394. =begin
  395. modal_ready(
  396. browser: browser1,
  397. )
  398. =end
  399. def modal_ready(params = {})
  400. switch_window_focus(params)
  401. log('modal_ready', params)
  402. instance = params[:browser] || @browser
  403. sleep 3
  404. end
  405. =begin
  406. modal_disappear(
  407. browser: browser1,
  408. )
  409. =end
  410. def modal_disappear(params = {})
  411. switch_window_focus(params)
  412. log('modal_disappear', params)
  413. instance = params[:browser] || @browser
  414. watch_for_disappear(
  415. browser: instance,
  416. css: '.modal',
  417. timeout: 6,
  418. )
  419. end
  420. =begin
  421. execute(
  422. browser: browser1,
  423. js: '.some_class',
  424. )
  425. =end
  426. def execute(params)
  427. switch_window_focus(params)
  428. log('js', params)
  429. instance = params[:browser] || @browser
  430. if params[:js]
  431. return instance.execute_script(params[:js])
  432. end
  433. raise "Invalid execute params #{params.inspect}"
  434. end
  435. =begin
  436. exists(
  437. browser: browser1,
  438. css: '.some_class',
  439. )
  440. =end
  441. def exists(params)
  442. switch_window_focus(params)
  443. log('exists', params)
  444. instance = params[:browser] || @browser
  445. if !instance.find_elements(css: params[:css])[0]
  446. screenshot(browser: instance, comment: 'exists_failed')
  447. raise "#{params[:css]} dosn't exist, but should"
  448. end
  449. true
  450. end
  451. =begin
  452. exists_not(
  453. browser: browser1,
  454. css: '.some_class',
  455. )
  456. =end
  457. def exists_not(params)
  458. switch_window_focus(params)
  459. log('exists_not', params)
  460. instance = params[:browser] || @browser
  461. if instance.find_elements(css: params[:css])[0]
  462. screenshot(browser: instance, comment: 'exists_not_failed')
  463. raise "#{params[:css]} exists but should not"
  464. end
  465. true
  466. end
  467. =begin
  468. set(
  469. browser: browser1,
  470. css: '.some_class',
  471. value: true,
  472. slow: false,
  473. blur: true,
  474. clear: true, # todo | default: true
  475. no_click: true,
  476. )
  477. =end
  478. def set(params)
  479. switch_window_focus(params)
  480. log('set', params)
  481. instance = params[:browser] || @browser
  482. element = instance.find_elements(css: params[:css])[0]
  483. if !params[:no_click]
  484. element.click
  485. end
  486. element.clear
  487. if !params[:slow]
  488. element.send_keys(params[:value])
  489. else
  490. element.send_keys('')
  491. keys = params[:value].to_s.split('')
  492. keys.each {|key|
  493. instance.action.send_keys(key).perform
  494. }
  495. end
  496. if params[:blur]
  497. instance.execute_script("$('#{params[:css]}').blur()")
  498. end
  499. sleep 0.2
  500. end
  501. =begin
  502. select(
  503. browser: browser1,
  504. css: '.some_class',
  505. value: 'Some Value',
  506. deselect_all: false, # default false
  507. )
  508. =end
  509. def select(params)
  510. switch_window_focus(params)
  511. log('select', params)
  512. instance = params[:browser] || @browser
  513. # searchable select
  514. element = instance.find_elements(css: "#{params[:css]}.js-shadow")[0]
  515. if element
  516. element = instance.find_elements(css: "#{params[:css]}.js-shadow + .js-input")[0]
  517. element.click
  518. element.clear
  519. sleep 0.4
  520. element.send_keys(params[:value])
  521. sleep 0.2
  522. element.send_keys(:enter)
  523. sleep 0.2
  524. return
  525. end
  526. # native select
  527. begin
  528. element = instance.find_elements(css: params[:css])[0]
  529. dropdown = Selenium::WebDriver::Support::Select.new(element)
  530. if params[:deselect_all]
  531. dropdown.deselect_all
  532. end
  533. dropdown.select_by(:text, params[:value])
  534. #puts "select - #{params.inspect}"
  535. rescue
  536. sleep 0.4
  537. # just try again
  538. log('select', { rescure: true })
  539. element = instance.find_elements(css: params[:css])[0]
  540. dropdown = Selenium::WebDriver::Support::Select.new(element)
  541. if params[:deselect_all]
  542. dropdown.deselect_all
  543. end
  544. dropdown.select_by(:text, params[:value])
  545. #puts "select2 - #{params.inspect}"
  546. end
  547. sleep 0.4
  548. end
  549. =begin
  550. switch(
  551. browser: browser1,
  552. css: '.some_class',
  553. type: 'on', # 'off'
  554. )
  555. =end
  556. def switch(params)
  557. switch_window_focus(params)
  558. log('switch', params)
  559. instance = params[:browser] || @browser
  560. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  561. checked = element.attribute('checked')
  562. if !checked
  563. if params[:type] == 'on'
  564. instance.find_elements(css: "#{params[:css]} label")[0].click
  565. sleep 2
  566. end
  567. elsif params[:type] == 'off'
  568. instance.find_elements(css: "#{params[:css]} label")[0].click
  569. sleep 2
  570. end
  571. end
  572. =begin
  573. check(
  574. browser: browser1,
  575. css: '.some_class',
  576. )
  577. =end
  578. def check(params)
  579. switch_window_focus(params)
  580. log('check', params)
  581. instance = params[:browser] || @browser
  582. instance.execute_script("if (!$('#{params[:css]}').prop('checked')) { $('#{params[:css]}').click() }")
  583. #element = instance.find_elements(css: params[:css])[0]
  584. #checked = element.attribute('checked')
  585. #element.click if !checked
  586. end
  587. =begin
  588. uncheck(
  589. browser: browser1,
  590. css: '.some_class',
  591. )
  592. =end
  593. def uncheck(params)
  594. switch_window_focus(params)
  595. log('uncheck', params)
  596. instance = params[:browser] || @browser
  597. instance.execute_script("if ($('#{params[:css]}').prop('checked')) { $('#{params[:css]}').click() }")
  598. #element = instance.find_elements(css: params[:css])[0]
  599. #checked = element.attribute('checked')
  600. #element.click if checked
  601. end
  602. =begin
  603. sendkey(
  604. browser: browser1,
  605. value: :enter,
  606. slow: false,
  607. )
  608. =end
  609. def sendkey(params)
  610. switch_window_focus(params)
  611. log('sendkey', params)
  612. instance = params[:browser] || @browser
  613. if params[:value].class == Array
  614. params[:value].each {|key|
  615. instance.action.send_keys(key).perform
  616. }
  617. return
  618. end
  619. instance.action.send_keys(params[:value]).perform
  620. if params[:slow]
  621. sleep 1.5
  622. else
  623. sleep 0.2
  624. end
  625. end
  626. =begin
  627. match(
  628. browser: browser1,
  629. css: '#content .text-1',
  630. value: 'some test for browser and some other for browser',
  631. attribute: 'some_attribute', # match on attribute
  632. should_not_match: true,
  633. no_quote: false, # use regex
  634. )
  635. =end
  636. def match(params, fallback = false)
  637. switch_window_focus(params)
  638. log('match', params)
  639. instance = params[:browser] || @browser
  640. element = instance.find_elements(css: params[:css])[0]
  641. if params[:css] =~ /select/
  642. dropdown = Selenium::WebDriver::Support::Select.new(element)
  643. success = false
  644. if dropdown.selected_options
  645. dropdown.selected_options.each {|option|
  646. if option.text == params[:value]
  647. success = true
  648. end
  649. }
  650. end
  651. if params[:should_not_match]
  652. if success
  653. raise "should not match '#{params[:value]}' in select list, but is matching"
  654. end
  655. elsif !success
  656. raise "not matching '#{params[:value]}' in select list"
  657. end
  658. return true
  659. end
  660. # match on attribute
  661. begin
  662. text = if params[:attribute]
  663. element.attribute(params[:attribute])
  664. elsif params[:css] =~ /(input|textarea)/i
  665. element.attribute('value')
  666. else
  667. element.text
  668. end
  669. rescue => e
  670. # just try again
  671. if !fallback
  672. return match(params, true)
  673. end
  674. raise e.inspect
  675. end
  676. # do cleanups (needed for richtext tests)
  677. if params[:cleanup]
  678. text.gsub!(/\s+$/m, '')
  679. params[:value].gsub!(/\s+$/m, '')
  680. end
  681. match = false
  682. if params[:no_quote]
  683. #puts "aaaa #{text}/#{params[:value]}"
  684. if text =~ /#{params[:value]}/i
  685. match = $1 || true
  686. end
  687. elsif text =~ /#{Regexp.quote(params[:value])}/i
  688. match = true
  689. end
  690. if match
  691. if params[:should_not_match]
  692. raise "matching '#{params[:value]}' in content '#{text}' but should not!"
  693. end
  694. elsif !params[:should_not_match]
  695. raise "not matching '#{params[:value]}' in content '#{text}' but should!"
  696. end
  697. sleep 0.2
  698. match
  699. end
  700. =begin
  701. match_not(
  702. browser: browser1,
  703. css: '#content .text-1',
  704. value: 'some test for browser and some other for browser',
  705. attribute: 'some_attribute', # match on attribute
  706. should_not_match: true,
  707. no_quote: false, # use regex
  708. )
  709. =end
  710. def match_not(params)
  711. switch_window_focus(params)
  712. log('match_not', params)
  713. params[:should_not_match] = true
  714. match(params)
  715. end
  716. =begin
  717. task_type(
  718. browser: browser1,
  719. type: 'stayOnTab',
  720. )
  721. =end
  722. def task_type(params)
  723. switch_window_focus(params)
  724. log('task_type', params)
  725. instance = params[:browser] || @browser
  726. if params[:type]
  727. instance.find_elements(css: '.content.active .js-secondaryActionButtonLabel')[0].click
  728. instance.find_elements(css: ".content.active .js-secondaryActionLabel[data-type=#{params[:type]}]")[0].click
  729. return
  730. end
  731. raise "Unknown params for task_type: #{params.inspect}"
  732. end
  733. =begin
  734. cookie(
  735. browser: browser1,
  736. name: '^_zammad.+?',
  737. value: '.+?',
  738. expires: nil,
  739. )
  740. cookie(
  741. browser: browser1,
  742. name: '^_zammad.+?',
  743. should_not_exist: true,
  744. )
  745. =end
  746. def cookie(params)
  747. switch_window_focus(params)
  748. log('cookie', params)
  749. instance = params[:browser] || @browser
  750. if !browser_support_cookies
  751. assert(true, "'#{params[:value]}' ups browser is not supporting reading cookies, go ahead")
  752. return true
  753. end
  754. cookies = instance.manage.all_cookies
  755. cookies.each {|cookie|
  756. #puts "CCC #{cookie.inspect}"
  757. # :name=>"_zammad_session_c25832f4de2", :value=>"adc31cd21615cb0a7ab269184ec8b76f", :path=>"/", :domain=>"localhost", :expires=>nil, :secure=>false}
  758. next if cookie[:name] !~ /#{params[:name]}/i
  759. if params.key?(:value) && cookie[:value].to_s =~ /#{params[:value]}/i
  760. assert(true, "matching value '#{params[:value]}' in cookie '#{cookie}'")
  761. else
  762. raise "not matching value '#{params[:value]}' in cookie '#{cookie}'"
  763. end
  764. if params.key?(:expires) && cookie[:expires].to_s =~ /#{params[:expires]}/i
  765. assert(true, "matching expires '#{params[:expires].inspect}' in cookie '#{cookie}'")
  766. else
  767. raise "not matching expires '#{params[:expires]}' in cookie '#{cookie}'"
  768. end
  769. return if !params[:should_not_exist]
  770. raise "cookie with name '#{params[:name]}' should not exist, but exists '#{cookies}'"
  771. }
  772. if params[:should_not_exist]
  773. assert(true, "cookie with name '#{params[:name]}' is not existing")
  774. return
  775. end
  776. raise "not matching name '#{params[:name]}' in cookie '#{cookies}'"
  777. end
  778. =begin
  779. verify_title(
  780. browser: browser1,
  781. value: 'some title',
  782. )
  783. =end
  784. def verify_title(params = {})
  785. switch_window_focus(params)
  786. log('verify_title', params)
  787. instance = params[:browser] || @browser
  788. title = instance.title
  789. if title =~ /#{params[:value]}/i
  790. assert(true, "matching '#{params[:value]}' in title '#{title}'")
  791. else
  792. raise "not matching '#{params[:value]}' in title '#{title}'"
  793. end
  794. end
  795. =begin
  796. verify_task(
  797. browser: browser1,
  798. data: {
  799. title: 'some title',
  800. modified: true, # optional
  801. }
  802. )
  803. =end
  804. def verify_task(params = {}, fallback = false)
  805. switch_window_focus(params)
  806. log('verify_task', params)
  807. instance = params[:browser] || @browser
  808. data = params[:data]
  809. sleep 1
  810. begin
  811. # verify title
  812. if data[:title]
  813. title = instance.find_elements(css: '.tasks .is-active')[0].text.strip
  814. if title =~ /#{data[:title]}/i
  815. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  816. else
  817. raise "not matching '#{data[:title]}' in title '#{title}'"
  818. end
  819. end
  820. puts "tv #{params.inspect}"
  821. # verify modified
  822. if data.key?(:modified)
  823. exists = instance.find_elements(css: '.tasks .is-active')[0]
  824. is_modified = instance.find_elements(css: '.tasks .is-modified')[0]
  825. puts "m #{data[:modified].inspect}"
  826. if exists
  827. puts ' exists'
  828. end
  829. if is_modified
  830. puts ' is_modified'
  831. end
  832. if data[:modified] == true
  833. if is_modified
  834. assert(true, "task '#{data[:title]}' is modifed")
  835. elsif !exists
  836. raise "task '#{data[:title]}' not exists, should not modified"
  837. else
  838. raise "task '#{data[:title]}' is not modifed"
  839. end
  840. elsif !is_modified
  841. assert(true, "task '#{data[:title]}' is modifed")
  842. elsif !exists
  843. raise "task '#{data[:title]}' not exists, should be not modified"
  844. else
  845. raise "task '#{data[:title]}' is modifed, but should not"
  846. end
  847. end
  848. rescue => e
  849. # just try again
  850. if !fallback
  851. verify_task(params, true)
  852. end
  853. raise 'ERROR: ' + e.inspect
  854. end
  855. true
  856. end
  857. =begin
  858. open_task(
  859. browser: browser1,
  860. data: {
  861. title: 'some title',
  862. }
  863. )
  864. =end
  865. def open_task(params = {})
  866. switch_window_focus(params)
  867. log('open_task', params)
  868. instance = params[:browser] || @browser
  869. data = params[:data]
  870. element = instance.find_elements(partial_link_text: data[:title])[0]
  871. if !element
  872. screenshot(browser: instance, comment: 'open_task_failed')
  873. raise "no task with title '#{data[:title]}' found"
  874. end
  875. element.click
  876. true
  877. end
  878. =begin
  879. close_task(
  880. browser: browser1,
  881. data: {
  882. title: 'some title',
  883. },
  884. discard_changes: true,
  885. )
  886. =end
  887. def close_task(params = {})
  888. switch_window_focus(params)
  889. log('close_task', params)
  890. instance = params[:browser] || @browser
  891. data = params[:data]
  892. element = instance.find_elements(partial_link_text: data[:title])[0]
  893. if !element
  894. screenshot(browser: instance, comment: 'close_task_failed')
  895. raise "no task with title '#{data[:title]}' found"
  896. end
  897. instance.mouse.move_to(element)
  898. sleep 0.1
  899. instance.execute_script("$('.navigation .tasks .task:contains(\"#{data[:title]}\") .js-close').click()")
  900. # accept task close warning
  901. if params[:discard_changes]
  902. modal_ready(browser: instance)
  903. instance.find_elements(css: '.modal button.js-submit')[0].click
  904. modal_disappear(browser: instance)
  905. end
  906. true
  907. end
  908. =begin
  909. file_upload(
  910. browser: browser1,
  911. css: '.active .attachmentPlaceholder-inputHolder input'
  912. files: ['path/in/home/some_file.ext'], # 'test/fixtures/test1.pdf'
  913. )
  914. =end
  915. def file_upload(params = {})
  916. switch_window_focus(params)
  917. log('file_upload', params)
  918. instance = params[:browser] || @browser
  919. params[:files].each {|file|
  920. instance.find_elements(css: params[:css])[0].send_keys "#{Rails.root}/#{file}"
  921. }
  922. sleep 2 * params[:files].count
  923. end
  924. =begin
  925. watch_for(
  926. browser: browser1,
  927. css: '#content .text-1',
  928. value: 'some text',
  929. attribute: 'some_attribute' # optional
  930. timeout: 16, # in sec, default 16
  931. )
  932. =end
  933. def watch_for(params = {})
  934. switch_window_focus(params)
  935. log('watch_for', params)
  936. instance = params[:browser] || @browser
  937. timeout = 16
  938. if params[:timeout]
  939. timeout = params[:timeout]
  940. end
  941. loops = timeout.to_i * 2
  942. text = ''
  943. (1..loops).each {
  944. element = instance.find_elements(css: params[:css])[0]
  945. if element #&& element.displayed?
  946. begin
  947. # match pn attribute
  948. text = if params[:attribute]
  949. element.attribute(params[:attribute])
  950. elsif params[:css] =~ /(input|textarea)/i
  951. element.attribute('value')
  952. else
  953. element.text
  954. end
  955. if text =~ /#{params[:value]}/i
  956. assert(true, "'#{params[:value]}' found in '#{text}'")
  957. sleep 0.5
  958. return true
  959. end
  960. rescue
  961. # try again
  962. end
  963. end
  964. sleep 0.5
  965. }
  966. screenshot(browser: instance, comment: 'watch_for_failed')
  967. raise "'#{params[:value]}' found in '#{text}'"
  968. end
  969. =begin
  970. wait untill selector disabppears
  971. watch_for_disappear(
  972. browser: browser1,
  973. css: '#content .text-1',
  974. timeout: 16, # in sec, default 16
  975. )
  976. wait untill text in selector disabppears
  977. watch_for_disappear(
  978. browser: browser1,
  979. css: '#content .text-1',
  980. value: 'some value as regexp',
  981. timeout: 16, # in sec, default 16
  982. )
  983. =end
  984. def watch_for_disappear(params = {})
  985. switch_window_focus(params)
  986. log('watch_for_disappear', params)
  987. instance = params[:browser] || @browser
  988. timeout = 16
  989. if params[:timeout]
  990. timeout = params[:timeout]
  991. end
  992. loops = timeout.to_i
  993. text = ''
  994. (1..loops).each {
  995. element = instance.find_elements(css: params[:css])[0]
  996. if !element #|| element.displayed?
  997. assert(true, 'not found')
  998. sleep 1
  999. return true
  1000. end
  1001. if params[:value]
  1002. begin
  1003. text = instance.find_elements(css: params[:css])[0].text
  1004. if text !~ /#{params[:value]}/i
  1005. assert(true, "not matching '#{params[:value]}' in text '#{text}'")
  1006. sleep 1
  1007. return true
  1008. end
  1009. rescue
  1010. # try again
  1011. end
  1012. end
  1013. sleep 1
  1014. }
  1015. screenshot(browser: instance, comment: 'disappear_failed')
  1016. raise "#{params[:css]}) still exsists"
  1017. end
  1018. =begin
  1019. shortcut(
  1020. browser: browser1,
  1021. key: 'x',
  1022. )
  1023. =end
  1024. def shortcut(params = {})
  1025. switch_window_focus(params)
  1026. log('shortcut', params)
  1027. instance = params[:browser] || @browser
  1028. instance.action.key_down(:control)
  1029. .key_down(:alt)
  1030. .send_keys(params[:key])
  1031. .key_up(:alt)
  1032. .key_up(:control)
  1033. .perform
  1034. end
  1035. =begin
  1036. window_keys(
  1037. browser: browser1,
  1038. value: 'x',
  1039. )
  1040. =end
  1041. def window_keys(params = {})
  1042. switch_window_focus(params)
  1043. log('window_keys', params)
  1044. instance = params[:browser] || @browser
  1045. instance.action.send_keys(params[:value]).perform
  1046. end
  1047. =begin
  1048. tasks_close_all(
  1049. browser: browser1,
  1050. )
  1051. =end
  1052. def tasks_close_all(params = {})
  1053. switch_window_focus(params)
  1054. log('tasks_close_all', params)
  1055. instance = params[:browser] || @browser
  1056. (1..100).each do
  1057. #sleep 0.5
  1058. begin
  1059. if instance.find_elements(css: '.navigation .tasks .task:first-child')[0]
  1060. instance.mouse.move_to(instance.find_elements(css: '.navigation .tasks .task:first-child')[0])
  1061. sleep 0.1
  1062. click_element = instance.find_elements(css: '.navigation .tasks .task:first-child .js-close')[0]
  1063. if click_element
  1064. click_element.click
  1065. # accept task close warning
  1066. if instance.find_elements(css: '.modal button.js-submit')[0]
  1067. sleep 0.4
  1068. instance.find_elements(css: '.modal button.js-submit')[0].click
  1069. end
  1070. end
  1071. else
  1072. break
  1073. end
  1074. rescue
  1075. # try again
  1076. end
  1077. end
  1078. assert(true, 'all tasks closed')
  1079. end
  1080. =begin
  1081. close_online_notitifcation(
  1082. browser: browser1,
  1083. data: {
  1084. #title: 'some title',
  1085. position: 3,
  1086. },
  1087. )
  1088. =end
  1089. def close_online_notitifcation(params = {})
  1090. switch_window_focus(params)
  1091. log('close_online_notitifcation', params)
  1092. instance = params[:browser] || @browser
  1093. data = params[:data]
  1094. if data[:title]
  1095. element = instance.find_elements(partial_link_text: data[:title])[0]
  1096. if !element
  1097. screenshot(browser: instance, comment: 'close_online_notitifcation')
  1098. raise "no online notification with title '#{data[:title]}' found"
  1099. end
  1100. instance.mouse.move_to(element)
  1101. sleep 0.1
  1102. instance.execute_script("$('.js-notificationsContainer .js-items .js-item .activity-text:contains(\"#{data[:title]}\") .js-remove').first().click()")
  1103. else
  1104. css = ".js-notificationsContainer .js-items .js-item:nth-child(#{data[:position]})"
  1105. element = instance.find_elements(css: css)[0]
  1106. if !element
  1107. screenshot(browser: instance, comment: 'close_online_notitifcation')
  1108. raise "no online notification with postion '#{css}' found"
  1109. end
  1110. instance.mouse.move_to(element)
  1111. sleep 0.1
  1112. instance.find_elements(css: "#{css} .js-remove")[0].click
  1113. end
  1114. true
  1115. end
  1116. =begin
  1117. online_notitifcation_close_all(
  1118. browser: browser1,
  1119. )
  1120. =end
  1121. def online_notitifcation_close_all(params = {})
  1122. switch_window_focus(params)
  1123. log('online_notitifcation_close_all', params)
  1124. instance = params[:browser] || @browser
  1125. (1..100).each do
  1126. sleep 0.5
  1127. begin
  1128. if instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0]
  1129. instance.mouse.move_to(instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0])
  1130. sleep 0.1
  1131. click_element = instance.find_elements(css: '.js-notificationsContainer .js-item:first-child .js-remove')[0]
  1132. if click_element
  1133. click_element.click
  1134. end
  1135. else
  1136. break
  1137. end
  1138. rescue
  1139. # try again
  1140. end
  1141. end
  1142. assert(true, 'all online notification closed')
  1143. end
  1144. =begin
  1145. empty_search(
  1146. browser: browser1,
  1147. )
  1148. =end
  1149. def empty_search(params = {})
  1150. switch_window_focus(params)
  1151. log('empty_search', params)
  1152. instance = params[:browser] || @browser
  1153. # empty search box by x
  1154. begin
  1155. instance.find_elements(css: '.search .js-emptySearch')[0].click
  1156. rescue
  1157. # in issues with ff & selenium, sometimes exeption appears
  1158. # "Element is not currently visible and so may not be interacted with"
  1159. log('empty_search via js')
  1160. instance.execute_script('$(".search .js-emptySearch").click()')
  1161. end
  1162. sleep 0.5
  1163. text = instance.find_elements(css: '#global-search')[0].attribute('value')
  1164. if !text
  1165. raise '#global-search is not empty!'
  1166. end
  1167. true
  1168. end
  1169. =begin
  1170. ticket_customer_select(
  1171. browser: browser1,
  1172. css: '#content .text-1',
  1173. customer: '',
  1174. )
  1175. =end
  1176. def ticket_customer_select(params = {})
  1177. switch_window_focus(params)
  1178. log('ticket_customer_select', params)
  1179. instance = params[:browser] || @browser
  1180. element = instance.find_elements(css: params[:css] + ' input[name="customer_id_completion"]')[0]
  1181. element.click
  1182. element.clear
  1183. element.send_keys(params[:customer])
  1184. sleep 2.5
  1185. element.send_keys(:enter)
  1186. #instance.find_elements(css: params[:css] + ' .recipientList-entry.js-user.is-active')[0].click
  1187. sleep 0.4
  1188. assert(true, 'ticket_customer_select')
  1189. end
  1190. =begin
  1191. overview_create(
  1192. browser: browser1,
  1193. data: {
  1194. name: name,
  1195. role: 'Agent',
  1196. selector: {
  1197. 'Priority': '1 low',
  1198. },
  1199. 'order::direction' => 'down',
  1200. }
  1201. )
  1202. =end
  1203. def overview_create(params)
  1204. switch_window_focus(params)
  1205. log('overview_create', params)
  1206. instance = params[:browser] || @browser
  1207. data = params[:data]
  1208. click(
  1209. browser: instance,
  1210. css: 'a[href="#manage"]',
  1211. mute_log: true,
  1212. )
  1213. click(
  1214. browser: instance,
  1215. css: 'a[href="#manage/overviews"]',
  1216. mute_log: true,
  1217. )
  1218. click(
  1219. browser: instance,
  1220. css: '#content a[data-type="new"]',
  1221. mute_log: true,
  1222. )
  1223. modal_ready(browser: instance)
  1224. if data[:name]
  1225. set(
  1226. browser: instance,
  1227. css: '.modal input[name=name]',
  1228. value: data[:name],
  1229. mute_log: true,
  1230. )
  1231. end
  1232. if data[:role]
  1233. select(
  1234. browser: instance,
  1235. css: '.modal select[name="role_id"]',
  1236. value: data[:role],
  1237. mute_log: true,
  1238. )
  1239. end
  1240. if data[:selector]
  1241. data[:selector].each {|key, value|
  1242. select(
  1243. browser: instance,
  1244. css: '.modal .ticket_selector .js-attributeSelector select',
  1245. value: key,
  1246. mute_log: true,
  1247. )
  1248. sleep 0.5
  1249. select(
  1250. browser: instance,
  1251. css: '.modal .ticket_selector .js-value select',
  1252. value: value,
  1253. deselect_all: true,
  1254. mute_log: true,
  1255. )
  1256. }
  1257. end
  1258. if data['order::direction']
  1259. select(
  1260. browser: instance,
  1261. css: '.modal select[name="order::direction"]',
  1262. value: data['order::direction'],
  1263. mute_log: true,
  1264. )
  1265. end
  1266. instance.find_elements(css: '.modal button.js-submit')[0].click
  1267. modal_disappear(browser: instance)
  1268. (1..12).each {
  1269. element = instance.find_elements(css: 'body')[0]
  1270. text = element.text
  1271. if text =~ /#{Regexp.quote(data[:name])}/
  1272. assert(true, 'overview created')
  1273. overview = {
  1274. name: name,
  1275. }
  1276. sleep 1
  1277. return overview
  1278. end
  1279. sleep 1
  1280. }
  1281. screenshot(browser: instance, comment: 'overview_create_failed')
  1282. raise 'overview creation failed'
  1283. end
  1284. =begin
  1285. overview_update(
  1286. browser: browser1,
  1287. data: {
  1288. name: name,
  1289. role: 'Agent',
  1290. selector: {
  1291. 'Priority': '1 low',
  1292. },
  1293. 'order::direction' => 'down',
  1294. }
  1295. )
  1296. =end
  1297. def overview_update(params)
  1298. switch_window_focus(params)
  1299. log('overview_create', params)
  1300. instance = params[:browser] || @browser
  1301. data = params[:data]
  1302. click(
  1303. browser: instance,
  1304. css: 'a[href="#manage"]',
  1305. mute_log: true,
  1306. )
  1307. click(
  1308. browser: instance,
  1309. css: 'a[href="#manage/overviews"]',
  1310. mute_log: true,
  1311. )
  1312. instance.execute_script("$(\"#content td:contains('#{data[:name]}')\").first().click()")
  1313. sleep 2
  1314. if data[:name]
  1315. set(
  1316. browser: instance,
  1317. css: '.modal input[name=name]',
  1318. value: data[:name],
  1319. mute_log: true,
  1320. )
  1321. end
  1322. if data[:role]
  1323. select(
  1324. browser: instance,
  1325. css: '.modal select[name="role_id"]',
  1326. value: data[:role],
  1327. mute_log: true,
  1328. )
  1329. end
  1330. if data[:selector]
  1331. data[:selector].each {|key, value|
  1332. select(
  1333. browser: instance,
  1334. css: '.modal .ticket_selector .js-attributeSelector select',
  1335. value: key,
  1336. mute_log: true,
  1337. )
  1338. sleep 0.5
  1339. select(
  1340. browser: instance,
  1341. css: '.modal .ticket_selector .js-value select',
  1342. value: value,
  1343. deselect_all: true,
  1344. mute_log: true,
  1345. )
  1346. }
  1347. end
  1348. if data['order::direction']
  1349. select(
  1350. browser: instance,
  1351. css: '.modal select[name="order::direction"]',
  1352. value: data['order::direction'],
  1353. mute_log: true,
  1354. )
  1355. end
  1356. instance.find_elements(css: '.modal button.js-submit')[0].click
  1357. modal_disappear(browser: instance)
  1358. (1..12).each {
  1359. element = instance.find_elements(css: 'body')[0]
  1360. text = element.text
  1361. if text =~ /#{Regexp.quote(data[:name])}/
  1362. assert(true, 'overview updated')
  1363. overview = {
  1364. name: name,
  1365. }
  1366. sleep 1
  1367. return overview
  1368. end
  1369. sleep 1
  1370. }
  1371. screenshot(browser: instance, comment: 'overview_update_failed')
  1372. raise 'overview update failed'
  1373. end
  1374. =begin
  1375. ticket = ticket_create(
  1376. browser: browser1,
  1377. data: {
  1378. customer: 'nico',
  1379. group: 'Users', # optional / '-NONE-' # if group selection should not be shown
  1380. priority: '2 normal',
  1381. state: 'open',
  1382. title: 'overview #1',
  1383. body: 'overview #1',
  1384. },
  1385. do_not_submit: true,
  1386. )
  1387. returns (in case of submitted)
  1388. {
  1389. id: 123,
  1390. number: '100001',
  1391. title: 'overview #1',
  1392. }
  1393. ticket = ticket_create(
  1394. browser: browser1,
  1395. data: {
  1396. customer: 'nico',
  1397. group: 'Users', # optional / '-NONE-' # if group selection should not be shown
  1398. priority: '2 normal',
  1399. state: 'open',
  1400. title: 'overview #1',
  1401. body: 'overview #1',
  1402. },
  1403. custom_data_select: {
  1404. key1: 'some value',
  1405. },
  1406. custom_data_input: {
  1407. key1: 'some value',
  1408. },
  1409. disable_group_check: true,
  1410. )
  1411. =end
  1412. def ticket_create(params)
  1413. switch_window_focus(params)
  1414. log('ticket_create', params)
  1415. instance = params[:browser] || @browser
  1416. data = params[:data]
  1417. click(
  1418. browser: instance,
  1419. css: 'a[href="#new"]',
  1420. mute_log: true,
  1421. )
  1422. click(
  1423. browser: instance,
  1424. css: 'a[href="#ticket/create"]',
  1425. mute_log: true,
  1426. )
  1427. found = false
  1428. (1..8).each {
  1429. element = instance.find_elements(css: '.active .newTicket')[0]
  1430. if element
  1431. found = true
  1432. break
  1433. end
  1434. sleep 1
  1435. }
  1436. if !found
  1437. screenshot(browser: instance, comment: 'ticket_create_failed')
  1438. raise 'no ticket create screen found!'
  1439. end
  1440. if data[:group]
  1441. if data[:group] == '-NONE-'
  1442. # check if owner selection exists
  1443. count = instance.find_elements(css: '.active .newTicket select[name="group_id"] option').count
  1444. assert_equal(0, count, 'owner selection should not be showm')
  1445. # check count of agents, should be only 3 / - selection + master + agent on init screen
  1446. count = instance.find_elements(css: '.active .newTicket select[name="owner_id"] option').count
  1447. assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
  1448. else
  1449. # check count of agents, should be only 1 / - selection on init screen
  1450. if !params[:disable_group_check]
  1451. count = instance.find_elements(css: '.active .newTicket select[name="owner_id"] option').count
  1452. assert_equal(1, count, 'check if owner selection is empty per default')
  1453. end
  1454. select(
  1455. browser: instance,
  1456. css: '.active .newTicket select[name="group_id"]',
  1457. value: data[:group],
  1458. mute_log: true,
  1459. )
  1460. sleep 0.2
  1461. end
  1462. end
  1463. if data[:priority]
  1464. select(
  1465. browser: instance,
  1466. css: '.active .newTicket select[name="priority_id"]',
  1467. value: data[:priority],
  1468. mute_log: true,
  1469. )
  1470. end
  1471. if data[:state]
  1472. select(
  1473. browser: instance,
  1474. css: '.active .newTicket select[name="state_id"]',
  1475. value: data[:state],
  1476. mute_log: true,
  1477. )
  1478. end
  1479. if data[:title]
  1480. set(
  1481. browser: instance,
  1482. css: '.active .newTicket input[name="title"]',
  1483. value: data[:title],
  1484. clear: true,
  1485. mute_log: true,
  1486. )
  1487. end
  1488. if data[:body]
  1489. set(
  1490. browser: instance,
  1491. css: '.active .newTicket div[data-name=body]',
  1492. value: data[:body],
  1493. clear: true,
  1494. mute_log: true,
  1495. )
  1496. # it's not working stable via selenium, use js
  1497. value = instance.find_elements(css: '.content .newTicket div[data-name=body]')[0].text
  1498. #puts "V #{value.inspect}"
  1499. if value != data[:body]
  1500. body_quoted = quote(data[:body])
  1501. instance.execute_script("$('.content.active div[data-name=body]').html('#{body_quoted}').trigger('focusout')")
  1502. end
  1503. end
  1504. if data[:customer]
  1505. element = instance.find_elements(css: '.active .newTicket input[name="customer_id_completion"]')[0]
  1506. element.click
  1507. element.clear
  1508. # ff issue, sometimes focus event gets dropped
  1509. # if drowdown is not open, try it again
  1510. if !instance.find_elements(css: '.active .newTicket .js-recipientDropdown.open')[0]
  1511. instance.execute_script('$(".active .newTicket .js-recipientDropdown").addClass("open")')
  1512. end
  1513. element.send_keys(data[:customer])
  1514. sleep 2.5
  1515. element.send_keys(:enter)
  1516. sleep 0.4
  1517. # ff issue, sometimes enter event gets dropped
  1518. # take user manually
  1519. if instance.find_elements(css: '.active .newTicket .js-recipientDropdown.open')[0]
  1520. instance.find_elements(css: '.active .newTicket .recipientList-entry.js-user.is-active')[0].click
  1521. sleep 0.4
  1522. end
  1523. end
  1524. if params[:custom_data_select]
  1525. params[:custom_data_select].each {|local_key, local_value|
  1526. select(
  1527. browser: instance,
  1528. css: ".active .newTicket select[name=\"#{local_key}\"]",
  1529. value: local_value,
  1530. )
  1531. }
  1532. end
  1533. if params[:custom_data_input]
  1534. params[:custom_data_input].each {|local_key, local_value|
  1535. set(
  1536. browser: instance,
  1537. css: ".active .newTicket input[name=\"#{local_key}\"]",
  1538. value: local_value,
  1539. clear: true,
  1540. )
  1541. }
  1542. end
  1543. if data[:attachment]
  1544. file_upload(
  1545. browser: instance,
  1546. css: '#content .text-1',
  1547. value: 'some text',
  1548. )
  1549. end
  1550. if params[:do_not_submit]
  1551. assert(true, 'ticket created without submit')
  1552. return
  1553. end
  1554. sleep 0.5
  1555. #instance.execute_script('$(".content.active .newTicket form").submit();')
  1556. click(
  1557. browser: instance,
  1558. css: '.active .newTicket button.js-submit',
  1559. mute_log: true,
  1560. )
  1561. sleep 1
  1562. (1..10).each {
  1563. if instance.current_url =~ /#{Regexp.quote('#ticket/zoom/')}/
  1564. assert(true, 'ticket created')
  1565. sleep 2.5
  1566. id = instance.current_url
  1567. id.gsub!(//,)
  1568. id.gsub!(%r{^.+?/(\d+)$}, '\\1')
  1569. element = instance.find_elements(css: '.active .ticketZoom-header .ticket-number')[0]
  1570. if element
  1571. number = element.text
  1572. ticket = {
  1573. id: id,
  1574. number: number,
  1575. title: data[:title],
  1576. }
  1577. sleep 3 # wait until notify is gone
  1578. screenshot(browser: instance, comment: 'ticket_create_ok')
  1579. return ticket
  1580. end
  1581. end
  1582. sleep 1
  1583. }
  1584. screenshot(browser: instance, comment: 'ticket_create_failed')
  1585. raise "ticket creation failed, can't get zoom url (current url is '#{instance.current_url}')"
  1586. end
  1587. =begin
  1588. ticket_update(
  1589. browser: browser1,
  1590. data: {
  1591. title: '',
  1592. customer: 'some_customer@example.com',
  1593. body: 'some body',
  1594. group: 'some group', # optional
  1595. priority: '1 low',
  1596. state: 'closed',
  1597. },
  1598. do_not_submit: true,
  1599. )
  1600. ticket_update(
  1601. browser: browser1,
  1602. data: {
  1603. title: '',
  1604. customer: 'some_customer@example.com',
  1605. body: 'some body',
  1606. group: 'some group', # optional
  1607. priority: '1 low',
  1608. state: 'closed',
  1609. },
  1610. custom_data_select: {
  1611. key1: 'some value',
  1612. },
  1613. custom_data_input: {
  1614. key1: 'some value',
  1615. },
  1616. do_not_submit: true,
  1617. )
  1618. =end
  1619. def ticket_update(params)
  1620. switch_window_focus(params)
  1621. log('ticket_update', params)
  1622. instance = params[:browser] || @browser
  1623. data = params[:data]
  1624. if data[:title]
  1625. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
  1626. #element.clear
  1627. #sleep 0.5
  1628. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
  1629. #element.send_keys(data[:title])
  1630. #sleep 0.5
  1631. #element.send_keys(:tab)
  1632. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").focus()')
  1633. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").text("' + data[:title] + '")')
  1634. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").blur()')
  1635. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").trigger("blur")')
  1636. # {
  1637. # :where => :instance2,
  1638. # :execute => 'sendkey',
  1639. # :css => '.content.active .ticketZoom-header .js-objectTitle',
  1640. # :value => 'TTT',
  1641. # },
  1642. # {
  1643. # :where => :instance2,
  1644. # :execute => 'sendkey',
  1645. # :css => '.content.active .ticketZoom-header .js-objectTitle',
  1646. # :value => :tab,
  1647. # },
  1648. end
  1649. if data[:customer]
  1650. # select tab
  1651. click(browser: instance, css: '.active .tabsSidebar-tab[data-tab="customer"]')
  1652. click(browser: instance, css: '.active div[data-tab="customer"] .js-actions .icon-arrow-down')
  1653. click(browser: instance, css: '.active div[data-tab="customer"] .js-actions [data-type="customer-change"]')
  1654. watch_for(
  1655. browser: instance,
  1656. css: '.modal',
  1657. value: 'change',
  1658. )
  1659. element = instance.find_elements(css: '.modal input[name="customer_id_completion"]')[0]
  1660. element.click
  1661. element.clear
  1662. element.send_keys(data[:customer])
  1663. sleep 2.5
  1664. element.send_keys(:enter)
  1665. #instance.find_elements(css: '.modal .user_autocompletion .recipientList-entry.js-user.is-active')[0].click
  1666. sleep 0.2
  1667. click(browser: instance, css: '.modal .js-submit')
  1668. modal_disappear(browser: instance)
  1669. watch_for(
  1670. browser: instance,
  1671. css: '.active .tabsSidebar',
  1672. value: data[:customer],
  1673. )
  1674. # select tab
  1675. click(browser: instance, css: '.active .tabsSidebar-tab[data-tab="ticket"]')
  1676. end
  1677. if data[:body]
  1678. set(
  1679. browser: instance,
  1680. css: '.content.active div[data-name=body]',
  1681. value: data[:body],
  1682. no_click: true,
  1683. mute_log: true,
  1684. )
  1685. # it's not working stable via selenium, use js
  1686. value = instance.find_elements(css: '.content.active div[data-name=body]')[0].text
  1687. if value != data[:body]
  1688. body_quoted = quote(data[:body])
  1689. instance.execute_script("$('.content.active div[data-name=body]').html('#{body_quoted}').trigger('focusout')")
  1690. end
  1691. end
  1692. if data[:group]
  1693. if data[:group] == '-NONE-'
  1694. # check if owner selection exists
  1695. count = instance.find_elements(css: '.active .sidebar select[name="group_id"] option').count
  1696. assert_equal(0, count, 'owner selection should not be showm')
  1697. # check count of agents, should be only 3 / - selection + master + agent on init screen
  1698. count = instance.find_elements(css: '.active .sidebar select[name="owner_id"] option').count
  1699. assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
  1700. else
  1701. select(
  1702. browser: instance,
  1703. css: '.active .sidebar select[name="group_id"]',
  1704. value: data[:group],
  1705. mute_log: true,
  1706. )
  1707. sleep 0.2
  1708. end
  1709. end
  1710. if data[:priority]
  1711. select(
  1712. browser: instance,
  1713. css: '.active .sidebar select[name="priority_id"]',
  1714. value: data[:priority],
  1715. mute_log: true,
  1716. )
  1717. end
  1718. if data[:state]
  1719. select(
  1720. browser: instance,
  1721. css: '.active .sidebar select[name="state_id"]',
  1722. value: data[:state],
  1723. mute_log: true,
  1724. )
  1725. end
  1726. if params[:custom_data_select]
  1727. params[:custom_data_select].each {|local_key, local_value|
  1728. select(
  1729. browser: instance,
  1730. css: ".active .sidebar select[name=\"#{local_key}\"]",
  1731. value: local_value,
  1732. )
  1733. }
  1734. end
  1735. if params[:custom_data_input]
  1736. params[:custom_data_input].each {|local_key, local_value|
  1737. set(
  1738. browser: instance,
  1739. css: ".active .sidebar input[name=\"#{local_key}\"]",
  1740. value: local_value,
  1741. clear: true,
  1742. )
  1743. }
  1744. end
  1745. if data[:state] || data[:group] || data[:body] || !params[:custom_data_select].empty? || !params[:custom_data_input].empty?
  1746. found = nil
  1747. (1..10).each {
  1748. break if found
  1749. begin
  1750. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  1751. if text =~ /(Discard your unsaved changes.|Verwerfen der)/
  1752. found = true
  1753. end
  1754. rescue
  1755. # try again
  1756. end
  1757. sleep 1
  1758. }
  1759. if !found
  1760. screenshot(browser: instance, comment: 'ticket_update_discard_message_failed')
  1761. raise 'no discard message found'
  1762. end
  1763. end
  1764. task_type(
  1765. browser: instance,
  1766. type: 'stayOnTab',
  1767. )
  1768. if params[:do_not_submit]
  1769. assert(true, 'ticket updated without submit')
  1770. return true
  1771. end
  1772. instance.find_elements(css: '.content.active .js-submit')[0].click
  1773. (1..10).each {
  1774. begin
  1775. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  1776. if !text || text.empty?
  1777. screenshot(browser: instance, comment: 'ticket_update_ok')
  1778. sleep 1
  1779. return true
  1780. end
  1781. rescue
  1782. # try again
  1783. end
  1784. sleep 1
  1785. }
  1786. screenshot(browser: instance, comment: 'ticket_update_failed')
  1787. raise 'unable to update ticket'
  1788. end
  1789. =begin
  1790. ticket_verify(
  1791. browser: browser1,
  1792. data: {
  1793. title: 'some title',
  1794. body: 'some body',
  1795. ## group: 'some group',
  1796. ## state: 'closed',
  1797. custom_data_select: {
  1798. key1: 'some value',
  1799. },
  1800. custom_data_input: {
  1801. key1: 'some value',
  1802. },
  1803. },
  1804. )
  1805. =end
  1806. def ticket_verify(params)
  1807. switch_window_focus(params)
  1808. log('ticket_verify', params)
  1809. instance = params[:browser] || @browser
  1810. data = params[:data]
  1811. if data[:title]
  1812. title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle').first.text.strip
  1813. if title =~ /#{data[:title]}/i
  1814. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  1815. else
  1816. raise "not matching '#{data[:title]}' in title '#{title}'"
  1817. end
  1818. end
  1819. if data[:body]
  1820. body = instance.find_elements(css: '.content.active [data-name="body"]').first.text.strip
  1821. if body =~ /#{data[:body]}/i
  1822. assert(true, "matching '#{data[:body]}' in body '#{body}'")
  1823. else
  1824. raise "not matching '#{data[:body]}' in body '#{body}'"
  1825. end
  1826. end
  1827. if params[:custom_data_select]
  1828. params[:custom_data_select].each {|local_key, local_value|
  1829. element = instance.find_elements(css: ".active .sidebar select[name=\"#{local_key}\"] option[selected]").first
  1830. value = element.text.strip
  1831. if value =~ /#{local_value}/i
  1832. assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
  1833. else
  1834. raise "not matching '#{value}' in #{local_key} '#{local_value}'"
  1835. end
  1836. }
  1837. end
  1838. if params[:custom_data_input]
  1839. params[:custom_data_input].each {|local_key, local_value|
  1840. element = instance.find_elements(css: ".active .sidebar input[name=\"#{local_key}\"]").first
  1841. value = element.text.strip
  1842. if value =~ /#{local_value}/i
  1843. assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
  1844. else
  1845. raise "not matching '#{value}' in #{local_key} '#{local_value}'"
  1846. end
  1847. }
  1848. end
  1849. true
  1850. end
  1851. =begin
  1852. ticket_open_by_overview(
  1853. browser: browser2,
  1854. number: ticket1[:number],
  1855. link: "#ticket/view/#{name}",
  1856. )
  1857. =end
  1858. def ticket_open_by_overview(params)
  1859. switch_window_focus(params)
  1860. log('ticket_open_by_overview', params)
  1861. instance = params[:browser] || @browser
  1862. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  1863. sleep 1
  1864. execute(
  1865. browser: instance,
  1866. js: '$(".content.active .sidebar").css("display", "block")',
  1867. )
  1868. instance.find_elements(css: ".content.active .sidebar a[href=\"#{params[:link]}\"]")[0].click
  1869. sleep 1
  1870. execute(
  1871. browser: instance,
  1872. js: '$(".content.active .sidebar").css("display", "none")',
  1873. )
  1874. instance.find_elements(partial_link_text: params[:number])[0].click
  1875. sleep 1
  1876. number = instance.find_elements(css: '.active .ticketZoom-header .ticket-number')[0].text
  1877. if number !~ /#{params[:number]}/
  1878. screenshot(browser: instance, comment: 'ticket_open_by_overview_failed')
  1879. raise "unable to search/find ticket #{params[:number]}!"
  1880. end
  1881. sleep 1
  1882. assert(true, "ticket #{params[:number]} found")
  1883. true
  1884. end
  1885. =begin
  1886. ticket_open_by_search(
  1887. browser: browser2,
  1888. number: ticket1[:number],
  1889. )
  1890. =end
  1891. def ticket_open_by_search(params)
  1892. switch_window_focus(params)
  1893. log('ticket_open_by_search', params)
  1894. instance = params[:browser] || @browser
  1895. # search by number
  1896. element = instance.find_elements(css: '#global-search')[0]
  1897. element.click
  1898. element.clear
  1899. element.send_keys(params[:number])
  1900. sleep 3
  1901. empty_search(browser: instance)
  1902. # search by number again
  1903. element = instance.find_elements(css: '#global-search')[0]
  1904. element.click
  1905. element.clear
  1906. element.send_keys(params[:number])
  1907. sleep 1
  1908. # open ticket
  1909. #instance.find_element(partial_link_text: params[:number] } ).click
  1910. instance.execute_script("$(\"#global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  1911. sleep 1
  1912. number = instance.find_elements(css: '.active .ticketZoom-header .ticket-number')[0].text
  1913. if number !~ /#{params[:number]}/
  1914. screenshot(browser: instance, comment: 'ticket_open_by_search_failed')
  1915. raise "unable to search/find ticket #{params[:number]}!"
  1916. end
  1917. sleep 1
  1918. true
  1919. end
  1920. =begin
  1921. ticket_open_by_title(
  1922. browser: browser2,
  1923. title: ticket1[:title],
  1924. )
  1925. =end
  1926. def ticket_open_by_title(params)
  1927. switch_window_focus(params)
  1928. log('ticket_open_by_title', params)
  1929. instance = params[:browser] || @browser
  1930. # search by number
  1931. element = instance.find_elements(css: '#global-search')[0]
  1932. element.click
  1933. element.clear
  1934. element.send_keys(params[:title])
  1935. sleep 3
  1936. # open ticket
  1937. #instance.find_element(partial_link_text: params[:title] } ).click
  1938. instance.execute_script("$(\"#global-search-result a:contains('#{params[:title]}') .nav-tab-icon\").click()")
  1939. sleep 1
  1940. title = instance.find_elements(css: '.active .ticketZoom-header .js-objectTitle')[0].text
  1941. if title !~ /#{params[:title]}/
  1942. screenshot(browser: instance, comment: 'ticket_open_by_title_failed')
  1943. raise "unable to search/find ticket #{params[:title]}!"
  1944. end
  1945. sleep 1
  1946. true
  1947. end
  1948. =begin
  1949. overview_count = overview_counter(
  1950. browser: browser2,
  1951. )
  1952. returns
  1953. {
  1954. '#ticket/view/all_unassigned' => 42,
  1955. }
  1956. =end
  1957. def overview_counter(params = {})
  1958. switch_window_focus(params)
  1959. log('overview_counter', params)
  1960. instance = params[:browser] || @browser
  1961. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  1962. sleep 2
  1963. execute(
  1964. browser: instance,
  1965. js: '$(".content.active .sidebar").css("display", "block")',
  1966. )
  1967. #execute(
  1968. # browser: instance,
  1969. # js: '$(".content.active .overview-header").css("display", "none")',
  1970. #)
  1971. overviews = {}
  1972. instance.find_elements(css: '.content.active .sidebar a[href]').each {|element|
  1973. url = element.attribute('href')
  1974. url.gsub!(%r{(http|https)://.+?/(.+?)$}, '\\2')
  1975. overviews[url] = 0
  1976. #puts url.inspect
  1977. #puts element.inspect
  1978. }
  1979. overviews.each {|url, _value|
  1980. count = instance.find_elements(css: ".content.active .sidebar a[href=\"#{url}\"] .badge")[0].text
  1981. overviews[url] = count.to_i
  1982. }
  1983. log('overview_counter', overviews)
  1984. overviews
  1985. end
  1986. =begin
  1987. organization_open_by_search(
  1988. browser: browser2,
  1989. value: 'some value',
  1990. )
  1991. =end
  1992. def organization_open_by_search(params = {})
  1993. switch_window_focus(params)
  1994. log('organization_open_by_search', params)
  1995. instance = params[:browser] || @browser
  1996. element = instance.find_elements(css: '#global-search')[0]
  1997. element.click
  1998. element.clear
  1999. element.send_keys(params[:value])
  2000. sleep 3
  2001. empty_search(browser: instance)
  2002. element = instance.find_elements(css: '#global-search')[0]
  2003. element.click
  2004. element.clear
  2005. element.send_keys(params[:value])
  2006. sleep 2
  2007. #instance.find_element(partial_link_text: params[:value] } ).click
  2008. instance.execute_script("$(\"#global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  2009. sleep 1
  2010. name = instance.find_elements(css: '.active h1')[0].text
  2011. if name !~ /#{params[:value]}/
  2012. screenshot(browser: instance, comment: 'organization_open_by_search_failed')
  2013. raise "unable to search/find org #{params[:value]}!"
  2014. end
  2015. assert(true, "org #{params[:value]} found")
  2016. sleep 2
  2017. true
  2018. end
  2019. =begin
  2020. user_open_by_search(
  2021. browser: browser2,
  2022. value: 'some value',
  2023. )
  2024. =end
  2025. def user_open_by_search(params = {})
  2026. switch_window_focus(params)
  2027. log('user_open_by_search', params)
  2028. instance = params[:browser] || @browser
  2029. element = instance.find_elements(css: '#global-search')[0]
  2030. element.click
  2031. element.clear
  2032. element.send_keys(params[:value])
  2033. sleep 3
  2034. #instance.find_element(partial_link_text: params[:value]).click
  2035. instance.execute_script("$(\"#global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  2036. sleep 1
  2037. name = instance.find_elements(css: '.active h1')[0].text
  2038. if name !~ /#{params[:value]}/
  2039. screenshot(browser: instance, comment: 'user_open_by_search_failed')
  2040. raise "unable to search/find user #{params[:value]}!"
  2041. end
  2042. assert(true, "user #{params[:term]} found")
  2043. sleep 2
  2044. true
  2045. end
  2046. =begin
  2047. user_create(
  2048. browser: browser2,
  2049. data: {
  2050. #login: 'some login' + random,
  2051. firstname: 'Manage Firstname' + random,
  2052. lastname: 'Manage Lastname' + random,
  2053. email: user_email,
  2054. password: 'some-pass',
  2055. },
  2056. )
  2057. =end
  2058. def user_create(params = {})
  2059. switch_window_focus(params)
  2060. log('user_create', params)
  2061. instance = params[:browser] || @browser
  2062. data = params[:data]
  2063. click(
  2064. browser: instance,
  2065. css: 'a[href="#manage"]',
  2066. mute_log: true,
  2067. )
  2068. click(
  2069. browser: instance,
  2070. css: 'a[href="#manage/users"]',
  2071. mute_log: true,
  2072. )
  2073. click(
  2074. browser: instance,
  2075. css: 'a[data-type="new"]',
  2076. mute_log: true,
  2077. )
  2078. modal_ready(browser: instance)
  2079. element = instance.find_elements(css: '.modal input[name=firstname]')[0]
  2080. element.clear
  2081. element.send_keys(data[:firstname])
  2082. element = instance.find_elements(css: '.modal input[name=lastname]')[0]
  2083. element.clear
  2084. element.send_keys(data[:lastname])
  2085. element = instance.find_elements(css: '.modal input[name=email]')[0]
  2086. element.clear
  2087. element.send_keys(data[:email])
  2088. element = instance.find_elements(css: '.modal input[name=password]')[0]
  2089. element.clear
  2090. element.send_keys(data[:password])
  2091. element = instance.find_elements(css: '.modal input[name=password_confirm]')[0]
  2092. element.clear
  2093. element.send_keys(data[:password])
  2094. check(
  2095. browser: instance,
  2096. css: '.modal input[name=role_ids][value=3]',
  2097. )
  2098. instance.find_elements(css: '.modal button.js-submit')[0].click
  2099. modal_disappear(browser: instance)
  2100. set(
  2101. browser: instance,
  2102. css: '.content .js-search',
  2103. value: data[:email],
  2104. )
  2105. watch_for(
  2106. browser: instance,
  2107. css: 'body',
  2108. value: data[:lastname],
  2109. )
  2110. assert(true, 'user created')
  2111. end
  2112. =begin
  2113. sla_create(
  2114. browser: browser2,
  2115. data: {
  2116. name: 'some sla' + random,
  2117. first_response_time_in_text: 61
  2118. },
  2119. )
  2120. =end
  2121. def sla_create(params = {})
  2122. switch_window_focus(params)
  2123. log('sla_create', params)
  2124. instance = params[:browser] || @browser
  2125. data = params[:data]
  2126. click(
  2127. browser: instance,
  2128. css: 'a[href="#manage"]',
  2129. mute_log: true,
  2130. )
  2131. click(
  2132. browser: instance,
  2133. css: 'a[href="#manage/slas"]',
  2134. mute_log: true,
  2135. )
  2136. click(
  2137. browser: instance,
  2138. css: 'a.js-new',
  2139. mute_log: true,
  2140. )
  2141. modal_ready(browser: instance)
  2142. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2143. element.clear
  2144. element.send_keys(data[:name])
  2145. element = instance.find_elements(css: '.modal input[name=first_response_time_in_text]')[0]
  2146. element.clear
  2147. element.send_keys(data[:first_response_time_in_text])
  2148. instance.find_elements(css: '.modal button.js-submit')[0].click
  2149. modal_disappear(browser: instance)
  2150. (1..8).each {
  2151. element = instance.find_elements(css: 'body')[0]
  2152. text = element.text
  2153. if text =~ /#{Regexp.quote(data[:name])}/
  2154. assert(true, 'sla created')
  2155. sleep 1
  2156. return true
  2157. end
  2158. sleep 1
  2159. }
  2160. screenshot(browser: instance, comment: 'sla_create_failed')
  2161. raise 'sla creation failed'
  2162. end
  2163. =begin
  2164. text_module_create(
  2165. browser: browser2,
  2166. data: {
  2167. name: 'some sla' + random,
  2168. keywords: 'some keywords',
  2169. content: 'some content',
  2170. },
  2171. )
  2172. =end
  2173. def text_module_create(params = {})
  2174. switch_window_focus(params)
  2175. log('text_module_create', params)
  2176. instance = params[:browser] || @browser
  2177. data = params[:data]
  2178. click(
  2179. browser: instance,
  2180. css: 'a[href="#manage"]',
  2181. mute_log: true,
  2182. )
  2183. click(
  2184. browser: instance,
  2185. css: 'a[href="#manage/text_modules"]',
  2186. mute_log: true,
  2187. )
  2188. click(
  2189. browser: instance,
  2190. css: 'a[data-type="new"]',
  2191. mute_log: true,
  2192. )
  2193. modal_ready(browser: instance)
  2194. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2195. element.clear
  2196. element.send_keys(data[:name])
  2197. element = instance.find_elements(css: '.modal input[name=keywords]')[0]
  2198. element.clear
  2199. element.send_keys(data[:keywords])
  2200. element = instance.find_elements(css: '.modal [data-name=content]')[0]
  2201. element.clear
  2202. element.send_keys(data[:content])
  2203. instance.find_elements(css: '.modal button.js-submit')[0].click
  2204. modal_disappear(browser: instance)
  2205. (1..8).each {
  2206. element = instance.find_elements(css: 'body')[0]
  2207. text = element.text
  2208. if text =~ /#{Regexp.quote(data[:name])}/
  2209. assert(true, 'text module created')
  2210. sleep 1
  2211. return true
  2212. end
  2213. sleep 1
  2214. }
  2215. screenshot(browser: instance, comment: 'text_module_create_failed')
  2216. raise 'text module creation failed'
  2217. end
  2218. =begin
  2219. signature_create(
  2220. browser: browser2,
  2221. data: {
  2222. name: 'some sla' + random,
  2223. body: 'some body',
  2224. },
  2225. )
  2226. =end
  2227. def signature_create(params = {})
  2228. switch_window_focus(params)
  2229. log('signature_create', params)
  2230. instance = params[:browser] || @browser
  2231. data = params[:data]
  2232. click(
  2233. browser: instance,
  2234. css: 'a[href="#manage"]',
  2235. mute_log: true,
  2236. )
  2237. click(
  2238. browser: instance,
  2239. css: 'a[href="#channels/email"]',
  2240. mute_log: true,
  2241. )
  2242. click(
  2243. browser: instance,
  2244. css: 'a[href="#c-signature"]',
  2245. mute_log: true,
  2246. )
  2247. sleep 4
  2248. click(
  2249. browser: instance,
  2250. css: '#content #c-signature a[data-type="new"]',
  2251. mute_log: true,
  2252. )
  2253. modal_ready(browser: instance)
  2254. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2255. element.clear
  2256. element.send_keys(data[:name])
  2257. element = instance.find_elements(css: '.modal [data-name=body]')[0]
  2258. element.clear
  2259. element.send_keys(data[:body])
  2260. instance.find_elements(css: '.modal button.js-submit')[0].click
  2261. modal_disappear(browser: instance)
  2262. (1..12).each {
  2263. element = instance.find_elements(css: 'body')[0]
  2264. text = element.text
  2265. if text =~ /#{Regexp.quote(data[:name])}/
  2266. assert(true, 'signature created')
  2267. sleep 1
  2268. return true
  2269. end
  2270. sleep 1
  2271. }
  2272. screenshot(browser: instance, comment: 'signature_create_failed')
  2273. raise 'signature creation failed'
  2274. end
  2275. =begin
  2276. group_create(
  2277. browser: browser2,
  2278. data: {
  2279. name: 'some sla' + random,
  2280. signature: 'some signature bame',
  2281. member: [
  2282. 'some_user_login',
  2283. ],
  2284. },
  2285. )
  2286. =end
  2287. def group_create(params = {})
  2288. switch_window_focus(params)
  2289. log('group_create', params)
  2290. instance = params[:browser] || @browser
  2291. data = params[:data]
  2292. click(
  2293. browser: instance,
  2294. css: 'a[href="#manage"]',
  2295. mute_log: true,
  2296. )
  2297. click(
  2298. browser: instance,
  2299. css: 'a[href="#manage/groups"]',
  2300. mute_log: true,
  2301. )
  2302. click(
  2303. browser: instance,
  2304. css: 'a[data-type="new"]',
  2305. mute_log: true,
  2306. )
  2307. modal_ready(browser: instance)
  2308. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2309. element.clear
  2310. element.send_keys(data[:name])
  2311. element = instance.find_elements(css: '.modal select[name="email_address_id"]')[0]
  2312. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2313. dropdown.select_by(:index, 1)
  2314. #dropdown.select_by(:text, action[:group])
  2315. if data[:signature]
  2316. element = instance.find_elements(css: '.modal select[name="signature_id"]')[0]
  2317. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2318. dropdown.select_by(:text, data[:signature])
  2319. end
  2320. instance.find_elements(css: '.modal button.js-submit')[0].click
  2321. modal_disappear(browser: instance)
  2322. (1..12).each {
  2323. element = instance.find_elements(css: 'body')[0]
  2324. text = element.text
  2325. if text =~ /#{Regexp.quote(data[:name])}/
  2326. assert(true, 'group created')
  2327. modal_disappear(browser: instance) # wait until modal has gone
  2328. # add member
  2329. if data[:member]
  2330. data[:member].each {|login|
  2331. instance.find_elements(css: 'a[href="#manage"]')[0].click
  2332. sleep 1
  2333. instance.find_elements(css: 'a[href="#manage/users"]')[0].click
  2334. sleep 3
  2335. element = instance.find_elements(css: '#content [name="search"]')[0]
  2336. element.clear
  2337. element.send_keys(login)
  2338. sleep 3
  2339. #instance.find_elements(:css => '#content table [data-id]')[0].click
  2340. instance.execute_script('$("#content table [data-id] td").first().click()')
  2341. sleep 3
  2342. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  2343. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  2344. instance.find_elements(css: '.modal button.js-submit')[0].click
  2345. modal_disappear(browser: instance)
  2346. }
  2347. end
  2348. end
  2349. sleep 1
  2350. return true
  2351. }
  2352. screenshot(browser: instance, comment: 'group_create_failed')
  2353. raise 'group creation failed'
  2354. end
  2355. =begin
  2356. object_manager_attribute_create(
  2357. browser: browser2,
  2358. data: {
  2359. name: 'field_name' + random,
  2360. display: 'Display Name of Field',
  2361. data_type: 'Select',
  2362. data_option: {
  2363. options: {
  2364. 'aa' => 'AA',
  2365. 'bb' => 'BB',
  2366. },
  2367. default: 'abc',
  2368. },
  2369. },
  2370. error: 'already exists'
  2371. )
  2372. object_manager_attribute_create(
  2373. browser: browser2,
  2374. data: {
  2375. name: 'field_name' + random,
  2376. display: 'Display Name of Field',
  2377. data_type: 'Text',
  2378. data_option: {
  2379. default: 'abc',
  2380. },
  2381. },
  2382. error: 'already exists'
  2383. )
  2384. object_manager_attribute_create(
  2385. browser: browser2,
  2386. data: {
  2387. name: 'field_name' + random,
  2388. display: 'Display Name of Field',
  2389. data_type: 'Integer',
  2390. data_option: {
  2391. default: '15',
  2392. min: 1,
  2393. max: 999999,
  2394. },
  2395. },
  2396. error: 'already exists'
  2397. )
  2398. object_manager_attribute_create(
  2399. browser: browser2,
  2400. data: {
  2401. name: 'field_name' + random,
  2402. display: 'Display Name of Field',
  2403. data_type: 'Datetime',
  2404. data_option: {
  2405. future: true,
  2406. past: true,
  2407. diff: 24,
  2408. },
  2409. },
  2410. error: 'already exists'
  2411. )
  2412. object_manager_attribute_create(
  2413. browser: browser2,
  2414. data: {
  2415. name: 'field_name' + random,
  2416. display: 'Display Name of Field',
  2417. data_type: 'Date',
  2418. data_option: {
  2419. future: true,
  2420. past: true,
  2421. diff: 24,
  2422. },
  2423. },
  2424. error: 'already exists'
  2425. )
  2426. object_manager_attribute_create(
  2427. browser: browser2,
  2428. data: {
  2429. name: 'field_name' + random,
  2430. display: 'Display Name of Field',
  2431. data_type: 'Boolean',
  2432. data_option: {
  2433. options: {
  2434. true: 'YES',
  2435. false: 'NO',
  2436. }
  2437. default: undefined,
  2438. },
  2439. },
  2440. error: 'already exists'
  2441. )
  2442. =end
  2443. def object_manager_attribute_create(params = {})
  2444. switch_window_focus(params)
  2445. log('object_manager_attribute_create', params)
  2446. instance = params[:browser] || @browser
  2447. data = params[:data]
  2448. click(
  2449. browser: instance,
  2450. css: 'a[href="#manage"]',
  2451. mute_log: true,
  2452. )
  2453. click(
  2454. browser: instance,
  2455. css: 'a[href="#system/object_manager"]',
  2456. mute_log: true,
  2457. )
  2458. sleep 4
  2459. click(
  2460. browser: instance,
  2461. css: '#content .js-new',
  2462. mute_log: true,
  2463. )
  2464. modal_ready(browser: instance)
  2465. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2466. element.clear
  2467. element.send_keys(data[:name])
  2468. element = instance.find_elements(css: '.modal input[name=display]')[0]
  2469. element.clear
  2470. element.send_keys(data[:display])
  2471. select(
  2472. browser: instance,
  2473. css: '.modal select[name="data_type"]',
  2474. value: data[:data_type],
  2475. mute_log: true,
  2476. )
  2477. if data[:data_option]
  2478. if data[:data_option][:options]
  2479. if data[:data_type] == 'Boolean'
  2480. element = instance.find_elements(css: '.modal .js-valueTrue').first
  2481. element.clear
  2482. element.send_keys(data[:data_option][:options][:true])
  2483. element = instance.find_elements(css: '.modal .js-valueFalse').first
  2484. element.clear
  2485. element.send_keys(data[:data_option][:options][:false])
  2486. else
  2487. data[:data_option][:options].each {|key, value|
  2488. element = instance.find_elements(css: '.modal .js-Table .js-key').last
  2489. element.clear
  2490. element.send_keys(key)
  2491. element = instance.find_elements(css: '.modal .js-Table .js-value').last
  2492. element.clear
  2493. element.send_keys(value)
  2494. element = instance.find_elements(css: '.modal .js-Table .js-add')[0]
  2495. element.click
  2496. }
  2497. end
  2498. end
  2499. [:default, :min, :max, :diff].each {|key|
  2500. next if !data[:data_option].key?(key)
  2501. element = instance.find_elements(css: ".modal [name=\"data_option::#{key}\"]").first
  2502. element.clear
  2503. element.send_keys(data[:data_option][key])
  2504. }
  2505. [:future, :past].each {|key|
  2506. next if !data[:data_option].key?(key)
  2507. select(
  2508. browser: instance,
  2509. css: ".modal select[name=\"data_option::#{key}\"]",
  2510. value: data[:data_option][key],
  2511. mute_log: true,
  2512. )
  2513. }
  2514. end
  2515. instance.find_elements(css: '.modal button.js-submit')[0].click
  2516. if params[:error]
  2517. sleep 4
  2518. watch_for(
  2519. css: '.modal',
  2520. value: params[:error],
  2521. )
  2522. click(
  2523. browser: instance,
  2524. css: '.modal .js-close',
  2525. )
  2526. modal_disappear(browser: instance)
  2527. return
  2528. end
  2529. (1..12).each {
  2530. element = instance.find_elements(css: 'body')[0]
  2531. text = element.text
  2532. if text =~ /#{Regexp.quote(data[:name])}/
  2533. assert(true, 'object manager attribute created')
  2534. sleep 1
  2535. return true
  2536. end
  2537. sleep 1
  2538. }
  2539. screenshot(browser: instance, comment: 'object_manager_attribute_create_failed')
  2540. raise 'object manager attribute creation failed'
  2541. end
  2542. =begin
  2543. object_manager_attribute_delete(
  2544. browser: browser2,
  2545. data: {
  2546. name: 'field_name' + random,
  2547. },
  2548. )
  2549. =end
  2550. def object_manager_attribute_delete(params = {})
  2551. switch_window_focus(params)
  2552. log('object_manager_attribute_delete', params)
  2553. instance = params[:browser] || @browser
  2554. data = params[:data]
  2555. click(
  2556. browser: instance,
  2557. css: 'a[href="#manage"]',
  2558. mute_log: true,
  2559. )
  2560. click(
  2561. browser: instance,
  2562. css: 'a[href="#system/object_manager"]',
  2563. mute_log: true,
  2564. )
  2565. sleep 4
  2566. instance = params[:browser] || @browser
  2567. data = params[:data]
  2568. r = instance.execute_script("$(\"#content td:contains('#{data[:name]}')\").first().closest('tr').find('.js-delete').click()")
  2569. p "rrr #{r.inspect}"
  2570. end
  2571. =begin
  2572. object_manager_attribute_discard_changes(
  2573. browser: browser2,
  2574. )
  2575. =end
  2576. def object_manager_attribute_discard_changes(params = {})
  2577. switch_window_focus(params)
  2578. log('object_manager_attribute_discard_changes', params)
  2579. instance = params[:browser] || @browser
  2580. click(
  2581. browser: instance,
  2582. css: 'a[href="#manage"]',
  2583. mute_log: true,
  2584. )
  2585. click(
  2586. browser: instance,
  2587. css: 'a[href="#system/object_manager"]',
  2588. mute_log: true,
  2589. )
  2590. sleep 4
  2591. element = instance.find_elements(css: '#content .js-discard').first
  2592. element.click
  2593. watch_for_disappear(
  2594. browser: instance,
  2595. css: '#content .js-discard',
  2596. )
  2597. end
  2598. =begin
  2599. tags_verify(
  2600. browser: browser2,
  2601. tags: {
  2602. 'tag 1' => true,
  2603. 'tag 2' => true,
  2604. 'tag 3' => false,
  2605. },
  2606. )
  2607. =end
  2608. def tags_verify(params = {})
  2609. switch_window_focus(params)
  2610. log('tags_verify', params)
  2611. instance = params[:browser] || @browser
  2612. tags = instance.find_elements({ css: '.content.active .js-tag' })
  2613. assert(tags)
  2614. assert(tags[0])
  2615. tags_found = {}
  2616. params[:tags].each {|key, _value|
  2617. tags_found[key] = false
  2618. }
  2619. tags.each {|element|
  2620. text = element.text
  2621. if tags_found.key?(text)
  2622. tags_found[text] = true
  2623. else
  2624. assert(false, "tag exists but is not in check to verify '#{text}'")
  2625. end
  2626. }
  2627. params[:tags].each {|key, value|
  2628. assert_equal(value, tags_found[key], "tag '#{key}'")
  2629. }
  2630. end
  2631. def quote(string)
  2632. string_quoted = string
  2633. string_quoted.gsub!(/&/, '&amp;')
  2634. string_quoted.gsub!(/</, '&lt;')
  2635. string_quoted.gsub!(/>/, '&gt;')
  2636. string_quoted
  2637. end
  2638. def switch_window_focus(params)
  2639. instance = params[:browser] || @browser
  2640. if instance != @last_used_browser
  2641. log('switch browser window focus', {})
  2642. instance.switch_to.window(instance.window_handles.first)
  2643. end
  2644. @last_used_browser = instance
  2645. end
  2646. def log(method, params = {})
  2647. begin
  2648. instance = params[:browser] || @browser
  2649. if instance
  2650. logs = instance.manage.logs.get(:browser)
  2651. logs.each {|log|
  2652. next if log.level == 'WARNING' && log.message =~ /Declaration\sdropped./ # ignore ff css warnings
  2653. time = Time.zone.parse(Time.zone.at(log.timestamp / 1000).to_datetime.to_s)
  2654. puts "#{time}/#{log.level}: #{log.message}"
  2655. }
  2656. end
  2657. rescue
  2658. # faild to get logs
  2659. end
  2660. return if !@@debug
  2661. return if params[:mute_log]
  2662. puts "#{Time.zone.now}/#{method}: #{params.inspect}"
  2663. end
  2664. end