browser_test_helper.rb 118 KB

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