browser_test_helper.rb 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522
  1. ENV['RAILS_ENV'] = 'test'
  2. # rubocop:disable HandleExceptions, NonLocalExitFromIterator, Style/GuardClause, Lint/MissingCopEnableDirective
  3. require File.expand_path('../config/environment', __dir__)
  4. require 'selenium-webdriver'
  5. require 'json'
  6. require 'net/http'
  7. require 'uri'
  8. class TestCase < Test::Unit::TestCase
  9. DEBUG = true
  10. def browser
  11. ENV['BROWSER'] || 'firefox'
  12. end
  13. def profile
  14. browser_profile = nil
  15. if browser == 'firefox'
  16. browser_profile = Selenium::WebDriver::Firefox::Profile.new
  17. browser_profile['intl.locale.matchOS'] = false
  18. browser_profile['intl.accept_languages'] = 'en-US'
  19. browser_profile['general.useragent.locale'] = 'en-US'
  20. # currently console log not working for firefox
  21. # https://github.com/SeleniumHQ/selenium/issues/1161
  22. #browser_profile['loggingPref'] = { browser: :all }
  23. elsif browser == 'chrome'
  24. # profile are only working on remote selenium
  25. if ENV['REMOTE_URL']
  26. browser_profile = Selenium::WebDriver::Chrome::Profile.new
  27. browser_profile['intl.accept_languages'] = 'en'
  28. browser_profile['loggingPref'] = { browser: :all }
  29. end
  30. end
  31. browser_profile
  32. end
  33. def browser_support_cookies
  34. if browser.match?(/(internet_explorer|ie)/i)
  35. return false
  36. end
  37. true
  38. end
  39. def browser_url
  40. ENV['BROWSER_URL'] || 'http://localhost:3000'
  41. end
  42. def browser_instance
  43. @browsers ||= {}
  44. if ENV['REMOTE_URL'].blank?
  45. params = {
  46. profile: profile,
  47. }
  48. if ENV['BROWSER_HEADLESS'].present?
  49. if browser == 'firefox'
  50. params[:options] = Selenium::WebDriver::Firefox::Options.new
  51. params[:options].add_argument('-headless')
  52. elsif browser == 'chrome'
  53. params[:options] = Selenium::WebDriver::Chrome::Options.new
  54. params[:options].add_argument('-headless')
  55. end
  56. end
  57. local_browser = Selenium::WebDriver.for(browser.to_sym, params)
  58. @browsers[local_browser.hash] = local_browser
  59. browser_instance_preferences(local_browser)
  60. return local_browser
  61. end
  62. # avoid "Cannot read property 'get_Current' of undefined" issues
  63. (1..5).each do |count|
  64. begin
  65. local_browser = browser_instance_remote
  66. break
  67. rescue
  68. wait_until_ready = rand(5..13)
  69. sleep wait_until_ready
  70. log('browser_instance', { rescure: true, count: count, sleep: wait_until_ready })
  71. end
  72. end
  73. local_browser
  74. end
  75. def browser_instance_remote
  76. caps = Selenium::WebDriver::Remote::Capabilities.send(browser)
  77. if ENV['BROWSER_OS']
  78. caps.platform = ENV['BROWSER_OS']
  79. end
  80. if ENV['BROWSER_VERSION']
  81. caps.version = ENV['BROWSER_VERSION']
  82. end
  83. local_browser = Selenium::WebDriver.for(
  84. :remote,
  85. url: ENV['REMOTE_URL'],
  86. desired_capabilities: caps,
  87. )
  88. @browsers[local_browser.hash] = local_browser
  89. browser_instance_preferences(local_browser)
  90. # upload files from remote dir
  91. local_browser.file_detector = lambda do |args|
  92. str = args.first.to_s
  93. str if File.file?(str)
  94. end
  95. local_browser
  96. end
  97. def browser_instance_close(local_browser)
  98. return if !@browsers[local_browser.hash]
  99. @browsers.delete(local_browser.hash)
  100. local_browser.quit
  101. end
  102. def browser_instance_preferences(local_browser)
  103. browser_width = ENV['BROWSER_WIDTH'] || 1024
  104. browser_height = ENV['BROWSER_HEIGHT'] || 800
  105. local_browser.manage.window.resize_to(browser_width, browser_height)
  106. if !ENV['REMOTE_URL']&.match?(/saucelabs|(grid|ci)\.(zammad\.org|znuny\.com)/i)
  107. if @browsers.count == 1
  108. local_browser.manage.window.move_to(0, 0)
  109. else
  110. local_browser.manage.window.move_to(browser_width, 0)
  111. end
  112. end
  113. local_browser.manage.timeouts.implicit_wait = 3 # seconds
  114. end
  115. def teardown
  116. return if !@browsers
  117. @browsers.each_value do |local_browser|
  118. screenshot(browser: local_browser, comment: 'teardown')
  119. browser_instance_close(local_browser)
  120. end
  121. end
  122. def screenshot(params = {})
  123. instance = params[:browser] || @browser
  124. comment = params[:comment] || ''
  125. filename = "tmp/#{Time.zone.now.strftime('screenshot_%Y_%m_%d__%H_%M_%S_%L')}_#{comment}#{instance.hash}.png"
  126. log('screenshot', { filename: filename })
  127. instance.save_screenshot(filename)
  128. end
  129. =begin
  130. username = login(
  131. browser: browser1,
  132. username: 'someuser',
  133. password: 'somepassword',
  134. url: 'some url', # optional, in case of aleady opened brower a reload is done because url is called again
  135. remember_me: true, # optional
  136. auto_wizard: false, # optional, in case of auto wizard, skip login
  137. success: false, #optional
  138. )
  139. =end
  140. def login(params)
  141. switch_window_focus(params)
  142. log('login', params)
  143. instance = params[:browser] || @browser
  144. if params[:url]
  145. instance.get(params[:url])
  146. end
  147. # submit logs anyway
  148. instance.execute_script('App.Track.force()')
  149. element = instance.find_elements(css: '#login input[name="username"]')[0]
  150. if !element
  151. if params[:auto_wizard]
  152. watch_for(
  153. browser: instance,
  154. css: 'body',
  155. value: 'auto wizard is enabled',
  156. timeout: 10,
  157. )
  158. location(url: "#{browser_url}/#getting_started/auto_wizard")
  159. sleep 10
  160. login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
  161. if login != params[:username]
  162. screenshot(browser: instance, comment: 'auto wizard login failed')
  163. raise 'auto wizard login failed'
  164. end
  165. assert(true, 'auto wizard login ok')
  166. clues_close(
  167. browser: instance,
  168. optional: true,
  169. )
  170. return
  171. end
  172. screenshot(browser: instance, comment: 'login_failed')
  173. raise 'No login box found'
  174. end
  175. element.clear
  176. element.send_keys(params[:username])
  177. element = instance.find_elements(css: '#login input[name="password"]')[0]
  178. element.clear
  179. element.send_keys(params[:password])
  180. if params[:remember_me]
  181. instance.find_elements(css: '#login .checkbox-replacement')[0].click
  182. end
  183. instance.find_elements(css: '#login button')[0].click
  184. sleep 4
  185. login_failed = false
  186. if !instance.find_elements(css: '.user-menu .user a')[0]
  187. login_failed = true
  188. else
  189. login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
  190. if login != params[:username]
  191. login_failed = true
  192. end
  193. end
  194. if login_failed
  195. if params[:success] == false
  196. assert(true, 'login not successfull, like wanted')
  197. return true
  198. end
  199. screenshot(browser: instance, comment: 'login_failed')
  200. raise 'login failed'
  201. end
  202. if params[:success] == false
  203. raise 'login successfull but should not'
  204. end
  205. clues_close(
  206. browser: instance,
  207. optional: true,
  208. )
  209. assert(true, 'login ok')
  210. login
  211. end
  212. =begin
  213. logout(
  214. browser: browser1
  215. )
  216. =end
  217. def logout(params = {})
  218. switch_window_focus(params)
  219. log('logout', params)
  220. instance = params[:browser] || @browser
  221. click(
  222. browser: instance,
  223. css: 'a[href="#current_user"]',
  224. mute_log: true,
  225. )
  226. click(
  227. browser: instance,
  228. css: 'a[href="#logout"]',
  229. mute_log: true,
  230. )
  231. 5.times do
  232. sleep 1
  233. login = instance.find_elements(css: '#login')[0]
  234. next if !login
  235. assert(true, 'logout ok')
  236. return
  237. end
  238. screenshot(browser: instance, comment: 'logout_failed')
  239. raise 'no login box found, seems logout was not successfully!'
  240. end
  241. =begin
  242. clues_close(
  243. browser: browser1,
  244. optional: false,
  245. )
  246. =end
  247. def clues_close(params = {})
  248. switch_window_focus(params)
  249. log('clues_close', params)
  250. instance = params[:browser] || @browser
  251. clues = instance.find_elements(css: '.js-modal--clue .js-close')[0]
  252. if !params[:optional] && !clues
  253. screenshot(browser: instance, comment: 'no_clues')
  254. raise 'Unable to closes clues, no clues found!'
  255. end
  256. return if !clues
  257. instance.execute_script("$('.js-modal--clue .js-close').click()")
  258. assert(true, 'clues closed')
  259. sleep 1
  260. end
  261. =begin
  262. notify_close(
  263. browser: browser1,
  264. optional: false,
  265. )
  266. =end
  267. def notify_close(params = {})
  268. switch_window_focus(params)
  269. log('notify_close', params)
  270. instance = params[:browser] || @browser
  271. notify = instance.find_elements(css: '.noty_inline_layout_container.i-am-new')[0]
  272. if !params[:optional] && !notify
  273. screenshot(browser: instance, comment: 'no_notify')
  274. raise 'Unable to closes notify, no notify found!'
  275. end
  276. return if !notify
  277. notify.click
  278. assert(true, 'notify closed')
  279. sleep 1
  280. end
  281. =begin
  282. location(
  283. browser: browser1,
  284. url: 'http://someurl',
  285. )
  286. =end
  287. def location(params)
  288. switch_window_focus(params)
  289. log('location', params)
  290. instance = params[:browser] || @browser
  291. instance.get(params[:url])
  292. # check if reload was successfull
  293. if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
  294. instance.navigate.refresh
  295. end
  296. end
  297. =begin
  298. location_check(
  299. browser: browser1,
  300. url: 'http://someurl',
  301. )
  302. =end
  303. def location_check(params)
  304. switch_window_focus(params)
  305. log('location_check', params)
  306. instance = params[:browser] || @browser
  307. sleep 0.7
  308. current_url = instance.current_url
  309. if !current_url.match?(/#{Regexp.quote(params[:url])}/)
  310. screenshot(browser: instance, comment: 'location_check_failed')
  311. raise "url #{current_url} is not matching #{params[:url]}"
  312. end
  313. assert(true, "url #{current_url} is matching #{params[:url]}")
  314. end
  315. =begin
  316. reload(
  317. browser: browser1,
  318. )
  319. =end
  320. def reload(params = {})
  321. switch_window_focus(params)
  322. log('reload', params)
  323. instance = params[:browser] || @browser
  324. instance.navigate.refresh
  325. # check if reload was successfull
  326. if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
  327. instance.navigate.refresh
  328. end
  329. screenshot(browser: instance, comment: 'reload_after')
  330. end
  331. =begin
  332. click(
  333. browser: browser1,
  334. css: '.some_class',
  335. fast: false, # do not wait
  336. wait: 1, # wait 1 sec.
  337. )
  338. click(
  339. browser: browser1,
  340. xpath: '//a[contains(@class,".text-1")]',
  341. fast: false, # do not wait
  342. wait: 1, # wait 1 sec.
  343. )
  344. click(
  345. browser: browser1,
  346. text: '.partial_link_text',
  347. fast: false, # do not wait
  348. wait: 1, # wait 1 sec.
  349. )
  350. =end
  351. def click(params)
  352. switch_window_focus(params)
  353. log('click', params)
  354. instance = params[:browser] || @browser
  355. if params.include?(:css)
  356. param_key = :css
  357. find_element_key = :css
  358. elsif params.include?(:xpath)
  359. param_key = :xpath
  360. find_element_key = :xpath
  361. else
  362. param_key = :text
  363. find_element_key = :partial_link_text
  364. sleep 0.5
  365. end
  366. begin
  367. elements = instance.find_elements(find_element_key => params[param_key])
  368. .tap { |e| e.slice!(1..-1) unless params[:all] }
  369. if elements.empty?
  370. return if params[:only_if_exists] == true
  371. raise "No such element '#{params[param_key]}'"
  372. end
  373. # a clumsy substitute for elements.each(&:click)
  374. # (we need to refresh element references after each element.click
  375. # because if clicks alter page content,
  376. # subsequent element.clicks will raise a StaleElementReferenceError)
  377. elements.length.times do |i|
  378. instance.find_elements(find_element_key => params[param_key])[i].try(:click)
  379. end
  380. rescue => e
  381. raise e if (fail_count ||= 0).positive?
  382. fail_count += 1
  383. log('click', { rescure: true })
  384. sleep 0.5
  385. retry
  386. end
  387. sleep 0.2 if !params[:fast]
  388. sleep params[:wait] if params[:wait]
  389. end
  390. =begin
  391. perform_macro('Close & Tag as Spam')
  392. # or
  393. perform_macro(
  394. name: 'Close & Tag as Spam',
  395. browser: browser1,
  396. )
  397. =end
  398. def perform_macro(params)
  399. switch_window_focus(params)
  400. log('perform_macro', params)
  401. instance = params[:browser] || @browser
  402. click(
  403. browser: instance,
  404. css: '.active.content .js-submitDropdown .js-openDropdownMacro'
  405. )
  406. click(
  407. browser: instance,
  408. xpath: "//div[contains(@class, 'content') and contains(@class, 'active')]//li[contains(@class, 'js-dropdownActionMacro') and contains(text(), '#{params[:name]}')]"
  409. )
  410. end
  411. =begin
  412. scroll_to(
  413. browser: browser1,
  414. position: 'top', # botton
  415. css: '.some_class',
  416. )
  417. =end
  418. def scroll_to(params)
  419. switch_window_focus(params)
  420. log('scroll_to', params)
  421. instance = params[:browser] || @browser
  422. position = 'true'
  423. if params[:position] == 'botton'
  424. position = 'false'
  425. end
  426. execute(
  427. browser: instance,
  428. js: "\$('#{params[:css]}').get(0).scrollIntoView(#{position})",
  429. mute_log: params[:mute_log]
  430. )
  431. sleep 0.3
  432. screenshot(browser: instance, comment: 'scroll_to_after')
  433. end
  434. =begin
  435. modal_close(
  436. browser: browser1,
  437. )
  438. =end
  439. def modal_close(params = {})
  440. switch_window_focus(params)
  441. log('modal_close', params)
  442. instance = params[:browser] || @browser
  443. element = instance.find_elements(css: '.modal .js-close')[0]
  444. raise "No such modal to close #{params.inspect}" if !element
  445. element.click
  446. end
  447. =begin
  448. modal_ready(
  449. browser: browser1,
  450. )
  451. =end
  452. def modal_ready(params = {})
  453. switch_window_focus(params)
  454. log('modal_ready', params)
  455. instance = params[:browser] || @browser
  456. watch_for(
  457. browser: instance,
  458. css: '.modal.in',
  459. timeout: params[:timeout] || 4,
  460. )
  461. end
  462. =begin
  463. modal_disappear(
  464. browser: browser1,
  465. timeout: 12, # default 8
  466. )
  467. =end
  468. def modal_disappear(params = {})
  469. switch_window_focus(params)
  470. log('modal_disappear', params)
  471. instance = params[:browser] || @browser
  472. watch_for_disappear(
  473. browser: instance,
  474. css: '.modal',
  475. timeout: params[:timeout] || 8,
  476. )
  477. end
  478. =begin
  479. execute(
  480. browser: browser1,
  481. js: '.some_class',
  482. )
  483. =end
  484. def execute(params)
  485. switch_window_focus(params)
  486. log('js', params)
  487. instance = params[:browser] || @browser
  488. if params[:js]
  489. return instance.execute_script(params[:js])
  490. end
  491. raise "Invalid execute params #{params.inspect}"
  492. end
  493. =begin
  494. exists(
  495. browser: browser1,
  496. css: '.some_class',
  497. )
  498. exists(
  499. displayed: false, # true|false
  500. browser: browser1,
  501. css: '.some_class',
  502. displayed: true, # true|false
  503. )
  504. =end
  505. def exists(params)
  506. retries ||= 0
  507. switch_window_focus(params)
  508. log('exists', params)
  509. instance = params[:browser] || @browser
  510. if !instance.find_elements(css: params[:css])[0]
  511. screenshot(browser: instance, comment: 'exists_failed')
  512. raise "#{params[:css]} dosn't exist, but should"
  513. end
  514. if params.key?(:displayed)
  515. if params[:displayed] == true && !instance.find_elements(css: params[:css])[0].displayed?
  516. raise "#{params[:css]} is not displayed, but should"
  517. end
  518. if params[:displayed] == false && instance.find_elements(css: params[:css])[0].displayed?
  519. raise "#{params[:css]} is displayed, but should not"
  520. end
  521. end
  522. true
  523. rescue Selenium::WebDriver::Error::StaleElementReferenceError
  524. sleep retries
  525. retries += 1
  526. retry if retries < 3
  527. end
  528. =begin
  529. exists_not(
  530. browser: browser1,
  531. css: '.some_class',
  532. )
  533. =end
  534. def exists_not(params)
  535. switch_window_focus(params)
  536. log('exists_not', params)
  537. instance = params[:browser] || @browser
  538. if instance.find_elements(css: params[:css])[0]
  539. screenshot(browser: instance, comment: 'exists_not_failed')
  540. raise "#{params[:css]} exists but should not"
  541. end
  542. true
  543. end
  544. =begin
  545. set(
  546. browser: browser1,
  547. css: '.some_class',
  548. value: true,
  549. slow: false,
  550. blur: true, # default false
  551. clear: true, # todo | default: true
  552. no_click: true,
  553. )
  554. =end
  555. def set(params)
  556. switch_window_focus(params)
  557. log('set', params)
  558. instance = params[:browser] || @browser
  559. element = instance.find_elements(css: params[:css])[0]
  560. if !params[:no_click]
  561. element.click
  562. end
  563. element.clear
  564. begin
  565. if !params[:slow]
  566. element.send_keys(params[:value])
  567. else
  568. element.send_keys('')
  569. keys = params[:value].to_s.split('')
  570. keys.each do |key|
  571. instance.action.send_keys(key).perform
  572. end
  573. end
  574. rescue => e
  575. sleep 0.5
  576. # just try again
  577. log('set', { rescure: true })
  578. element = instance.find_elements(css: params[:css])[0]
  579. raise "No such element '#{params[:css]}'" if !element
  580. if !params[:slow]
  581. element.send_keys(params[:value])
  582. else
  583. element.send_keys('')
  584. keys = params[:value].to_s.split('')
  585. keys.each do |key|
  586. instance.action.send_keys(key).perform
  587. end
  588. end
  589. end
  590. # it's not working stable with ff via selenium, use js
  591. if browser =~ /firefox/i && params[:css] =~ /\[data-name=/
  592. log('set_ff_trigger_workaround', params)
  593. instance.execute_script("$('#{params[:css]}').trigger('focusout')")
  594. end
  595. if params[:blur]
  596. instance.execute_script("$('#{params[:css]}').blur()")
  597. end
  598. sleep 0.2
  599. end
  600. =begin
  601. select(
  602. browser: browser1,
  603. css: '.some_class',
  604. value: 'Some Value',
  605. deselect_all: false, # default false
  606. )
  607. =end
  608. def select(params)
  609. switch_window_focus(params)
  610. log('select', params)
  611. instance = params[:browser] || @browser
  612. # searchable select
  613. element = instance.find_elements(css: "#{params[:css]}.js-shadow")[0]
  614. if element
  615. element = instance.find_elements(css: "#{params[:css]}.js-shadow + .js-input")[0]
  616. element.click
  617. element.clear
  618. sleep 0.2
  619. element.send_keys(params[:value])
  620. sleep 0.2
  621. element.send_keys(:enter)
  622. sleep 0.2
  623. return
  624. end
  625. # native select
  626. begin
  627. element = instance.find_elements(css: params[:css])[0]
  628. dropdown = Selenium::WebDriver::Support::Select.new(element)
  629. if params[:deselect_all]
  630. dropdown.deselect_all
  631. end
  632. dropdown.select_by(:text, params[:value])
  633. #puts "select - #{params.inspect}"
  634. rescue
  635. sleep 0.4
  636. # just try again
  637. log('select', { rescure: true })
  638. element = instance.find_elements(css: params[:css])[0]
  639. dropdown = Selenium::WebDriver::Support::Select.new(element)
  640. if params[:deselect_all]
  641. dropdown.deselect_all
  642. end
  643. dropdown.select_by(:text, params[:value])
  644. #puts "select2 - #{params.inspect}"
  645. end
  646. end
  647. =begin
  648. switch(
  649. browser: browser1,
  650. css: '.some_class',
  651. type: 'on', # 'off'
  652. no_check: true, # do not check is switch has changed, in case if js alert
  653. )
  654. =end
  655. def switch(params)
  656. switch_window_focus(params)
  657. log('switch', params)
  658. instance = params[:browser] || @browser
  659. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  660. checked = element.attribute('checked')
  661. if !checked
  662. if params[:type] == 'on'
  663. instance.find_elements(css: "#{params[:css]} label")[0].click
  664. sleep 2
  665. if params[:no_check] != true
  666. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  667. checked = element.attribute('checked')
  668. raise 'Switch not on!' if !checked
  669. end
  670. end
  671. elsif params[:type] == 'off'
  672. instance.find_elements(css: "#{params[:css]} label")[0].click
  673. sleep 2
  674. if params[:no_check] != true
  675. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  676. checked = element.attribute('checked')
  677. raise 'Switch not off!' if checked
  678. end
  679. end
  680. end
  681. =begin
  682. check(
  683. browser: browser1,
  684. css: '.some_class',
  685. )
  686. =end
  687. def check(params)
  688. switch_window_focus(params)
  689. log('check', params)
  690. instance = params[:browser] || @browser
  691. instance.execute_script("$('#{params[:css]}:not(:checked)').click()")
  692. #element = instance.find_elements(css: params[:css])[0]
  693. #checked = element.attribute('checked')
  694. #element.click if !checked
  695. end
  696. =begin
  697. uncheck(
  698. browser: browser1,
  699. css: '.some_class',
  700. )
  701. =end
  702. def uncheck(params)
  703. switch_window_focus(params)
  704. log('uncheck', params)
  705. instance = params[:browser] || @browser
  706. instance.execute_script("$('#{params[:css]}:checked').click()")
  707. #element = instance.find_elements(css: params[:css])[0]
  708. #checked = element.attribute('checked')
  709. #element.click if checked
  710. end
  711. =begin
  712. sendkey(
  713. browser: browser1,
  714. value: :enter,
  715. slow: false, # default false
  716. )
  717. =end
  718. def sendkey(params)
  719. switch_window_focus(params)
  720. log('sendkey', params)
  721. instance = params[:browser] || @browser
  722. element = nil
  723. if params[:css]
  724. element = instance.find_elements(css: params[:css])[0]
  725. end
  726. if params[:value].class == Array
  727. params[:value].each do |key|
  728. if element
  729. element.send_keys(key)
  730. else
  731. instance.action.send_keys(key).perform
  732. end
  733. end
  734. return
  735. end
  736. if element
  737. element.send_keys(params[:value])
  738. else
  739. instance.action.send_keys(params[:value]).perform
  740. end
  741. if params[:slow]
  742. sleep 1.5
  743. else
  744. sleep 0.2
  745. end
  746. end
  747. =begin
  748. match(
  749. browser: browser1,
  750. css: '#content .text-1',
  751. value: 'some test for browser and some other for browser',
  752. attribute: 'some_attribute', # match on attribute
  753. should_not_match: true,
  754. no_quote: false, # use regex
  755. )
  756. =end
  757. def match(params, fallback = false)
  758. switch_window_focus(params)
  759. log('match', params)
  760. instance = params[:browser] || @browser
  761. element = instance.find_elements(css: params[:css])[0]
  762. if params[:css].match?(/select/)
  763. dropdown = Selenium::WebDriver::Support::Select.new(element)
  764. success = false
  765. dropdown.selected_options&.each do |option|
  766. if option.text == params[:value]
  767. success = true
  768. end
  769. end
  770. if params[:should_not_match]
  771. if success
  772. screenshot(browser: instance, comment: 'match_failed')
  773. raise "should not match '#{params[:value]}' in select list, but is matching"
  774. end
  775. elsif !success
  776. screenshot(browser: instance, comment: 'match_failed')
  777. raise "not matching '#{params[:value]}' in select list"
  778. end
  779. return true
  780. end
  781. # match on attribute
  782. begin
  783. text = if params[:attribute]
  784. element.attribute(params[:attribute])
  785. elsif params[:css].match?(/(input|textarea)/i)
  786. element.attribute('value')
  787. else
  788. element.text
  789. end
  790. rescue => e
  791. # just try again
  792. if !fallback
  793. return match(params, true)
  794. end
  795. raise e.inspect
  796. end
  797. # do cleanups (needed for richtext tests)
  798. if params[:cleanup]
  799. text.gsub!(/\s+$/m, '')
  800. params[:value].gsub!(/\s+$/m, '')
  801. end
  802. match = false
  803. if params[:no_quote]
  804. #puts "aaaa #{text}/#{params[:value]}"
  805. if text =~ /#{params[:value]}/i
  806. match = $1 || true
  807. end
  808. elsif text.match?(/#{Regexp.quote(params[:value])}/i)
  809. match = true
  810. end
  811. if match
  812. if params[:should_not_match]
  813. screenshot(browser: instance, comment: 'match_failed')
  814. raise "matching '#{params[:value]}' in content '#{text}' but should not!"
  815. end
  816. elsif !params[:should_not_match]
  817. screenshot(browser: instance, comment: 'match_failed')
  818. raise "not matching '#{params[:value]}' in content '#{text}' but should!"
  819. end
  820. sleep 0.2
  821. match
  822. end
  823. =begin
  824. match_not(
  825. browser: browser1,
  826. css: '#content .text-1',
  827. value: 'some test for browser and some other for browser',
  828. attribute: 'some_attribute', # match on attribute
  829. should_not_match: true,
  830. no_quote: false, # use regex
  831. )
  832. =end
  833. def match_not(params)
  834. switch_window_focus(params)
  835. log('match_not', params)
  836. params[:should_not_match] = true
  837. match(params)
  838. end
  839. =begin
  840. Get the on-screen pixel coordinates of a given DOM element. Can be used to compare
  841. the relative location of table rows before and after sort, for example.
  842. Returns a Selenium::WebDriver::Point object. Use result.x and result.y to access
  843. its X and Y coordinates respectively.
  844. get_location(
  845. browser: browser1,
  846. css: '.some_class',
  847. )
  848. =end
  849. def get_location(params)
  850. switch_window_focus(params)
  851. log('exists', params)
  852. instance = params[:browser] || @browser
  853. if params[:css]
  854. query = { css: params[:css] }
  855. end
  856. if params[:xpath]
  857. query = { xpath: params[:xpath] }
  858. end
  859. if !instance.find_elements(query)[0]
  860. screenshot(browser: instance, comment: 'exists_failed')
  861. raise "#{query} dosn't exist, but should"
  862. end
  863. instance.find_elements(query)[0].location
  864. end
  865. =begin
  866. set type of task (closeTab, closeNextInOverview, stayOnTab)
  867. task_type(
  868. browser: browser1,
  869. type: 'stayOnTab',
  870. )
  871. =end
  872. def task_type(params)
  873. switch_window_focus(params)
  874. log('task_type', params)
  875. instance = params[:browser] || @browser
  876. if params[:type]
  877. instance.find_elements(css: '.content.active .js-secondaryActionButtonLabel')[0].click
  878. instance.find_elements(css: ".content.active .js-secondaryActionLabel[data-type=#{params[:type]}]")[0].click
  879. return
  880. end
  881. raise "Unknown params for task_type: #{params.inspect}"
  882. end
  883. =begin
  884. cookie(
  885. browser: browser1,
  886. name: '^_zammad.+?',
  887. value: '.+?',
  888. expires: nil,
  889. )
  890. cookie(
  891. browser: browser1,
  892. name: '^_zammad.+?',
  893. should_not_exist: true,
  894. )
  895. =end
  896. def cookie(params)
  897. switch_window_focus(params)
  898. log('cookie', params)
  899. instance = params[:browser] || @browser
  900. if !browser_support_cookies
  901. assert(true, "'#{params[:value]}' ups browser is not supporting reading cookies, go ahead")
  902. return true
  903. end
  904. cookies = instance.manage.all_cookies
  905. cookies.each do |cookie|
  906. # :name=>"_zammad_session_c25832f4de2", :value=>"adc31cd21615cb0a7ab269184ec8b76f", :path=>"/", :domain=>"localhost", :expires=>nil, :secure=>false}
  907. next if cookie[:name] !~ /#{params[:name]}/i
  908. if params.key?(:value) && cookie[:value].to_s =~ /#{params[:value]}/i
  909. assert(true, "matching value '#{params[:value]}' in cookie '#{cookie}'")
  910. else
  911. raise "not matching value '#{params[:value]}' in cookie '#{cookie}'"
  912. end
  913. if params.key?(:expires) && cookie[:expires].to_s =~ /#{params[:expires]}/i
  914. assert(true, "matching expires '#{params[:expires].inspect}' in cookie '#{cookie}'")
  915. else
  916. raise "not matching expires '#{params[:expires]}' in cookie '#{cookie}'"
  917. end
  918. return if !params[:should_not_exist]
  919. raise "cookie with name '#{params[:name]}' should not exist, but exists '#{cookies}'"
  920. end
  921. if params[:should_not_exist]
  922. assert(true, "cookie with name '#{params[:name]}' is not existing")
  923. return
  924. end
  925. raise "not matching name '#{params[:name]}' in cookie '#{cookies}'"
  926. end
  927. =begin
  928. verify_title(
  929. browser: browser1,
  930. value: 'some title',
  931. )
  932. =end
  933. def verify_title(params = {})
  934. switch_window_focus(params)
  935. log('verify_title', params)
  936. instance = params[:browser] || @browser
  937. title = instance.title
  938. if title.match?(/#{params[:value]}/i)
  939. assert(true, "matching '#{params[:value]}' in title '#{title}'")
  940. else
  941. raise "not matching '#{params[:value]}' in title '#{title}'"
  942. end
  943. end
  944. =begin
  945. verify_task(
  946. browser: browser1,
  947. data: {
  948. title: 'some title',
  949. modified: true, # optional
  950. }
  951. )
  952. =end
  953. def verify_task(params = {}, fallback = false)
  954. switch_window_focus(params)
  955. log('verify_task', params)
  956. instance = params[:browser] || @browser
  957. data = params[:data]
  958. sleep 1
  959. begin
  960. # verify title
  961. if data[:title]
  962. title = instance.find_elements(css: '.tasks .is-active')[0].text.strip
  963. if title.match?(/#{data[:title]}/i)
  964. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  965. else
  966. screenshot(browser: instance, comment: 'verify_task_failed')
  967. raise "not matching '#{data[:title]}' in title '#{title}'"
  968. end
  969. end
  970. # verify modified
  971. if data.key?(:modified)
  972. exists = instance.find_elements(css: '.tasks .is-active')[0]
  973. is_modified = instance.find_elements(css: '.tasks .is-modified')[0]
  974. puts "m #{data[:modified].inspect}"
  975. if exists
  976. puts ' exists'
  977. end
  978. if is_modified
  979. puts ' is_modified'
  980. end
  981. if data[:modified] == true
  982. if is_modified
  983. assert(true, "task '#{data[:title]}' is modifed")
  984. elsif !exists
  985. screenshot(browser: instance, comment: 'verify_task_failed')
  986. raise "task '#{data[:title]}' not exists, should not modified"
  987. else
  988. screenshot(browser: instance, comment: 'verify_task_failed')
  989. raise "task '#{data[:title]}' is not modifed"
  990. end
  991. elsif !is_modified
  992. assert(true, "task '#{data[:title]}' is modifed")
  993. elsif !exists
  994. screenshot(browser: instance, comment: 'verify_task_failed')
  995. raise "task '#{data[:title]}' not exists, should be not modified"
  996. else
  997. screenshot(browser: instance, comment: 'verify_task_failed')
  998. raise "task '#{data[:title]}' is modifed, but should not"
  999. end
  1000. end
  1001. rescue => e
  1002. # just try again
  1003. if !fallback
  1004. verify_task(params, true)
  1005. end
  1006. raise 'ERROR: ' + e.inspect
  1007. end
  1008. true
  1009. end
  1010. =begin
  1011. open_task(
  1012. browser: browser1,
  1013. data: {
  1014. title: 'some title',
  1015. }
  1016. )
  1017. =end
  1018. def open_task(params = {})
  1019. switch_window_focus(params)
  1020. log('open_task', params)
  1021. instance = params[:browser] || @browser
  1022. data = params[:data]
  1023. element = instance.find_element(css: '#navigation').find_element(partial_link_text: data[:title])
  1024. if !element
  1025. screenshot(browser: instance, comment: 'open_task_failed')
  1026. raise "no task with title '#{data[:title]}' found"
  1027. end
  1028. # firefix/marionette issue with Selenium::WebDriver::Error::ElementNotInteractableError: could not be scrolled into view
  1029. # use js workaround instead of native click
  1030. instance.execute_script("$('#navigation .tasks .task:contains(\"#{data[:title]}\") .nav-tab-name').click()")
  1031. #element.click
  1032. true
  1033. end
  1034. =begin
  1035. close_task(
  1036. browser: browser1,
  1037. data: {
  1038. title: 'some title',
  1039. },
  1040. discard_changes: true,
  1041. )
  1042. =end
  1043. def close_task(params = {})
  1044. switch_window_focus(params)
  1045. log('close_task', params)
  1046. instance = params[:browser] || @browser
  1047. data = params[:data]
  1048. element = instance.find_element(css: '#navigation').find_element(partial_link_text: data[:title])
  1049. if !element
  1050. screenshot(browser: instance, comment: 'close_task_failed')
  1051. raise "no task with title '#{data[:title]}' found"
  1052. end
  1053. instance.action.move_to(element).release.perform
  1054. sleep 0.1
  1055. instance.execute_script("$('#navigation .tasks .task:contains(\"#{data[:title]}\") .js-close').click()")
  1056. # accept task close warning
  1057. if params[:discard_changes]
  1058. modal_ready(browser: instance)
  1059. instance.find_elements(css: '.modal button.js-submit')[0].click
  1060. modal_disappear(browser: instance)
  1061. end
  1062. true
  1063. end
  1064. =begin
  1065. file_upload(
  1066. browser: browser1,
  1067. css: '.content.active .attachmentPlaceholder-inputHolder input'
  1068. files: ['path/in/home/some_file.ext'], # 'test/data/pdf/test1.pdf'
  1069. )
  1070. =end
  1071. def file_upload(params = {})
  1072. switch_window_focus(params)
  1073. log('file_upload', params)
  1074. instance = params[:browser] || @browser
  1075. params[:files].each do |file|
  1076. instance.find_elements(css: params[:css])[0].send_keys(Rails.root.join(file))
  1077. end
  1078. return if params[:no_sleep]
  1079. sleep 2 * params[:files].count
  1080. end
  1081. =begin
  1082. watch_for(
  1083. browser: browser1,
  1084. container: element # optional, defaults to browser, must exist at the time of dispatch
  1085. css: '#content .text-1', # xpath or css required
  1086. xpath: '/content[contains(@class,".text-1")]', # xpath or css required
  1087. value: 'some text',
  1088. attribute: 'some_attribute' # optional
  1089. timeout: 16, # in sec, default 16
  1090. )
  1091. =end
  1092. def watch_for(params = {})
  1093. switch_window_focus(params)
  1094. log('watch_for', params)
  1095. browser = params[:browser] || @browser
  1096. instance = params[:container] || browser
  1097. selector = params[:css] || params[:xpath]
  1098. selector_type = if params.key?(:css)
  1099. :css
  1100. elsif params.key?(:xpath)
  1101. :xpath
  1102. end
  1103. timeout = 16
  1104. if params[:timeout]
  1105. timeout = params[:timeout]
  1106. end
  1107. loops = timeout.to_i * 2
  1108. text = ''
  1109. (1..loops).each do
  1110. element = instance.find_elements(selector_type => selector)[0]
  1111. if element #&& element.displayed?
  1112. begin
  1113. # watch for selector
  1114. if !params[:attribute] && !params[:value]
  1115. assert(true, "'#{selector}' found")
  1116. sleep 0.5
  1117. return true
  1118. # match an attribute
  1119. else
  1120. text = if params[:attribute]
  1121. element.attribute(params[:attribute])
  1122. elsif selector.match?(/(input|textarea)/i)
  1123. element.attribute('value')
  1124. else
  1125. element.text
  1126. end
  1127. if text.match?(/#{params[:value]}/i)
  1128. assert(true, "'#{params[:value]}' found in '#{text}'")
  1129. sleep 0.5
  1130. return true
  1131. end
  1132. end
  1133. rescue
  1134. # try again
  1135. end
  1136. end
  1137. sleep 0.5
  1138. end
  1139. screenshot(browser: browser, comment: 'watch_for_failed')
  1140. if !params[:attribute] && !params[:value]
  1141. raise "'#{selector}' not found"
  1142. end
  1143. raise "'#{params[:value]}' not found in '#{text}'"
  1144. end
  1145. =begin
  1146. wait untill selector disabppears
  1147. watch_for_disappear(
  1148. browser: browser1,
  1149. css: '#content .text-1',
  1150. timeout: 16, # in sec, default 16
  1151. )
  1152. wait untill text in selector disabppears
  1153. watch_for_disappear(
  1154. browser: browser1,
  1155. css: '#content .text-1',
  1156. value: 'some value as regexp',
  1157. timeout: 16, # in sec, default 16
  1158. )
  1159. =end
  1160. def watch_for_disappear(params = {})
  1161. switch_window_focus(params)
  1162. log('watch_for_disappear', params)
  1163. instance = params[:browser] || @browser
  1164. timeout = 16
  1165. if params[:timeout]
  1166. timeout = params[:timeout]
  1167. end
  1168. loops = timeout.to_i
  1169. text = ''
  1170. (1..loops).each do
  1171. element = instance.find_elements(css: params[:css])[0]
  1172. if !element #|| element.displayed?
  1173. assert(true, 'not found')
  1174. sleep 1
  1175. return true
  1176. end
  1177. if params[:value]
  1178. begin
  1179. text = instance.find_elements(css: params[:css])[0].text
  1180. if !text.match?(/#{params[:value]}/i)
  1181. assert(true, "not matching '#{params[:value]}' in text '#{text}'")
  1182. sleep 1
  1183. return true
  1184. end
  1185. rescue
  1186. # try again
  1187. end
  1188. end
  1189. sleep 1
  1190. end
  1191. screenshot(browser: instance, comment: 'disappear_failed')
  1192. raise "#{params[:css]}) still exsists"
  1193. end
  1194. =begin
  1195. shortcut(
  1196. browser: browser1,
  1197. key: 'x',
  1198. )
  1199. =end
  1200. def shortcut(params = {})
  1201. switch_window_focus(params)
  1202. log('shortcut', params)
  1203. instance = params[:browser] || @browser
  1204. screenshot(browser: instance, comment: 'shortcut_before')
  1205. instance.action.key_down(:control)
  1206. .key_down(:shift)
  1207. .send_keys(params[:key])
  1208. .key_up(:shift)
  1209. .key_up(:control)
  1210. .perform
  1211. screenshot(browser: instance, comment: 'shortcut_after')
  1212. end
  1213. =begin
  1214. window_keys(
  1215. browser: browser1,
  1216. value: 'x',
  1217. )
  1218. =end
  1219. def window_keys(params = {})
  1220. switch_window_focus(params)
  1221. log('window_keys', params)
  1222. instance = params[:browser] || @browser
  1223. instance.action.send_keys(params[:value]).perform
  1224. end
  1225. =begin
  1226. tasks_close_all(
  1227. browser: browser1,
  1228. )
  1229. =end
  1230. def tasks_close_all(params = {})
  1231. switch_window_focus(params)
  1232. log('tasks_close_all', params)
  1233. instance = params[:browser] || @browser
  1234. 99.times do
  1235. #sleep 0.5
  1236. begin
  1237. if instance.find_elements(css: '#navigation .tasks .task:first-child')[0]
  1238. instance.action.move_to(instance.find_elements(css: '#navigation .tasks .task:first-child')[0]).release.perform
  1239. click_element = instance.find_elements(css: '#navigation .tasks .task:first-child .js-close')[0]
  1240. if click_element
  1241. click_element.click
  1242. # accept task close warning
  1243. if instance.find_elements(css: '.modal button.js-submit')[0]
  1244. sleep 0.4
  1245. instance.find_elements(css: '.modal button.js-submit')[0].click
  1246. end
  1247. end
  1248. else
  1249. break
  1250. end
  1251. rescue
  1252. # try again
  1253. end
  1254. end
  1255. assert(true, 'all tasks closed')
  1256. end
  1257. =begin
  1258. close_online_notitifcation(
  1259. browser: browser1,
  1260. data: {
  1261. #title: 'some title',
  1262. position: 3,
  1263. },
  1264. )
  1265. =end
  1266. def close_online_notitifcation(params = {})
  1267. switch_window_focus(params)
  1268. log('close_online_notitifcation', params)
  1269. instance = params[:browser] || @browser
  1270. data = params[:data]
  1271. if data[:title]
  1272. element = instance.find_elements(partial_link_text: data[:title])[0]
  1273. if !element
  1274. screenshot(browser: instance, comment: 'close_online_notitifcation')
  1275. raise "no online notification with title '#{data[:title]}' found"
  1276. end
  1277. instance.action.move_to(element).release.perform
  1278. sleep 0.1
  1279. instance.execute_script("$('.js-notificationsContainer .js-items .js-item .activity-text:contains(\"#{data[:title]}\") .js-remove').first().click()")
  1280. else
  1281. css = ".js-notificationsContainer .js-items .js-item:nth-child(#{data[:position]})"
  1282. element = instance.find_elements(css: css)[0]
  1283. if !element
  1284. screenshot(browser: instance, comment: 'close_online_notitifcation')
  1285. raise "no online notification with postion '#{css}' found"
  1286. end
  1287. instance.action.move_to(element).release.perform
  1288. sleep 0.1
  1289. instance.find_elements(css: "#{css} .js-remove")[0].click
  1290. end
  1291. true
  1292. end
  1293. =begin
  1294. online_notitifcation_close_all(
  1295. browser: browser1,
  1296. )
  1297. =end
  1298. def online_notitifcation_close_all(params = {})
  1299. switch_window_focus(params)
  1300. log('online_notitifcation_close_all', params)
  1301. instance = params[:browser] || @browser
  1302. 99.times do
  1303. sleep 0.5
  1304. begin
  1305. if instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0]
  1306. instance.action.move_to(instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0]).perform
  1307. sleep 0.1
  1308. click_element = instance.find_elements(css: '.js-notificationsContainer .js-item:first-child .js-remove')[0]
  1309. click_element&.click
  1310. else
  1311. break
  1312. end
  1313. rescue
  1314. # try again
  1315. end
  1316. end
  1317. assert(true, 'all online notification closed')
  1318. end
  1319. =begin
  1320. empty_search(
  1321. browser: browser1,
  1322. )
  1323. =end
  1324. def empty_search(params = {})
  1325. switch_window_focus(params)
  1326. log('empty_search', params)
  1327. instance = params[:browser] || @browser
  1328. # empty search box by x
  1329. begin
  1330. instance.find_elements(css: '.search .js-emptySearch')[0].click
  1331. rescue
  1332. # in issues with ff & selenium, sometimes exeption appears
  1333. # "Element is not currently visible and so may not be interacted with"
  1334. log('empty_search via js')
  1335. instance.execute_script('$(".search .js-emptySearch").click()')
  1336. end
  1337. sleep 0.5
  1338. text = instance.find_elements(css: '#global-search')[0].attribute('value')
  1339. if !text
  1340. raise '#global-search is not empty!'
  1341. end
  1342. true
  1343. end
  1344. =begin
  1345. ticket_customer_select(
  1346. browser: browser1,
  1347. css: '#content .text-1',
  1348. customer: '',
  1349. )
  1350. =end
  1351. def ticket_customer_select(params = {})
  1352. switch_window_focus(params)
  1353. log('ticket_customer_select', params)
  1354. instance = params[:browser] || @browser
  1355. element = instance.find_elements(css: params[:css] + ' input[name="customer_id_completion"]')[0]
  1356. element.click
  1357. element.clear
  1358. element.send_keys(params[:customer])
  1359. sleep 2.5
  1360. element.send_keys(:enter)
  1361. #instance.find_elements(css: params[:css] + ' .recipientList-entry.js-object.is-active')[0].click
  1362. sleep 0.4
  1363. assert(true, 'ticket_customer_select')
  1364. end
  1365. =begin
  1366. overview_create(
  1367. browser: browser1,
  1368. data: {
  1369. name: name,
  1370. roles: ['Agent'],
  1371. selector: {
  1372. 'Priority': '1 low',
  1373. },
  1374. 'order::direction' => 'down',
  1375. }
  1376. )
  1377. =end
  1378. def overview_create(params)
  1379. switch_window_focus(params)
  1380. log('overview_create', params)
  1381. instance = params[:browser] || @browser
  1382. data = params[:data]
  1383. click(
  1384. browser: instance,
  1385. css: 'a[href="#manage"]',
  1386. mute_log: true,
  1387. )
  1388. click(
  1389. browser: instance,
  1390. css: '.content.active a[href="#manage/overviews"]',
  1391. mute_log: true,
  1392. )
  1393. click(
  1394. browser: instance,
  1395. css: '.content.active a[data-type="new"]',
  1396. mute_log: true,
  1397. )
  1398. modal_ready(browser: instance)
  1399. if data[:name]
  1400. set(
  1401. browser: instance,
  1402. css: '.modal input[name=name]',
  1403. value: data[:name],
  1404. mute_log: true,
  1405. )
  1406. end
  1407. if data[:roles]
  1408. 99.times do
  1409. begin
  1410. element = instance.find_elements(css: '.modal .js-selected[data-name=role_ids] .js-option:not(.is-hidden)')[0]
  1411. break if !element
  1412. element.click
  1413. sleep 0.1
  1414. end
  1415. end
  1416. data[:roles].each do |role|
  1417. instance.execute_script("$(\".modal [data-name=role_ids] .js-pool .js-option:not(.is-hidden):contains('#{role}')\").first().click()")
  1418. end
  1419. end
  1420. data[:selector]&.each do |key, value|
  1421. select(
  1422. browser: instance,
  1423. css: '.modal .ticket_selector .js-attributeSelector select',
  1424. value: key,
  1425. mute_log: true,
  1426. )
  1427. sleep 0.5
  1428. if data.key?('text_input')
  1429. set(
  1430. browser: instance,
  1431. css: '.modal .ticket_selector .js-value input',
  1432. value: value,
  1433. mute_log: true,
  1434. )
  1435. else
  1436. select(
  1437. browser: instance,
  1438. css: '.modal .ticket_selector .js-value select',
  1439. value: value,
  1440. deselect_all: true,
  1441. mute_log: true,
  1442. )
  1443. end
  1444. end
  1445. if data['order::direction']
  1446. select(
  1447. browser: instance,
  1448. css: '.modal select[name="order::direction"]',
  1449. value: data['order::direction'],
  1450. mute_log: true,
  1451. )
  1452. end
  1453. if data[:group_by]
  1454. select(
  1455. browser: instance,
  1456. css: '.modal select[name="group_by"]',
  1457. value: data[:group_by],
  1458. mute_log: true,
  1459. )
  1460. end
  1461. if data[:group_direction]
  1462. select(
  1463. browser: instance,
  1464. css: '.modal select[name="group_direction"]',
  1465. value: data[:group_direction],
  1466. mute_log: true,
  1467. )
  1468. end
  1469. instance.find_elements(css: '.modal button.js-submit')[0].click
  1470. modal_disappear(browser: instance)
  1471. 11.times do
  1472. element = instance.find_elements(css: 'body')[0]
  1473. text = element.text
  1474. if text.match?(/#{Regexp.quote(data[:name])}/)
  1475. assert(true, 'overview created')
  1476. overview = {
  1477. name: name,
  1478. }
  1479. sleep 1
  1480. return overview
  1481. end
  1482. sleep 1
  1483. end
  1484. screenshot(browser: instance, comment: 'overview_create_failed')
  1485. raise 'overview creation failed'
  1486. end
  1487. =begin
  1488. overview_update(
  1489. browser: browser1,
  1490. data: {
  1491. name: name,
  1492. roles: ['Agent'],
  1493. selector: {
  1494. 'Priority': '1 low',
  1495. },
  1496. 'order::direction' => 'down',
  1497. }
  1498. )
  1499. =end
  1500. def overview_update(params)
  1501. switch_window_focus(params)
  1502. log('overview_create', params)
  1503. instance = params[:browser] || @browser
  1504. data = params[:data]
  1505. click(
  1506. browser: instance,
  1507. css: 'a[href="#manage"]',
  1508. mute_log: true,
  1509. )
  1510. click(
  1511. browser: instance,
  1512. css: '.content.active a[href="#manage/overviews"]',
  1513. mute_log: true,
  1514. )
  1515. instance.execute_script("$(\".content.active td:contains('#{data[:name]}')\").first().click()")
  1516. sleep 2
  1517. if data[:name]
  1518. set(
  1519. browser: instance,
  1520. css: '.modal input[name=name]',
  1521. value: data[:name],
  1522. mute_log: true,
  1523. )
  1524. end
  1525. if data[:roles]
  1526. 99.times do
  1527. begin
  1528. element = instance.find_elements(css: '.modal .js-selected[data-name=role_ids] .js-option:not(.is-hidden)')[0]
  1529. break if !element
  1530. element.click
  1531. sleep 0.1
  1532. end
  1533. end
  1534. data[:roles].each do |role|
  1535. instance.execute_script("$(\".modal [data-name=role_ids] .js-pool .js-option:not(.is-hidden):contains('#{role}')\").first().click()")
  1536. end
  1537. end
  1538. data[:selector]&.each do |key, value|
  1539. select(
  1540. browser: instance,
  1541. css: '.modal .ticket_selector .js-attributeSelector select',
  1542. value: key,
  1543. mute_log: true,
  1544. )
  1545. sleep 0.5
  1546. select(
  1547. browser: instance,
  1548. css: '.modal .ticket_selector .js-value select',
  1549. value: value,
  1550. deselect_all: true,
  1551. mute_log: true,
  1552. )
  1553. end
  1554. if data['order::direction']
  1555. select(
  1556. browser: instance,
  1557. css: '.modal select[name="order::direction"]',
  1558. value: data['order::direction'],
  1559. mute_log: true,
  1560. )
  1561. end
  1562. if data[:group_direction]
  1563. select(
  1564. browser: instance,
  1565. css: '.modal select[name="group_direction"]',
  1566. value: data[:group_direction],
  1567. mute_log: true,
  1568. )
  1569. end
  1570. instance.find_elements(css: '.modal button.js-submit')[0].click
  1571. modal_disappear(browser: instance)
  1572. 11.times do
  1573. element = instance.find_elements(css: 'body')[0]
  1574. text = element.text
  1575. if text.match?(/#{Regexp.quote(data[:name])}/)
  1576. assert(true, 'overview updated')
  1577. overview = {
  1578. name: name,
  1579. }
  1580. sleep 1
  1581. return overview
  1582. end
  1583. sleep 1
  1584. end
  1585. screenshot(browser: instance, comment: 'overview_update_failed')
  1586. raise 'overview update failed'
  1587. end
  1588. =begin
  1589. ticket = ticket_create(
  1590. browser: browser1,
  1591. data: {
  1592. customer: 'nico',
  1593. group: 'Users', # optional / '-NONE-' # if group selection should not be shown
  1594. priority: '2 normal',
  1595. state: 'open',
  1596. title: 'overview #1',
  1597. body: 'overview #1',
  1598. },
  1599. do_not_submit: true,
  1600. )
  1601. returns (in case of submitted)
  1602. {
  1603. id: 123,
  1604. number: '100001',
  1605. title: 'overview #1',
  1606. }
  1607. ticket = ticket_create(
  1608. browser: browser1,
  1609. data: {
  1610. customer: 'nico',
  1611. group: 'Users', # optional / '-NONE-' # if group selection should not be shown
  1612. priority: '2 normal',
  1613. state: 'open',
  1614. title: 'overview #1',
  1615. body: 'overview #1',
  1616. },
  1617. custom_data_select: {
  1618. key1: 'some value',
  1619. },
  1620. custom_data_input: {
  1621. key1: 'some value',
  1622. },
  1623. custom_data_date: {
  1624. key!: '02/28/2018',
  1625. }
  1626. disable_group_check: true,
  1627. )
  1628. =end
  1629. def ticket_create(params)
  1630. switch_window_focus(params)
  1631. log('ticket_create', params)
  1632. instance = params[:browser] || @browser
  1633. data = params[:data]
  1634. click(
  1635. browser: instance,
  1636. css: 'a[href="#new"]',
  1637. mute_log: true,
  1638. only_if_exists: true,
  1639. )
  1640. click(
  1641. browser: instance,
  1642. css: 'a[href="#ticket/create"]',
  1643. mute_log: true,
  1644. )
  1645. found = false
  1646. 7.times do
  1647. element = instance.find_elements(css: '.content.active .newTicket')[0]
  1648. if element
  1649. found = true
  1650. break
  1651. end
  1652. sleep 1
  1653. end
  1654. if !found
  1655. screenshot(browser: instance, comment: 'ticket_create_failed')
  1656. raise 'no ticket create screen found!'
  1657. end
  1658. if data[:group]
  1659. if data[:group] == '-NONE-'
  1660. # check if owner selection exists
  1661. count = instance.find_elements(css: '.content.active .newTicket select[name="group_id"] option').count
  1662. if count.nonzero?
  1663. instance.find_elements(css: '.content.active .newTicket select[name="group_id"] option').each do |element|
  1664. log('ticket_create invalid group count', text: element.text)
  1665. end
  1666. end
  1667. assert_equal(0, count, 'owner selection should not be showm')
  1668. # check count of agents, should be only 3 / - selection + master + agent on init screen
  1669. count = instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').count
  1670. if count != 3
  1671. instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').each do |element|
  1672. log('ticket_create invalid owner count', text: element.text)
  1673. end
  1674. end
  1675. assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
  1676. else
  1677. # check count of agents, should be only 1 selection, the "-" selection on init screen
  1678. if !params[:disable_group_check]
  1679. count = instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').count
  1680. if count != 1
  1681. instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').each do |element|
  1682. log('ticket_create invalid owner count', text: element.text)
  1683. end
  1684. end
  1685. assert_equal(1, count, 'check if owner selection is empty per default')
  1686. end
  1687. select(
  1688. browser: instance,
  1689. css: '.content.active .newTicket select[name="group_id"]',
  1690. value: data[:group],
  1691. mute_log: true,
  1692. )
  1693. end
  1694. end
  1695. if data[:priority]
  1696. select(
  1697. browser: instance,
  1698. css: '.content.active .newTicket select[name="priority_id"]',
  1699. value: data[:priority],
  1700. mute_log: true,
  1701. )
  1702. end
  1703. if data[:state]
  1704. select(
  1705. browser: instance,
  1706. css: '.content.active .newTicket select[name="state_id"]',
  1707. value: data[:state],
  1708. mute_log: true,
  1709. )
  1710. end
  1711. if data[:title]
  1712. set(
  1713. browser: instance,
  1714. css: '.content.active .newTicket input[name="title"]',
  1715. value: data[:title],
  1716. clear: true,
  1717. mute_log: true,
  1718. )
  1719. end
  1720. if data[:body]
  1721. set(
  1722. browser: instance,
  1723. css: '.content.active .newTicket div[data-name=body]',
  1724. value: data[:body],
  1725. clear: true,
  1726. mute_log: true,
  1727. )
  1728. end
  1729. if data[:customer]
  1730. element = instance.find_elements(css: '.content.active .newTicket input[name="customer_id_completion"]')[0]
  1731. element.click
  1732. element.clear
  1733. # ff issue, sometimes focus event gets dropped
  1734. # if drowdown is not open, try it again
  1735. if !instance.find_elements(css: '.content.active .newTicket .js-recipientDropdown.open')[0]
  1736. instance.execute_script('$(".active .newTicket .js-recipientDropdown").addClass("open")')
  1737. end
  1738. element.send_keys(data[:customer])
  1739. sleep 2.5
  1740. element.send_keys(:enter)
  1741. sleep 0.2
  1742. # ff issue, sometimes enter event gets dropped
  1743. # take user manually
  1744. if instance.find_elements(css: '.content.active .newTicket .js-recipientDropdown.open')[0]
  1745. instance.find_elements(css: '.content.active .newTicket .recipientList-entry.js-object.is-active')[0].click
  1746. sleep 0.4
  1747. end
  1748. end
  1749. params[:custom_data_select]&.each do |local_key, local_value|
  1750. select(
  1751. browser: instance,
  1752. css: ".content.active .newTicket select[name=\"#{local_key}\"]",
  1753. value: local_value,
  1754. )
  1755. end
  1756. params[:custom_data_input]&.each do |local_key, local_value|
  1757. set(
  1758. browser: instance,
  1759. css: ".content.active .newTicket input[name=\"#{local_key}\"]",
  1760. value: local_value,
  1761. clear: true,
  1762. )
  1763. end
  1764. params[:custom_data_date]&.each do |local_key, local_value|
  1765. set(
  1766. browser: instance,
  1767. css: ".content.active .newTicket div[data-name=\"#{local_key}\"] input[data-item=\"date\"]",
  1768. value: local_value,
  1769. clear: true,
  1770. )
  1771. end
  1772. if data[:attachment]
  1773. file_upload(
  1774. browser: instance,
  1775. css: '.content.active .text-1',
  1776. value: 'some text',
  1777. )
  1778. end
  1779. if params[:do_not_submit]
  1780. assert(true, 'ticket created without submit')
  1781. return
  1782. end
  1783. #instance.execute_script('$(".content.active .newTicket form").submit();')
  1784. click(
  1785. browser: instance,
  1786. css: '.content.active .newTicket button.js-submit',
  1787. mute_log: true,
  1788. )
  1789. sleep 1
  1790. 9.times do
  1791. if instance.current_url.match?(/#{Regexp.quote('#ticket/zoom/')}/)
  1792. assert(true, 'ticket created')
  1793. sleep 2
  1794. id = instance.current_url
  1795. id.gsub!(//,)
  1796. id.gsub!(%r{^.+?/(\d+)$}, '\\1')
  1797. element = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0]
  1798. if element
  1799. number = element.text
  1800. ticket = {
  1801. id: id,
  1802. number: number,
  1803. title: data[:title],
  1804. }
  1805. sleep 2 # wait until notify is gone
  1806. return ticket
  1807. end
  1808. end
  1809. sleep 1
  1810. end
  1811. screenshot(browser: instance, comment: 'ticket_create_failed')
  1812. raise "ticket creation failed, can't get zoom url (current url is '#{instance.current_url}')"
  1813. end
  1814. =begin
  1815. ticket_update(
  1816. browser: browser1,
  1817. data: {
  1818. title: '',
  1819. customer: 'some_customer@example.com',
  1820. body: 'some body',
  1821. group: 'some group', # optional
  1822. priority: '1 low',
  1823. state: 'closed',
  1824. },
  1825. do_not_submit: true,
  1826. )
  1827. ticket_update(
  1828. browser: browser1,
  1829. data: {
  1830. title: '',
  1831. customer: 'some_customer@example.com',
  1832. body: 'some body',
  1833. group: 'some group', # optional
  1834. priority: '1 low',
  1835. state: 'closed',
  1836. },
  1837. custom_data_select: {
  1838. key1: 'some value',
  1839. },
  1840. custom_data_input: {
  1841. key1: 'some value',
  1842. },
  1843. custom_data_date: {
  1844. key1: '02/21/2018',
  1845. },
  1846. do_not_submit: true,
  1847. task_type: 'stayOnTab', # default: stayOnTab / possible: closeTab, closeNextInOverview, stayOnTab
  1848. )
  1849. =end
  1850. def ticket_update(params)
  1851. switch_window_focus(params)
  1852. log('ticket_update', params)
  1853. instance = params[:browser] || @browser
  1854. data = params[:data]
  1855. if data[:title]
  1856. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
  1857. #element.clear
  1858. #sleep 0.5
  1859. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
  1860. #element.send_keys(data[:title])
  1861. #sleep 0.5
  1862. #element.send_keys(:tab)
  1863. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").focus()')
  1864. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").text("' + data[:title] + '")')
  1865. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").blur()')
  1866. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").trigger("blur")')
  1867. # {
  1868. # :where => :instance2,
  1869. # :execute => 'sendkey',
  1870. # :css => '.content.active .ticketZoom-header .js-objectTitle',
  1871. # :value => 'TTT',
  1872. # },
  1873. # {
  1874. # :where => :instance2,
  1875. # :execute => 'sendkey',
  1876. # :css => '.content.active .ticketZoom-header .js-objectTitle',
  1877. # :value => :tab,
  1878. # },
  1879. end
  1880. if data[:customer]
  1881. # select tab
  1882. click(browser: instance, css: '.content.active .tabsSidebar-tab[data-tab="customer"]')
  1883. click(browser: instance, css: '.content.active div[data-tab="customer"] .js-actions .icon-arrow-down')
  1884. click(browser: instance, css: '.content.active div[data-tab="customer"] .js-actions [data-type="customer-change"]')
  1885. watch_for(
  1886. browser: instance,
  1887. css: '.modal',
  1888. value: 'change',
  1889. )
  1890. element = instance.find_elements(css: '.modal input[name="customer_id_completion"]')[0]
  1891. element.click
  1892. element.clear
  1893. element.send_keys(data[:customer])
  1894. sleep 2.5
  1895. element.send_keys(:enter)
  1896. #instance.find_elements(css: '.modal .user_autocompletion .recipientList-entry.js-object.is-active')[0].click
  1897. sleep 0.2
  1898. click(browser: instance, css: '.modal .js-submit')
  1899. modal_disappear(browser: instance)
  1900. watch_for(
  1901. browser: instance,
  1902. css: '.content.active .tabsSidebar',
  1903. value: data[:customer],
  1904. )
  1905. # select tab
  1906. click(browser: instance, css: '.content.active .tabsSidebar-tab[data-tab="ticket"]')
  1907. end
  1908. if data[:body]
  1909. set(
  1910. browser: instance,
  1911. css: '.content.active div[data-name=body]',
  1912. value: data[:body],
  1913. no_click: true,
  1914. mute_log: true,
  1915. )
  1916. # it's not working stable via selenium, use js
  1917. value = instance.find_elements(css: '.content.active div[data-name=body]')[0].text
  1918. if value != data[:body]
  1919. body_quoted = quote(data[:body])
  1920. instance.execute_script("$('.content.active div[data-name=body]').html('#{body_quoted}').trigger('focusout')")
  1921. end
  1922. end
  1923. if data[:group]
  1924. if data[:group] == '-NONE-'
  1925. # check if owner selection exists
  1926. count = instance.find_elements(css: '.content.active .sidebar select[name="group_id"] option').count
  1927. assert_equal(0, count, 'owner selection should not be showm')
  1928. # check count of agents, should be only 3 / - selection + master + agent on init screen
  1929. count = instance.find_elements(css: '.content.active .sidebar select[name="owner_id"] option').count
  1930. assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
  1931. else
  1932. select(
  1933. browser: instance,
  1934. css: '.content.active .sidebar select[name="group_id"]',
  1935. value: data[:group],
  1936. mute_log: true,
  1937. )
  1938. sleep 0.2
  1939. end
  1940. end
  1941. if data[:priority]
  1942. select(
  1943. browser: instance,
  1944. css: '.content.active .sidebar select[name="priority_id"]',
  1945. value: data[:priority],
  1946. mute_log: true,
  1947. )
  1948. end
  1949. if data[:state]
  1950. select(
  1951. browser: instance,
  1952. css: '.content.active .sidebar select[name="state_id"]',
  1953. value: data[:state],
  1954. mute_log: true,
  1955. )
  1956. end
  1957. if data[:files]
  1958. file_upload(
  1959. css: '.content.active .attachmentPlaceholder-inputHolder input',
  1960. files: data[:files],
  1961. )
  1962. end
  1963. params[:custom_data_select]&.each do |local_key, local_value|
  1964. select(
  1965. browser: instance,
  1966. css: ".active .sidebar select[name=\"#{local_key}\"]",
  1967. value: local_value,
  1968. )
  1969. end
  1970. params[:custom_data_input]&.each do |local_key, local_value|
  1971. set(
  1972. browser: instance,
  1973. css: ".active .sidebar input[name=\"#{local_key}\"]",
  1974. value: local_value,
  1975. clear: true,
  1976. )
  1977. end
  1978. params[:custom_data_date]&.each do |local_key, local_value|
  1979. click(
  1980. browser: instance,
  1981. css: ".active .sidebar div[data-name=\"#{local_key}\"] input[data-item=\"date\"]",
  1982. mute_log: true,
  1983. )
  1984. # weird bug where you cannot "clear" for date/time input
  1985. # this is specific chrome problem, chrome bug report: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1319#c2
  1986. # indirect issue: https://github.com/angular/protractor/issues/562#issuecomment-47745263
  1987. 11.times do
  1988. sendkey(
  1989. value: :backspace,
  1990. )
  1991. end
  1992. set(
  1993. browser: instance,
  1994. css: ".active .sidebar div[data-name=\"#{local_key}\"] input[data-item=\"date\"]",
  1995. value: local_value,
  1996. )
  1997. end
  1998. if data[:state] || data[:group] || data[:body] || params[:custom_data_select].present? || params[:custom_data_input].present?
  1999. found = nil
  2000. 9.times do
  2001. break if found
  2002. begin
  2003. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  2004. if text.match?(/(Discard your unsaved changes.|Verwerfen der)/)
  2005. found = true
  2006. end
  2007. rescue
  2008. # try again
  2009. end
  2010. sleep 1
  2011. end
  2012. if !found
  2013. screenshot(browser: instance, comment: 'ticket_update_discard_message_failed')
  2014. raise 'no discard message found'
  2015. end
  2016. end
  2017. task_type(
  2018. browser: instance,
  2019. type: params[:task_type] || 'stayOnTab',
  2020. )
  2021. if params[:do_not_submit]
  2022. assert(true, 'ticket updated without submit')
  2023. return true
  2024. end
  2025. instance.find_elements(css: '.content.active .js-submit')[0].click
  2026. # do not stay on tab
  2027. if params[:task_type] == 'closeTab' || params[:task_type] == 'closeNextInOverview'
  2028. sleep 1
  2029. return
  2030. end
  2031. 9.times do
  2032. begin
  2033. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  2034. if text.blank?
  2035. sleep 1
  2036. return true
  2037. end
  2038. rescue
  2039. # try again
  2040. end
  2041. sleep 1
  2042. end
  2043. screenshot(browser: instance, comment: 'ticket_update_failed')
  2044. raise 'unable to update ticket'
  2045. end
  2046. =begin
  2047. ticket_verify(
  2048. browser: browser1,
  2049. data: {
  2050. title: 'some title',
  2051. body: 'some body',
  2052. ## group: 'some group',
  2053. ## state: 'closed',
  2054. custom_data_select: {
  2055. key1: 'some value',
  2056. },
  2057. custom_data_input: {
  2058. key1: 'some value',
  2059. },
  2060. },
  2061. )
  2062. =end
  2063. def ticket_verify(params)
  2064. switch_window_focus(params)
  2065. log('ticket_verify', params)
  2066. instance = params[:browser] || @browser
  2067. data = params[:data]
  2068. if data[:title]
  2069. title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle').first.text.strip
  2070. if title.match?(/#{data[:title]}/i)
  2071. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  2072. else
  2073. raise "not matching '#{data[:title]}' in title '#{title}'"
  2074. end
  2075. end
  2076. if data[:body]
  2077. body = instance.find_elements(css: '.content.active [data-name="body"]').first.text.strip
  2078. if body.match?(/#{data[:body]}/i)
  2079. assert(true, "matching '#{data[:body]}' in body '#{body}'")
  2080. else
  2081. raise "not matching '#{data[:body]}' in body '#{body}'"
  2082. end
  2083. end
  2084. params[:custom_data_select]&.each do |local_key, local_value|
  2085. element = instance.find_elements(css: ".active .sidebar select[name=\"#{local_key}\"] option[selected]").first
  2086. value = element.text.strip
  2087. if value.match?(/#{local_value}/i)
  2088. assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
  2089. else
  2090. raise "not matching '#{value}' in #{local_key} '#{local_value}'"
  2091. end
  2092. end
  2093. params[:custom_data_input]&.each do |local_key, local_value|
  2094. element = instance.find_elements(css: ".active .sidebar input[name=\"#{local_key}\"]").first
  2095. value = element.text.strip
  2096. if value.match?(/#{local_value}/i)
  2097. assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
  2098. else
  2099. raise "not matching '#{value}' in #{local_key} '#{local_value}'"
  2100. end
  2101. end
  2102. true
  2103. end
  2104. =begin
  2105. overview_open(
  2106. browser: browser2,
  2107. name: overview_name,
  2108. )
  2109. overview_open(
  2110. browser: browser2,
  2111. link: "#ticket/view/some_special_name",
  2112. )
  2113. =end
  2114. def overview_open(params)
  2115. switch_window_focus(params)
  2116. log('overview_open', params)
  2117. instance = params[:browser] || @browser
  2118. # click on overview task in sidebar
  2119. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  2120. # show larger overview selection list
  2121. sleep 0.5
  2122. execute(
  2123. browser: instance,
  2124. js: '$(".content.active .sidebar").css("display", "block")',
  2125. )
  2126. link = if params[:link]
  2127. params[:link]
  2128. elsif params[:name]
  2129. "\#ticket/view/#{params[:name]}"
  2130. end
  2131. # switch to overview
  2132. instance.find_elements(css: ".content.active .sidebar a[href=\"#{link}\"]")[0].click
  2133. # hide larger overview selection list again
  2134. sleep 0.5
  2135. execute(
  2136. browser: instance,
  2137. js: '$(".content.active .sidebar").css("display", "none")',
  2138. )
  2139. end
  2140. =begin
  2141. ticket_open_by_overview(
  2142. browser: browser2,
  2143. number: ticket1[:number],
  2144. link: "#ticket/view/#{name}",
  2145. )
  2146. ticket_open_by_overview(
  2147. browser: browser2,
  2148. number: ticket1[:number],
  2149. text: title,
  2150. link: "#ticket/view/#{name}",
  2151. )
  2152. =end
  2153. def ticket_open_by_overview(params)
  2154. switch_window_focus(params)
  2155. log('ticket_open_by_overview', params)
  2156. instance = params[:browser] || @browser
  2157. overview_open(params)
  2158. if params[:title]
  2159. element = instance.find_element(css: '.content.active').find_element(partial_link_text: params[:title])
  2160. if !element
  2161. screenshot(browser: instance, comment: 'ticket_open_by_overview_no_ticket_failed')
  2162. raise "unable to find ticket #{params[:title]} in overview #{params[:link]}!"
  2163. end
  2164. else
  2165. element = instance.find_elements(partial_link_text: params[:number])[0]
  2166. if !element
  2167. screenshot(browser: instance, comment: 'ticket_open_by_overview_no_ticket_failed')
  2168. raise "unable to find ticket #{params[:number]} in overview #{params[:link]}!"
  2169. end
  2170. end
  2171. element.click
  2172. sleep 1
  2173. number = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0].text
  2174. if !number.match?(/#{params[:number]}/)
  2175. screenshot(browser: instance, comment: 'ticket_open_by_overview_open_failed_failed')
  2176. raise "unable to open ticket #{params[:number]}!"
  2177. end
  2178. assert(true, "ticket #{params[:number]} found")
  2179. true
  2180. end
  2181. =begin
  2182. ticket_open_by_search(
  2183. browser: browser2,
  2184. number: ticket1[:number],
  2185. )
  2186. =end
  2187. def ticket_open_by_search(params)
  2188. switch_window_focus(params)
  2189. log('ticket_open_by_search', params)
  2190. instance = params[:browser] || @browser
  2191. # search by number
  2192. element = instance.find_elements(css: '#global-search')[0]
  2193. element.click
  2194. element.clear
  2195. element.send_keys(params[:number])
  2196. sleep 3
  2197. empty_search(browser: instance)
  2198. # search by number again
  2199. element = instance.find_elements(css: '#global-search')[0]
  2200. element.click
  2201. element.clear
  2202. element.send_keys(params[:number])
  2203. sleep 1
  2204. # open ticket
  2205. #instance.find_element(partial_link_text: params[:number] } ).click
  2206. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:number]}') .nav-tab-icon\").first().click()")
  2207. sleep 1
  2208. number = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0].text
  2209. if !number.match?(/#{params[:number]}/)
  2210. screenshot(browser: instance, comment: 'ticket_open_by_search_failed')
  2211. raise "unable to search/find ticket #{params[:number]}!"
  2212. end
  2213. true
  2214. end
  2215. =begin
  2216. ticket_open_by_title(
  2217. browser: browser2,
  2218. title: ticket1[:title],
  2219. )
  2220. =end
  2221. def ticket_open_by_title(params)
  2222. switch_window_focus(params)
  2223. log('ticket_open_by_title', params)
  2224. instance = params[:browser] || @browser
  2225. # search by number
  2226. element = instance.find_elements(css: '#global-search')[0]
  2227. element.click
  2228. element.clear
  2229. element.send_keys(params[:title])
  2230. sleep 3
  2231. # open ticket
  2232. #instance.find_element(partial_link_text: params[:title] } ).click
  2233. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:title]}') .nav-tab-icon\").click()")
  2234. sleep 1
  2235. title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle')[0].text
  2236. if !title.match?(/#{params[:title]}/)
  2237. screenshot(browser: instance, comment: 'ticket_open_by_title_failed')
  2238. raise "unable to search/find ticket #{params[:title]}!"
  2239. end
  2240. true
  2241. end
  2242. =begin
  2243. overview_count = overview_counter(
  2244. browser: browser2,
  2245. )
  2246. returns
  2247. {
  2248. '#ticket/view/all_unassigned' => 42,
  2249. }
  2250. =end
  2251. def overview_counter(params = {})
  2252. switch_window_focus(params)
  2253. log('overview_counter', params)
  2254. instance = params[:browser] || @browser
  2255. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  2256. sleep 2
  2257. execute(
  2258. browser: instance,
  2259. js: '$(".content.active .sidebar").css("display", "block")',
  2260. )
  2261. #execute(
  2262. # browser: instance,
  2263. # js: '$(".content.active .overview-header").css("display", "none")',
  2264. #)
  2265. overviews = {}
  2266. instance.find_elements(css: '.content.active .sidebar a[href]').each do |element|
  2267. url = element.attribute('href')
  2268. url.gsub!(%r{(http|https)://.+?/(.+?)$}, '\\2')
  2269. overviews[url] = 0
  2270. #puts url.inspect
  2271. #puts element.inspect
  2272. end
  2273. overviews.each_key do |url|
  2274. count = instance.find_elements(css: ".content.active .sidebar a[href=\"#{url}\"] .badge")[0].text
  2275. overviews[url] = count.to_i
  2276. end
  2277. log('overview_counter', overviews)
  2278. overviews
  2279. end
  2280. =begin
  2281. organization_open_by_search(
  2282. browser: browser2,
  2283. value: 'some value',
  2284. )
  2285. =end
  2286. def organization_open_by_search(params = {})
  2287. switch_window_focus(params)
  2288. log('organization_open_by_search', params)
  2289. instance = params[:browser] || @browser
  2290. element = instance.find_elements(css: '#global-search')[0]
  2291. element.click
  2292. element.clear
  2293. element.send_keys(params[:value])
  2294. sleep 3
  2295. empty_search(browser: instance)
  2296. element = instance.find_elements(css: '#global-search')[0]
  2297. element.click
  2298. element.clear
  2299. element.send_keys(params[:value])
  2300. sleep 2
  2301. #instance.find_element(partial_link_text: params[:value] } ).click
  2302. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  2303. sleep 1
  2304. name = instance.find_elements(css: '.content.active h1')[0].text
  2305. if !name.match?(/#{params[:value]}/)
  2306. screenshot(browser: instance, comment: 'organization_open_by_search_failed')
  2307. raise "unable to search/find org #{params[:value]}!"
  2308. end
  2309. assert(true, "org #{params[:value]} found")
  2310. true
  2311. end
  2312. =begin
  2313. user_open_by_search(
  2314. browser: browser2,
  2315. value: 'some value',
  2316. )
  2317. =end
  2318. def user_open_by_search(params = {})
  2319. switch_window_focus(params)
  2320. log('user_open_by_search', params)
  2321. instance = params[:browser] || @browser
  2322. element = instance.find_elements(css: '#global-search')[0]
  2323. element.click
  2324. element.clear
  2325. element.send_keys(params[:value])
  2326. sleep 3
  2327. #instance.find_element(partial_link_text: params[:value]).click
  2328. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  2329. sleep 1
  2330. name = instance.find_elements(css: '.content.active h1')[0].text
  2331. if !name.match?(/#{params[:value]}/)
  2332. screenshot(browser: instance, comment: 'user_open_by_search_failed')
  2333. raise "unable to search/find user #{params[:value]}!"
  2334. end
  2335. assert(true, "user #{params[:term]} found")
  2336. true
  2337. end
  2338. =begin
  2339. user_create(
  2340. browser: browser2,
  2341. data: {
  2342. #login: 'some login' + random,
  2343. firstname: 'Manage Firstname' + random,
  2344. lastname: 'Manage Lastname' + random,
  2345. email: user_email,
  2346. password: 'some-pass',
  2347. role: 'Admin', # optional, choose among [Admin, Agent, Customer]
  2348. # defaults to Customer if not provided
  2349. },
  2350. )
  2351. user_create(
  2352. browser: browser2,
  2353. data: {
  2354. #login: 'some login' + random,
  2355. firstname: 'Manage Firstname' + random,
  2356. lastname: 'Manage Lastname' + random,
  2357. email: user_email,
  2358. password: 'some-pass',
  2359. role: 'Agent', # when the role is Agent an array of permissions for each group is optionally accepted
  2360. permissions: { 1 => %w[read create overview],
  2361. 2 => ['full'], }
  2362. },
  2363. )
  2364. =end
  2365. def user_create(params = {})
  2366. switch_window_focus(params)
  2367. log('user_create', params)
  2368. instance = params[:browser] || @browser
  2369. data = params[:data]
  2370. click(
  2371. browser: instance,
  2372. css: 'a[href="#manage"]',
  2373. mute_log: true,
  2374. )
  2375. click(
  2376. browser: instance,
  2377. css: '.content.active a[href="#manage/users"]',
  2378. mute_log: true,
  2379. )
  2380. click(
  2381. browser: instance,
  2382. css: '.content.active a[data-type="new"]',
  2383. mute_log: true,
  2384. )
  2385. modal_ready(browser: instance)
  2386. element = instance.find_elements(css: '.modal input[name=firstname]')[0]
  2387. element.clear
  2388. element.send_keys(data[:firstname])
  2389. element = instance.find_elements(css: '.modal input[name=lastname]')[0]
  2390. element.clear
  2391. element.send_keys(data[:lastname])
  2392. element = instance.find_elements(css: '.modal input[name=email]')[0]
  2393. element.clear
  2394. element.send_keys(data[:email])
  2395. element = instance.find_elements(css: '.modal input[name=password]')[0]
  2396. element.clear
  2397. element.send_keys(data[:password])
  2398. element = instance.find_elements(css: '.modal input[name=password_confirm]')[0]
  2399. element.clear
  2400. element.send_keys(data[:password])
  2401. element = instance.find_elements(css: '.modal input[name=phone]')[0]
  2402. element.clear
  2403. element.send_keys(data[:phone])
  2404. if data[:active] == false
  2405. select(css: 'select[name="active"]', value: 'inactive')
  2406. end
  2407. if data[:organization]
  2408. element = instance.find_elements(css: '.modal input.searchableSelect-main')[0]
  2409. element.clear
  2410. element.send_keys(data[:organization])
  2411. begin
  2412. retries ||= 0
  2413. target = nil
  2414. until target
  2415. sleep 0.5
  2416. target = instance.find_elements(css: ".modal li[title='#{data[:organization]}']")[0]
  2417. end
  2418. target.click()
  2419. rescue Selenium::WebDriver::Error::StaleElementReferenceError
  2420. sleep retries
  2421. retries += 1
  2422. retry if retries < 3
  2423. end
  2424. end
  2425. if data[:role]
  2426. if data[:role] == 'Admin'
  2427. check(
  2428. browser: instance,
  2429. css: '.modal input[name=role_ids][value=1]',
  2430. )
  2431. elsif data[:role] == 'Customer'
  2432. check(
  2433. browser: instance,
  2434. css: '.modal input[name=role_ids][value=3]',
  2435. )
  2436. elsif data[:role] == 'Agent'
  2437. check(
  2438. browser: instance,
  2439. css: '.modal input[name=role_ids][value=2]',
  2440. )
  2441. data[:permissions].each do |key, value|
  2442. value.each do |permission|
  2443. check(
  2444. browser: instance,
  2445. css: ".modal input[name=\"group_ids::#{key}\"][value=\"#{permission}\"]",
  2446. )
  2447. end
  2448. end
  2449. else
  2450. raise "Unknown :role \"#{data[:role]}\" in user_create()"
  2451. end
  2452. else
  2453. check(
  2454. browser: instance,
  2455. css: '.modal input[name=role_ids][value=3]',
  2456. )
  2457. end
  2458. click(
  2459. browser: instance,
  2460. css: '.modal .js-submit',
  2461. )
  2462. modal_disappear(
  2463. browser: instance,
  2464. timeout: 10,
  2465. )
  2466. set(
  2467. browser: instance,
  2468. css: '.content .js-search',
  2469. value: data[:email],
  2470. )
  2471. watch_for(
  2472. browser: instance,
  2473. css: 'body',
  2474. value: data[:lastname],
  2475. )
  2476. assert(true, 'user created')
  2477. end
  2478. =begin
  2479. user_edit(
  2480. browser: browser2,
  2481. data: {
  2482. login: 'some login' + random,
  2483. firstname: 'Manage Firstname' + random,
  2484. lastname: 'Manage Lastname' + random,
  2485. email: user_email,
  2486. password: 'some-pass',
  2487. role: 'Agent', # when the role is Agent an array of permissions for each group is optionally accepted
  2488. permissions: { 1 => %w[read create overview],
  2489. 2 => ['full'], }
  2490. },
  2491. )
  2492. =end
  2493. def user_edit(params = {})
  2494. switch_window_focus(params)
  2495. log('user_edit', params)
  2496. instance = params[:browser] || @browser
  2497. data = params[:data]
  2498. click(
  2499. browser: instance,
  2500. css: 'a[href="#manage"]',
  2501. mute_log: true,
  2502. )
  2503. click(
  2504. browser: instance,
  2505. css: '.content.active a[href="#manage/users"]',
  2506. mute_log: true,
  2507. )
  2508. instance.find_elements(css: '.content.active .user-list td:first-child').each do |element|
  2509. next if element.text.strip != data[:login]
  2510. element.click
  2511. break
  2512. end
  2513. modal_ready(browser: instance)
  2514. if data[:firstname]
  2515. element = instance.find_elements(css: '.modal input[name=firstname]')[0]
  2516. element.clear
  2517. element.send_keys(data[:firstname])
  2518. end
  2519. if data[:lastname]
  2520. element = instance.find_elements(css: '.modal input[name=lastname]')[0]
  2521. element.clear
  2522. element.send_keys(data[:lastname])
  2523. end
  2524. if data[:email]
  2525. element = instance.find_elements(css: '.modal input[name=email]')[0]
  2526. element.clear
  2527. element.send_keys(data[:email])
  2528. end
  2529. if data[:password]
  2530. element = instance.find_elements(css: '.modal input[name=password]')[0]
  2531. element.clear
  2532. element.send_keys(data[:password])
  2533. element = instance.find_elements(css: '.modal input[name=password_confirm]')[0]
  2534. element.clear
  2535. element.send_keys(data[:password])
  2536. end
  2537. if data[:phone]
  2538. element = instance.find_elements(css: '.modal input[name=phone]')[0]
  2539. element.clear
  2540. element.send_keys(data[:phone])
  2541. end
  2542. if data[:active].present?
  2543. select(css: 'select[name="active"]', value: data[:active] ? 'active' : 'inactive' )
  2544. end
  2545. if data[:organization]
  2546. element = instance.find_elements(css: '.modal input.searchableSelect-main')[0]
  2547. element.clear
  2548. element.send_keys(data[:organization])
  2549. begin
  2550. retries ||= 0
  2551. target = nil
  2552. until target
  2553. sleep 0.5
  2554. target = instance.find_elements(css: ".modal li[title='#{data[:organization]}']")[0]
  2555. end
  2556. target.click()
  2557. rescue Selenium::WebDriver::Error::StaleElementReferenceError
  2558. sleep retries
  2559. retries += 1
  2560. retry if retries < 3
  2561. end
  2562. end
  2563. if data[:role]
  2564. if data[:role] == 'Admin'
  2565. check(
  2566. browser: instance,
  2567. css: '.modal input[name=role_ids][value=1]',
  2568. )
  2569. elsif data[:role] == 'Customer'
  2570. check(
  2571. browser: instance,
  2572. css: '.modal input[name=role_ids][value=3]',
  2573. )
  2574. elsif data[:role] == 'Agent'
  2575. check(
  2576. browser: instance,
  2577. css: '.modal input[name=role_ids][value=2]',
  2578. )
  2579. else
  2580. raise "Unknown :role \"#{data[:role]}\" in user_create()"
  2581. end
  2582. end
  2583. if data[:permissions].present?
  2584. data[:permissions].each do |key, value|
  2585. value.each do |permission|
  2586. check(
  2587. browser: instance,
  2588. css: ".modal input[name=\"group_ids::#{key}\"][value=\"#{permission}\"]",
  2589. )
  2590. end
  2591. end
  2592. end
  2593. click(
  2594. browser: instance,
  2595. css: '.modal .js-submit',
  2596. )
  2597. modal_disappear(
  2598. browser: instance,
  2599. timeout: 10,
  2600. )
  2601. assert(true, 'user updated')
  2602. end
  2603. =begin
  2604. organization_create(
  2605. browser: browser2,
  2606. data: {
  2607. name: 'Test Organization',
  2608. }
  2609. )
  2610. =end
  2611. def organization_create(params = {})
  2612. switch_window_focus(params)
  2613. log('organization_create', params)
  2614. instance = params[:browser] || @browser
  2615. data = params[:data]
  2616. click(
  2617. browser: instance,
  2618. css: 'a[href="#manage"]',
  2619. mute_log: true,
  2620. )
  2621. click(
  2622. browser: instance,
  2623. css: '.content.active a[href="#manage/organizations"]',
  2624. mute_log: true,
  2625. )
  2626. click(
  2627. browser: instance,
  2628. css: '.content.active a[data-type="new"]',
  2629. mute_log: true,
  2630. )
  2631. modal_ready(browser: instance)
  2632. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2633. element.clear
  2634. element.send_keys(data[:name])
  2635. instance.find_elements(css: '.modal button.js-submit')[0].click
  2636. modal_disappear(
  2637. browser: instance,
  2638. timeout: 5,
  2639. )
  2640. watch_for(
  2641. browser: instance,
  2642. css: 'body',
  2643. value: data[:name],
  2644. )
  2645. end
  2646. =begin
  2647. calendar_create(
  2648. browser: browser2,
  2649. data: {
  2650. name: 'some calendar' + random,
  2651. first_response_time_in_text: 61
  2652. },
  2653. )
  2654. =end
  2655. def calendar_create(params = {})
  2656. switch_window_focus(params)
  2657. log('calendar_create', params)
  2658. instance = params[:browser] || @browser
  2659. data = params[:data]
  2660. click(
  2661. browser: instance,
  2662. css: 'a[href="#manage"]',
  2663. mute_log: true,
  2664. )
  2665. click(
  2666. browser: instance,
  2667. css: '.content.active a[href="#manage/calendars"]',
  2668. mute_log: true,
  2669. )
  2670. sleep 4
  2671. click(
  2672. browser: instance,
  2673. css: '.content.active a.js-new',
  2674. mute_log: true,
  2675. )
  2676. modal_ready(browser: instance)
  2677. element = instance.find_elements(css: '.content.active .modal input[name=name]')[0]
  2678. element.clear
  2679. element.send_keys(data[:name])
  2680. element = instance.find_elements(css: '.content.active .modal .js-input')[0]
  2681. element.clear
  2682. element.send_keys(data[:timezone])
  2683. element.send_keys(:enter)
  2684. instance.find_elements(css: '.modal button.js-submit')[0].click
  2685. modal_disappear(browser: instance)
  2686. 7.times do
  2687. element = instance.find_elements(css: 'body')[0]
  2688. text = element.text
  2689. if text.match?(/#{Regexp.quote(data[:name])}/)
  2690. assert(true, 'calendar created')
  2691. sleep 1
  2692. return true
  2693. end
  2694. sleep 1
  2695. end
  2696. screenshot(browser: instance, comment: 'calendar_create_failed')
  2697. raise 'calendar creation failed'
  2698. end
  2699. =begin
  2700. sla_create(
  2701. browser: browser2,
  2702. data: {
  2703. name: 'some sla' + random,
  2704. calendar: 'some calendar name',
  2705. first_response_time_in_text: 61
  2706. },
  2707. )
  2708. =end
  2709. def sla_create(params = {})
  2710. switch_window_focus(params)
  2711. log('sla_create', params)
  2712. instance = params[:browser] || @browser
  2713. data = params[:data]
  2714. click(
  2715. browser: instance,
  2716. css: 'a[href="#manage"]',
  2717. mute_log: true,
  2718. )
  2719. click(
  2720. browser: instance,
  2721. css: '.content.active a[href="#manage/slas"]',
  2722. mute_log: true,
  2723. )
  2724. click(
  2725. browser: instance,
  2726. css: '.content.active a.js-new',
  2727. mute_log: true,
  2728. )
  2729. modal_ready(browser: instance)
  2730. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2731. element.clear
  2732. element.send_keys(data[:name])
  2733. if data[:calendar].present?
  2734. element = instance.find_elements(css: '.modal select[name="calendar_id"]')[0]
  2735. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2736. dropdown.select_by(:text, data[:calendar])
  2737. end
  2738. element = instance.find_elements(css: '.modal input[name=first_response_time_in_text]')[0]
  2739. element.clear
  2740. element.send_keys(data[:first_response_time_in_text])
  2741. instance.find_elements(css: '.modal button.js-submit')[0].click
  2742. modal_disappear(browser: instance)
  2743. 7.times do
  2744. element = instance.find_elements(css: 'body')[0]
  2745. text = element.text
  2746. if text.match?(/#{Regexp.quote(data[:name])}/)
  2747. assert(true, 'sla created')
  2748. sleep 1
  2749. return true
  2750. end
  2751. sleep 1
  2752. end
  2753. screenshot(browser: instance, comment: 'sla_create_failed')
  2754. raise 'sla creation failed'
  2755. end
  2756. =begin
  2757. text_module_create(
  2758. browser: browser2,
  2759. data: {
  2760. name: 'some sla' + random,
  2761. keywords: 'some keywords',
  2762. content: 'some content',
  2763. },
  2764. )
  2765. =end
  2766. def text_module_create(params = {})
  2767. switch_window_focus(params)
  2768. log('text_module_create', params)
  2769. instance = params[:browser] || @browser
  2770. data = params[:data]
  2771. click(
  2772. browser: instance,
  2773. css: 'a[href="#manage"]',
  2774. mute_log: true,
  2775. )
  2776. click(
  2777. browser: instance,
  2778. css: '.content.active a[href="#manage/text_modules"]',
  2779. mute_log: true,
  2780. )
  2781. click(
  2782. browser: instance,
  2783. css: '.content.active a[data-type="new"]',
  2784. mute_log: true,
  2785. )
  2786. modal_ready(browser: instance)
  2787. set(
  2788. browser: instance,
  2789. css: '.modal input[name=name]',
  2790. value: data[:name],
  2791. )
  2792. set(
  2793. browser: instance,
  2794. css: '.modal input[name=keywords]',
  2795. value: data[:keywords],
  2796. )
  2797. set(
  2798. browser: instance,
  2799. css: '.modal [data-name=content]',
  2800. value: data[:content],
  2801. )
  2802. instance.find_elements(css: '.modal button.js-submit')[0].click
  2803. modal_disappear(browser: instance)
  2804. 7.times do
  2805. element = instance.find_elements(css: 'body')[0]
  2806. text = element.text
  2807. if text.match?(/#{Regexp.quote(data[:name])}/)
  2808. assert(true, 'text module created')
  2809. sleep 1
  2810. return true
  2811. end
  2812. sleep 1
  2813. end
  2814. screenshot(browser: instance, comment: 'text_module_create_failed')
  2815. raise 'text module creation failed'
  2816. end
  2817. =begin
  2818. signature_create(
  2819. browser: browser2,
  2820. data: {
  2821. name: 'some sla' + random,
  2822. body: 'some body',
  2823. },
  2824. )
  2825. =end
  2826. def signature_create(params = {})
  2827. switch_window_focus(params)
  2828. log('signature_create', params)
  2829. instance = params[:browser] || @browser
  2830. data = params[:data]
  2831. click(
  2832. browser: instance,
  2833. css: 'a[href="#manage"]',
  2834. mute_log: true,
  2835. )
  2836. click(
  2837. browser: instance,
  2838. css: '.content.active a[href="#channels/email"]',
  2839. mute_log: true,
  2840. )
  2841. click(
  2842. browser: instance,
  2843. css: '.content.active a[href="#c-signature"]',
  2844. mute_log: true,
  2845. )
  2846. sleep 4
  2847. click(
  2848. browser: instance,
  2849. css: '.content.active #c-signature a[data-type="new"]',
  2850. mute_log: true,
  2851. )
  2852. modal_ready(browser: instance)
  2853. set(
  2854. browser: instance,
  2855. css: '.modal input[name=name]',
  2856. value: data[:name],
  2857. )
  2858. set(
  2859. browser: instance,
  2860. css: '.modal [data-name=body]',
  2861. value: data[:body],
  2862. )
  2863. instance.find_elements(css: '.modal button.js-submit')[0].click
  2864. modal_disappear(browser: instance)
  2865. 11.times do
  2866. element = instance.find_elements(css: 'body')[0]
  2867. text = element.text
  2868. if text.match?(/#{Regexp.quote(data[:name])}/)
  2869. assert(true, 'signature created')
  2870. sleep 1
  2871. return true
  2872. end
  2873. sleep 1
  2874. end
  2875. screenshot(browser: instance, comment: 'signature_create_failed')
  2876. raise 'signature creation failed'
  2877. end
  2878. =begin
  2879. group_create(
  2880. browser: browser2,
  2881. data: {
  2882. name: 'some sla' + random,
  2883. signature: 'some signature bame',
  2884. member: [
  2885. {
  2886. login: 'some_user_login',
  2887. access: 'all',
  2888. },
  2889. ],
  2890. },
  2891. )
  2892. =end
  2893. def group_create(params = {})
  2894. switch_window_focus(params)
  2895. log('group_create', params)
  2896. instance = params[:browser] || @browser
  2897. data = params[:data]
  2898. click(
  2899. browser: instance,
  2900. css: 'a[href="#manage"]',
  2901. mute_log: true,
  2902. )
  2903. click(
  2904. browser: instance,
  2905. css: '.content.active a[href="#manage/groups"]',
  2906. mute_log: true,
  2907. )
  2908. click(
  2909. browser: instance,
  2910. css: '.content.active a[data-type="new"]',
  2911. mute_log: true,
  2912. )
  2913. modal_ready(browser: instance)
  2914. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2915. element.clear
  2916. element.send_keys(data[:name])
  2917. element = instance.find_elements(css: '.modal select[name="email_address_id"]')[0]
  2918. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2919. dropdown.select_by(:index, 1)
  2920. #dropdown.select_by(:text, action[:group])
  2921. if data[:signature]
  2922. element = instance.find_elements(css: '.modal select[name="signature_id"]')[0]
  2923. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2924. dropdown.select_by(:text, data[:signature])
  2925. end
  2926. instance.find_elements(css: '.modal button.js-submit')[0].click
  2927. modal_disappear(browser: instance)
  2928. 11.times do
  2929. element = instance.find_elements(css: 'body')[0]
  2930. text = element.text
  2931. if text.match?(/#{Regexp.quote(data[:name])}/)
  2932. assert(true, 'group created')
  2933. modal_disappear(browser: instance) # wait until modal has gone
  2934. # add member
  2935. data[:member]&.each do |member|
  2936. instance.find_elements(css: 'a[href="#manage"]')[0].click
  2937. sleep 1
  2938. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  2939. sleep 3
  2940. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  2941. element.clear
  2942. element.send_keys(member[:login])
  2943. sleep 3
  2944. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  2945. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  2946. modal_ready(browser: instance)
  2947. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  2948. instance.execute_script('$(".js-groupList tr:contains(\"' + data[:name] + '\") .js-groupListItem[value=' + member[:access] + ']").prop("checked", true)')
  2949. instance.find_elements(css: '.modal button.js-submit')[0].click
  2950. modal_disappear(browser: instance)
  2951. end
  2952. end
  2953. sleep 1
  2954. return true
  2955. end
  2956. screenshot(browser: instance, comment: 'group_create_failed')
  2957. raise 'group creation failed'
  2958. end
  2959. =begin
  2960. macro_create(
  2961. browser: browser1,
  2962. name: 'Emmanuel Macro',
  2963. ux_flow_next_up: 'Stay on tab', # possible: 'Stay on tab', 'Close tab', 'Advance to next ticket from overview'
  2964. actions: {
  2965. 'Tags' => { # currently only 'Tags' is supported
  2966. operator: 'add',
  2967. value: 'spam',
  2968. }
  2969. }
  2970. )
  2971. =end
  2972. def macro_create(params)
  2973. switch_window_focus(params)
  2974. log('macro_create', params)
  2975. instance = params[:browser] || @browser
  2976. click(
  2977. browser: instance,
  2978. css: 'a[href="#manage"]',
  2979. mute_log: true,
  2980. )
  2981. click(
  2982. browser: instance,
  2983. css: '.sidebar a[href="#manage/macros"]',
  2984. mute_log: true,
  2985. )
  2986. click(
  2987. browser: instance,
  2988. css: '.page-header-meta > a[data-type="new"]'
  2989. )
  2990. sendkey(
  2991. browser: instance,
  2992. css: '.modal-body input[name="name"]',
  2993. value: params[:name]
  2994. )
  2995. params[:actions]&.each do |attribute, changes|
  2996. select(
  2997. browser: instance,
  2998. css: '.modal .ticket_perform_action .js-filterElement .js-attributeSelector select',
  2999. value: attribute,
  3000. mute_log: true,
  3001. )
  3002. next if attribute != 'Tags'
  3003. select(
  3004. browser: instance,
  3005. css: '.modal .ticket_perform_action .js-filterElement .js-operator select',
  3006. value: changes[:operator],
  3007. mute_log: true,
  3008. )
  3009. sendkey(
  3010. browser: instance,
  3011. css: '.modal .ticket_perform_action .js-filterElement .js-value .token-input',
  3012. value: changes[:value],
  3013. mute_log: true,
  3014. )
  3015. sendkey(
  3016. browser: instance,
  3017. value: :enter,
  3018. )
  3019. end
  3020. select(
  3021. browser: instance,
  3022. css: '.modal-body select[name="ux_flow_next_up"]',
  3023. value: params[:ux_flow_next_up]
  3024. )
  3025. click(
  3026. browser: instance,
  3027. css: '.modal-footer button[type="submit"]'
  3028. )
  3029. watch_for(
  3030. browser: instance,
  3031. css: 'body',
  3032. value: params[:name],
  3033. )
  3034. assert(true, 'macro created')
  3035. end
  3036. =begin
  3037. role_create(
  3038. browser: browser2,
  3039. data: {
  3040. name: 'some role' + random,
  3041. default_at_signup: false,
  3042. permission: {
  3043. 'admin.group' => true,
  3044. 'preferences.password' => true,
  3045. },
  3046. member: [
  3047. 'some_user_login',
  3048. ],
  3049. },
  3050. )
  3051. =end
  3052. def role_create(params = {})
  3053. switch_window_focus(params)
  3054. log('role_create', params)
  3055. instance = params[:browser] || @browser
  3056. data = params[:data]
  3057. click(
  3058. browser: instance,
  3059. css: 'a[href="#manage"]',
  3060. mute_log: true,
  3061. )
  3062. click(
  3063. browser: instance,
  3064. css: '.content.active a[href="#manage/roles"]',
  3065. mute_log: true,
  3066. )
  3067. click(
  3068. browser: instance,
  3069. css: '.content.active a[data-type="new"]',
  3070. mute_log: true,
  3071. )
  3072. modal_ready(browser: instance)
  3073. element = instance.find_elements(css: '.modal input[name=name]')[0]
  3074. element.clear
  3075. element.send_keys(data[:name])
  3076. if data.key?(:default_at_signup)
  3077. element = instance.find_elements(css: '.modal select[name="default_at_signup"]')[0]
  3078. dropdown = Selenium::WebDriver::Support::Select.new(element)
  3079. if data[:default_at_signup] == true
  3080. dropdown.select_by(:text, 'yes')
  3081. else
  3082. dropdown.select_by(:text, 'no')
  3083. end
  3084. end
  3085. if data.key?(:permission)
  3086. data[:permission].each do |permission_name, permission_value|
  3087. if permission_value == false
  3088. uncheck(
  3089. browser: instance,
  3090. css: ".modal [data-permission-name=\"#{permission_name}\"]",
  3091. )
  3092. else
  3093. check(
  3094. browser: instance,
  3095. css: ".modal [data-permission-name=\"#{permission_name}\"]",
  3096. )
  3097. end
  3098. end
  3099. end
  3100. instance.find_elements(css: '.modal button.js-submit')[0].click
  3101. modal_disappear(browser: instance)
  3102. 11.times do
  3103. element = instance.find_elements(css: 'body')[0]
  3104. text = element.text
  3105. if text.match?(/#{Regexp.quote(data[:name])}/)
  3106. assert(true, 'role created')
  3107. modal_disappear(browser: instance) # wait until modal has gone
  3108. # add member
  3109. data[:member]&.each do |login|
  3110. instance.find_elements(css: 'a[href="#manage"]')[0].click
  3111. sleep 1
  3112. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  3113. sleep 3
  3114. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  3115. element.clear
  3116. element.send_keys(login)
  3117. sleep 3
  3118. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  3119. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  3120. sleep 3
  3121. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  3122. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  3123. instance.find_elements(css: '.modal button.js-submit')[0].click
  3124. modal_disappear(browser: instance)
  3125. end
  3126. end
  3127. sleep 1
  3128. return true
  3129. end
  3130. screenshot(browser: instance, comment: 'role_create_failed')
  3131. raise 'role creation failed'
  3132. end
  3133. =begin
  3134. role_create(
  3135. browser: browser2,
  3136. data: {
  3137. name: 'some role' + random,
  3138. default_at_signup: false,
  3139. permission: {
  3140. 'admin.group' => true,
  3141. 'preferences.password' => true,
  3142. },
  3143. member: [
  3144. 'some_user_login',
  3145. ],
  3146. },
  3147. )
  3148. =end
  3149. def role_edit(params = {})
  3150. switch_window_focus(params)
  3151. log('role_edit', params)
  3152. instance = params[:browser] || @browser
  3153. data = params[:data]
  3154. click(
  3155. browser: instance,
  3156. css: 'a[href="#manage"]',
  3157. mute_log: true,
  3158. )
  3159. click(
  3160. browser: instance,
  3161. css: '.content.active a[href="#manage/roles"]',
  3162. mute_log: true,
  3163. )
  3164. instance.execute_script('$(\'.content.active table tr td:contains(" ' + data[:name] + '")\').first().click()')
  3165. modal_ready(browser: instance)
  3166. element = instance.find_elements(css: '.modal input[name=name]')[0]
  3167. element.clear
  3168. element.send_keys(data[:name])
  3169. if data.key?(:default_at_signup)
  3170. element = instance.find_elements(css: '.modal select[name="default_at_signup"]')[0]
  3171. dropdown = Selenium::WebDriver::Support::Select.new(element)
  3172. if data[:default_at_signup] == true
  3173. dropdown.select_by(:text, 'yes')
  3174. else
  3175. dropdown.select_by(:text, 'no')
  3176. end
  3177. end
  3178. if data.key?(:permission)
  3179. data[:permission].each do |permission_name, permission_value|
  3180. if permission_value == false
  3181. uncheck(
  3182. browser: instance,
  3183. css: ".modal [data-permission-name=\"#{permission_name}\"]",
  3184. )
  3185. else
  3186. check(
  3187. browser: instance,
  3188. css: ".modal [data-permission-name=\"#{permission_name}\"]",
  3189. )
  3190. end
  3191. end
  3192. end
  3193. if data.key?(:group_permissions)
  3194. data[:group_permissions].each do |key, value|
  3195. value.each do |permission|
  3196. check(
  3197. browser: instance,
  3198. css: ".modal input[name=\"group_ids::#{key}\"][value=\"#{permission}\"]",
  3199. )
  3200. end
  3201. end
  3202. end
  3203. if data.key?(:active)
  3204. element = instance.find_elements(css: '.modal select[name="active"]')[0]
  3205. dropdown = Selenium::WebDriver::Support::Select.new(element)
  3206. if data[:active] == true
  3207. dropdown.select_by(:text, 'active')
  3208. else
  3209. dropdown.select_by(:text, 'inactive')
  3210. end
  3211. end
  3212. instance.find_elements(css: '.modal button.js-submit')[0].click
  3213. modal_disappear(browser: instance)
  3214. 11.times do
  3215. element = instance.find_elements(css: 'body')[0]
  3216. text = element.text
  3217. if text.match?(/#{Regexp.quote(data[:name])}/)
  3218. assert(true, 'role created')
  3219. modal_disappear(browser: instance) # wait until modal has gone
  3220. # add member
  3221. data[:member]&.each do |login|
  3222. instance.find_elements(css: 'a[href="#manage"]')[0].click
  3223. sleep 1
  3224. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  3225. sleep 3
  3226. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  3227. element.clear
  3228. element.send_keys(login)
  3229. sleep 3
  3230. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  3231. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  3232. sleep 3
  3233. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  3234. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  3235. instance.find_elements(css: '.modal button.js-submit')[0].click
  3236. modal_disappear(browser: instance)
  3237. end
  3238. end
  3239. sleep 1
  3240. return true
  3241. end
  3242. screenshot(browser: instance, comment: 'role_edit_failed')
  3243. raise 'role edit failed'
  3244. end
  3245. =begin
  3246. object_manager_attribute_create(
  3247. browser: browser2,
  3248. data: {
  3249. object: 'Ticket', # optional, defaults to Ticket
  3250. name: 'field_name' + random,
  3251. display: 'Display Name of Field',
  3252. data_type: 'Select',
  3253. data_option: {
  3254. options: {
  3255. 'aa' => 'AA',
  3256. 'bb' => 'BB',
  3257. },
  3258. default: 'abc',
  3259. },
  3260. },
  3261. error: 'already exists'
  3262. )
  3263. object_manager_attribute_create(
  3264. browser: browser2,
  3265. data: {
  3266. object: 'Ticket', # optional, defaults to Ticket
  3267. name: 'field_name' + random,
  3268. display: 'Display Name of Field',
  3269. data_type: 'Text',
  3270. data_option: {
  3271. default: 'abc',
  3272. maxlength: 20,
  3273. },
  3274. },
  3275. error: 'already exists'
  3276. )
  3277. object_manager_attribute_create(
  3278. browser: browser2,
  3279. data: {
  3280. object: 'Ticket', # optional, defaults to Ticket
  3281. name: 'field_name' + random,
  3282. display: 'Display Name of Field',
  3283. data_type: 'Integer',
  3284. data_option: {
  3285. default: '15',
  3286. min: 1,
  3287. max: 999999,
  3288. },
  3289. },
  3290. error: 'already exists'
  3291. )
  3292. object_manager_attribute_create(
  3293. browser: browser2,
  3294. data: {
  3295. object: 'Ticket', # optional, defaults to Ticket
  3296. name: 'field_name' + random,
  3297. display: 'Display Name of Field',
  3298. data_type: 'Datetime',
  3299. data_option: {
  3300. future: true,
  3301. past: true,
  3302. diff: 24,
  3303. },
  3304. },
  3305. error: 'already exists'
  3306. )
  3307. object_manager_attribute_create(
  3308. browser: browser2,
  3309. data: {
  3310. object: 'Ticket', # optional, defaults to Ticket
  3311. name: 'field_name' + random,
  3312. display: 'Display Name of Field',
  3313. data_type: 'Date',
  3314. data_option: {
  3315. future: true,
  3316. past: true,
  3317. diff: 24,
  3318. },
  3319. },
  3320. error: 'already exists'
  3321. )
  3322. object_manager_attribute_create(
  3323. browser: browser2,
  3324. data: {
  3325. object: 'Ticket', # optional, defaults to Ticket
  3326. name: 'field_name' + random,
  3327. display: 'Display Name of Field',
  3328. data_type: 'Boolean',
  3329. data_option: {
  3330. options: {
  3331. true: 'YES',
  3332. false: 'NO',
  3333. }
  3334. default: undefined,
  3335. },
  3336. },
  3337. error: 'already exists'
  3338. )
  3339. =end
  3340. def object_manager_attribute_create(params = {})
  3341. switch_window_focus(params)
  3342. log('object_manager_attribute_create', params)
  3343. instance = params[:browser] || @browser
  3344. data = params[:data]
  3345. data[:object] = data[:object] || 'Ticket'
  3346. raise 'invalid object parameter in object_manager_attribute_create' if %w[Ticket User Organization Group].exclude? data[:object]
  3347. # make sure that required params are supplied
  3348. %i[name display data_type].each do |s|
  3349. next if data.key? s
  3350. raise "missing required param #{s} in object_manager_attribute_create()"
  3351. end
  3352. click(
  3353. browser: instance,
  3354. css: 'a[href="#manage"]',
  3355. mute_log: true,
  3356. )
  3357. click(
  3358. browser: instance,
  3359. css: '.content.active a[href="#system/object_manager"]',
  3360. mute_log: true,
  3361. )
  3362. watch_for(
  3363. browser: instance,
  3364. css: '.content.active .js-new',
  3365. )
  3366. click(
  3367. browser: instance,
  3368. css: ".content.active a[href='#c-#{data[:object]}']",
  3369. mute_log: true,
  3370. )
  3371. click(
  3372. browser: instance,
  3373. css: ".content.active #c-#{data[:object]} .js-new",
  3374. mute_log: true,
  3375. )
  3376. object_manager_attribute_perform('create', params)
  3377. end
  3378. =begin
  3379. object_manager_attribute_update(
  3380. browser: browser2,
  3381. data: {
  3382. object: 'Ticket', # optional, defaults to Ticket
  3383. name: 'field_name' + random,
  3384. display: 'Display Name of Field',
  3385. data_type: 'Select',
  3386. data_option: {
  3387. options: {
  3388. 'aa' => 'AA',
  3389. 'bb' => 'BB',
  3390. },
  3391. default: 'abc',
  3392. },
  3393. },
  3394. error: 'already exists'
  3395. )
  3396. =end
  3397. def object_manager_attribute_update(params = {})
  3398. switch_window_focus(params)
  3399. log('object_manager_attribute_update', params)
  3400. instance = params[:browser] || @browser
  3401. data = params[:data]
  3402. data[:object] = data[:object] || 'Ticket'
  3403. raise 'invalid object parameter in object_manager_attribute_update' if %w[Ticket User Organization Group].exclude? data[:object]
  3404. click(
  3405. browser: instance,
  3406. css: 'a[href="#manage"]',
  3407. mute_log: true,
  3408. )
  3409. click(
  3410. browser: instance,
  3411. css: '.content.active a[href="#system/object_manager"]',
  3412. mute_log: true,
  3413. )
  3414. watch_for(
  3415. browser: instance,
  3416. css: '.content.active .js-new',
  3417. )
  3418. click(
  3419. browser: instance,
  3420. css: ".content.active a[href='#c-#{data[:object]}']",
  3421. mute_log: true,
  3422. )
  3423. instance.execute_script("$(\".content.active #c-#{data[:object]} td:contains('#{data[:name]}')\").first().click()")
  3424. object_manager_attribute_perform('update', params)
  3425. end
  3426. =begin
  3427. object_manager_attribute_delete(
  3428. browser: browser2,
  3429. data: {
  3430. object: 'Ticket', # optional, defaults to Ticket
  3431. name: 'field_name' + random,
  3432. },
  3433. )
  3434. =end
  3435. def object_manager_attribute_delete(params = {})
  3436. switch_window_focus(params)
  3437. log('object_manager_attribute_delete', params)
  3438. instance = params[:browser] || @browser
  3439. data = params[:data]
  3440. data[:object] = data[:object] || 'Ticket'
  3441. raise 'invalid object parameter in object_manager_attribute_delete' if %w[Ticket User Organization Group].exclude? data[:object]
  3442. click(
  3443. browser: instance,
  3444. css: 'a[href="#manage"]',
  3445. mute_log: true,
  3446. )
  3447. click(
  3448. browser: instance,
  3449. css: '.content.active a[href="#system/object_manager"]',
  3450. mute_log: true,
  3451. )
  3452. watch_for(
  3453. browser: instance,
  3454. css: '.content.active .js-new',
  3455. )
  3456. click(
  3457. browser: instance,
  3458. css: ".content.active a[href='#c-#{data[:object]}']",
  3459. mute_log: true,
  3460. )
  3461. sleep 4
  3462. r = instance.execute_script("$(\".content.active #c-#{data[:object]} td:contains('#{data[:name]}')\").first().closest('tr').find('.js-delete').click()")
  3463. end
  3464. =begin
  3465. object_manager_attribute_discard_changes(
  3466. browser: browser2,
  3467. )
  3468. =end
  3469. def object_manager_attribute_discard_changes(params = {})
  3470. switch_window_focus(params)
  3471. log('object_manager_attribute_discard_changes', params)
  3472. instance = params[:browser] || @browser
  3473. click(
  3474. browser: instance,
  3475. css: 'a[href="#manage"]',
  3476. mute_log: true,
  3477. )
  3478. click(
  3479. browser: instance,
  3480. css: '.content.active a[href="#system/object_manager"]',
  3481. mute_log: true,
  3482. )
  3483. sleep 4
  3484. element = instance.find_elements(css: '.content.active .js-discard').first
  3485. element.click
  3486. watch_for_disappear(
  3487. browser: instance,
  3488. css: '.content.active .js-discard',
  3489. )
  3490. end
  3491. =begin
  3492. Execute any pending migrations in the object attribute manager
  3493. object_manager_attribute_migrate(
  3494. browser: browser2,
  3495. )
  3496. =end
  3497. def object_manager_attribute_migrate(params = {})
  3498. switch_window_focus(params)
  3499. log('object_manager_attribute_migrate', params)
  3500. instance = params[:browser] || @browser
  3501. watch_for(
  3502. browser: instance,
  3503. css: '.content.active',
  3504. value: 'Database Update required',
  3505. mute_log: true,
  3506. )
  3507. click(
  3508. browser: instance,
  3509. css: '.content.active .tab-pane.active div.js-execute',
  3510. mute_log: true,
  3511. )
  3512. modal_ready(
  3513. browser: instance,
  3514. )
  3515. title_text = instance.find_elements(css: '.modal .modal-title').first.text
  3516. if title_text == 'Zammad is restarting...'
  3517. # in the complex case, wait for server to restart
  3518. modal_disappear(
  3519. browser: instance,
  3520. timeout: 7.minutes,
  3521. )
  3522. elsif title_text == 'Config has changed'
  3523. # in the simple case, just click the submit button
  3524. click(
  3525. browser: instance,
  3526. css: '.modal .js-submit',
  3527. mute_log: true,
  3528. )
  3529. else
  3530. raise "Unknown title text \"#{title_text}\" found when trying to update database"
  3531. end
  3532. sleep 5
  3533. watch_for(
  3534. browser: instance,
  3535. css: '.content.active',
  3536. mute_log: true,
  3537. )
  3538. end
  3539. =begin
  3540. tags_verify(
  3541. browser: browser2,
  3542. tags: {
  3543. 'tag 1' => true,
  3544. 'tag 2' => true,
  3545. 'tag 3' => false,
  3546. },
  3547. )
  3548. =end
  3549. def tags_verify(params = {})
  3550. switch_window_focus(params)
  3551. log('tags_verify', params)
  3552. instance = params[:browser] || @browser
  3553. tags = instance.find_elements({ css: '.content.active .js-tag' })
  3554. assert(tags)
  3555. assert(tags[0])
  3556. tags_found = {}
  3557. params[:tags].each_key do |key|
  3558. tags_found[key] = false
  3559. end
  3560. tags.each do |element|
  3561. text = element.text
  3562. if tags_found.key?(text)
  3563. tags_found[text] = true
  3564. else
  3565. assert(false, "tag exists but is not in check to verify '#{text}'")
  3566. end
  3567. end
  3568. params[:tags].each do |key, value|
  3569. assert_equal(value, tags_found[key], "tag '#{key}'")
  3570. end
  3571. end
  3572. def quote(string)
  3573. string_quoted = string
  3574. string_quoted.gsub!(/&/, '&amp;')
  3575. string_quoted.gsub!(/</, '&lt;')
  3576. string_quoted.gsub!(/>/, '&gt;')
  3577. string_quoted
  3578. end
  3579. def switch_window_focus(params)
  3580. instance = params[:browser] || @browser
  3581. if instance != @last_used_browser
  3582. log('switch browser window focus', {})
  3583. instance.switch_to.window(instance.window_handles.first)
  3584. end
  3585. @last_used_browser = instance
  3586. end
  3587. def log(method, params = {})
  3588. begin
  3589. instance = params[:browser] || @browser
  3590. if instance
  3591. logs = instance.manage.logs.get(:browser)
  3592. logs.each do |log|
  3593. next if log.level == 'WARNING' && log.message =~ /Declaration\sdropped./ # ignore ff css warnings
  3594. time = Time.zone.parse(Time.zone.at(log.timestamp / 1000).to_datetime.to_s)
  3595. puts "#{time}/#{log.level}: #{log.message}"
  3596. end
  3597. end
  3598. rescue
  3599. # failed to get logs
  3600. end
  3601. return if !DEBUG
  3602. return if params[:mute_log]
  3603. puts "#{Time.zone.now}/#{method}: #{params.inspect}"
  3604. end
  3605. private
  3606. def add_tree_options(instance:, options:)
  3607. # first level entries have to get added in regular order
  3608. options.each_key.with_index do |option, index|
  3609. if index != 0
  3610. element = instance.find_elements(css: '.modal .js-treeTable .js-addRow')[index - 1]
  3611. element.click
  3612. end
  3613. element = instance.find_elements(css: '.modal .js-treeTable .js-key')[index]
  3614. element.clear
  3615. element.send_keys(option)
  3616. end
  3617. add_sub_tree_recursion(
  3618. instance: instance,
  3619. options: options,
  3620. )
  3621. end
  3622. def add_sub_tree_recursion(instance:, options:, offset: 0)
  3623. options.each_value.inject(offset) do |child_offset, children|
  3624. child_offset += 1
  3625. # put your recursion glasses on 8-)
  3626. add_sub_tree_options(
  3627. instance: instance,
  3628. options: children,
  3629. offset: child_offset,
  3630. )
  3631. end
  3632. end
  3633. def add_sub_tree_options(instance:, options:, offset:)
  3634. # sub level entries have to get added in reversed order
  3635. level_options = options.to_a.reverse.to_h.keys
  3636. level_options.each do |option|
  3637. # sub level entries have to get added via 'add child row' link
  3638. click_index = offset - 1
  3639. element = instance.find_elements(css: '.modal .js-treeTable .js-addChild')[click_index]
  3640. element.click
  3641. element = instance.find_elements(css: '.modal .js-treeTable .js-key')[offset]
  3642. element.clear
  3643. element.send_keys(option)
  3644. sleep 0.25
  3645. end
  3646. add_sub_tree_recursion(
  3647. instance: instance,
  3648. options: options,
  3649. offset: offset,
  3650. )
  3651. end
  3652. def token_verify(css, value)
  3653. original_element = @browser.find_element(:css, css)
  3654. elem = original_element.find_element(xpath: '../input[contains(@class, "token-input")]')
  3655. elem.send_keys value
  3656. elem.send_keys :enter
  3657. watch_for(
  3658. xpath: '../*/span[contains(@class,"token-label")]',
  3659. value: value,
  3660. container: original_element
  3661. )
  3662. end
  3663. def toggle_checkbox(scope, value)
  3664. checkbox = scope.find_element(css: "input[value=#{value}]")
  3665. @browser
  3666. .action
  3667. .move_to(checkbox)
  3668. .click
  3669. .perform
  3670. end
  3671. def checkbox_is_selected(scope, value)
  3672. scope.find_element(css: "input[value=#{value}]").property('checked')
  3673. end
  3674. =begin
  3675. Retrieve a hash of all the avaiable Zammad settings and their current values.
  3676. settings = fetch_settings()
  3677. =end
  3678. def fetch_settings
  3679. url = URI.parse(browser_url)
  3680. req = Net::HTTP::Get.new(browser_url + '/api/v1/settings/')
  3681. req.basic_auth('master@example.com', 'test')
  3682. res = Net::HTTP.start(url.host, url.port) do |http|
  3683. http.request(req)
  3684. end
  3685. raise "HTTP error #{res.code} while fetching #{browser_url}/api/v1/settings/" if res.code != '200'
  3686. JSON.parse(res.body)
  3687. end
  3688. =begin
  3689. Enable or disable Zammad experiemental features remotely.
  3690. set_setting('ui_ticket_zoom_attachments_preview', true)
  3691. =end
  3692. def set_setting(name, value)
  3693. name_to_id = fetch_settings.map { |s| [s['name'], s['id']] }.to_h
  3694. id = name_to_id[name]
  3695. url = URI.parse(browser_url)
  3696. req = Net::HTTP::Put.new("#{browser_url}/api/v1/settings/#{id}")
  3697. req['Content-Type'] = 'application/json'
  3698. req.basic_auth('master@example.com', 'test')
  3699. req.body = { 'state_current' => { 'value' => value } }.to_json
  3700. res = Net::HTTP.start(url.host, url.port) do |http|
  3701. http.request(req)
  3702. end
  3703. raise "HTTP error #{res.code} while POSTing to #{browser_url}/api/v1/settings/" if res.code != '200'
  3704. end
  3705. =begin
  3706. Helper method for both object_manager_attribute_create and object_manager_attribute_update
  3707. =end
  3708. def object_manager_attribute_perform(action = 'create', params = {})
  3709. instance = params[:browser] || @browser
  3710. data = params[:data]
  3711. modal_ready(browser: instance)
  3712. if action == 'create'
  3713. set(
  3714. browser: instance,
  3715. css: '.modal input[name=name]',
  3716. value: data[:name],
  3717. mute_log: true,
  3718. )
  3719. end
  3720. if data[:display]
  3721. set(
  3722. browser: instance,
  3723. css: '.modal input[name=display]',
  3724. value: data[:display],
  3725. mute_log: true,
  3726. )
  3727. end
  3728. if data[:data_type]
  3729. select(
  3730. browser: instance,
  3731. css: '.modal select[name="data_type"]',
  3732. value: data[:data_type],
  3733. mute_log: true,
  3734. )
  3735. end
  3736. if data[:data_option]
  3737. if data[:data_option][:options]
  3738. if data[:data_type] == 'Boolean'
  3739. # rubocop:disable Lint/BooleanSymbol
  3740. element = instance.find_elements(css: '.modal .js-valueTrue').first
  3741. element.clear
  3742. element.send_keys(data[:data_option][:options][:true])
  3743. element = instance.find_elements(css: '.modal .js-valueFalse').first
  3744. element.clear
  3745. element.send_keys(data[:data_option][:options][:false])
  3746. # rubocop:enable Lint/BooleanSymbol
  3747. elsif data[:data_type] == 'Tree Select'
  3748. add_tree_options(
  3749. instance: instance,
  3750. options: data[:data_option][:options],
  3751. )
  3752. else
  3753. if action == 'update'
  3754. # first clear all existing entries
  3755. loop do
  3756. target = {
  3757. browser: instance,
  3758. css: '.modal .js-Table .js-remove',
  3759. mute_log: true,
  3760. }
  3761. break if !instance.find_elements(css: target[:css])[0]
  3762. click(target)
  3763. end
  3764. sleep 1
  3765. end
  3766. # then populate the table with the new values
  3767. data[:data_option][:options].each do |key, value|
  3768. element = instance.find_elements(css: '.modal .js-Table .js-key').last
  3769. element.clear
  3770. element.send_keys(key)
  3771. element = instance.find_elements(css: '.modal .js-Table .js-value').last
  3772. element.clear
  3773. element.send_keys(value)
  3774. element = instance.find_elements(css: '.modal .js-Table .js-add')[0]
  3775. element.click
  3776. end
  3777. end
  3778. end
  3779. %i[default min max diff].each do |key|
  3780. next if !data[:data_option].key?(key)
  3781. element = instance.find_elements(css: ".modal [name=\"data_option::#{key}\"]").first
  3782. element.clear
  3783. element.send_keys(data[:data_option][key])
  3784. end
  3785. %i[future past].each do |key|
  3786. next if !data[:data_option].key?(key)
  3787. select(
  3788. browser: instance,
  3789. css: ".modal select[name=\"data_option::#{key}\"]",
  3790. value: data[:data_option][key],
  3791. mute_log: true,
  3792. )
  3793. end
  3794. %i[maxlength].each do |key|
  3795. next if !data[:data_option].key?(key)
  3796. set(
  3797. browser: instance,
  3798. css: ".modal input[name=\"data_option::#{key}\"]",
  3799. value: data[:data_option][key],
  3800. mute_log: true,
  3801. )
  3802. end
  3803. end
  3804. if params[:do_not_submit]
  3805. assert(true, "attribute #{action}d without submit")
  3806. return true
  3807. end
  3808. instance.find_elements(css: '.modal button.js-submit')[0].click
  3809. if params[:error]
  3810. sleep 4
  3811. watch_for(
  3812. css: '.modal',
  3813. value: params[:error],
  3814. )
  3815. click(
  3816. browser: instance,
  3817. css: '.modal .js-close',
  3818. )
  3819. modal_disappear(browser: instance)
  3820. return
  3821. end
  3822. 11.times do
  3823. element = instance.find_elements(css: 'body')[0]
  3824. text = element.text
  3825. if text.match?(/#{Regexp.quote(data[:name])}/)
  3826. assert(true, 'object manager attribute updated')
  3827. sleep 1
  3828. return true
  3829. end
  3830. sleep 1
  3831. end
  3832. screenshot(browser: instance, comment: "object_manager_attribute_#{action}_failed")
  3833. raise "object_manager_attribute_#{action}_failed"
  3834. end
  3835. end