browser_test_helper.rb 127 KB

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