browser_test_helper.rb 122 KB

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