browser_test_helper.rb 122 KB

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