browser_test_helper.rb 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463
  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_%L')}_#{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. 5.times {
  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. screenshot(browser: instance, comment: 'click_before')
  342. if params[:css]
  343. begin
  344. element = instance.find_elements(css: params[:css])[0]
  345. #if element
  346. # instance.mouse.move_to(element)
  347. #end
  348. element.click
  349. rescue => e
  350. sleep 0.5
  351. # just try again
  352. log('click', { rescure: true })
  353. element = instance.find_elements(css: params[:css])[0]
  354. #if element
  355. # instance.mouse.move_to(element)
  356. #end
  357. element.click
  358. end
  359. else
  360. sleep 0.5
  361. begin
  362. instance.find_elements(partial_link_text: params[:text])[0].click
  363. rescue => e
  364. sleep 0.5
  365. # just try again
  366. log('click', { rescure: true })
  367. instance.find_elements(partial_link_text: params[:text])[0].click
  368. end
  369. end
  370. sleep 0.2 if !params[:fast]
  371. sleep params[:wait] if params[:wait]
  372. end
  373. =begin
  374. scroll_to(
  375. browser: browser1,
  376. position: 'top', # botton
  377. css: '.some_class',
  378. )
  379. =end
  380. def scroll_to(params)
  381. switch_window_focus(params)
  382. log('scroll_to', params)
  383. instance = params[:browser] || @browser
  384. position = 'true'
  385. if params[:position] == 'botton'
  386. position = 'false'
  387. end
  388. execute(
  389. browser: instance,
  390. js: "\$('#{params[:css]}').get(0).scrollIntoView(#{position})",
  391. mute_log: params[:mute_log]
  392. )
  393. sleep 0.3
  394. end
  395. =begin
  396. modal_ready(
  397. browser: browser1,
  398. )
  399. =end
  400. def modal_ready(params = {})
  401. switch_window_focus(params)
  402. log('modal_ready', params)
  403. instance = params[:browser] || @browser
  404. sleep 3
  405. end
  406. =begin
  407. modal_disappear(
  408. browser: browser1,
  409. timeout: 12, # default 8
  410. )
  411. =end
  412. def modal_disappear(params = {})
  413. switch_window_focus(params)
  414. log('modal_disappear', params)
  415. instance = params[:browser] || @browser
  416. watch_for_disappear(
  417. browser: instance,
  418. css: '.modal',
  419. timeout: params[:timeout] || 8,
  420. )
  421. end
  422. =begin
  423. execute(
  424. browser: browser1,
  425. js: '.some_class',
  426. )
  427. =end
  428. def execute(params)
  429. switch_window_focus(params)
  430. log('js', params)
  431. instance = params[:browser] || @browser
  432. if params[:js]
  433. return instance.execute_script(params[:js])
  434. end
  435. raise "Invalid execute params #{params.inspect}"
  436. end
  437. =begin
  438. exists(
  439. browser: browser1,
  440. css: '.some_class',
  441. )
  442. exists(
  443. displayed: false, # true|false
  444. browser: browser1,
  445. css: '.some_class',
  446. )
  447. =end
  448. def exists(params)
  449. switch_window_focus(params)
  450. log('exists', params)
  451. instance = params[:browser] || @browser
  452. if !instance.find_elements(css: params[:css])[0]
  453. screenshot(browser: instance, comment: 'exists_failed')
  454. raise "#{params[:css]} dosn't exist, but should"
  455. end
  456. if params.key?(:displayed)
  457. if params[:displayed] == true && !instance.find_elements(css: params[:css])[0].displayed?
  458. raise "#{params[:css]} is not displayed, but should"
  459. end
  460. if params[:displayed] == false && instance.find_elements(css: params[:css])[0].displayed?
  461. raise "#{params[:css]} is displayed, but should not"
  462. end
  463. end
  464. true
  465. end
  466. =begin
  467. exists_not(
  468. browser: browser1,
  469. css: '.some_class',
  470. )
  471. =end
  472. def exists_not(params)
  473. switch_window_focus(params)
  474. log('exists_not', params)
  475. instance = params[:browser] || @browser
  476. if instance.find_elements(css: params[:css])[0]
  477. screenshot(browser: instance, comment: 'exists_not_failed')
  478. raise "#{params[:css]} exists but should not"
  479. end
  480. true
  481. end
  482. =begin
  483. set(
  484. browser: browser1,
  485. css: '.some_class',
  486. value: true,
  487. slow: false,
  488. blur: true, # default false
  489. clear: true, # todo | default: true
  490. no_click: true,
  491. )
  492. =end
  493. def set(params)
  494. switch_window_focus(params)
  495. log('set', params)
  496. instance = params[:browser] || @browser
  497. screenshot(browser: instance, comment: 'set_before')
  498. element = instance.find_elements(css: params[:css])[0]
  499. if !params[:no_click]
  500. element.click
  501. end
  502. element.clear
  503. if !params[:slow]
  504. element.send_keys(params[:value])
  505. else
  506. element.send_keys('')
  507. keys = params[:value].to_s.split('')
  508. keys.each { |key|
  509. instance.action.send_keys(key).perform
  510. }
  511. end
  512. # it's not working stable with ff via selenium, use js
  513. if browser =~ /firefox/i && params[:css] =~ /\[data-name=/
  514. log('set_ff_check', params)
  515. value = instance.find_elements(css: params[:css])[0].text
  516. if value != params[:value]
  517. log('set_ff_check_failed_use_js', params)
  518. value_quoted = quote(params[:value])
  519. puts "DEBUG $('#{params[:css]}').html('#{value_quoted}').trigger('focusout')"
  520. instance.execute_script("$('#{params[:css]}').html('#{value_quoted}').trigger('focusout')")
  521. end
  522. end
  523. if params[:blur]
  524. instance.execute_script("$('#{params[:css]}').blur()")
  525. end
  526. sleep 0.2
  527. screenshot(browser: instance, comment: 'set_after')
  528. end
  529. =begin
  530. select(
  531. browser: browser1,
  532. css: '.some_class',
  533. value: 'Some Value',
  534. deselect_all: false, # default false
  535. )
  536. =end
  537. def select(params)
  538. switch_window_focus(params)
  539. log('select', params)
  540. instance = params[:browser] || @browser
  541. screenshot(browser: instance, comment: 'select_before')
  542. # searchable select
  543. element = instance.find_elements(css: "#{params[:css]}.js-shadow")[0]
  544. if element
  545. element = instance.find_elements(css: "#{params[:css]}.js-shadow + .js-input")[0]
  546. element.click
  547. element.clear
  548. sleep 0.4
  549. element.send_keys(params[:value])
  550. sleep 0.2
  551. element.send_keys(:enter)
  552. sleep 0.2
  553. return
  554. end
  555. # native select
  556. begin
  557. element = instance.find_elements(css: params[:css])[0]
  558. dropdown = Selenium::WebDriver::Support::Select.new(element)
  559. if params[:deselect_all]
  560. dropdown.deselect_all
  561. end
  562. dropdown.select_by(:text, params[:value])
  563. #puts "select - #{params.inspect}"
  564. rescue
  565. sleep 0.4
  566. # just try again
  567. log('select', { rescure: true })
  568. element = instance.find_elements(css: params[:css])[0]
  569. dropdown = Selenium::WebDriver::Support::Select.new(element)
  570. if params[:deselect_all]
  571. dropdown.deselect_all
  572. end
  573. dropdown.select_by(:text, params[:value])
  574. #puts "select2 - #{params.inspect}"
  575. end
  576. sleep 0.4
  577. screenshot(browser: instance, comment: 'select_after')
  578. end
  579. =begin
  580. switch(
  581. browser: browser1,
  582. css: '.some_class',
  583. type: 'on', # 'off'
  584. no_check: true, # do not check is switch has changed, in case if js alert
  585. )
  586. =end
  587. def switch(params)
  588. switch_window_focus(params)
  589. log('switch', params)
  590. instance = params[:browser] || @browser
  591. screenshot(browser: instance, comment: 'switch_before')
  592. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  593. checked = element.attribute('checked')
  594. if !checked
  595. if params[:type] == 'on'
  596. instance.find_elements(css: "#{params[:css]} label")[0].click
  597. sleep 2
  598. if params[:no_check] != true
  599. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  600. checked = element.attribute('checked')
  601. raise 'Switch not on!' if !checked
  602. end
  603. end
  604. elsif params[:type] == 'off'
  605. instance.find_elements(css: "#{params[:css]} label")[0].click
  606. sleep 2
  607. if params[:no_check] != true
  608. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  609. checked = element.attribute('checked')
  610. raise 'Switch not off!' if checked
  611. end
  612. end
  613. screenshot(browser: instance, comment: 'switch_after')
  614. end
  615. =begin
  616. check(
  617. browser: browser1,
  618. css: '.some_class',
  619. )
  620. =end
  621. def check(params)
  622. switch_window_focus(params)
  623. log('check', params)
  624. instance = params[:browser] || @browser
  625. screenshot(browser: instance, comment: 'check_before')
  626. instance.execute_script("if (!$('#{params[:css]}').prop('checked')) { $('#{params[:css]}').click() }")
  627. #element = instance.find_elements(css: params[:css])[0]
  628. #checked = element.attribute('checked')
  629. #element.click if !checked
  630. screenshot(browser: instance, comment: 'check_after')
  631. end
  632. =begin
  633. uncheck(
  634. browser: browser1,
  635. css: '.some_class',
  636. )
  637. =end
  638. def uncheck(params)
  639. switch_window_focus(params)
  640. log('uncheck', params)
  641. instance = params[:browser] || @browser
  642. screenshot(browser: instance, comment: 'uncheck_before')
  643. instance.execute_script("if ($('#{params[:css]}').prop('checked')) { $('#{params[:css]}').click() }")
  644. #element = instance.find_elements(css: params[:css])[0]
  645. #checked = element.attribute('checked')
  646. #element.click if checked
  647. screenshot(browser: instance, comment: 'uncheck_after')
  648. end
  649. =begin
  650. sendkey(
  651. browser: browser1,
  652. value: :enter,
  653. slow: false, # default false
  654. )
  655. =end
  656. def sendkey(params)
  657. switch_window_focus(params)
  658. log('sendkey', params)
  659. instance = params[:browser] || @browser
  660. element = nil
  661. if params[:css]
  662. element = instance.find_elements(css: params[:css])[0]
  663. end
  664. screenshot(browser: instance, comment: 'sendkey_before')
  665. if params[:value].class == Array
  666. params[:value].each { |key|
  667. if element
  668. element.send_keys(key)
  669. else
  670. instance.action.send_keys(key).perform
  671. end
  672. }
  673. screenshot(browser: instance, comment: 'sendkey_after')
  674. return
  675. end
  676. if element
  677. element.send_keys(params[:value])
  678. else
  679. instance.action.send_keys(params[:value]).perform
  680. end
  681. if params[:slow]
  682. sleep 1.5
  683. else
  684. sleep 0.2
  685. end
  686. screenshot(browser: instance, comment: 'sendkey_after')
  687. end
  688. =begin
  689. match(
  690. browser: browser1,
  691. css: '#content .text-1',
  692. value: 'some test for browser and some other for browser',
  693. attribute: 'some_attribute', # match on attribute
  694. should_not_match: true,
  695. no_quote: false, # use regex
  696. )
  697. =end
  698. def match(params, fallback = false)
  699. switch_window_focus(params)
  700. log('match', params)
  701. instance = params[:browser] || @browser
  702. element = instance.find_elements(css: params[:css])[0]
  703. if params[:css] =~ /select/
  704. dropdown = Selenium::WebDriver::Support::Select.new(element)
  705. success = false
  706. if dropdown.selected_options
  707. dropdown.selected_options.each { |option|
  708. if option.text == params[:value]
  709. success = true
  710. end
  711. }
  712. end
  713. if params[:should_not_match]
  714. if success
  715. screenshot(browser: instance, comment: 'match_failed')
  716. raise "should not match '#{params[:value]}' in select list, but is matching"
  717. end
  718. elsif !success
  719. screenshot(browser: instance, comment: 'match_failed')
  720. raise "not matching '#{params[:value]}' in select list"
  721. end
  722. return true
  723. end
  724. # match on attribute
  725. begin
  726. text = if params[:attribute]
  727. element.attribute(params[:attribute])
  728. elsif params[:css] =~ /(input|textarea)/i
  729. element.attribute('value')
  730. else
  731. element.text
  732. end
  733. rescue => e
  734. # just try again
  735. if !fallback
  736. return match(params, true)
  737. end
  738. raise e.inspect
  739. end
  740. # do cleanups (needed for richtext tests)
  741. if params[:cleanup]
  742. text.gsub!(/\s+$/m, '')
  743. params[:value].gsub!(/\s+$/m, '')
  744. end
  745. match = false
  746. if params[:no_quote]
  747. #puts "aaaa #{text}/#{params[:value]}"
  748. if text =~ /#{params[:value]}/i
  749. match = $1 || true
  750. end
  751. elsif text =~ /#{Regexp.quote(params[:value])}/i
  752. match = true
  753. end
  754. if match
  755. if params[:should_not_match]
  756. screenshot(browser: instance, comment: 'match_failed')
  757. raise "matching '#{params[:value]}' in content '#{text}' but should not!"
  758. end
  759. elsif !params[:should_not_match]
  760. screenshot(browser: instance, comment: 'match_failed')
  761. raise "not matching '#{params[:value]}' in content '#{text}' but should!"
  762. end
  763. sleep 0.2
  764. match
  765. end
  766. =begin
  767. match_not(
  768. browser: browser1,
  769. css: '#content .text-1',
  770. value: 'some test for browser and some other for browser',
  771. attribute: 'some_attribute', # match on attribute
  772. should_not_match: true,
  773. no_quote: false, # use regex
  774. )
  775. =end
  776. def match_not(params)
  777. switch_window_focus(params)
  778. log('match_not', params)
  779. params[:should_not_match] = true
  780. match(params)
  781. end
  782. =begin
  783. task_type(
  784. browser: browser1,
  785. type: 'stayOnTab',
  786. )
  787. =end
  788. def task_type(params)
  789. switch_window_focus(params)
  790. log('task_type', params)
  791. instance = params[:browser] || @browser
  792. if params[:type]
  793. instance.find_elements(css: '.content.active .js-secondaryActionButtonLabel')[0].click
  794. instance.find_elements(css: ".content.active .js-secondaryActionLabel[data-type=#{params[:type]}]")[0].click
  795. return
  796. end
  797. raise "Unknown params for task_type: #{params.inspect}"
  798. end
  799. =begin
  800. cookie(
  801. browser: browser1,
  802. name: '^_zammad.+?',
  803. value: '.+?',
  804. expires: nil,
  805. )
  806. cookie(
  807. browser: browser1,
  808. name: '^_zammad.+?',
  809. should_not_exist: true,
  810. )
  811. =end
  812. def cookie(params)
  813. switch_window_focus(params)
  814. log('cookie', params)
  815. instance = params[:browser] || @browser
  816. if !browser_support_cookies
  817. assert(true, "'#{params[:value]}' ups browser is not supporting reading cookies, go ahead")
  818. return true
  819. end
  820. cookies = instance.manage.all_cookies
  821. cookies.each { |cookie|
  822. #puts "CCC #{cookie.inspect}"
  823. # :name=>"_zammad_session_c25832f4de2", :value=>"adc31cd21615cb0a7ab269184ec8b76f", :path=>"/", :domain=>"localhost", :expires=>nil, :secure=>false}
  824. next if cookie[:name] !~ /#{params[:name]}/i
  825. if params.key?(:value) && cookie[:value].to_s =~ /#{params[:value]}/i
  826. assert(true, "matching value '#{params[:value]}' in cookie '#{cookie}'")
  827. else
  828. raise "not matching value '#{params[:value]}' in cookie '#{cookie}'"
  829. end
  830. if params.key?(:expires) && cookie[:expires].to_s =~ /#{params[:expires]}/i
  831. assert(true, "matching expires '#{params[:expires].inspect}' in cookie '#{cookie}'")
  832. else
  833. raise "not matching expires '#{params[:expires]}' in cookie '#{cookie}'"
  834. end
  835. return if !params[:should_not_exist]
  836. raise "cookie with name '#{params[:name]}' should not exist, but exists '#{cookies}'"
  837. }
  838. if params[:should_not_exist]
  839. assert(true, "cookie with name '#{params[:name]}' is not existing")
  840. return
  841. end
  842. raise "not matching name '#{params[:name]}' in cookie '#{cookies}'"
  843. end
  844. =begin
  845. verify_title(
  846. browser: browser1,
  847. value: 'some title',
  848. )
  849. =end
  850. def verify_title(params = {})
  851. switch_window_focus(params)
  852. log('verify_title', params)
  853. instance = params[:browser] || @browser
  854. title = instance.title
  855. if title =~ /#{params[:value]}/i
  856. assert(true, "matching '#{params[:value]}' in title '#{title}'")
  857. else
  858. raise "not matching '#{params[:value]}' in title '#{title}'"
  859. end
  860. end
  861. =begin
  862. verify_task(
  863. browser: browser1,
  864. data: {
  865. title: 'some title',
  866. modified: true, # optional
  867. }
  868. )
  869. =end
  870. def verify_task(params = {}, fallback = false)
  871. switch_window_focus(params)
  872. log('verify_task', params)
  873. instance = params[:browser] || @browser
  874. data = params[:data]
  875. sleep 1
  876. begin
  877. # verify title
  878. if data[:title]
  879. title = instance.find_elements(css: '.tasks .is-active')[0].text.strip
  880. if title =~ /#{data[:title]}/i
  881. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  882. else
  883. screenshot(browser: instance, comment: 'verify_task_failed')
  884. raise "not matching '#{data[:title]}' in title '#{title}'"
  885. end
  886. end
  887. # verify modified
  888. if data.key?(:modified)
  889. exists = instance.find_elements(css: '.tasks .is-active')[0]
  890. is_modified = instance.find_elements(css: '.tasks .is-modified')[0]
  891. puts "m #{data[:modified].inspect}"
  892. if exists
  893. puts ' exists'
  894. end
  895. if is_modified
  896. puts ' is_modified'
  897. end
  898. if data[:modified] == true
  899. if is_modified
  900. assert(true, "task '#{data[:title]}' is modifed")
  901. elsif !exists
  902. screenshot(browser: instance, comment: 'verify_task_failed')
  903. raise "task '#{data[:title]}' not exists, should not modified"
  904. else
  905. screenshot(browser: instance, comment: 'verify_task_failed')
  906. raise "task '#{data[:title]}' is not modifed"
  907. end
  908. elsif !is_modified
  909. assert(true, "task '#{data[:title]}' is modifed")
  910. elsif !exists
  911. screenshot(browser: instance, comment: 'verify_task_failed')
  912. raise "task '#{data[:title]}' not exists, should be not modified"
  913. else
  914. screenshot(browser: instance, comment: 'verify_task_failed')
  915. raise "task '#{data[:title]}' is modifed, but should not"
  916. end
  917. end
  918. rescue => e
  919. # just try again
  920. if !fallback
  921. verify_task(params, true)
  922. end
  923. raise 'ERROR: ' + e.inspect
  924. end
  925. true
  926. end
  927. =begin
  928. open_task(
  929. browser: browser1,
  930. data: {
  931. title: 'some title',
  932. }
  933. )
  934. =end
  935. def open_task(params = {})
  936. switch_window_focus(params)
  937. log('open_task', params)
  938. instance = params[:browser] || @browser
  939. data = params[:data]
  940. element = instance.find_elements(partial_link_text: data[:title])[0]
  941. if !element
  942. screenshot(browser: instance, comment: 'open_task_failed')
  943. raise "no task with title '#{data[:title]}' found"
  944. end
  945. element.click
  946. true
  947. end
  948. =begin
  949. close_task(
  950. browser: browser1,
  951. data: {
  952. title: 'some title',
  953. },
  954. discard_changes: true,
  955. )
  956. =end
  957. def close_task(params = {})
  958. switch_window_focus(params)
  959. log('close_task', params)
  960. instance = params[:browser] || @browser
  961. data = params[:data]
  962. element = instance.find_elements(partial_link_text: data[:title])[0]
  963. if !element
  964. screenshot(browser: instance, comment: 'close_task_failed')
  965. raise "no task with title '#{data[:title]}' found"
  966. end
  967. instance.mouse.move_to(element)
  968. sleep 0.1
  969. instance.execute_script("$('.navigation .tasks .task:contains(\"#{data[:title]}\") .js-close').click()")
  970. # accept task close warning
  971. if params[:discard_changes]
  972. modal_ready(browser: instance)
  973. instance.find_elements(css: '.modal button.js-submit')[0].click
  974. modal_disappear(browser: instance)
  975. end
  976. true
  977. end
  978. =begin
  979. file_upload(
  980. browser: browser1,
  981. css: '.content.active .attachmentPlaceholder-inputHolder input'
  982. files: ['path/in/home/some_file.ext'], # 'test/fixtures/test1.pdf'
  983. )
  984. =end
  985. def file_upload(params = {})
  986. switch_window_focus(params)
  987. log('file_upload', params)
  988. instance = params[:browser] || @browser
  989. params[:files].each { |file|
  990. instance.find_elements(css: params[:css])[0].send_keys "#{Rails.root}/#{file}"
  991. }
  992. sleep 2 * params[:files].count
  993. end
  994. =begin
  995. watch_for(
  996. browser: browser1,
  997. css: '#content .text-1',
  998. value: 'some text',
  999. attribute: 'some_attribute' # optional
  1000. timeout: 16, # in sec, default 16
  1001. )
  1002. =end
  1003. def watch_for(params = {})
  1004. switch_window_focus(params)
  1005. log('watch_for', params)
  1006. instance = params[:browser] || @browser
  1007. timeout = 16
  1008. if params[:timeout]
  1009. timeout = params[:timeout]
  1010. end
  1011. loops = timeout.to_i * 2
  1012. text = ''
  1013. (1..loops).each {
  1014. element = instance.find_elements(css: params[:css])[0]
  1015. if element #&& element.displayed?
  1016. begin
  1017. # match pn attribute
  1018. text = if params[:attribute]
  1019. element.attribute(params[:attribute])
  1020. elsif params[:css] =~ /(input|textarea)/i
  1021. element.attribute('value')
  1022. else
  1023. element.text
  1024. end
  1025. if text =~ /#{params[:value]}/i
  1026. assert(true, "'#{params[:value]}' found in '#{text}'")
  1027. sleep 0.5
  1028. return true
  1029. end
  1030. rescue
  1031. # try again
  1032. end
  1033. end
  1034. sleep 0.5
  1035. }
  1036. screenshot(browser: instance, comment: 'watch_for_failed')
  1037. raise "'#{params[:value]}' found in '#{text}'"
  1038. end
  1039. =begin
  1040. wait untill selector disabppears
  1041. watch_for_disappear(
  1042. browser: browser1,
  1043. css: '#content .text-1',
  1044. timeout: 16, # in sec, default 16
  1045. )
  1046. wait untill text in selector disabppears
  1047. watch_for_disappear(
  1048. browser: browser1,
  1049. css: '#content .text-1',
  1050. value: 'some value as regexp',
  1051. timeout: 16, # in sec, default 16
  1052. )
  1053. =end
  1054. def watch_for_disappear(params = {})
  1055. switch_window_focus(params)
  1056. log('watch_for_disappear', params)
  1057. instance = params[:browser] || @browser
  1058. timeout = 16
  1059. if params[:timeout]
  1060. timeout = params[:timeout]
  1061. end
  1062. loops = timeout.to_i
  1063. text = ''
  1064. (1..loops).each {
  1065. element = instance.find_elements(css: params[:css])[0]
  1066. if !element #|| element.displayed?
  1067. assert(true, 'not found')
  1068. sleep 1
  1069. return true
  1070. end
  1071. if params[:value]
  1072. begin
  1073. text = instance.find_elements(css: params[:css])[0].text
  1074. if text !~ /#{params[:value]}/i
  1075. assert(true, "not matching '#{params[:value]}' in text '#{text}'")
  1076. sleep 1
  1077. return true
  1078. end
  1079. rescue
  1080. # try again
  1081. end
  1082. end
  1083. sleep 1
  1084. }
  1085. screenshot(browser: instance, comment: 'disappear_failed')
  1086. raise "#{params[:css]}) still exsists"
  1087. end
  1088. =begin
  1089. shortcut(
  1090. browser: browser1,
  1091. key: 'x',
  1092. )
  1093. =end
  1094. def shortcut(params = {})
  1095. switch_window_focus(params)
  1096. log('shortcut', params)
  1097. instance = params[:browser] || @browser
  1098. screenshot(browser: instance, comment: 'shortcut_before')
  1099. instance.action.key_down(:control)
  1100. .key_down(:alt)
  1101. .send_keys(params[:key])
  1102. .key_up(:alt)
  1103. .key_up(:control)
  1104. .perform
  1105. screenshot(browser: instance, comment: 'shortcut_after')
  1106. end
  1107. =begin
  1108. window_keys(
  1109. browser: browser1,
  1110. value: 'x',
  1111. )
  1112. =end
  1113. def window_keys(params = {})
  1114. switch_window_focus(params)
  1115. log('window_keys', params)
  1116. instance = params[:browser] || @browser
  1117. instance.action.send_keys(params[:value]).perform
  1118. end
  1119. =begin
  1120. tasks_close_all(
  1121. browser: browser1,
  1122. )
  1123. =end
  1124. def tasks_close_all(params = {})
  1125. switch_window_focus(params)
  1126. log('tasks_close_all', params)
  1127. instance = params[:browser] || @browser
  1128. 99.times do
  1129. #sleep 0.5
  1130. begin
  1131. if instance.find_elements(css: '.navigation .tasks .task:first-child')[0]
  1132. instance.mouse.move_to(instance.find_elements(css: '.navigation .tasks .task:first-child')[0])
  1133. sleep 0.1
  1134. click_element = instance.find_elements(css: '.navigation .tasks .task:first-child .js-close')[0]
  1135. if click_element
  1136. click_element.click
  1137. # accept task close warning
  1138. if instance.find_elements(css: '.modal button.js-submit')[0]
  1139. sleep 0.4
  1140. instance.find_elements(css: '.modal button.js-submit')[0].click
  1141. end
  1142. end
  1143. else
  1144. break
  1145. end
  1146. rescue
  1147. # try again
  1148. end
  1149. end
  1150. assert(true, 'all tasks closed')
  1151. end
  1152. =begin
  1153. close_online_notitifcation(
  1154. browser: browser1,
  1155. data: {
  1156. #title: 'some title',
  1157. position: 3,
  1158. },
  1159. )
  1160. =end
  1161. def close_online_notitifcation(params = {})
  1162. switch_window_focus(params)
  1163. log('close_online_notitifcation', params)
  1164. instance = params[:browser] || @browser
  1165. data = params[:data]
  1166. if data[:title]
  1167. element = instance.find_elements(partial_link_text: data[:title])[0]
  1168. if !element
  1169. screenshot(browser: instance, comment: 'close_online_notitifcation')
  1170. raise "no online notification with title '#{data[:title]}' found"
  1171. end
  1172. instance.mouse.move_to(element)
  1173. sleep 0.1
  1174. instance.execute_script("$('.js-notificationsContainer .js-items .js-item .activity-text:contains(\"#{data[:title]}\") .js-remove').first().click()")
  1175. else
  1176. css = ".js-notificationsContainer .js-items .js-item:nth-child(#{data[:position]})"
  1177. element = instance.find_elements(css: css)[0]
  1178. if !element
  1179. screenshot(browser: instance, comment: 'close_online_notitifcation')
  1180. raise "no online notification with postion '#{css}' found"
  1181. end
  1182. instance.mouse.move_to(element)
  1183. sleep 0.1
  1184. instance.find_elements(css: "#{css} .js-remove")[0].click
  1185. end
  1186. true
  1187. end
  1188. =begin
  1189. online_notitifcation_close_all(
  1190. browser: browser1,
  1191. )
  1192. =end
  1193. def online_notitifcation_close_all(params = {})
  1194. switch_window_focus(params)
  1195. log('online_notitifcation_close_all', params)
  1196. instance = params[:browser] || @browser
  1197. 99.times do
  1198. sleep 0.5
  1199. begin
  1200. if instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0]
  1201. instance.mouse.move_to(instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0])
  1202. sleep 0.1
  1203. click_element = instance.find_elements(css: '.js-notificationsContainer .js-item:first-child .js-remove')[0]
  1204. if click_element
  1205. click_element.click
  1206. end
  1207. else
  1208. break
  1209. end
  1210. rescue
  1211. # try again
  1212. end
  1213. end
  1214. assert(true, 'all online notification closed')
  1215. end
  1216. =begin
  1217. empty_search(
  1218. browser: browser1,
  1219. )
  1220. =end
  1221. def empty_search(params = {})
  1222. switch_window_focus(params)
  1223. log('empty_search', params)
  1224. instance = params[:browser] || @browser
  1225. # empty search box by x
  1226. begin
  1227. instance.find_elements(css: '.search .js-emptySearch')[0].click
  1228. rescue
  1229. # in issues with ff & selenium, sometimes exeption appears
  1230. # "Element is not currently visible and so may not be interacted with"
  1231. log('empty_search via js')
  1232. instance.execute_script('$(".search .js-emptySearch").click()')
  1233. end
  1234. sleep 0.5
  1235. text = instance.find_elements(css: '#global-search')[0].attribute('value')
  1236. if !text
  1237. raise '#global-search is not empty!'
  1238. end
  1239. true
  1240. end
  1241. =begin
  1242. ticket_customer_select(
  1243. browser: browser1,
  1244. css: '#content .text-1',
  1245. customer: '',
  1246. )
  1247. =end
  1248. def ticket_customer_select(params = {})
  1249. switch_window_focus(params)
  1250. log('ticket_customer_select', params)
  1251. instance = params[:browser] || @browser
  1252. element = instance.find_elements(css: params[:css] + ' input[name="customer_id_completion"]')[0]
  1253. element.click
  1254. element.clear
  1255. element.send_keys(params[:customer])
  1256. sleep 2.5
  1257. element.send_keys(:enter)
  1258. #instance.find_elements(css: params[:css] + ' .recipientList-entry.js-user.is-active')[0].click
  1259. sleep 0.4
  1260. assert(true, 'ticket_customer_select')
  1261. end
  1262. =begin
  1263. overview_create(
  1264. browser: browser1,
  1265. data: {
  1266. name: name,
  1267. role: 'Agent',
  1268. selector: {
  1269. 'Priority': '1 low',
  1270. },
  1271. 'order::direction' => 'down',
  1272. }
  1273. )
  1274. =end
  1275. def overview_create(params)
  1276. switch_window_focus(params)
  1277. log('overview_create', params)
  1278. instance = params[:browser] || @browser
  1279. data = params[:data]
  1280. click(
  1281. browser: instance,
  1282. css: 'a[href="#manage"]',
  1283. mute_log: true,
  1284. )
  1285. click(
  1286. browser: instance,
  1287. css: '.content.active a[href="#manage/overviews"]',
  1288. mute_log: true,
  1289. )
  1290. click(
  1291. browser: instance,
  1292. css: '.content.active a[data-type="new"]',
  1293. mute_log: true,
  1294. )
  1295. modal_ready(browser: instance)
  1296. if data[:name]
  1297. set(
  1298. browser: instance,
  1299. css: '.modal input[name=name]',
  1300. value: data[:name],
  1301. mute_log: true,
  1302. )
  1303. end
  1304. if data[:role]
  1305. select(
  1306. browser: instance,
  1307. css: '.modal select[name="role_id"]',
  1308. value: data[:role],
  1309. mute_log: true,
  1310. )
  1311. end
  1312. if data[:selector]
  1313. data[:selector].each { |key, value|
  1314. select(
  1315. browser: instance,
  1316. css: '.modal .ticket_selector .js-attributeSelector select',
  1317. value: key,
  1318. mute_log: true,
  1319. )
  1320. sleep 0.5
  1321. select(
  1322. browser: instance,
  1323. css: '.modal .ticket_selector .js-value select',
  1324. value: value,
  1325. deselect_all: true,
  1326. mute_log: true,
  1327. )
  1328. }
  1329. end
  1330. if data['order::direction']
  1331. select(
  1332. browser: instance,
  1333. css: '.modal select[name="order::direction"]',
  1334. value: data['order::direction'],
  1335. mute_log: true,
  1336. )
  1337. end
  1338. instance.find_elements(css: '.modal button.js-submit')[0].click
  1339. modal_disappear(browser: instance)
  1340. 11.times {
  1341. element = instance.find_elements(css: 'body')[0]
  1342. text = element.text
  1343. if text =~ /#{Regexp.quote(data[:name])}/
  1344. assert(true, 'overview created')
  1345. overview = {
  1346. name: name,
  1347. }
  1348. sleep 1
  1349. return overview
  1350. end
  1351. sleep 1
  1352. }
  1353. screenshot(browser: instance, comment: 'overview_create_failed')
  1354. raise 'overview creation failed'
  1355. end
  1356. =begin
  1357. overview_update(
  1358. browser: browser1,
  1359. data: {
  1360. name: name,
  1361. role: 'Agent',
  1362. selector: {
  1363. 'Priority': '1 low',
  1364. },
  1365. 'order::direction' => 'down',
  1366. }
  1367. )
  1368. =end
  1369. def overview_update(params)
  1370. switch_window_focus(params)
  1371. log('overview_create', params)
  1372. instance = params[:browser] || @browser
  1373. data = params[:data]
  1374. click(
  1375. browser: instance,
  1376. css: 'a[href="#manage"]',
  1377. mute_log: true,
  1378. )
  1379. click(
  1380. browser: instance,
  1381. css: '.content.active a[href="#manage/overviews"]',
  1382. mute_log: true,
  1383. )
  1384. instance.execute_script("$(\".content.active td:contains('#{data[:name]}')\").first().click()")
  1385. sleep 2
  1386. if data[:name]
  1387. set(
  1388. browser: instance,
  1389. css: '.modal input[name=name]',
  1390. value: data[:name],
  1391. mute_log: true,
  1392. )
  1393. end
  1394. if data[:role]
  1395. select(
  1396. browser: instance,
  1397. css: '.modal select[name="role_id"]',
  1398. value: data[:role],
  1399. mute_log: true,
  1400. )
  1401. end
  1402. if data[:selector]
  1403. data[:selector].each { |key, value|
  1404. select(
  1405. browser: instance,
  1406. css: '.modal .ticket_selector .js-attributeSelector select',
  1407. value: key,
  1408. mute_log: true,
  1409. )
  1410. sleep 0.5
  1411. select(
  1412. browser: instance,
  1413. css: '.modal .ticket_selector .js-value select',
  1414. value: value,
  1415. deselect_all: true,
  1416. mute_log: true,
  1417. )
  1418. }
  1419. end
  1420. if data['order::direction']
  1421. select(
  1422. browser: instance,
  1423. css: '.modal select[name="order::direction"]',
  1424. value: data['order::direction'],
  1425. mute_log: true,
  1426. )
  1427. end
  1428. instance.find_elements(css: '.modal button.js-submit')[0].click
  1429. modal_disappear(browser: instance)
  1430. 11.times {
  1431. element = instance.find_elements(css: 'body')[0]
  1432. text = element.text
  1433. if text =~ /#{Regexp.quote(data[:name])}/
  1434. assert(true, 'overview updated')
  1435. overview = {
  1436. name: name,
  1437. }
  1438. sleep 1
  1439. return overview
  1440. end
  1441. sleep 1
  1442. }
  1443. screenshot(browser: instance, comment: 'overview_update_failed')
  1444. raise 'overview update failed'
  1445. end
  1446. =begin
  1447. ticket = ticket_create(
  1448. browser: browser1,
  1449. data: {
  1450. customer: 'nico',
  1451. group: 'Users', # optional / '-NONE-' # if group selection should not be shown
  1452. priority: '2 normal',
  1453. state: 'open',
  1454. title: 'overview #1',
  1455. body: 'overview #1',
  1456. },
  1457. do_not_submit: true,
  1458. )
  1459. returns (in case of submitted)
  1460. {
  1461. id: 123,
  1462. number: '100001',
  1463. title: 'overview #1',
  1464. }
  1465. ticket = ticket_create(
  1466. browser: browser1,
  1467. data: {
  1468. customer: 'nico',
  1469. group: 'Users', # optional / '-NONE-' # if group selection should not be shown
  1470. priority: '2 normal',
  1471. state: 'open',
  1472. title: 'overview #1',
  1473. body: 'overview #1',
  1474. },
  1475. custom_data_select: {
  1476. key1: 'some value',
  1477. },
  1478. custom_data_input: {
  1479. key1: 'some value',
  1480. },
  1481. disable_group_check: true,
  1482. )
  1483. =end
  1484. def ticket_create(params)
  1485. switch_window_focus(params)
  1486. log('ticket_create', params)
  1487. instance = params[:browser] || @browser
  1488. data = params[:data]
  1489. click(
  1490. browser: instance,
  1491. css: 'a[href="#new"]',
  1492. mute_log: true,
  1493. )
  1494. click(
  1495. browser: instance,
  1496. css: 'a[href="#ticket/create"]',
  1497. mute_log: true,
  1498. )
  1499. found = false
  1500. 7.times {
  1501. element = instance.find_elements(css: '.content.active .newTicket')[0]
  1502. if element
  1503. found = true
  1504. break
  1505. end
  1506. sleep 1
  1507. }
  1508. if !found
  1509. screenshot(browser: instance, comment: 'ticket_create_failed')
  1510. raise 'no ticket create screen found!'
  1511. end
  1512. if data[:group]
  1513. if data[:group] == '-NONE-'
  1514. # check if owner selection exists
  1515. count = instance.find_elements(css: '.content.active .newTicket select[name="group_id"] option').count
  1516. assert_equal(0, count, 'owner selection should not be showm')
  1517. # check count of agents, should be only 3 / - selection + master + agent on init screen
  1518. count = instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').count
  1519. assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
  1520. else
  1521. # check count of agents, should be only 1 / - selection on init screen
  1522. if !params[:disable_group_check]
  1523. count = instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').count
  1524. assert_equal(1, count, 'check if owner selection is empty per default')
  1525. end
  1526. select(
  1527. browser: instance,
  1528. css: '.content.active .newTicket select[name="group_id"]',
  1529. value: data[:group],
  1530. mute_log: true,
  1531. )
  1532. sleep 0.2
  1533. end
  1534. end
  1535. if data[:priority]
  1536. select(
  1537. browser: instance,
  1538. css: '.content.active .newTicket select[name="priority_id"]',
  1539. value: data[:priority],
  1540. mute_log: true,
  1541. )
  1542. end
  1543. if data[:state]
  1544. select(
  1545. browser: instance,
  1546. css: '.content.active .newTicket select[name="state_id"]',
  1547. value: data[:state],
  1548. mute_log: true,
  1549. )
  1550. end
  1551. if data[:title]
  1552. set(
  1553. browser: instance,
  1554. css: '.content.active .newTicket input[name="title"]',
  1555. value: data[:title],
  1556. clear: true,
  1557. mute_log: true,
  1558. )
  1559. end
  1560. if data[:body]
  1561. set(
  1562. browser: instance,
  1563. css: '.content.active .newTicket div[data-name=body]',
  1564. value: data[:body],
  1565. clear: true,
  1566. mute_log: true,
  1567. )
  1568. end
  1569. if data[:customer]
  1570. element = instance.find_elements(css: '.content.active .newTicket input[name="customer_id_completion"]')[0]
  1571. element.click
  1572. element.clear
  1573. # ff issue, sometimes focus event gets dropped
  1574. # if drowdown is not open, try it again
  1575. if !instance.find_elements(css: '.content.active .newTicket .js-recipientDropdown.open')[0]
  1576. instance.execute_script('$(".active .newTicket .js-recipientDropdown").addClass("open")')
  1577. end
  1578. element.send_keys(data[:customer])
  1579. sleep 2.5
  1580. element.send_keys(:enter)
  1581. sleep 0.4
  1582. # ff issue, sometimes enter event gets dropped
  1583. # take user manually
  1584. if instance.find_elements(css: '.content.active .newTicket .js-recipientDropdown.open')[0]
  1585. instance.find_elements(css: '.content.active .newTicket .recipientList-entry.js-user.is-active')[0].click
  1586. sleep 0.4
  1587. end
  1588. end
  1589. if params[:custom_data_select]
  1590. params[:custom_data_select].each { |local_key, local_value|
  1591. select(
  1592. browser: instance,
  1593. css: ".content.active .newTicket select[name=\"#{local_key}\"]",
  1594. value: local_value,
  1595. )
  1596. }
  1597. end
  1598. if params[:custom_data_input]
  1599. params[:custom_data_input].each { |local_key, local_value|
  1600. set(
  1601. browser: instance,
  1602. css: ".content.active .newTicket input[name=\"#{local_key}\"]",
  1603. value: local_value,
  1604. clear: true,
  1605. )
  1606. }
  1607. end
  1608. if data[:attachment]
  1609. file_upload(
  1610. browser: instance,
  1611. css: '.content.active .text-1',
  1612. value: 'some text',
  1613. )
  1614. end
  1615. if params[:do_not_submit]
  1616. assert(true, 'ticket created without submit')
  1617. return
  1618. end
  1619. sleep 0.5
  1620. #instance.execute_script('$(".content.active .newTicket form").submit();')
  1621. click(
  1622. browser: instance,
  1623. css: '.content.active .newTicket button.js-submit',
  1624. mute_log: true,
  1625. )
  1626. sleep 1
  1627. 9.times {
  1628. if instance.current_url =~ /#{Regexp.quote('#ticket/zoom/')}/
  1629. assert(true, 'ticket created')
  1630. sleep 2.5
  1631. id = instance.current_url
  1632. id.gsub!(//,)
  1633. id.gsub!(%r{^.+?/(\d+)$}, '\\1')
  1634. element = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0]
  1635. if element
  1636. number = element.text
  1637. ticket = {
  1638. id: id,
  1639. number: number,
  1640. title: data[:title],
  1641. }
  1642. sleep 3 # wait until notify is gone
  1643. screenshot(browser: instance, comment: 'ticket_create_ok')
  1644. return ticket
  1645. end
  1646. end
  1647. sleep 1
  1648. }
  1649. screenshot(browser: instance, comment: 'ticket_create_failed')
  1650. raise "ticket creation failed, can't get zoom url (current url is '#{instance.current_url}')"
  1651. end
  1652. =begin
  1653. ticket_update(
  1654. browser: browser1,
  1655. data: {
  1656. title: '',
  1657. customer: 'some_customer@example.com',
  1658. body: 'some body',
  1659. group: 'some group', # optional
  1660. priority: '1 low',
  1661. state: 'closed',
  1662. },
  1663. do_not_submit: true,
  1664. )
  1665. ticket_update(
  1666. browser: browser1,
  1667. data: {
  1668. title: '',
  1669. customer: 'some_customer@example.com',
  1670. body: 'some body',
  1671. group: 'some group', # optional
  1672. priority: '1 low',
  1673. state: 'closed',
  1674. },
  1675. custom_data_select: {
  1676. key1: 'some value',
  1677. },
  1678. custom_data_input: {
  1679. key1: 'some value',
  1680. },
  1681. do_not_submit: true,
  1682. )
  1683. =end
  1684. def ticket_update(params)
  1685. switch_window_focus(params)
  1686. log('ticket_update', params)
  1687. instance = params[:browser] || @browser
  1688. data = params[:data]
  1689. if data[:title]
  1690. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
  1691. #element.clear
  1692. #sleep 0.5
  1693. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
  1694. #element.send_keys(data[:title])
  1695. #sleep 0.5
  1696. #element.send_keys(:tab)
  1697. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").focus()')
  1698. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").text("' + data[:title] + '")')
  1699. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").blur()')
  1700. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").trigger("blur")')
  1701. # {
  1702. # :where => :instance2,
  1703. # :execute => 'sendkey',
  1704. # :css => '.content.active .ticketZoom-header .js-objectTitle',
  1705. # :value => 'TTT',
  1706. # },
  1707. # {
  1708. # :where => :instance2,
  1709. # :execute => 'sendkey',
  1710. # :css => '.content.active .ticketZoom-header .js-objectTitle',
  1711. # :value => :tab,
  1712. # },
  1713. end
  1714. if data[:customer]
  1715. # select tab
  1716. click(browser: instance, css: '.content.active .tabsSidebar-tab[data-tab="customer"]')
  1717. click(browser: instance, css: '.content.active div[data-tab="customer"] .js-actions .icon-arrow-down')
  1718. click(browser: instance, css: '.content.active div[data-tab="customer"] .js-actions [data-type="customer-change"]')
  1719. watch_for(
  1720. browser: instance,
  1721. css: '.modal',
  1722. value: 'change',
  1723. )
  1724. element = instance.find_elements(css: '.modal input[name="customer_id_completion"]')[0]
  1725. element.click
  1726. element.clear
  1727. element.send_keys(data[:customer])
  1728. sleep 2.5
  1729. element.send_keys(:enter)
  1730. #instance.find_elements(css: '.modal .user_autocompletion .recipientList-entry.js-user.is-active')[0].click
  1731. sleep 0.2
  1732. click(browser: instance, css: '.modal .js-submit')
  1733. modal_disappear(browser: instance)
  1734. watch_for(
  1735. browser: instance,
  1736. css: '.content.active .tabsSidebar',
  1737. value: data[:customer],
  1738. )
  1739. # select tab
  1740. click(browser: instance, css: '.content.active .tabsSidebar-tab[data-tab="ticket"]')
  1741. end
  1742. if data[:body]
  1743. set(
  1744. browser: instance,
  1745. css: '.content.active div[data-name=body]',
  1746. value: data[:body],
  1747. no_click: true,
  1748. mute_log: true,
  1749. )
  1750. # it's not working stable via selenium, use js
  1751. value = instance.find_elements(css: '.content.active div[data-name=body]')[0].text
  1752. if value != data[:body]
  1753. body_quoted = quote(data[:body])
  1754. instance.execute_script("$('.content.active div[data-name=body]').html('#{body_quoted}').trigger('focusout')")
  1755. end
  1756. end
  1757. if data[:group]
  1758. if data[:group] == '-NONE-'
  1759. # check if owner selection exists
  1760. count = instance.find_elements(css: '.content.active .sidebar select[name="group_id"] option').count
  1761. assert_equal(0, count, 'owner selection should not be showm')
  1762. # check count of agents, should be only 3 / - selection + master + agent on init screen
  1763. count = instance.find_elements(css: '.content.active .sidebar select[name="owner_id"] option').count
  1764. assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
  1765. else
  1766. select(
  1767. browser: instance,
  1768. css: '.content.active .sidebar select[name="group_id"]',
  1769. value: data[:group],
  1770. mute_log: true,
  1771. )
  1772. sleep 0.2
  1773. end
  1774. end
  1775. if data[:priority]
  1776. select(
  1777. browser: instance,
  1778. css: '.content.active .sidebar select[name="priority_id"]',
  1779. value: data[:priority],
  1780. mute_log: true,
  1781. )
  1782. end
  1783. if data[:state]
  1784. select(
  1785. browser: instance,
  1786. css: '.content.active .sidebar select[name="state_id"]',
  1787. value: data[:state],
  1788. mute_log: true,
  1789. )
  1790. end
  1791. if params[:custom_data_select]
  1792. params[:custom_data_select].each { |local_key, local_value|
  1793. select(
  1794. browser: instance,
  1795. css: ".active .sidebar select[name=\"#{local_key}\"]",
  1796. value: local_value,
  1797. )
  1798. }
  1799. end
  1800. if params[:custom_data_input]
  1801. params[:custom_data_input].each { |local_key, local_value|
  1802. set(
  1803. browser: instance,
  1804. css: ".active .sidebar input[name=\"#{local_key}\"]",
  1805. value: local_value,
  1806. clear: true,
  1807. )
  1808. }
  1809. end
  1810. if data[:state] || data[:group] || data[:body] || !params[:custom_data_select].empty? || !params[:custom_data_input].empty?
  1811. found = nil
  1812. 9.times {
  1813. break if found
  1814. begin
  1815. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  1816. if text =~ /(Discard your unsaved changes.|Verwerfen der)/
  1817. found = true
  1818. end
  1819. rescue
  1820. # try again
  1821. end
  1822. sleep 1
  1823. }
  1824. if !found
  1825. screenshot(browser: instance, comment: 'ticket_update_discard_message_failed')
  1826. raise 'no discard message found'
  1827. end
  1828. end
  1829. task_type(
  1830. browser: instance,
  1831. type: 'stayOnTab',
  1832. )
  1833. if params[:do_not_submit]
  1834. assert(true, 'ticket updated without submit')
  1835. return true
  1836. end
  1837. instance.find_elements(css: '.content.active .js-submit')[0].click
  1838. 9.times {
  1839. begin
  1840. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  1841. if !text || text.empty?
  1842. screenshot(browser: instance, comment: 'ticket_update_ok')
  1843. sleep 1
  1844. return true
  1845. end
  1846. rescue
  1847. # try again
  1848. end
  1849. sleep 1
  1850. }
  1851. screenshot(browser: instance, comment: 'ticket_update_failed')
  1852. raise 'unable to update ticket'
  1853. end
  1854. =begin
  1855. ticket_verify(
  1856. browser: browser1,
  1857. data: {
  1858. title: 'some title',
  1859. body: 'some body',
  1860. ## group: 'some group',
  1861. ## state: 'closed',
  1862. custom_data_select: {
  1863. key1: 'some value',
  1864. },
  1865. custom_data_input: {
  1866. key1: 'some value',
  1867. },
  1868. },
  1869. )
  1870. =end
  1871. def ticket_verify(params)
  1872. switch_window_focus(params)
  1873. log('ticket_verify', params)
  1874. instance = params[:browser] || @browser
  1875. data = params[:data]
  1876. if data[:title]
  1877. title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle').first.text.strip
  1878. if title =~ /#{data[:title]}/i
  1879. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  1880. else
  1881. raise "not matching '#{data[:title]}' in title '#{title}'"
  1882. end
  1883. end
  1884. if data[:body]
  1885. body = instance.find_elements(css: '.content.active [data-name="body"]').first.text.strip
  1886. if body =~ /#{data[:body]}/i
  1887. assert(true, "matching '#{data[:body]}' in body '#{body}'")
  1888. else
  1889. raise "not matching '#{data[:body]}' in body '#{body}'"
  1890. end
  1891. end
  1892. if params[:custom_data_select]
  1893. params[:custom_data_select].each { |local_key, local_value|
  1894. element = instance.find_elements(css: ".active .sidebar select[name=\"#{local_key}\"] option[selected]").first
  1895. value = element.text.strip
  1896. if value =~ /#{local_value}/i
  1897. assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
  1898. else
  1899. raise "not matching '#{value}' in #{local_key} '#{local_value}'"
  1900. end
  1901. }
  1902. end
  1903. if params[:custom_data_input]
  1904. params[:custom_data_input].each { |local_key, local_value|
  1905. element = instance.find_elements(css: ".active .sidebar input[name=\"#{local_key}\"]").first
  1906. value = element.text.strip
  1907. if value =~ /#{local_value}/i
  1908. assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
  1909. else
  1910. raise "not matching '#{value}' in #{local_key} '#{local_value}'"
  1911. end
  1912. }
  1913. end
  1914. true
  1915. end
  1916. =begin
  1917. ticket_open_by_overview(
  1918. browser: browser2,
  1919. number: ticket1[:number],
  1920. link: "#ticket/view/#{name}",
  1921. )
  1922. =end
  1923. def ticket_open_by_overview(params)
  1924. switch_window_focus(params)
  1925. log('ticket_open_by_overview', params)
  1926. instance = params[:browser] || @browser
  1927. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  1928. sleep 1
  1929. execute(
  1930. browser: instance,
  1931. js: '$(".content.active .sidebar").css("display", "block")',
  1932. )
  1933. screenshot(browser: instance, comment: 'ticket_open_by_overview')
  1934. instance.find_elements(css: ".content.active .sidebar a[href=\"#{params[:link]}\"]")[0].click
  1935. sleep 1
  1936. execute(
  1937. browser: instance,
  1938. js: '$(".content.active .sidebar").css("display", "none")',
  1939. )
  1940. screenshot(browser: instance, comment: 'ticket_open_by_overview_search')
  1941. instance.find_elements(partial_link_text: params[:number])[0].click
  1942. sleep 1
  1943. number = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0].text
  1944. if number !~ /#{params[:number]}/
  1945. screenshot(browser: instance, comment: 'ticket_open_by_overview_failed')
  1946. raise "unable to search/find ticket #{params[:number]}!"
  1947. end
  1948. sleep 1
  1949. assert(true, "ticket #{params[:number]} found")
  1950. true
  1951. end
  1952. =begin
  1953. ticket_open_by_search(
  1954. browser: browser2,
  1955. number: ticket1[:number],
  1956. )
  1957. =end
  1958. def ticket_open_by_search(params)
  1959. switch_window_focus(params)
  1960. log('ticket_open_by_search', params)
  1961. instance = params[:browser] || @browser
  1962. # search by number
  1963. element = instance.find_elements(css: '#global-search')[0]
  1964. element.click
  1965. element.clear
  1966. element.send_keys(params[:number])
  1967. sleep 3
  1968. empty_search(browser: instance)
  1969. # search by number again
  1970. element = instance.find_elements(css: '#global-search')[0]
  1971. element.click
  1972. element.clear
  1973. element.send_keys(params[:number])
  1974. sleep 1
  1975. # open ticket
  1976. screenshot(browser: instance, comment: 'ticket_open_by_search')
  1977. #instance.find_element(partial_link_text: params[:number] } ).click
  1978. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:number]}') .nav-tab-icon\").first().click()")
  1979. sleep 1
  1980. number = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0].text
  1981. if number !~ /#{params[:number]}/
  1982. screenshot(browser: instance, comment: 'ticket_open_by_search_failed')
  1983. raise "unable to search/find ticket #{params[:number]}!"
  1984. end
  1985. sleep 1
  1986. true
  1987. end
  1988. =begin
  1989. ticket_open_by_title(
  1990. browser: browser2,
  1991. title: ticket1[:title],
  1992. )
  1993. =end
  1994. def ticket_open_by_title(params)
  1995. switch_window_focus(params)
  1996. log('ticket_open_by_title', params)
  1997. instance = params[:browser] || @browser
  1998. # search by number
  1999. element = instance.find_elements(css: '#global-search')[0]
  2000. element.click
  2001. element.clear
  2002. element.send_keys(params[:title])
  2003. sleep 3
  2004. # open ticket
  2005. screenshot(browser: instance, comment: 'ticket_open_by_title_search')
  2006. #instance.find_element(partial_link_text: params[:title] } ).click
  2007. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:title]}') .nav-tab-icon\").click()")
  2008. sleep 1
  2009. title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle')[0].text
  2010. if title !~ /#{params[:title]}/
  2011. screenshot(browser: instance, comment: 'ticket_open_by_title_failed')
  2012. raise "unable to search/find ticket #{params[:title]}!"
  2013. end
  2014. sleep 1
  2015. true
  2016. end
  2017. =begin
  2018. overview_count = overview_counter(
  2019. browser: browser2,
  2020. )
  2021. returns
  2022. {
  2023. '#ticket/view/all_unassigned' => 42,
  2024. }
  2025. =end
  2026. def overview_counter(params = {})
  2027. switch_window_focus(params)
  2028. log('overview_counter', params)
  2029. instance = params[:browser] || @browser
  2030. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  2031. sleep 2
  2032. execute(
  2033. browser: instance,
  2034. js: '$(".content.active .sidebar").css("display", "block")',
  2035. )
  2036. #execute(
  2037. # browser: instance,
  2038. # js: '$(".content.active .overview-header").css("display", "none")',
  2039. #)
  2040. overviews = {}
  2041. instance.find_elements(css: '.content.active .sidebar a[href]').each { |element|
  2042. url = element.attribute('href')
  2043. url.gsub!(%r{(http|https)://.+?/(.+?)$}, '\\2')
  2044. overviews[url] = 0
  2045. #puts url.inspect
  2046. #puts element.inspect
  2047. }
  2048. overviews.each { |url, _value|
  2049. count = instance.find_elements(css: ".content.active .sidebar a[href=\"#{url}\"] .badge")[0].text
  2050. overviews[url] = count.to_i
  2051. }
  2052. log('overview_counter', overviews)
  2053. overviews
  2054. end
  2055. =begin
  2056. organization_open_by_search(
  2057. browser: browser2,
  2058. value: 'some value',
  2059. )
  2060. =end
  2061. def organization_open_by_search(params = {})
  2062. switch_window_focus(params)
  2063. log('organization_open_by_search', params)
  2064. instance = params[:browser] || @browser
  2065. element = instance.find_elements(css: '#global-search')[0]
  2066. element.click
  2067. element.clear
  2068. element.send_keys(params[:value])
  2069. sleep 3
  2070. empty_search(browser: instance)
  2071. element = instance.find_elements(css: '#global-search')[0]
  2072. element.click
  2073. element.clear
  2074. element.send_keys(params[:value])
  2075. sleep 2
  2076. #instance.find_element(partial_link_text: params[:value] } ).click
  2077. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  2078. sleep 1
  2079. name = instance.find_elements(css: '.content.active h1')[0].text
  2080. if name !~ /#{params[:value]}/
  2081. screenshot(browser: instance, comment: 'organization_open_by_search_failed')
  2082. raise "unable to search/find org #{params[:value]}!"
  2083. end
  2084. assert(true, "org #{params[:value]} found")
  2085. sleep 2
  2086. true
  2087. end
  2088. =begin
  2089. user_open_by_search(
  2090. browser: browser2,
  2091. value: 'some value',
  2092. )
  2093. =end
  2094. def user_open_by_search(params = {})
  2095. switch_window_focus(params)
  2096. log('user_open_by_search', params)
  2097. instance = params[:browser] || @browser
  2098. element = instance.find_elements(css: '#global-search')[0]
  2099. element.click
  2100. element.clear
  2101. element.send_keys(params[:value])
  2102. sleep 3
  2103. screenshot(browser: instance, comment: 'user_open_by_search')
  2104. #instance.find_element(partial_link_text: params[:value]).click
  2105. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  2106. sleep 1
  2107. name = instance.find_elements(css: '.content.active h1')[0].text
  2108. if name !~ /#{params[:value]}/
  2109. screenshot(browser: instance, comment: 'user_open_by_search_failed')
  2110. raise "unable to search/find user #{params[:value]}!"
  2111. end
  2112. assert(true, "user #{params[:term]} found")
  2113. sleep 2
  2114. true
  2115. end
  2116. =begin
  2117. user_create(
  2118. browser: browser2,
  2119. data: {
  2120. #login: 'some login' + random,
  2121. firstname: 'Manage Firstname' + random,
  2122. lastname: 'Manage Lastname' + random,
  2123. email: user_email,
  2124. password: 'some-pass',
  2125. },
  2126. )
  2127. =end
  2128. def user_create(params = {})
  2129. switch_window_focus(params)
  2130. log('user_create', params)
  2131. instance = params[:browser] || @browser
  2132. data = params[:data]
  2133. click(
  2134. browser: instance,
  2135. css: 'a[href="#manage"]',
  2136. mute_log: true,
  2137. )
  2138. click(
  2139. browser: instance,
  2140. css: '.content.active a[href="#manage/users"]',
  2141. mute_log: true,
  2142. )
  2143. click(
  2144. browser: instance,
  2145. css: '.content.active a[data-type="new"]',
  2146. mute_log: true,
  2147. )
  2148. modal_ready(browser: instance)
  2149. element = instance.find_elements(css: '.modal input[name=firstname]')[0]
  2150. element.clear
  2151. element.send_keys(data[:firstname])
  2152. element = instance.find_elements(css: '.modal input[name=lastname]')[0]
  2153. element.clear
  2154. element.send_keys(data[:lastname])
  2155. element = instance.find_elements(css: '.modal input[name=email]')[0]
  2156. element.clear
  2157. element.send_keys(data[:email])
  2158. element = instance.find_elements(css: '.modal input[name=password]')[0]
  2159. element.clear
  2160. element.send_keys(data[:password])
  2161. element = instance.find_elements(css: '.modal input[name=password_confirm]')[0]
  2162. element.clear
  2163. element.send_keys(data[:password])
  2164. check(
  2165. browser: instance,
  2166. css: '.modal input[name=role_ids][value=3]',
  2167. )
  2168. instance.find_elements(css: '.modal button.js-submit')[0].click
  2169. modal_disappear(
  2170. browser: instance,
  2171. timeout: 10,
  2172. )
  2173. set(
  2174. browser: instance,
  2175. css: '.content .js-search',
  2176. value: data[:email],
  2177. )
  2178. watch_for(
  2179. browser: instance,
  2180. css: 'body',
  2181. value: data[:lastname],
  2182. )
  2183. assert(true, 'user created')
  2184. end
  2185. =begin
  2186. sla_create(
  2187. browser: browser2,
  2188. data: {
  2189. name: 'some sla' + random,
  2190. first_response_time_in_text: 61
  2191. },
  2192. )
  2193. =end
  2194. def sla_create(params = {})
  2195. switch_window_focus(params)
  2196. log('sla_create', params)
  2197. instance = params[:browser] || @browser
  2198. data = params[:data]
  2199. click(
  2200. browser: instance,
  2201. css: 'a[href="#manage"]',
  2202. mute_log: true,
  2203. )
  2204. click(
  2205. browser: instance,
  2206. css: '.content.active a[href="#manage/slas"]',
  2207. mute_log: true,
  2208. )
  2209. click(
  2210. browser: instance,
  2211. css: '.content.active a.js-new',
  2212. mute_log: true,
  2213. )
  2214. modal_ready(browser: instance)
  2215. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2216. element.clear
  2217. element.send_keys(data[:name])
  2218. element = instance.find_elements(css: '.modal input[name=first_response_time_in_text]')[0]
  2219. element.clear
  2220. element.send_keys(data[:first_response_time_in_text])
  2221. instance.find_elements(css: '.modal button.js-submit')[0].click
  2222. modal_disappear(browser: instance)
  2223. 7.times {
  2224. element = instance.find_elements(css: 'body')[0]
  2225. text = element.text
  2226. if text =~ /#{Regexp.quote(data[:name])}/
  2227. assert(true, 'sla created')
  2228. sleep 1
  2229. return true
  2230. end
  2231. sleep 1
  2232. }
  2233. screenshot(browser: instance, comment: 'sla_create_failed')
  2234. raise 'sla creation failed'
  2235. end
  2236. =begin
  2237. text_module_create(
  2238. browser: browser2,
  2239. data: {
  2240. name: 'some sla' + random,
  2241. keywords: 'some keywords',
  2242. content: 'some content',
  2243. },
  2244. )
  2245. =end
  2246. def text_module_create(params = {})
  2247. switch_window_focus(params)
  2248. log('text_module_create', params)
  2249. instance = params[:browser] || @browser
  2250. data = params[:data]
  2251. click(
  2252. browser: instance,
  2253. css: 'a[href="#manage"]',
  2254. mute_log: true,
  2255. )
  2256. click(
  2257. browser: instance,
  2258. css: '.content.active a[href="#manage/text_modules"]',
  2259. mute_log: true,
  2260. )
  2261. click(
  2262. browser: instance,
  2263. css: '.content.active a[data-type="new"]',
  2264. mute_log: true,
  2265. )
  2266. modal_ready(browser: instance)
  2267. set(
  2268. browser: instance,
  2269. css: '.modal input[name=name]',
  2270. value: data[:name],
  2271. )
  2272. set(
  2273. browser: instance,
  2274. css: '.modal input[name=keywords]',
  2275. value: data[:keywords],
  2276. )
  2277. set(
  2278. browser: instance,
  2279. css: '.modal [data-name=content]',
  2280. value: data[:content],
  2281. )
  2282. instance.find_elements(css: '.modal button.js-submit')[0].click
  2283. modal_disappear(browser: instance)
  2284. 7.times {
  2285. element = instance.find_elements(css: 'body')[0]
  2286. text = element.text
  2287. if text =~ /#{Regexp.quote(data[:name])}/
  2288. assert(true, 'text module created')
  2289. sleep 1
  2290. return true
  2291. end
  2292. sleep 1
  2293. }
  2294. screenshot(browser: instance, comment: 'text_module_create_failed')
  2295. raise 'text module creation failed'
  2296. end
  2297. =begin
  2298. signature_create(
  2299. browser: browser2,
  2300. data: {
  2301. name: 'some sla' + random,
  2302. body: 'some body',
  2303. },
  2304. )
  2305. =end
  2306. def signature_create(params = {})
  2307. switch_window_focus(params)
  2308. log('signature_create', params)
  2309. instance = params[:browser] || @browser
  2310. data = params[:data]
  2311. click(
  2312. browser: instance,
  2313. css: 'a[href="#manage"]',
  2314. mute_log: true,
  2315. )
  2316. click(
  2317. browser: instance,
  2318. css: '.content.active a[href="#channels/email"]',
  2319. mute_log: true,
  2320. )
  2321. click(
  2322. browser: instance,
  2323. css: '.content.active a[href="#c-signature"]',
  2324. mute_log: true,
  2325. )
  2326. sleep 4
  2327. click(
  2328. browser: instance,
  2329. css: '.content.active #c-signature a[data-type="new"]',
  2330. mute_log: true,
  2331. )
  2332. modal_ready(browser: instance)
  2333. set(
  2334. browser: instance,
  2335. css: '.modal input[name=name]',
  2336. value: data[:name],
  2337. )
  2338. set(
  2339. browser: instance,
  2340. css: '.modal [data-name=body]',
  2341. value: data[:body],
  2342. )
  2343. instance.find_elements(css: '.modal button.js-submit')[0].click
  2344. modal_disappear(browser: instance)
  2345. 11.times {
  2346. element = instance.find_elements(css: 'body')[0]
  2347. text = element.text
  2348. if text =~ /#{Regexp.quote(data[:name])}/
  2349. assert(true, 'signature created')
  2350. sleep 1
  2351. return true
  2352. end
  2353. sleep 1
  2354. }
  2355. screenshot(browser: instance, comment: 'signature_create_failed')
  2356. raise 'signature creation failed'
  2357. end
  2358. =begin
  2359. group_create(
  2360. browser: browser2,
  2361. data: {
  2362. name: 'some sla' + random,
  2363. signature: 'some signature bame',
  2364. member: [
  2365. 'some_user_login',
  2366. ],
  2367. },
  2368. )
  2369. =end
  2370. def group_create(params = {})
  2371. switch_window_focus(params)
  2372. log('group_create', params)
  2373. instance = params[:browser] || @browser
  2374. data = params[:data]
  2375. click(
  2376. browser: instance,
  2377. css: 'a[href="#manage"]',
  2378. mute_log: true,
  2379. )
  2380. click(
  2381. browser: instance,
  2382. css: '.content.active a[href="#manage/groups"]',
  2383. mute_log: true,
  2384. )
  2385. click(
  2386. browser: instance,
  2387. css: '.content.active a[data-type="new"]',
  2388. mute_log: true,
  2389. )
  2390. modal_ready(browser: instance)
  2391. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2392. element.clear
  2393. element.send_keys(data[:name])
  2394. element = instance.find_elements(css: '.modal select[name="email_address_id"]')[0]
  2395. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2396. dropdown.select_by(:index, 1)
  2397. #dropdown.select_by(:text, action[:group])
  2398. if data[:signature]
  2399. element = instance.find_elements(css: '.modal select[name="signature_id"]')[0]
  2400. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2401. dropdown.select_by(:text, data[:signature])
  2402. end
  2403. instance.find_elements(css: '.modal button.js-submit')[0].click
  2404. modal_disappear(browser: instance)
  2405. 11.times {
  2406. element = instance.find_elements(css: 'body')[0]
  2407. text = element.text
  2408. if text =~ /#{Regexp.quote(data[:name])}/
  2409. assert(true, 'group created')
  2410. modal_disappear(browser: instance) # wait until modal has gone
  2411. # add member
  2412. if data[:member]
  2413. data[:member].each { |login|
  2414. instance.find_elements(css: 'a[href="#manage"]')[0].click
  2415. sleep 1
  2416. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  2417. sleep 3
  2418. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  2419. element.clear
  2420. element.send_keys(login)
  2421. sleep 3
  2422. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  2423. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  2424. sleep 3
  2425. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  2426. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  2427. instance.find_elements(css: '.modal button.js-submit')[0].click
  2428. modal_disappear(browser: instance)
  2429. }
  2430. end
  2431. end
  2432. sleep 1
  2433. return true
  2434. }
  2435. screenshot(browser: instance, comment: 'group_create_failed')
  2436. raise 'group creation failed'
  2437. end
  2438. =begin
  2439. role_create(
  2440. browser: browser2,
  2441. data: {
  2442. name: 'some role' + random,
  2443. default_at_signup: false,
  2444. permission: [
  2445. 'admin.group',
  2446. 'preferences.password',
  2447. ],
  2448. member: [
  2449. 'some_user_login',
  2450. ],
  2451. },
  2452. )
  2453. =end
  2454. def role_create(params = {})
  2455. switch_window_focus(params)
  2456. log('role_create', params)
  2457. instance = params[:browser] || @browser
  2458. data = params[:data]
  2459. click(
  2460. browser: instance,
  2461. css: 'a[href="#manage"]',
  2462. mute_log: true,
  2463. )
  2464. click(
  2465. browser: instance,
  2466. css: '.content.active a[href="#manage/roles"]',
  2467. mute_log: true,
  2468. )
  2469. click(
  2470. browser: instance,
  2471. css: '.content.active a[data-type="new"]',
  2472. mute_log: true,
  2473. )
  2474. modal_ready(browser: instance)
  2475. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2476. element.clear
  2477. element.send_keys(data[:name])
  2478. if data.key?(:default_at_signup)
  2479. element = instance.find_elements(css: '.modal select[name="default_at_signup"]')[0]
  2480. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2481. if data[:default_at_signup] == true
  2482. dropdown.select_by(:text, 'yes')
  2483. else
  2484. dropdown.select_by(:text, 'no')
  2485. end
  2486. end
  2487. if data.key?(:permission)
  2488. data[:permission].each { |permission_name|
  2489. check(
  2490. browser: instance,
  2491. css: ".modal [data-permission-name=\"#{permission_name}\"]",
  2492. )
  2493. }
  2494. end
  2495. instance.find_elements(css: '.modal button.js-submit')[0].click
  2496. modal_disappear(browser: instance)
  2497. 11.times {
  2498. element = instance.find_elements(css: 'body')[0]
  2499. text = element.text
  2500. if text =~ /#{Regexp.quote(data[:name])}/
  2501. assert(true, 'role created')
  2502. modal_disappear(browser: instance) # wait until modal has gone
  2503. # add member
  2504. if data[:member]
  2505. data[:member].each { |login|
  2506. instance.find_elements(css: 'a[href="#manage"]')[0].click
  2507. sleep 1
  2508. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  2509. sleep 3
  2510. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  2511. element.clear
  2512. element.send_keys(login)
  2513. sleep 3
  2514. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  2515. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  2516. sleep 3
  2517. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  2518. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  2519. instance.find_elements(css: '.modal button.js-submit')[0].click
  2520. modal_disappear(browser: instance)
  2521. }
  2522. end
  2523. end
  2524. sleep 1
  2525. return true
  2526. }
  2527. screenshot(browser: instance, comment: 'role_create_failed')
  2528. raise 'role creation failed'
  2529. end
  2530. =begin
  2531. role_create(
  2532. browser: browser2,
  2533. data: {
  2534. name: 'some role' + random,
  2535. default_at_signup: false,
  2536. permission: [
  2537. 'admin.group',
  2538. 'preferences.password',
  2539. ],
  2540. member: [
  2541. 'some_user_login',
  2542. ],
  2543. },
  2544. )
  2545. =end
  2546. def role_edit(params = {})
  2547. switch_window_focus(params)
  2548. log('role_edit', params)
  2549. instance = params[:browser] || @browser
  2550. data = params[:data]
  2551. click(
  2552. browser: instance,
  2553. css: 'a[href="#manage"]',
  2554. mute_log: true,
  2555. )
  2556. click(
  2557. browser: instance,
  2558. css: '.content.active a[href="#manage/roles"]',
  2559. mute_log: true,
  2560. )
  2561. instance.execute_script('$(\'.content.active table tr td:contains(" ' + data[:name] + '")\').first().click()')
  2562. modal_ready(browser: instance)
  2563. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2564. element.clear
  2565. element.send_keys(data[:name])
  2566. if data.key?(:default_at_signup)
  2567. element = instance.find_elements(css: '.modal select[name="default_at_signup"]')[0]
  2568. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2569. if data[:default_at_signup] == true
  2570. dropdown.select_by(:text, 'yes')
  2571. else
  2572. dropdown.select_by(:text, 'no')
  2573. end
  2574. end
  2575. if data.key?(:permission)
  2576. data[:permission].each { |permission_name|
  2577. check(
  2578. browser: instance,
  2579. css: ".modal [data-permission-name=\"#{permission_name}\"]",
  2580. )
  2581. }
  2582. end
  2583. if data.key?(:active)
  2584. element = instance.find_elements(css: '.modal select[name="active"]')[0]
  2585. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2586. if data[:active] == true
  2587. dropdown.select_by(:text, 'active')
  2588. else
  2589. dropdown.select_by(:text, 'inactive')
  2590. end
  2591. end
  2592. instance.find_elements(css: '.modal button.js-submit')[0].click
  2593. modal_disappear(browser: instance)
  2594. 11.times {
  2595. element = instance.find_elements(css: 'body')[0]
  2596. text = element.text
  2597. if text =~ /#{Regexp.quote(data[:name])}/
  2598. assert(true, 'role created')
  2599. modal_disappear(browser: instance) # wait until modal has gone
  2600. # add member
  2601. if data[:member]
  2602. data[:member].each { |login|
  2603. instance.find_elements(css: 'a[href="#manage"]')[0].click
  2604. sleep 1
  2605. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  2606. sleep 3
  2607. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  2608. element.clear
  2609. element.send_keys(login)
  2610. sleep 3
  2611. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  2612. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  2613. sleep 3
  2614. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  2615. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  2616. instance.find_elements(css: '.modal button.js-submit')[0].click
  2617. modal_disappear(browser: instance)
  2618. }
  2619. end
  2620. end
  2621. sleep 1
  2622. return true
  2623. }
  2624. screenshot(browser: instance, comment: 'role_edit_failed')
  2625. raise 'role edit failed'
  2626. end
  2627. =begin
  2628. object_manager_attribute_create(
  2629. browser: browser2,
  2630. data: {
  2631. name: 'field_name' + random,
  2632. display: 'Display Name of Field',
  2633. data_type: 'Select',
  2634. data_option: {
  2635. options: {
  2636. 'aa' => 'AA',
  2637. 'bb' => 'BB',
  2638. },
  2639. default: 'abc',
  2640. },
  2641. },
  2642. error: 'already exists'
  2643. )
  2644. object_manager_attribute_create(
  2645. browser: browser2,
  2646. data: {
  2647. name: 'field_name' + random,
  2648. display: 'Display Name of Field',
  2649. data_type: 'Text',
  2650. data_option: {
  2651. default: 'abc',
  2652. },
  2653. },
  2654. error: 'already exists'
  2655. )
  2656. object_manager_attribute_create(
  2657. browser: browser2,
  2658. data: {
  2659. name: 'field_name' + random,
  2660. display: 'Display Name of Field',
  2661. data_type: 'Integer',
  2662. data_option: {
  2663. default: '15',
  2664. min: 1,
  2665. max: 999999,
  2666. },
  2667. },
  2668. error: 'already exists'
  2669. )
  2670. object_manager_attribute_create(
  2671. browser: browser2,
  2672. data: {
  2673. name: 'field_name' + random,
  2674. display: 'Display Name of Field',
  2675. data_type: 'Datetime',
  2676. data_option: {
  2677. future: true,
  2678. past: true,
  2679. diff: 24,
  2680. },
  2681. },
  2682. error: 'already exists'
  2683. )
  2684. object_manager_attribute_create(
  2685. browser: browser2,
  2686. data: {
  2687. name: 'field_name' + random,
  2688. display: 'Display Name of Field',
  2689. data_type: 'Date',
  2690. data_option: {
  2691. future: true,
  2692. past: true,
  2693. diff: 24,
  2694. },
  2695. },
  2696. error: 'already exists'
  2697. )
  2698. object_manager_attribute_create(
  2699. browser: browser2,
  2700. data: {
  2701. name: 'field_name' + random,
  2702. display: 'Display Name of Field',
  2703. data_type: 'Boolean',
  2704. data_option: {
  2705. options: {
  2706. true: 'YES',
  2707. false: 'NO',
  2708. }
  2709. default: undefined,
  2710. },
  2711. },
  2712. error: 'already exists'
  2713. )
  2714. =end
  2715. def object_manager_attribute_create(params = {})
  2716. switch_window_focus(params)
  2717. log('object_manager_attribute_create', params)
  2718. instance = params[:browser] || @browser
  2719. data = params[:data]
  2720. click(
  2721. browser: instance,
  2722. css: 'a[href="#manage"]',
  2723. mute_log: true,
  2724. )
  2725. click(
  2726. browser: instance,
  2727. css: '.content.active a[href="#system/object_manager"]',
  2728. mute_log: true,
  2729. )
  2730. sleep 4
  2731. click(
  2732. browser: instance,
  2733. css: '.content.active .js-new',
  2734. mute_log: true,
  2735. )
  2736. modal_ready(browser: instance)
  2737. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2738. element.clear
  2739. element.send_keys(data[:name])
  2740. element = instance.find_elements(css: '.modal input[name=display]')[0]
  2741. element.clear
  2742. element.send_keys(data[:display])
  2743. select(
  2744. browser: instance,
  2745. css: '.modal select[name="data_type"]',
  2746. value: data[:data_type],
  2747. mute_log: true,
  2748. )
  2749. if data[:data_option]
  2750. if data[:data_option][:options]
  2751. if data[:data_type] == 'Boolean'
  2752. element = instance.find_elements(css: '.modal .js-valueTrue').first
  2753. element.clear
  2754. element.send_keys(data[:data_option][:options][:true])
  2755. element = instance.find_elements(css: '.modal .js-valueFalse').first
  2756. element.clear
  2757. element.send_keys(data[:data_option][:options][:false])
  2758. else
  2759. data[:data_option][:options].each { |key, value|
  2760. element = instance.find_elements(css: '.modal .js-Table .js-key').last
  2761. element.clear
  2762. element.send_keys(key)
  2763. element = instance.find_elements(css: '.modal .js-Table .js-value').last
  2764. element.clear
  2765. element.send_keys(value)
  2766. element = instance.find_elements(css: '.modal .js-Table .js-add')[0]
  2767. element.click
  2768. }
  2769. end
  2770. end
  2771. [:default, :min, :max, :diff].each { |key|
  2772. next if !data[:data_option].key?(key)
  2773. element = instance.find_elements(css: ".modal [name=\"data_option::#{key}\"]").first
  2774. element.clear
  2775. element.send_keys(data[:data_option][key])
  2776. }
  2777. [:future, :past].each { |key|
  2778. next if !data[:data_option].key?(key)
  2779. select(
  2780. browser: instance,
  2781. css: ".modal select[name=\"data_option::#{key}\"]",
  2782. value: data[:data_option][key],
  2783. mute_log: true,
  2784. )
  2785. }
  2786. end
  2787. instance.find_elements(css: '.modal button.js-submit')[0].click
  2788. if params[:error]
  2789. sleep 4
  2790. watch_for(
  2791. css: '.modal',
  2792. value: params[:error],
  2793. )
  2794. click(
  2795. browser: instance,
  2796. css: '.modal .js-close',
  2797. )
  2798. modal_disappear(browser: instance)
  2799. return
  2800. end
  2801. 11.times {
  2802. element = instance.find_elements(css: 'body')[0]
  2803. text = element.text
  2804. if text =~ /#{Regexp.quote(data[:name])}/
  2805. assert(true, 'object manager attribute created')
  2806. sleep 1
  2807. return true
  2808. end
  2809. sleep 1
  2810. }
  2811. screenshot(browser: instance, comment: 'object_manager_attribute_create_failed')
  2812. raise 'object manager attribute creation failed'
  2813. end
  2814. =begin
  2815. object_manager_attribute_delete(
  2816. browser: browser2,
  2817. data: {
  2818. name: 'field_name' + random,
  2819. },
  2820. )
  2821. =end
  2822. def object_manager_attribute_delete(params = {})
  2823. switch_window_focus(params)
  2824. log('object_manager_attribute_delete', params)
  2825. instance = params[:browser] || @browser
  2826. data = params[:data]
  2827. click(
  2828. browser: instance,
  2829. css: 'a[href="#manage"]',
  2830. mute_log: true,
  2831. )
  2832. click(
  2833. browser: instance,
  2834. css: '.content.active a[href="#system/object_manager"]',
  2835. mute_log: true,
  2836. )
  2837. sleep 4
  2838. instance = params[:browser] || @browser
  2839. data = params[:data]
  2840. r = instance.execute_script("$(\".content.active td:contains('#{data[:name]}')\").first().closest('tr').find('.js-delete').click()")
  2841. #p "rrr #{r.inspect}"
  2842. end
  2843. =begin
  2844. object_manager_attribute_discard_changes(
  2845. browser: browser2,
  2846. )
  2847. =end
  2848. def object_manager_attribute_discard_changes(params = {})
  2849. switch_window_focus(params)
  2850. log('object_manager_attribute_discard_changes', params)
  2851. instance = params[:browser] || @browser
  2852. click(
  2853. browser: instance,
  2854. css: 'a[href="#manage"]',
  2855. mute_log: true,
  2856. )
  2857. click(
  2858. browser: instance,
  2859. css: '.content.active a[href="#system/object_manager"]',
  2860. mute_log: true,
  2861. )
  2862. sleep 4
  2863. element = instance.find_elements(css: '.content.active .js-discard').first
  2864. element.click
  2865. watch_for_disappear(
  2866. browser: instance,
  2867. css: '.content.active .js-discard',
  2868. )
  2869. end
  2870. =begin
  2871. tags_verify(
  2872. browser: browser2,
  2873. tags: {
  2874. 'tag 1' => true,
  2875. 'tag 2' => true,
  2876. 'tag 3' => false,
  2877. },
  2878. )
  2879. =end
  2880. def tags_verify(params = {})
  2881. switch_window_focus(params)
  2882. log('tags_verify', params)
  2883. instance = params[:browser] || @browser
  2884. tags = instance.find_elements({ css: '.content.active .js-tag' })
  2885. assert(tags)
  2886. assert(tags[0])
  2887. tags_found = {}
  2888. params[:tags].each { |key, _value|
  2889. tags_found[key] = false
  2890. }
  2891. tags.each { |element|
  2892. text = element.text
  2893. if tags_found.key?(text)
  2894. tags_found[text] = true
  2895. else
  2896. assert(false, "tag exists but is not in check to verify '#{text}'")
  2897. end
  2898. }
  2899. params[:tags].each { |key, value|
  2900. assert_equal(value, tags_found[key], "tag '#{key}'")
  2901. }
  2902. end
  2903. def quote(string)
  2904. string_quoted = string
  2905. string_quoted.gsub!(/&/, '&amp;')
  2906. string_quoted.gsub!(/</, '&lt;')
  2907. string_quoted.gsub!(/>/, '&gt;')
  2908. string_quoted
  2909. end
  2910. def switch_window_focus(params)
  2911. instance = params[:browser] || @browser
  2912. if instance != @last_used_browser
  2913. log('switch browser window focus', {})
  2914. instance.switch_to.window(instance.window_handles.first)
  2915. end
  2916. @last_used_browser = instance
  2917. end
  2918. def log(method, params = {})
  2919. begin
  2920. instance = params[:browser] || @browser
  2921. if instance
  2922. logs = instance.manage.logs.get(:browser)
  2923. logs.each { |log|
  2924. next if log.level == 'WARNING' && log.message =~ /Declaration\sdropped./ # ignore ff css warnings
  2925. time = Time.zone.parse(Time.zone.at(log.timestamp / 1000).to_datetime.to_s)
  2926. puts "#{time}/#{log.level}: #{log.message}"
  2927. }
  2928. end
  2929. rescue
  2930. # faild to get logs
  2931. end
  2932. return if !@@debug
  2933. return if params[:mute_log]
  2934. puts "#{Time.zone.now}/#{method}: #{params.inspect}"
  2935. end
  2936. end