browser_test_helper.rb 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624
  1. ENV['RAILS_ENV'] = 'test'
  2. # rubocop:disable HandleExceptions, ClassVars, NonLocalExitFromIterator
  3. require File.expand_path('../../config/environment', __FILE__)
  4. require 'selenium-webdriver'
  5. class TestCase < Test::Unit::TestCase
  6. @@debug = true
  7. def browser
  8. ENV['BROWSER'] || 'firefox'
  9. end
  10. def profile
  11. browser_profile = nil
  12. if browser == 'firefox'
  13. browser_profile = Selenium::WebDriver::Firefox::Profile.new
  14. browser_profile['intl.locale.matchOS'] = false
  15. browser_profile['intl.accept_languages'] = 'en-US'
  16. browser_profile['general.useragent.locale'] = 'en-US'
  17. # currently console log not working for firefox
  18. # https://github.com/SeleniumHQ/selenium/issues/1161
  19. #browser_profile['loggingPref'] = { browser: :all }
  20. elsif browser == 'chrome'
  21. # profile are only working on remote selenium
  22. if ENV['REMOTE_URL']
  23. browser_profile = Selenium::WebDriver::Chrome::Profile.new
  24. browser_profile['intl.accept_languages'] = 'en'
  25. browser_profile['loggingPref'] = { browser: :all }
  26. end
  27. end
  28. browser_profile
  29. end
  30. def browser_support_cookies
  31. if browser =~ /(internet_explorer|ie)/i
  32. return false
  33. end
  34. true
  35. end
  36. def browser_url
  37. ENV['BROWSER_URL'] || 'http://localhost:3000'
  38. end
  39. def browser_instance
  40. if !@browsers
  41. @browsers = {}
  42. end
  43. if !ENV['REMOTE_URL'] || ENV['REMOTE_URL'].empty?
  44. local_browser = Selenium::WebDriver.for(browser.to_sym, profile: profile)
  45. @browsers[local_browser.hash] = local_browser
  46. browser_instance_preferences(local_browser)
  47. return local_browser
  48. end
  49. # avoid "Cannot read property 'get_Current' of undefined" issues
  50. (1..5).each { |count|
  51. begin
  52. local_browser = browser_instance_remote
  53. break
  54. rescue
  55. wait_until_ready = rand(9) + 5
  56. sleep wait_until_ready
  57. log('browser_instance', { rescure: true, count: count, sleep: wait_until_ready })
  58. end
  59. }
  60. local_browser
  61. end
  62. def browser_instance_remote
  63. caps = Selenium::WebDriver::Remote::Capabilities.send(browser)
  64. if ENV['BROWSER_OS']
  65. caps.platform = ENV['BROWSER_OS']
  66. end
  67. if ENV['BROWSER_VERSION']
  68. caps.version = ENV['BROWSER_VERSION']
  69. end
  70. local_browser = Selenium::WebDriver.for(
  71. :remote,
  72. url: ENV['REMOTE_URL'],
  73. desired_capabilities: caps,
  74. )
  75. @browsers[local_browser.hash] = local_browser
  76. browser_instance_preferences(local_browser)
  77. # upload files from remote dir
  78. local_browser.file_detector = lambda do |args|
  79. str = args.first.to_s
  80. str if File.file?(str)
  81. end
  82. local_browser
  83. end
  84. def browser_instance_close(local_browser)
  85. return if !@browsers[local_browser.hash]
  86. @browsers.delete(local_browser.hash)
  87. local_browser.quit
  88. end
  89. def browser_instance_preferences(local_browser)
  90. browser_width = ENV['BROWSER_WIDTH'] || 1024
  91. browser_height = ENV['BROWSER_HEIGHT'] || 800
  92. local_browser.manage.window.resize_to(browser_width, browser_height)
  93. if ENV['REMOTE_URL'] !~ /saucelabs|(grid|ci)\.(zammad\.org|znuny\.com)/i
  94. if @browsers.count == 1
  95. local_browser.manage.window.move_to(0, 0)
  96. else
  97. local_browser.manage.window.move_to(browser_width, 0)
  98. end
  99. end
  100. local_browser.manage.timeouts.implicit_wait = 3 # seconds
  101. end
  102. def teardown
  103. return if !@browsers
  104. @browsers.each { |_hash, local_browser|
  105. screenshot(browser: local_browser, comment: 'teardown')
  106. browser_instance_close(local_browser)
  107. }
  108. end
  109. def screenshot(params)
  110. instance = params[:browser] || @browser
  111. comment = params[:comment] || ''
  112. filename = "tmp/#{Time.zone.now.strftime('screenshot_%Y_%m_%d__%H_%M_%S_%L')}_#{comment}#{instance.hash}.png"
  113. log('screenshot', { filename: filename })
  114. instance.save_screenshot(filename)
  115. end
  116. =begin
  117. username = login(
  118. browser: browser1,
  119. username: 'someuser',
  120. password: 'somepassword',
  121. url: 'some url', # optional
  122. remember_me: true, # optional
  123. auto_wizard: false, # optional, in case of auto wizard, skip login
  124. success: false, #optional
  125. )
  126. =end
  127. def login(params)
  128. switch_window_focus(params)
  129. log('login', params)
  130. instance = params[:browser] || @browser
  131. if params[:url]
  132. instance.get(params[:url])
  133. end
  134. # submit logs anyway
  135. instance.execute_script('App.Track.force()')
  136. element = instance.find_elements(css: '#login input[name="username"]')[0]
  137. if !element
  138. if params[:auto_wizard]
  139. watch_for(
  140. browser: instance,
  141. css: 'body',
  142. value: 'auto wizard is enabled',
  143. timeout: 10,
  144. )
  145. location(url: "#{browser_url}/#getting_started/auto_wizard")
  146. sleep 10
  147. login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
  148. if login != params[:username]
  149. screenshot(browser: instance, comment: 'auto wizard login failed')
  150. raise 'auto wizard login failed'
  151. end
  152. assert(true, 'auto wizard login ok')
  153. clues_close(
  154. browser: instance,
  155. optional: true,
  156. )
  157. return
  158. end
  159. screenshot(browser: instance, comment: 'login_failed')
  160. raise 'No login box found'
  161. end
  162. screenshot(browser: instance, comment: 'login')
  163. element.clear
  164. element.send_keys(params[:username])
  165. element = instance.find_elements(css: '#login input[name="password"]')[0]
  166. element.clear
  167. element.send_keys(params[:password])
  168. if params[:remember_me]
  169. instance.find_elements(css: '#login .checkbox-replacement')[0].click
  170. end
  171. instance.find_elements(css: '#login button')[0].click
  172. sleep 4
  173. login_failed = false
  174. if !instance.find_elements(css: '.user-menu .user a')[0]
  175. login_failed = true
  176. else
  177. login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
  178. if login != params[:username]
  179. login_failed = true
  180. end
  181. end
  182. if login_failed
  183. if params[:success] == false
  184. assert(true, 'login not successfull, like wanted')
  185. return true
  186. end
  187. screenshot(browser: instance, comment: 'login_failed')
  188. raise 'login failed'
  189. end
  190. if params[:success] == false
  191. raise 'login successfull but should not'
  192. end
  193. clues_close(
  194. browser: instance,
  195. optional: true,
  196. )
  197. screenshot(browser: instance, comment: 'login_ok')
  198. assert(true, 'login ok')
  199. login
  200. end
  201. =begin
  202. logout(
  203. browser: browser1
  204. )
  205. =end
  206. def logout(params = {})
  207. switch_window_focus(params)
  208. log('logout', params)
  209. instance = params[:browser] || @browser
  210. click(
  211. browser: instance,
  212. css: 'a[href="#current_user"]',
  213. mute_log: true,
  214. )
  215. click(
  216. browser: instance,
  217. css: 'a[href="#logout"]',
  218. mute_log: true,
  219. )
  220. 5.times {
  221. sleep 1
  222. login = instance.find_elements(css: '#login')[0]
  223. next if !login
  224. screenshot(browser: instance, comment: 'logout_ok')
  225. assert(true, 'logout ok')
  226. return
  227. }
  228. screenshot(browser: instance, comment: 'logout_failed')
  229. raise 'no login box found, seems logout was not successfully!'
  230. end
  231. =begin
  232. clues_close(
  233. browser: browser1,
  234. optional: false,
  235. )
  236. =end
  237. def clues_close(params = {})
  238. switch_window_focus(params)
  239. log('clues_close', params)
  240. instance = params[:browser] || @browser
  241. clues = instance.find_elements(css: '.js-modal--clue .js-close')[0]
  242. if !params[:optional] && !clues
  243. screenshot(browser: instance, comment: 'no_clues')
  244. raise 'Unable to closes clues, no clues found!'
  245. end
  246. return if !clues
  247. instance.execute_script("$('.js-modal--clue .js-close').click()")
  248. assert(true, 'clues closed')
  249. sleep 1
  250. end
  251. =begin
  252. notify_close(
  253. browser: browser1,
  254. optional: false,
  255. )
  256. =end
  257. def notify_close(params = {})
  258. switch_window_focus(params)
  259. log('notify_close', params)
  260. instance = params[:browser] || @browser
  261. notify = instance.find_elements(css: '.noty_inline_layout_container.i-am-new')[0]
  262. if !params[:optional] && !notify
  263. screenshot(browser: instance, comment: 'no_notify')
  264. raise 'Unable to closes notify, no notify found!'
  265. end
  266. return if !notify
  267. notify.click
  268. assert(true, 'notify closed')
  269. sleep 1
  270. end
  271. =begin
  272. location(
  273. browser: browser1,
  274. url: 'http://someurl',
  275. )
  276. =end
  277. def location(params)
  278. switch_window_focus(params)
  279. log('location', params)
  280. instance = params[:browser] || @browser
  281. instance.get(params[:url])
  282. # check if reload was successfull
  283. if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
  284. instance.navigate.refresh
  285. end
  286. screenshot(browser: instance, comment: 'location')
  287. end
  288. =begin
  289. location_check(
  290. browser: browser1,
  291. url: 'http://someurl',
  292. )
  293. =end
  294. def location_check(params)
  295. switch_window_focus(params)
  296. log('location_check', params)
  297. instance = params[:browser] || @browser
  298. sleep 0.7
  299. current_url = instance.current_url
  300. if current_url !~ /#{Regexp.quote(params[:url])}/
  301. screenshot(browser: instance, comment: 'location_check_failed')
  302. raise "url #{current_url} is not matching #{params[:url]}"
  303. end
  304. assert(true, "url #{current_url} is matching #{params[:url]}")
  305. end
  306. =begin
  307. reload(
  308. browser: browser1,
  309. )
  310. =end
  311. def reload(params = {})
  312. switch_window_focus(params)
  313. log('reload', params)
  314. instance = params[:browser] || @browser
  315. screenshot(browser: instance, comment: 'reload_before')
  316. instance.navigate.refresh
  317. # check if reload was successfull
  318. if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
  319. instance.navigate.refresh
  320. end
  321. screenshot(browser: instance, comment: 'reload_after')
  322. end
  323. =begin
  324. click(
  325. browser: browser1,
  326. css: '.some_class',
  327. fast: false, # do not wait
  328. wait: 1, # wait 1 sec.
  329. )
  330. click(
  331. browser: browser1,
  332. text: '.partial_link_text',
  333. fast: false, # do not wait
  334. wait: 1, # wait 1 sec.
  335. )
  336. =end
  337. def click(params)
  338. switch_window_focus(params)
  339. log('click', params)
  340. instance = params[:browser] || @browser
  341. screenshot(browser: instance, comment: 'click_before')
  342. if params[:css]
  343. begin
  344. element = instance.find_elements(css: params[:css])[0]
  345. #if element
  346. # instance.mouse.move_to(element)
  347. #end
  348. element.click
  349. rescue => e
  350. sleep 0.5
  351. # just try again
  352. log('click', { rescure: true })
  353. element = instance.find_elements(css: params[:css])[0]
  354. #if element
  355. # instance.mouse.move_to(element)
  356. #end
  357. element.click
  358. end
  359. else
  360. sleep 0.5
  361. begin
  362. instance.find_elements(partial_link_text: params[:text])[0].click
  363. rescue => e
  364. sleep 0.5
  365. # just try again
  366. log('click', { rescure: true })
  367. instance.find_elements(partial_link_text: params[:text])[0].click
  368. end
  369. end
  370. sleep 0.2 if !params[:fast]
  371. sleep params[:wait] if params[:wait]
  372. end
  373. =begin
  374. scroll_to(
  375. browser: browser1,
  376. position: 'top', # botton
  377. css: '.some_class',
  378. )
  379. =end
  380. def scroll_to(params)
  381. switch_window_focus(params)
  382. log('scroll_to', params)
  383. instance = params[:browser] || @browser
  384. position = 'true'
  385. if params[:position] == 'botton'
  386. position = 'false'
  387. end
  388. execute(
  389. browser: instance,
  390. js: "\$('#{params[:css]}').get(0).scrollIntoView(#{position})",
  391. mute_log: params[:mute_log]
  392. )
  393. sleep 0.3
  394. end
  395. =begin
  396. modal_ready(
  397. browser: browser1,
  398. )
  399. =end
  400. def modal_ready(params = {})
  401. switch_window_focus(params)
  402. log('modal_ready', params)
  403. instance = params[:browser] || @browser
  404. sleep 3
  405. end
  406. =begin
  407. modal_disappear(
  408. browser: browser1,
  409. timeout: 12, # default 8
  410. )
  411. =end
  412. def modal_disappear(params = {})
  413. switch_window_focus(params)
  414. log('modal_disappear', params)
  415. instance = params[:browser] || @browser
  416. watch_for_disappear(
  417. browser: instance,
  418. css: '.modal',
  419. timeout: params[:timeout] || 8,
  420. )
  421. end
  422. =begin
  423. execute(
  424. browser: browser1,
  425. js: '.some_class',
  426. )
  427. =end
  428. def execute(params)
  429. switch_window_focus(params)
  430. log('js', params)
  431. instance = params[:browser] || @browser
  432. if params[:js]
  433. return instance.execute_script(params[:js])
  434. end
  435. raise "Invalid execute params #{params.inspect}"
  436. end
  437. =begin
  438. exists(
  439. browser: browser1,
  440. css: '.some_class',
  441. )
  442. exists(
  443. displayed: false, # true|false
  444. browser: browser1,
  445. css: '.some_class',
  446. )
  447. =end
  448. def exists(params)
  449. switch_window_focus(params)
  450. log('exists', params)
  451. instance = params[:browser] || @browser
  452. if !instance.find_elements(css: params[:css])[0]
  453. screenshot(browser: instance, comment: 'exists_failed')
  454. raise "#{params[:css]} dosn't exist, but should"
  455. end
  456. if params.key?(:displayed)
  457. if params[:displayed] == true && !instance.find_elements(css: params[:css])[0].displayed?
  458. raise "#{params[:css]} is not displayed, but should"
  459. end
  460. if params[:displayed] == false && instance.find_elements(css: params[:css])[0].displayed?
  461. raise "#{params[:css]} is displayed, but should not"
  462. end
  463. end
  464. true
  465. end
  466. =begin
  467. exists_not(
  468. browser: browser1,
  469. css: '.some_class',
  470. )
  471. =end
  472. def exists_not(params)
  473. switch_window_focus(params)
  474. log('exists_not', params)
  475. instance = params[:browser] || @browser
  476. if instance.find_elements(css: params[:css])[0]
  477. screenshot(browser: instance, comment: 'exists_not_failed')
  478. raise "#{params[:css]} exists but should not"
  479. end
  480. true
  481. end
  482. =begin
  483. set(
  484. browser: browser1,
  485. css: '.some_class',
  486. value: true,
  487. slow: false,
  488. blur: true, # default false
  489. clear: true, # todo | default: true
  490. no_click: true,
  491. )
  492. =end
  493. def set(params)
  494. switch_window_focus(params)
  495. log('set', params)
  496. instance = params[:browser] || @browser
  497. screenshot(browser: instance, comment: 'set_before')
  498. element = instance.find_elements(css: params[:css])[0]
  499. if !params[:no_click]
  500. element.click
  501. end
  502. element.clear
  503. if !params[:slow]
  504. element.send_keys(params[:value])
  505. else
  506. element.send_keys('')
  507. keys = params[:value].to_s.split('')
  508. keys.each { |key|
  509. instance.action.send_keys(key).perform
  510. }
  511. end
  512. # it's not working stable with ff via selenium, use js
  513. if browser =~ /firefox/i && params[:css] =~ /\[data-name=/
  514. log('set_ff_check', params)
  515. value = instance.find_elements(css: params[:css])[0].text
  516. if value != params[:value]
  517. log('set_ff_check_failed_use_js', params)
  518. value_quoted = quote(params[:value])
  519. puts "DEBUG $('#{params[:css]}').html('#{value_quoted}').trigger('focusout')"
  520. instance.execute_script("$('#{params[:css]}').html('#{value_quoted}').trigger('focusout')")
  521. end
  522. end
  523. if params[:blur]
  524. instance.execute_script("$('#{params[:css]}').blur()")
  525. end
  526. sleep 0.2
  527. screenshot(browser: instance, comment: 'set_after')
  528. end
  529. =begin
  530. select(
  531. browser: browser1,
  532. css: '.some_class',
  533. value: 'Some Value',
  534. deselect_all: false, # default false
  535. )
  536. =end
  537. def select(params)
  538. switch_window_focus(params)
  539. log('select', params)
  540. instance = params[:browser] || @browser
  541. screenshot(browser: instance, comment: 'select_before')
  542. # searchable select
  543. element = instance.find_elements(css: "#{params[:css]}.js-shadow")[0]
  544. if element
  545. element = instance.find_elements(css: "#{params[:css]}.js-shadow + .js-input")[0]
  546. element.click
  547. element.clear
  548. sleep 0.4
  549. element.send_keys(params[:value])
  550. sleep 0.2
  551. element.send_keys(:enter)
  552. sleep 0.2
  553. return
  554. end
  555. # native select
  556. begin
  557. element = instance.find_elements(css: params[:css])[0]
  558. dropdown = Selenium::WebDriver::Support::Select.new(element)
  559. if params[:deselect_all]
  560. dropdown.deselect_all
  561. end
  562. dropdown.select_by(:text, params[:value])
  563. #puts "select - #{params.inspect}"
  564. rescue
  565. sleep 0.4
  566. # just try again
  567. log('select', { rescure: true })
  568. element = instance.find_elements(css: params[:css])[0]
  569. dropdown = Selenium::WebDriver::Support::Select.new(element)
  570. if params[:deselect_all]
  571. dropdown.deselect_all
  572. end
  573. dropdown.select_by(:text, params[:value])
  574. #puts "select2 - #{params.inspect}"
  575. end
  576. sleep 0.4
  577. screenshot(browser: instance, comment: 'select_after')
  578. end
  579. =begin
  580. switch(
  581. browser: browser1,
  582. css: '.some_class',
  583. type: 'on', # 'off'
  584. no_check: true, # do not check is switch has changed, in case if js alert
  585. )
  586. =end
  587. def switch(params)
  588. switch_window_focus(params)
  589. log('switch', params)
  590. instance = params[:browser] || @browser
  591. screenshot(browser: instance, comment: 'switch_before')
  592. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  593. checked = element.attribute('checked')
  594. if !checked
  595. if params[:type] == 'on'
  596. instance.find_elements(css: "#{params[:css]} label")[0].click
  597. sleep 2
  598. if params[:no_check] != true
  599. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  600. checked = element.attribute('checked')
  601. raise 'Switch not on!' if !checked
  602. end
  603. end
  604. elsif params[:type] == 'off'
  605. instance.find_elements(css: "#{params[:css]} label")[0].click
  606. sleep 2
  607. if params[:no_check] != true
  608. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  609. checked = element.attribute('checked')
  610. raise 'Switch not off!' if checked
  611. end
  612. end
  613. screenshot(browser: instance, comment: 'switch_after')
  614. end
  615. =begin
  616. check(
  617. browser: browser1,
  618. css: '.some_class',
  619. )
  620. =end
  621. def check(params)
  622. switch_window_focus(params)
  623. log('check', params)
  624. instance = params[:browser] || @browser
  625. screenshot(browser: instance, comment: 'check_before')
  626. instance.execute_script("if (!$('#{params[:css]}').prop('checked')) { $('#{params[:css]}').click() }")
  627. #element = instance.find_elements(css: params[:css])[0]
  628. #checked = element.attribute('checked')
  629. #element.click if !checked
  630. screenshot(browser: instance, comment: 'check_after')
  631. end
  632. =begin
  633. uncheck(
  634. browser: browser1,
  635. css: '.some_class',
  636. )
  637. =end
  638. def uncheck(params)
  639. switch_window_focus(params)
  640. log('uncheck', params)
  641. instance = params[:browser] || @browser
  642. screenshot(browser: instance, comment: 'uncheck_before')
  643. instance.execute_script("if ($('#{params[:css]}').prop('checked')) { $('#{params[:css]}').click() }")
  644. #element = instance.find_elements(css: params[:css])[0]
  645. #checked = element.attribute('checked')
  646. #element.click if checked
  647. screenshot(browser: instance, comment: 'uncheck_after')
  648. end
  649. =begin
  650. sendkey(
  651. browser: browser1,
  652. value: :enter,
  653. slow: false, # default false
  654. )
  655. =end
  656. def sendkey(params)
  657. switch_window_focus(params)
  658. log('sendkey', params)
  659. instance = params[:browser] || @browser
  660. element = nil
  661. if params[:css]
  662. element = instance.find_elements(css: params[:css])[0]
  663. end
  664. screenshot(browser: instance, comment: 'sendkey_before')
  665. if params[:value].class == Array
  666. params[:value].each { |key|
  667. if element
  668. element.send_keys(key)
  669. else
  670. instance.action.send_keys(key).perform
  671. end
  672. }
  673. screenshot(browser: instance, comment: 'sendkey_after')
  674. return
  675. end
  676. if element
  677. element.send_keys(params[:value])
  678. else
  679. instance.action.send_keys(params[:value]).perform
  680. end
  681. if params[:slow]
  682. sleep 1.5
  683. else
  684. sleep 0.2
  685. end
  686. screenshot(browser: instance, comment: 'sendkey_after')
  687. end
  688. =begin
  689. match(
  690. browser: browser1,
  691. css: '#content .text-1',
  692. value: 'some test for browser and some other for browser',
  693. attribute: 'some_attribute', # match on attribute
  694. should_not_match: true,
  695. no_quote: false, # use regex
  696. )
  697. =end
  698. def match(params, fallback = false)
  699. switch_window_focus(params)
  700. log('match', params)
  701. instance = params[:browser] || @browser
  702. element = instance.find_elements(css: params[:css])[0]
  703. if params[:css] =~ /select/
  704. dropdown = Selenium::WebDriver::Support::Select.new(element)
  705. success = false
  706. if dropdown.selected_options
  707. dropdown.selected_options.each { |option|
  708. if option.text == params[:value]
  709. success = true
  710. end
  711. }
  712. end
  713. if params[:should_not_match]
  714. if success
  715. screenshot(browser: instance, comment: 'match_failed')
  716. raise "should not match '#{params[:value]}' in select list, but is matching"
  717. end
  718. elsif !success
  719. screenshot(browser: instance, comment: 'match_failed')
  720. raise "not matching '#{params[:value]}' in select list"
  721. end
  722. return true
  723. end
  724. # match on attribute
  725. begin
  726. text = if params[:attribute]
  727. element.attribute(params[:attribute])
  728. elsif params[:css] =~ /(input|textarea)/i
  729. element.attribute('value')
  730. else
  731. element.text
  732. end
  733. rescue => e
  734. # just try again
  735. if !fallback
  736. return match(params, true)
  737. end
  738. raise e.inspect
  739. end
  740. # do cleanups (needed for richtext tests)
  741. if params[:cleanup]
  742. text.gsub!(/\s+$/m, '')
  743. params[:value].gsub!(/\s+$/m, '')
  744. end
  745. match = false
  746. if params[:no_quote]
  747. #puts "aaaa #{text}/#{params[:value]}"
  748. if text =~ /#{params[:value]}/i
  749. match = $1 || true
  750. end
  751. elsif text =~ /#{Regexp.quote(params[:value])}/i
  752. match = true
  753. end
  754. if match
  755. if params[:should_not_match]
  756. screenshot(browser: instance, comment: 'match_failed')
  757. raise "matching '#{params[:value]}' in content '#{text}' but should not!"
  758. end
  759. elsif !params[:should_not_match]
  760. screenshot(browser: instance, comment: 'match_failed')
  761. raise "not matching '#{params[:value]}' in content '#{text}' but should!"
  762. end
  763. sleep 0.2
  764. match
  765. end
  766. =begin
  767. match_not(
  768. browser: browser1,
  769. css: '#content .text-1',
  770. value: 'some test for browser and some other for browser',
  771. attribute: 'some_attribute', # match on attribute
  772. should_not_match: true,
  773. no_quote: false, # use regex
  774. )
  775. =end
  776. def match_not(params)
  777. switch_window_focus(params)
  778. log('match_not', params)
  779. params[:should_not_match] = true
  780. match(params)
  781. end
  782. =begin
  783. set type of task (closeTab, closeNextInOverview, stayOnTab)
  784. task_type(
  785. browser: browser1,
  786. type: 'stayOnTab',
  787. )
  788. =end
  789. def task_type(params)
  790. switch_window_focus(params)
  791. log('task_type', params)
  792. instance = params[:browser] || @browser
  793. if params[:type]
  794. instance.find_elements(css: '.content.active .js-secondaryActionButtonLabel')[0].click
  795. instance.find_elements(css: ".content.active .js-secondaryActionLabel[data-type=#{params[:type]}]")[0].click
  796. return
  797. end
  798. raise "Unknown params for task_type: #{params.inspect}"
  799. end
  800. =begin
  801. cookie(
  802. browser: browser1,
  803. name: '^_zammad.+?',
  804. value: '.+?',
  805. expires: nil,
  806. )
  807. cookie(
  808. browser: browser1,
  809. name: '^_zammad.+?',
  810. should_not_exist: true,
  811. )
  812. =end
  813. def cookie(params)
  814. switch_window_focus(params)
  815. log('cookie', params)
  816. instance = params[:browser] || @browser
  817. if !browser_support_cookies
  818. assert(true, "'#{params[:value]}' ups browser is not supporting reading cookies, go ahead")
  819. return true
  820. end
  821. cookies = instance.manage.all_cookies
  822. cookies.each { |cookie|
  823. #puts "CCC #{cookie.inspect}"
  824. # :name=>"_zammad_session_c25832f4de2", :value=>"adc31cd21615cb0a7ab269184ec8b76f", :path=>"/", :domain=>"localhost", :expires=>nil, :secure=>false}
  825. next if cookie[:name] !~ /#{params[:name]}/i
  826. if params.key?(:value) && cookie[:value].to_s =~ /#{params[:value]}/i
  827. assert(true, "matching value '#{params[:value]}' in cookie '#{cookie}'")
  828. else
  829. raise "not matching value '#{params[:value]}' in cookie '#{cookie}'"
  830. end
  831. if params.key?(:expires) && cookie[:expires].to_s =~ /#{params[:expires]}/i
  832. assert(true, "matching expires '#{params[:expires].inspect}' in cookie '#{cookie}'")
  833. else
  834. raise "not matching expires '#{params[:expires]}' in cookie '#{cookie}'"
  835. end
  836. return if !params[:should_not_exist]
  837. raise "cookie with name '#{params[:name]}' should not exist, but exists '#{cookies}'"
  838. }
  839. if params[:should_not_exist]
  840. assert(true, "cookie with name '#{params[:name]}' is not existing")
  841. return
  842. end
  843. raise "not matching name '#{params[:name]}' in cookie '#{cookies}'"
  844. end
  845. =begin
  846. verify_title(
  847. browser: browser1,
  848. value: 'some title',
  849. )
  850. =end
  851. def verify_title(params = {})
  852. switch_window_focus(params)
  853. log('verify_title', params)
  854. instance = params[:browser] || @browser
  855. title = instance.title
  856. if title =~ /#{params[:value]}/i
  857. assert(true, "matching '#{params[:value]}' in title '#{title}'")
  858. else
  859. raise "not matching '#{params[:value]}' in title '#{title}'"
  860. end
  861. end
  862. =begin
  863. verify_task(
  864. browser: browser1,
  865. data: {
  866. title: 'some title',
  867. modified: true, # optional
  868. }
  869. )
  870. =end
  871. def verify_task(params = {}, fallback = false)
  872. switch_window_focus(params)
  873. log('verify_task', params)
  874. instance = params[:browser] || @browser
  875. data = params[:data]
  876. sleep 1
  877. begin
  878. # verify title
  879. if data[:title]
  880. title = instance.find_elements(css: '.tasks .is-active')[0].text.strip
  881. if title =~ /#{data[:title]}/i
  882. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  883. else
  884. screenshot(browser: instance, comment: 'verify_task_failed')
  885. raise "not matching '#{data[:title]}' in title '#{title}'"
  886. end
  887. end
  888. # verify modified
  889. if data.key?(:modified)
  890. exists = instance.find_elements(css: '.tasks .is-active')[0]
  891. is_modified = instance.find_elements(css: '.tasks .is-modified')[0]
  892. puts "m #{data[:modified].inspect}"
  893. if exists
  894. puts ' exists'
  895. end
  896. if is_modified
  897. puts ' is_modified'
  898. end
  899. if data[:modified] == true
  900. if is_modified
  901. assert(true, "task '#{data[:title]}' is modifed")
  902. elsif !exists
  903. screenshot(browser: instance, comment: 'verify_task_failed')
  904. raise "task '#{data[:title]}' not exists, should not modified"
  905. else
  906. screenshot(browser: instance, comment: 'verify_task_failed')
  907. raise "task '#{data[:title]}' is not modifed"
  908. end
  909. elsif !is_modified
  910. assert(true, "task '#{data[:title]}' is modifed")
  911. elsif !exists
  912. screenshot(browser: instance, comment: 'verify_task_failed')
  913. raise "task '#{data[:title]}' not exists, should be not modified"
  914. else
  915. screenshot(browser: instance, comment: 'verify_task_failed')
  916. raise "task '#{data[:title]}' is modifed, but should not"
  917. end
  918. end
  919. rescue => e
  920. # just try again
  921. if !fallback
  922. verify_task(params, true)
  923. end
  924. raise 'ERROR: ' + e.inspect
  925. end
  926. true
  927. end
  928. =begin
  929. open_task(
  930. browser: browser1,
  931. data: {
  932. title: 'some title',
  933. }
  934. )
  935. =end
  936. def open_task(params = {})
  937. switch_window_focus(params)
  938. log('open_task', params)
  939. instance = params[:browser] || @browser
  940. data = params[:data]
  941. element = instance.find_elements(partial_link_text: data[:title])[0]
  942. if !element
  943. screenshot(browser: instance, comment: 'open_task_failed')
  944. raise "no task with title '#{data[:title]}' found"
  945. end
  946. element.click
  947. true
  948. end
  949. =begin
  950. close_task(
  951. browser: browser1,
  952. data: {
  953. title: 'some title',
  954. },
  955. discard_changes: true,
  956. )
  957. =end
  958. def close_task(params = {})
  959. switch_window_focus(params)
  960. log('close_task', params)
  961. instance = params[:browser] || @browser
  962. data = params[:data]
  963. element = instance.find_elements(partial_link_text: data[:title])[0]
  964. if !element
  965. screenshot(browser: instance, comment: 'close_task_failed')
  966. raise "no task with title '#{data[:title]}' found"
  967. end
  968. instance.mouse.move_to(element)
  969. sleep 0.1
  970. instance.execute_script("$('.navigation .tasks .task:contains(\"#{data[:title]}\") .js-close').click()")
  971. # accept task close warning
  972. if params[:discard_changes]
  973. modal_ready(browser: instance)
  974. instance.find_elements(css: '.modal button.js-submit')[0].click
  975. modal_disappear(browser: instance)
  976. end
  977. true
  978. end
  979. =begin
  980. file_upload(
  981. browser: browser1,
  982. css: '.content.active .attachmentPlaceholder-inputHolder input'
  983. files: ['path/in/home/some_file.ext'], # 'test/fixtures/test1.pdf'
  984. )
  985. =end
  986. def file_upload(params = {})
  987. switch_window_focus(params)
  988. log('file_upload', params)
  989. instance = params[:browser] || @browser
  990. params[:files].each { |file|
  991. instance.find_elements(css: params[:css])[0].send_keys "#{Rails.root}/#{file}"
  992. }
  993. sleep 2 * params[:files].count
  994. end
  995. =begin
  996. watch_for(
  997. browser: browser1,
  998. css: '#content .text-1',
  999. value: 'some text',
  1000. attribute: 'some_attribute' # optional
  1001. timeout: 16, # in sec, default 16
  1002. )
  1003. =end
  1004. def watch_for(params = {})
  1005. switch_window_focus(params)
  1006. log('watch_for', params)
  1007. instance = params[:browser] || @browser
  1008. timeout = 16
  1009. if params[:timeout]
  1010. timeout = params[:timeout]
  1011. end
  1012. loops = timeout.to_i * 2
  1013. text = ''
  1014. (1..loops).each {
  1015. element = instance.find_elements(css: params[:css])[0]
  1016. if element #&& element.displayed?
  1017. begin
  1018. # watch for selector
  1019. if !params[:attribute] && !params[:value]
  1020. assert(true, "'#{params[:css]}' found")
  1021. sleep 0.5
  1022. return true
  1023. # match pn attribute
  1024. else
  1025. text = if params[:attribute]
  1026. element.attribute(params[:attribute])
  1027. elsif params[:css] =~ /(input|textarea)/i
  1028. element.attribute('value')
  1029. else
  1030. element.text
  1031. end
  1032. if text =~ /#{params[:value]}/i
  1033. assert(true, "'#{params[:value]}' found in '#{text}'")
  1034. sleep 0.5
  1035. return true
  1036. end
  1037. end
  1038. rescue
  1039. # try again
  1040. end
  1041. end
  1042. sleep 0.5
  1043. }
  1044. screenshot(browser: instance, comment: 'watch_for_failed')
  1045. if !params[:attribute] && !params[:value]
  1046. raise "'#{params[:css]}' not found"
  1047. end
  1048. raise "'#{params[:value]}' not found in '#{text}'"
  1049. end
  1050. =begin
  1051. wait untill selector disabppears
  1052. watch_for_disappear(
  1053. browser: browser1,
  1054. css: '#content .text-1',
  1055. timeout: 16, # in sec, default 16
  1056. )
  1057. wait untill text in selector disabppears
  1058. watch_for_disappear(
  1059. browser: browser1,
  1060. css: '#content .text-1',
  1061. value: 'some value as regexp',
  1062. timeout: 16, # in sec, default 16
  1063. )
  1064. =end
  1065. def watch_for_disappear(params = {})
  1066. switch_window_focus(params)
  1067. log('watch_for_disappear', params)
  1068. instance = params[:browser] || @browser
  1069. timeout = 16
  1070. if params[:timeout]
  1071. timeout = params[:timeout]
  1072. end
  1073. loops = timeout.to_i
  1074. text = ''
  1075. (1..loops).each {
  1076. element = instance.find_elements(css: params[:css])[0]
  1077. if !element #|| element.displayed?
  1078. assert(true, 'not found')
  1079. sleep 1
  1080. return true
  1081. end
  1082. if params[:value]
  1083. begin
  1084. text = instance.find_elements(css: params[:css])[0].text
  1085. if text !~ /#{params[:value]}/i
  1086. assert(true, "not matching '#{params[:value]}' in text '#{text}'")
  1087. sleep 1
  1088. return true
  1089. end
  1090. rescue
  1091. # try again
  1092. end
  1093. end
  1094. sleep 1
  1095. }
  1096. screenshot(browser: instance, comment: 'disappear_failed')
  1097. raise "#{params[:css]}) still exsists"
  1098. end
  1099. =begin
  1100. shortcut(
  1101. browser: browser1,
  1102. key: 'x',
  1103. )
  1104. =end
  1105. def shortcut(params = {})
  1106. switch_window_focus(params)
  1107. log('shortcut', params)
  1108. instance = params[:browser] || @browser
  1109. screenshot(browser: instance, comment: 'shortcut_before')
  1110. instance.action.key_down(:control)
  1111. .key_down(:alt)
  1112. .send_keys(params[:key])
  1113. .key_up(:alt)
  1114. .key_up(:control)
  1115. .perform
  1116. screenshot(browser: instance, comment: 'shortcut_after')
  1117. end
  1118. =begin
  1119. window_keys(
  1120. browser: browser1,
  1121. value: 'x',
  1122. )
  1123. =end
  1124. def window_keys(params = {})
  1125. switch_window_focus(params)
  1126. log('window_keys', params)
  1127. instance = params[:browser] || @browser
  1128. instance.action.send_keys(params[:value]).perform
  1129. end
  1130. =begin
  1131. tasks_close_all(
  1132. browser: browser1,
  1133. )
  1134. =end
  1135. def tasks_close_all(params = {})
  1136. switch_window_focus(params)
  1137. log('tasks_close_all', params)
  1138. instance = params[:browser] || @browser
  1139. 99.times do
  1140. #sleep 0.5
  1141. begin
  1142. if instance.find_elements(css: '.navigation .tasks .task:first-child')[0]
  1143. instance.mouse.move_to(instance.find_elements(css: '.navigation .tasks .task:first-child')[0])
  1144. sleep 0.1
  1145. click_element = instance.find_elements(css: '.navigation .tasks .task:first-child .js-close')[0]
  1146. if click_element
  1147. click_element.click
  1148. # accept task close warning
  1149. if instance.find_elements(css: '.modal button.js-submit')[0]
  1150. sleep 0.4
  1151. instance.find_elements(css: '.modal button.js-submit')[0].click
  1152. end
  1153. end
  1154. else
  1155. break
  1156. end
  1157. rescue
  1158. # try again
  1159. end
  1160. end
  1161. assert(true, 'all tasks closed')
  1162. end
  1163. =begin
  1164. close_online_notitifcation(
  1165. browser: browser1,
  1166. data: {
  1167. #title: 'some title',
  1168. position: 3,
  1169. },
  1170. )
  1171. =end
  1172. def close_online_notitifcation(params = {})
  1173. switch_window_focus(params)
  1174. log('close_online_notitifcation', params)
  1175. instance = params[:browser] || @browser
  1176. data = params[:data]
  1177. if data[:title]
  1178. element = instance.find_elements(partial_link_text: data[:title])[0]
  1179. if !element
  1180. screenshot(browser: instance, comment: 'close_online_notitifcation')
  1181. raise "no online notification with title '#{data[:title]}' found"
  1182. end
  1183. instance.mouse.move_to(element)
  1184. sleep 0.1
  1185. instance.execute_script("$('.js-notificationsContainer .js-items .js-item .activity-text:contains(\"#{data[:title]}\") .js-remove').first().click()")
  1186. else
  1187. css = ".js-notificationsContainer .js-items .js-item:nth-child(#{data[:position]})"
  1188. element = instance.find_elements(css: css)[0]
  1189. if !element
  1190. screenshot(browser: instance, comment: 'close_online_notitifcation')
  1191. raise "no online notification with postion '#{css}' found"
  1192. end
  1193. instance.mouse.move_to(element)
  1194. sleep 0.1
  1195. instance.find_elements(css: "#{css} .js-remove")[0].click
  1196. end
  1197. true
  1198. end
  1199. =begin
  1200. online_notitifcation_close_all(
  1201. browser: browser1,
  1202. )
  1203. =end
  1204. def online_notitifcation_close_all(params = {})
  1205. switch_window_focus(params)
  1206. log('online_notitifcation_close_all', params)
  1207. instance = params[:browser] || @browser
  1208. 99.times do
  1209. sleep 0.5
  1210. begin
  1211. if instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0]
  1212. instance.mouse.move_to(instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0])
  1213. sleep 0.1
  1214. click_element = instance.find_elements(css: '.js-notificationsContainer .js-item:first-child .js-remove')[0]
  1215. if click_element
  1216. click_element.click
  1217. end
  1218. else
  1219. break
  1220. end
  1221. rescue
  1222. # try again
  1223. end
  1224. end
  1225. assert(true, 'all online notification closed')
  1226. end
  1227. =begin
  1228. empty_search(
  1229. browser: browser1,
  1230. )
  1231. =end
  1232. def empty_search(params = {})
  1233. switch_window_focus(params)
  1234. log('empty_search', params)
  1235. instance = params[:browser] || @browser
  1236. # empty search box by x
  1237. begin
  1238. instance.find_elements(css: '.search .js-emptySearch')[0].click
  1239. rescue
  1240. # in issues with ff & selenium, sometimes exeption appears
  1241. # "Element is not currently visible and so may not be interacted with"
  1242. log('empty_search via js')
  1243. instance.execute_script('$(".search .js-emptySearch").click()')
  1244. end
  1245. sleep 0.5
  1246. text = instance.find_elements(css: '#global-search')[0].attribute('value')
  1247. if !text
  1248. raise '#global-search is not empty!'
  1249. end
  1250. true
  1251. end
  1252. =begin
  1253. ticket_customer_select(
  1254. browser: browser1,
  1255. css: '#content .text-1',
  1256. customer: '',
  1257. )
  1258. =end
  1259. def ticket_customer_select(params = {})
  1260. switch_window_focus(params)
  1261. log('ticket_customer_select', params)
  1262. instance = params[:browser] || @browser
  1263. element = instance.find_elements(css: params[:css] + ' input[name="customer_id_completion"]')[0]
  1264. element.click
  1265. element.clear
  1266. element.send_keys(params[:customer])
  1267. sleep 2.5
  1268. element.send_keys(:enter)
  1269. #instance.find_elements(css: params[:css] + ' .recipientList-entry.js-user.is-active')[0].click
  1270. sleep 0.4
  1271. assert(true, 'ticket_customer_select')
  1272. end
  1273. =begin
  1274. overview_create(
  1275. browser: browser1,
  1276. data: {
  1277. name: name,
  1278. role: 'Agent',
  1279. selector: {
  1280. 'Priority': '1 low',
  1281. },
  1282. 'order::direction' => 'down',
  1283. }
  1284. )
  1285. =end
  1286. def overview_create(params)
  1287. switch_window_focus(params)
  1288. log('overview_create', params)
  1289. instance = params[:browser] || @browser
  1290. data = params[:data]
  1291. click(
  1292. browser: instance,
  1293. css: 'a[href="#manage"]',
  1294. mute_log: true,
  1295. )
  1296. click(
  1297. browser: instance,
  1298. css: '.content.active a[href="#manage/overviews"]',
  1299. mute_log: true,
  1300. )
  1301. click(
  1302. browser: instance,
  1303. css: '.content.active a[data-type="new"]',
  1304. mute_log: true,
  1305. )
  1306. modal_ready(browser: instance)
  1307. if data[:name]
  1308. set(
  1309. browser: instance,
  1310. css: '.modal input[name=name]',
  1311. value: data[:name],
  1312. mute_log: true,
  1313. )
  1314. end
  1315. if data[:role]
  1316. select(
  1317. browser: instance,
  1318. css: '.modal select[name="role_id"]',
  1319. value: data[:role],
  1320. mute_log: true,
  1321. )
  1322. end
  1323. if data[:selector]
  1324. data[:selector].each { |key, value|
  1325. select(
  1326. browser: instance,
  1327. css: '.modal .ticket_selector .js-attributeSelector select',
  1328. value: key,
  1329. mute_log: true,
  1330. )
  1331. sleep 0.5
  1332. select(
  1333. browser: instance,
  1334. css: '.modal .ticket_selector .js-value select',
  1335. value: value,
  1336. deselect_all: true,
  1337. mute_log: true,
  1338. )
  1339. }
  1340. end
  1341. if data['order::direction']
  1342. select(
  1343. browser: instance,
  1344. css: '.modal select[name="order::direction"]',
  1345. value: data['order::direction'],
  1346. mute_log: true,
  1347. )
  1348. end
  1349. instance.find_elements(css: '.modal button.js-submit')[0].click
  1350. modal_disappear(browser: instance)
  1351. 11.times {
  1352. element = instance.find_elements(css: 'body')[0]
  1353. text = element.text
  1354. if text =~ /#{Regexp.quote(data[:name])}/
  1355. assert(true, 'overview created')
  1356. overview = {
  1357. name: name,
  1358. }
  1359. sleep 1
  1360. return overview
  1361. end
  1362. sleep 1
  1363. }
  1364. screenshot(browser: instance, comment: 'overview_create_failed')
  1365. raise 'overview creation failed'
  1366. end
  1367. =begin
  1368. overview_update(
  1369. browser: browser1,
  1370. data: {
  1371. name: name,
  1372. role: 'Agent',
  1373. selector: {
  1374. 'Priority': '1 low',
  1375. },
  1376. 'order::direction' => 'down',
  1377. }
  1378. )
  1379. =end
  1380. def overview_update(params)
  1381. switch_window_focus(params)
  1382. log('overview_create', params)
  1383. instance = params[:browser] || @browser
  1384. data = params[:data]
  1385. click(
  1386. browser: instance,
  1387. css: 'a[href="#manage"]',
  1388. mute_log: true,
  1389. )
  1390. click(
  1391. browser: instance,
  1392. css: '.content.active a[href="#manage/overviews"]',
  1393. mute_log: true,
  1394. )
  1395. instance.execute_script("$(\".content.active td:contains('#{data[:name]}')\").first().click()")
  1396. sleep 2
  1397. if data[:name]
  1398. set(
  1399. browser: instance,
  1400. css: '.modal input[name=name]',
  1401. value: data[:name],
  1402. mute_log: true,
  1403. )
  1404. end
  1405. if data[:role]
  1406. select(
  1407. browser: instance,
  1408. css: '.modal select[name="role_id"]',
  1409. value: data[:role],
  1410. mute_log: true,
  1411. )
  1412. end
  1413. if data[:selector]
  1414. data[:selector].each { |key, value|
  1415. select(
  1416. browser: instance,
  1417. css: '.modal .ticket_selector .js-attributeSelector select',
  1418. value: key,
  1419. mute_log: true,
  1420. )
  1421. sleep 0.5
  1422. select(
  1423. browser: instance,
  1424. css: '.modal .ticket_selector .js-value select',
  1425. value: value,
  1426. deselect_all: true,
  1427. mute_log: true,
  1428. )
  1429. }
  1430. end
  1431. if data['order::direction']
  1432. select(
  1433. browser: instance,
  1434. css: '.modal select[name="order::direction"]',
  1435. value: data['order::direction'],
  1436. mute_log: true,
  1437. )
  1438. end
  1439. instance.find_elements(css: '.modal button.js-submit')[0].click
  1440. modal_disappear(browser: instance)
  1441. 11.times {
  1442. element = instance.find_elements(css: 'body')[0]
  1443. text = element.text
  1444. if text =~ /#{Regexp.quote(data[:name])}/
  1445. assert(true, 'overview updated')
  1446. overview = {
  1447. name: name,
  1448. }
  1449. sleep 1
  1450. return overview
  1451. end
  1452. sleep 1
  1453. }
  1454. screenshot(browser: instance, comment: 'overview_update_failed')
  1455. raise 'overview update failed'
  1456. end
  1457. =begin
  1458. ticket = ticket_create(
  1459. browser: browser1,
  1460. data: {
  1461. customer: 'nico',
  1462. group: 'Users', # optional / '-NONE-' # if group selection should not be shown
  1463. priority: '2 normal',
  1464. state: 'open',
  1465. title: 'overview #1',
  1466. body: 'overview #1',
  1467. },
  1468. do_not_submit: true,
  1469. )
  1470. returns (in case of submitted)
  1471. {
  1472. id: 123,
  1473. number: '100001',
  1474. title: 'overview #1',
  1475. }
  1476. ticket = ticket_create(
  1477. browser: browser1,
  1478. data: {
  1479. customer: 'nico',
  1480. group: 'Users', # optional / '-NONE-' # if group selection should not be shown
  1481. priority: '2 normal',
  1482. state: 'open',
  1483. title: 'overview #1',
  1484. body: 'overview #1',
  1485. },
  1486. custom_data_select: {
  1487. key1: 'some value',
  1488. },
  1489. custom_data_input: {
  1490. key1: 'some value',
  1491. },
  1492. disable_group_check: true,
  1493. )
  1494. =end
  1495. def ticket_create(params)
  1496. switch_window_focus(params)
  1497. log('ticket_create', params)
  1498. instance = params[:browser] || @browser
  1499. data = params[:data]
  1500. click(
  1501. browser: instance,
  1502. css: 'a[href="#new"]',
  1503. mute_log: true,
  1504. )
  1505. click(
  1506. browser: instance,
  1507. css: 'a[href="#ticket/create"]',
  1508. mute_log: true,
  1509. )
  1510. found = false
  1511. 7.times {
  1512. element = instance.find_elements(css: '.content.active .newTicket')[0]
  1513. if element
  1514. found = true
  1515. break
  1516. end
  1517. sleep 1
  1518. }
  1519. if !found
  1520. screenshot(browser: instance, comment: 'ticket_create_failed')
  1521. raise 'no ticket create screen found!'
  1522. end
  1523. if data[:group]
  1524. if data[:group] == '-NONE-'
  1525. # check if owner selection exists
  1526. count = instance.find_elements(css: '.content.active .newTicket select[name="group_id"] option').count
  1527. assert_equal(0, count, 'owner selection should not be showm')
  1528. # check count of agents, should be only 3 / - selection + master + agent on init screen
  1529. count = instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').count
  1530. assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
  1531. else
  1532. # check count of agents, should be only 1 / - selection on init screen
  1533. if !params[:disable_group_check]
  1534. count = instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').count
  1535. assert_equal(1, count, 'check if owner selection is empty per default')
  1536. end
  1537. select(
  1538. browser: instance,
  1539. css: '.content.active .newTicket select[name="group_id"]',
  1540. value: data[:group],
  1541. mute_log: true,
  1542. )
  1543. sleep 0.2
  1544. end
  1545. end
  1546. if data[:priority]
  1547. select(
  1548. browser: instance,
  1549. css: '.content.active .newTicket select[name="priority_id"]',
  1550. value: data[:priority],
  1551. mute_log: true,
  1552. )
  1553. end
  1554. if data[:state]
  1555. select(
  1556. browser: instance,
  1557. css: '.content.active .newTicket select[name="state_id"]',
  1558. value: data[:state],
  1559. mute_log: true,
  1560. )
  1561. end
  1562. if data[:title]
  1563. set(
  1564. browser: instance,
  1565. css: '.content.active .newTicket input[name="title"]',
  1566. value: data[:title],
  1567. clear: true,
  1568. mute_log: true,
  1569. )
  1570. end
  1571. if data[:body]
  1572. set(
  1573. browser: instance,
  1574. css: '.content.active .newTicket div[data-name=body]',
  1575. value: data[:body],
  1576. clear: true,
  1577. mute_log: true,
  1578. )
  1579. end
  1580. if data[:customer]
  1581. element = instance.find_elements(css: '.content.active .newTicket input[name="customer_id_completion"]')[0]
  1582. element.click
  1583. element.clear
  1584. # ff issue, sometimes focus event gets dropped
  1585. # if drowdown is not open, try it again
  1586. if !instance.find_elements(css: '.content.active .newTicket .js-recipientDropdown.open')[0]
  1587. instance.execute_script('$(".active .newTicket .js-recipientDropdown").addClass("open")')
  1588. end
  1589. element.send_keys(data[:customer])
  1590. sleep 2.5
  1591. element.send_keys(:enter)
  1592. sleep 0.4
  1593. # ff issue, sometimes enter event gets dropped
  1594. # take user manually
  1595. if instance.find_elements(css: '.content.active .newTicket .js-recipientDropdown.open')[0]
  1596. instance.find_elements(css: '.content.active .newTicket .recipientList-entry.js-user.is-active')[0].click
  1597. sleep 0.4
  1598. end
  1599. end
  1600. if params[:custom_data_select]
  1601. params[:custom_data_select].each { |local_key, local_value|
  1602. select(
  1603. browser: instance,
  1604. css: ".content.active .newTicket select[name=\"#{local_key}\"]",
  1605. value: local_value,
  1606. )
  1607. }
  1608. end
  1609. if params[:custom_data_input]
  1610. params[:custom_data_input].each { |local_key, local_value|
  1611. set(
  1612. browser: instance,
  1613. css: ".content.active .newTicket input[name=\"#{local_key}\"]",
  1614. value: local_value,
  1615. clear: true,
  1616. )
  1617. }
  1618. end
  1619. if data[:attachment]
  1620. file_upload(
  1621. browser: instance,
  1622. css: '.content.active .text-1',
  1623. value: 'some text',
  1624. )
  1625. end
  1626. if params[:do_not_submit]
  1627. assert(true, 'ticket created without submit')
  1628. return
  1629. end
  1630. sleep 0.5
  1631. #instance.execute_script('$(".content.active .newTicket form").submit();')
  1632. click(
  1633. browser: instance,
  1634. css: '.content.active .newTicket button.js-submit',
  1635. mute_log: true,
  1636. )
  1637. sleep 1
  1638. 9.times {
  1639. if instance.current_url =~ /#{Regexp.quote('#ticket/zoom/')}/
  1640. assert(true, 'ticket created')
  1641. sleep 2.5
  1642. id = instance.current_url
  1643. id.gsub!(//,)
  1644. id.gsub!(%r{^.+?/(\d+)$}, '\\1')
  1645. element = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0]
  1646. if element
  1647. number = element.text
  1648. ticket = {
  1649. id: id,
  1650. number: number,
  1651. title: data[:title],
  1652. }
  1653. sleep 3 # wait until notify is gone
  1654. screenshot(browser: instance, comment: 'ticket_create_ok')
  1655. return ticket
  1656. end
  1657. end
  1658. sleep 1
  1659. }
  1660. screenshot(browser: instance, comment: 'ticket_create_failed')
  1661. raise "ticket creation failed, can't get zoom url (current url is '#{instance.current_url}')"
  1662. end
  1663. =begin
  1664. ticket_update(
  1665. browser: browser1,
  1666. data: {
  1667. title: '',
  1668. customer: 'some_customer@example.com',
  1669. body: 'some body',
  1670. group: 'some group', # optional
  1671. priority: '1 low',
  1672. state: 'closed',
  1673. },
  1674. do_not_submit: true,
  1675. )
  1676. ticket_update(
  1677. browser: browser1,
  1678. data: {
  1679. title: '',
  1680. customer: 'some_customer@example.com',
  1681. body: 'some body',
  1682. group: 'some group', # optional
  1683. priority: '1 low',
  1684. state: 'closed',
  1685. },
  1686. custom_data_select: {
  1687. key1: 'some value',
  1688. },
  1689. custom_data_input: {
  1690. key1: 'some value',
  1691. },
  1692. do_not_submit: true,
  1693. task_type: 'stayOnTab', # default: stayOnTab / possible: closeTab, closeNextInOverview, stayOnTab
  1694. )
  1695. =end
  1696. def ticket_update(params)
  1697. switch_window_focus(params)
  1698. log('ticket_update', params)
  1699. instance = params[:browser] || @browser
  1700. data = params[:data]
  1701. if data[:title]
  1702. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
  1703. #element.clear
  1704. #sleep 0.5
  1705. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
  1706. #element.send_keys(data[:title])
  1707. #sleep 0.5
  1708. #element.send_keys(:tab)
  1709. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").focus()')
  1710. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").text("' + data[:title] + '")')
  1711. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").blur()')
  1712. instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").trigger("blur")')
  1713. # {
  1714. # :where => :instance2,
  1715. # :execute => 'sendkey',
  1716. # :css => '.content.active .ticketZoom-header .js-objectTitle',
  1717. # :value => 'TTT',
  1718. # },
  1719. # {
  1720. # :where => :instance2,
  1721. # :execute => 'sendkey',
  1722. # :css => '.content.active .ticketZoom-header .js-objectTitle',
  1723. # :value => :tab,
  1724. # },
  1725. end
  1726. if data[:customer]
  1727. # select tab
  1728. click(browser: instance, css: '.content.active .tabsSidebar-tab[data-tab="customer"]')
  1729. click(browser: instance, css: '.content.active div[data-tab="customer"] .js-actions .icon-arrow-down')
  1730. click(browser: instance, css: '.content.active div[data-tab="customer"] .js-actions [data-type="customer-change"]')
  1731. watch_for(
  1732. browser: instance,
  1733. css: '.modal',
  1734. value: 'change',
  1735. )
  1736. element = instance.find_elements(css: '.modal input[name="customer_id_completion"]')[0]
  1737. element.click
  1738. element.clear
  1739. element.send_keys(data[:customer])
  1740. sleep 2.5
  1741. element.send_keys(:enter)
  1742. #instance.find_elements(css: '.modal .user_autocompletion .recipientList-entry.js-user.is-active')[0].click
  1743. sleep 0.2
  1744. click(browser: instance, css: '.modal .js-submit')
  1745. modal_disappear(browser: instance)
  1746. watch_for(
  1747. browser: instance,
  1748. css: '.content.active .tabsSidebar',
  1749. value: data[:customer],
  1750. )
  1751. # select tab
  1752. click(browser: instance, css: '.content.active .tabsSidebar-tab[data-tab="ticket"]')
  1753. end
  1754. if data[:body]
  1755. set(
  1756. browser: instance,
  1757. css: '.content.active div[data-name=body]',
  1758. value: data[:body],
  1759. no_click: true,
  1760. mute_log: true,
  1761. )
  1762. # it's not working stable via selenium, use js
  1763. value = instance.find_elements(css: '.content.active div[data-name=body]')[0].text
  1764. if value != data[:body]
  1765. body_quoted = quote(data[:body])
  1766. instance.execute_script("$('.content.active div[data-name=body]').html('#{body_quoted}').trigger('focusout')")
  1767. end
  1768. end
  1769. if data[:group]
  1770. if data[:group] == '-NONE-'
  1771. # check if owner selection exists
  1772. count = instance.find_elements(css: '.content.active .sidebar select[name="group_id"] option').count
  1773. assert_equal(0, count, 'owner selection should not be showm')
  1774. # check count of agents, should be only 3 / - selection + master + agent on init screen
  1775. count = instance.find_elements(css: '.content.active .sidebar select[name="owner_id"] option').count
  1776. assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
  1777. else
  1778. select(
  1779. browser: instance,
  1780. css: '.content.active .sidebar select[name="group_id"]',
  1781. value: data[:group],
  1782. mute_log: true,
  1783. )
  1784. sleep 0.2
  1785. end
  1786. end
  1787. if data[:priority]
  1788. select(
  1789. browser: instance,
  1790. css: '.content.active .sidebar select[name="priority_id"]',
  1791. value: data[:priority],
  1792. mute_log: true,
  1793. )
  1794. end
  1795. if data[:state]
  1796. select(
  1797. browser: instance,
  1798. css: '.content.active .sidebar select[name="state_id"]',
  1799. value: data[:state],
  1800. mute_log: true,
  1801. )
  1802. end
  1803. if params[:custom_data_select]
  1804. params[:custom_data_select].each { |local_key, local_value|
  1805. select(
  1806. browser: instance,
  1807. css: ".active .sidebar select[name=\"#{local_key}\"]",
  1808. value: local_value,
  1809. )
  1810. }
  1811. end
  1812. if params[:custom_data_input]
  1813. params[:custom_data_input].each { |local_key, local_value|
  1814. set(
  1815. browser: instance,
  1816. css: ".active .sidebar input[name=\"#{local_key}\"]",
  1817. value: local_value,
  1818. clear: true,
  1819. )
  1820. }
  1821. end
  1822. if data[:state] || data[:group] || data[:body] || !params[:custom_data_select].empty? || !params[:custom_data_input].empty?
  1823. found = nil
  1824. 9.times {
  1825. break if found
  1826. begin
  1827. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  1828. if text =~ /(Discard your unsaved changes.|Verwerfen der)/
  1829. found = true
  1830. end
  1831. rescue
  1832. # try again
  1833. end
  1834. sleep 1
  1835. }
  1836. if !found
  1837. screenshot(browser: instance, comment: 'ticket_update_discard_message_failed')
  1838. raise 'no discard message found'
  1839. end
  1840. end
  1841. task_type(
  1842. browser: instance,
  1843. type: params[:task_type] || 'stayOnTab',
  1844. )
  1845. if params[:do_not_submit]
  1846. assert(true, 'ticket updated without submit')
  1847. return true
  1848. end
  1849. instance.find_elements(css: '.content.active .js-submit')[0].click
  1850. # do not stay on tab
  1851. if params[:task_type] == 'closeTab' || params[:task_type] == 'closeNextInOverview'
  1852. sleep 1
  1853. screenshot(browser: instance, comment: 'ticket_update')
  1854. return
  1855. end
  1856. 9.times {
  1857. begin
  1858. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  1859. if !text || text.empty?
  1860. screenshot(browser: instance, comment: 'ticket_update_ok')
  1861. sleep 1
  1862. return true
  1863. end
  1864. rescue
  1865. # try again
  1866. end
  1867. sleep 1
  1868. }
  1869. screenshot(browser: instance, comment: 'ticket_update_failed')
  1870. raise 'unable to update ticket'
  1871. end
  1872. =begin
  1873. ticket_verify(
  1874. browser: browser1,
  1875. data: {
  1876. title: 'some title',
  1877. body: 'some body',
  1878. ## group: 'some group',
  1879. ## state: 'closed',
  1880. custom_data_select: {
  1881. key1: 'some value',
  1882. },
  1883. custom_data_input: {
  1884. key1: 'some value',
  1885. },
  1886. },
  1887. )
  1888. =end
  1889. def ticket_verify(params)
  1890. switch_window_focus(params)
  1891. log('ticket_verify', params)
  1892. instance = params[:browser] || @browser
  1893. data = params[:data]
  1894. if data[:title]
  1895. title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle').first.text.strip
  1896. if title =~ /#{data[:title]}/i
  1897. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  1898. else
  1899. raise "not matching '#{data[:title]}' in title '#{title}'"
  1900. end
  1901. end
  1902. if data[:body]
  1903. body = instance.find_elements(css: '.content.active [data-name="body"]').first.text.strip
  1904. if body =~ /#{data[:body]}/i
  1905. assert(true, "matching '#{data[:body]}' in body '#{body}'")
  1906. else
  1907. raise "not matching '#{data[:body]}' in body '#{body}'"
  1908. end
  1909. end
  1910. if params[:custom_data_select]
  1911. params[:custom_data_select].each { |local_key, local_value|
  1912. element = instance.find_elements(css: ".active .sidebar select[name=\"#{local_key}\"] option[selected]").first
  1913. value = element.text.strip
  1914. if value =~ /#{local_value}/i
  1915. assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
  1916. else
  1917. raise "not matching '#{value}' in #{local_key} '#{local_value}'"
  1918. end
  1919. }
  1920. end
  1921. if params[:custom_data_input]
  1922. params[:custom_data_input].each { |local_key, local_value|
  1923. element = instance.find_elements(css: ".active .sidebar input[name=\"#{local_key}\"]").first
  1924. value = element.text.strip
  1925. if value =~ /#{local_value}/i
  1926. assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
  1927. else
  1928. raise "not matching '#{value}' in #{local_key} '#{local_value}'"
  1929. end
  1930. }
  1931. end
  1932. true
  1933. end
  1934. =begin
  1935. ticket_open_by_overview(
  1936. browser: browser2,
  1937. number: ticket1[:number],
  1938. link: "#ticket/view/#{name}",
  1939. )
  1940. ticket_open_by_overview(
  1941. browser: browser2,
  1942. number: ticket1[:number],
  1943. text: title,
  1944. link: "#ticket/view/#{name}",
  1945. )
  1946. =end
  1947. def ticket_open_by_overview(params)
  1948. switch_window_focus(params)
  1949. log('ticket_open_by_overview', params)
  1950. instance = params[:browser] || @browser
  1951. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  1952. sleep 1
  1953. execute(
  1954. browser: instance,
  1955. js: '$(".content.active .sidebar").css("display", "block")',
  1956. )
  1957. screenshot(browser: instance, comment: 'ticket_open_by_overview')
  1958. instance.find_elements(css: ".content.active .sidebar a[href=\"#{params[:link]}\"]")[0].click
  1959. sleep 1
  1960. execute(
  1961. browser: instance,
  1962. js: '$(".content.active .sidebar").css("display", "none")',
  1963. )
  1964. screenshot(browser: instance, comment: 'ticket_open_by_overview_search')
  1965. if params[:title]
  1966. element = instance.find_elements(partial_link_text: params[:title])[0]
  1967. if !element
  1968. screenshot(browser: instance, comment: 'ticket_open_by_overview_no_ticket_failed')
  1969. raise "unable to find ticket #{params[:title]} in overview #{params[:link]}!"
  1970. end
  1971. else
  1972. element = instance.find_elements(partial_link_text: params[:number])[0]
  1973. if !element
  1974. screenshot(browser: instance, comment: 'ticket_open_by_overview_no_ticket_failed')
  1975. raise "unable to find ticket #{params[:number]} in overview #{params[:link]}!"
  1976. end
  1977. end
  1978. element.click
  1979. sleep 1
  1980. number = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0].text
  1981. if number !~ /#{params[:number]}/
  1982. screenshot(browser: instance, comment: 'ticket_open_by_overview_open_failed_failed')
  1983. raise "unable to open ticket #{params[:number]}!"
  1984. end
  1985. sleep 1
  1986. assert(true, "ticket #{params[:number]} found")
  1987. true
  1988. end
  1989. =begin
  1990. ticket_open_by_search(
  1991. browser: browser2,
  1992. number: ticket1[:number],
  1993. )
  1994. =end
  1995. def ticket_open_by_search(params)
  1996. switch_window_focus(params)
  1997. log('ticket_open_by_search', params)
  1998. instance = params[:browser] || @browser
  1999. # search by number
  2000. element = instance.find_elements(css: '#global-search')[0]
  2001. element.click
  2002. element.clear
  2003. element.send_keys(params[:number])
  2004. sleep 3
  2005. empty_search(browser: instance)
  2006. # search by number again
  2007. element = instance.find_elements(css: '#global-search')[0]
  2008. element.click
  2009. element.clear
  2010. element.send_keys(params[:number])
  2011. sleep 1
  2012. # open ticket
  2013. screenshot(browser: instance, comment: 'ticket_open_by_search')
  2014. #instance.find_element(partial_link_text: params[:number] } ).click
  2015. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:number]}') .nav-tab-icon\").first().click()")
  2016. sleep 1
  2017. number = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0].text
  2018. if number !~ /#{params[:number]}/
  2019. screenshot(browser: instance, comment: 'ticket_open_by_search_failed')
  2020. raise "unable to search/find ticket #{params[:number]}!"
  2021. end
  2022. sleep 1
  2023. true
  2024. end
  2025. =begin
  2026. ticket_open_by_title(
  2027. browser: browser2,
  2028. title: ticket1[:title],
  2029. )
  2030. =end
  2031. def ticket_open_by_title(params)
  2032. switch_window_focus(params)
  2033. log('ticket_open_by_title', params)
  2034. instance = params[:browser] || @browser
  2035. # search by number
  2036. element = instance.find_elements(css: '#global-search')[0]
  2037. element.click
  2038. element.clear
  2039. element.send_keys(params[:title])
  2040. sleep 3
  2041. # open ticket
  2042. screenshot(browser: instance, comment: 'ticket_open_by_title_search')
  2043. #instance.find_element(partial_link_text: params[:title] } ).click
  2044. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:title]}') .nav-tab-icon\").click()")
  2045. sleep 1
  2046. title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle')[0].text
  2047. if title !~ /#{params[:title]}/
  2048. screenshot(browser: instance, comment: 'ticket_open_by_title_failed')
  2049. raise "unable to search/find ticket #{params[:title]}!"
  2050. end
  2051. sleep 1
  2052. true
  2053. end
  2054. =begin
  2055. overview_count = overview_counter(
  2056. browser: browser2,
  2057. )
  2058. returns
  2059. {
  2060. '#ticket/view/all_unassigned' => 42,
  2061. }
  2062. =end
  2063. def overview_counter(params = {})
  2064. switch_window_focus(params)
  2065. log('overview_counter', params)
  2066. instance = params[:browser] || @browser
  2067. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  2068. sleep 2
  2069. execute(
  2070. browser: instance,
  2071. js: '$(".content.active .sidebar").css("display", "block")',
  2072. )
  2073. #execute(
  2074. # browser: instance,
  2075. # js: '$(".content.active .overview-header").css("display", "none")',
  2076. #)
  2077. overviews = {}
  2078. instance.find_elements(css: '.content.active .sidebar a[href]').each { |element|
  2079. url = element.attribute('href')
  2080. url.gsub!(%r{(http|https)://.+?/(.+?)$}, '\\2')
  2081. overviews[url] = 0
  2082. #puts url.inspect
  2083. #puts element.inspect
  2084. }
  2085. overviews.each { |url, _value|
  2086. count = instance.find_elements(css: ".content.active .sidebar a[href=\"#{url}\"] .badge")[0].text
  2087. overviews[url] = count.to_i
  2088. }
  2089. log('overview_counter', overviews)
  2090. overviews
  2091. end
  2092. =begin
  2093. organization_open_by_search(
  2094. browser: browser2,
  2095. value: 'some value',
  2096. )
  2097. =end
  2098. def organization_open_by_search(params = {})
  2099. switch_window_focus(params)
  2100. log('organization_open_by_search', params)
  2101. instance = params[:browser] || @browser
  2102. element = instance.find_elements(css: '#global-search')[0]
  2103. element.click
  2104. element.clear
  2105. element.send_keys(params[:value])
  2106. sleep 3
  2107. empty_search(browser: instance)
  2108. element = instance.find_elements(css: '#global-search')[0]
  2109. element.click
  2110. element.clear
  2111. element.send_keys(params[:value])
  2112. sleep 2
  2113. #instance.find_element(partial_link_text: params[:value] } ).click
  2114. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  2115. sleep 1
  2116. name = instance.find_elements(css: '.content.active h1')[0].text
  2117. if name !~ /#{params[:value]}/
  2118. screenshot(browser: instance, comment: 'organization_open_by_search_failed')
  2119. raise "unable to search/find org #{params[:value]}!"
  2120. end
  2121. assert(true, "org #{params[:value]} found")
  2122. sleep 2
  2123. true
  2124. end
  2125. =begin
  2126. user_open_by_search(
  2127. browser: browser2,
  2128. value: 'some value',
  2129. )
  2130. =end
  2131. def user_open_by_search(params = {})
  2132. switch_window_focus(params)
  2133. log('user_open_by_search', params)
  2134. instance = params[:browser] || @browser
  2135. element = instance.find_elements(css: '#global-search')[0]
  2136. element.click
  2137. element.clear
  2138. element.send_keys(params[:value])
  2139. sleep 3
  2140. screenshot(browser: instance, comment: 'user_open_by_search')
  2141. #instance.find_element(partial_link_text: params[:value]).click
  2142. instance.execute_script("$(\".js-global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  2143. sleep 1
  2144. name = instance.find_elements(css: '.content.active h1')[0].text
  2145. if name !~ /#{params[:value]}/
  2146. screenshot(browser: instance, comment: 'user_open_by_search_failed')
  2147. raise "unable to search/find user #{params[:value]}!"
  2148. end
  2149. assert(true, "user #{params[:term]} found")
  2150. sleep 2
  2151. true
  2152. end
  2153. =begin
  2154. user_create(
  2155. browser: browser2,
  2156. data: {
  2157. #login: 'some login' + random,
  2158. firstname: 'Manage Firstname' + random,
  2159. lastname: 'Manage Lastname' + random,
  2160. email: user_email,
  2161. password: 'some-pass',
  2162. },
  2163. )
  2164. =end
  2165. def user_create(params = {})
  2166. switch_window_focus(params)
  2167. log('user_create', params)
  2168. instance = params[:browser] || @browser
  2169. data = params[:data]
  2170. click(
  2171. browser: instance,
  2172. css: 'a[href="#manage"]',
  2173. mute_log: true,
  2174. )
  2175. click(
  2176. browser: instance,
  2177. css: '.content.active a[href="#manage/users"]',
  2178. mute_log: true,
  2179. )
  2180. click(
  2181. browser: instance,
  2182. css: '.content.active a[data-type="new"]',
  2183. mute_log: true,
  2184. )
  2185. modal_ready(browser: instance)
  2186. element = instance.find_elements(css: '.modal input[name=firstname]')[0]
  2187. element.clear
  2188. element.send_keys(data[:firstname])
  2189. element = instance.find_elements(css: '.modal input[name=lastname]')[0]
  2190. element.clear
  2191. element.send_keys(data[:lastname])
  2192. element = instance.find_elements(css: '.modal input[name=email]')[0]
  2193. element.clear
  2194. element.send_keys(data[:email])
  2195. element = instance.find_elements(css: '.modal input[name=password]')[0]
  2196. element.clear
  2197. element.send_keys(data[:password])
  2198. element = instance.find_elements(css: '.modal input[name=password_confirm]')[0]
  2199. element.clear
  2200. element.send_keys(data[:password])
  2201. check(
  2202. browser: instance,
  2203. css: '.modal input[name=role_ids][value=3]',
  2204. )
  2205. instance.find_elements(css: '.modal button.js-submit')[0].click
  2206. modal_disappear(
  2207. browser: instance,
  2208. timeout: 10,
  2209. )
  2210. set(
  2211. browser: instance,
  2212. css: '.content .js-search',
  2213. value: data[:email],
  2214. )
  2215. watch_for(
  2216. browser: instance,
  2217. css: 'body',
  2218. value: data[:lastname],
  2219. )
  2220. assert(true, 'user created')
  2221. end
  2222. =begin
  2223. sla_create(
  2224. browser: browser2,
  2225. data: {
  2226. name: 'some sla' + random,
  2227. first_response_time_in_text: 61
  2228. },
  2229. )
  2230. =end
  2231. def sla_create(params = {})
  2232. switch_window_focus(params)
  2233. log('sla_create', params)
  2234. instance = params[:browser] || @browser
  2235. data = params[:data]
  2236. click(
  2237. browser: instance,
  2238. css: 'a[href="#manage"]',
  2239. mute_log: true,
  2240. )
  2241. click(
  2242. browser: instance,
  2243. css: '.content.active a[href="#manage/slas"]',
  2244. mute_log: true,
  2245. )
  2246. click(
  2247. browser: instance,
  2248. css: '.content.active a.js-new',
  2249. mute_log: true,
  2250. )
  2251. modal_ready(browser: instance)
  2252. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2253. element.clear
  2254. element.send_keys(data[:name])
  2255. element = instance.find_elements(css: '.modal input[name=first_response_time_in_text]')[0]
  2256. element.clear
  2257. element.send_keys(data[:first_response_time_in_text])
  2258. instance.find_elements(css: '.modal button.js-submit')[0].click
  2259. modal_disappear(browser: instance)
  2260. 7.times {
  2261. element = instance.find_elements(css: 'body')[0]
  2262. text = element.text
  2263. if text =~ /#{Regexp.quote(data[:name])}/
  2264. assert(true, 'sla created')
  2265. sleep 1
  2266. return true
  2267. end
  2268. sleep 1
  2269. }
  2270. screenshot(browser: instance, comment: 'sla_create_failed')
  2271. raise 'sla creation failed'
  2272. end
  2273. =begin
  2274. text_module_create(
  2275. browser: browser2,
  2276. data: {
  2277. name: 'some sla' + random,
  2278. keywords: 'some keywords',
  2279. content: 'some content',
  2280. },
  2281. )
  2282. =end
  2283. def text_module_create(params = {})
  2284. switch_window_focus(params)
  2285. log('text_module_create', params)
  2286. instance = params[:browser] || @browser
  2287. data = params[:data]
  2288. click(
  2289. browser: instance,
  2290. css: 'a[href="#manage"]',
  2291. mute_log: true,
  2292. )
  2293. click(
  2294. browser: instance,
  2295. css: '.content.active a[href="#manage/text_modules"]',
  2296. mute_log: true,
  2297. )
  2298. click(
  2299. browser: instance,
  2300. css: '.content.active a[data-type="new"]',
  2301. mute_log: true,
  2302. )
  2303. modal_ready(browser: instance)
  2304. set(
  2305. browser: instance,
  2306. css: '.modal input[name=name]',
  2307. value: data[:name],
  2308. )
  2309. set(
  2310. browser: instance,
  2311. css: '.modal input[name=keywords]',
  2312. value: data[:keywords],
  2313. )
  2314. set(
  2315. browser: instance,
  2316. css: '.modal [data-name=content]',
  2317. value: data[:content],
  2318. )
  2319. instance.find_elements(css: '.modal button.js-submit')[0].click
  2320. modal_disappear(browser: instance)
  2321. 7.times {
  2322. element = instance.find_elements(css: 'body')[0]
  2323. text = element.text
  2324. if text =~ /#{Regexp.quote(data[:name])}/
  2325. assert(true, 'text module created')
  2326. sleep 1
  2327. return true
  2328. end
  2329. sleep 1
  2330. }
  2331. screenshot(browser: instance, comment: 'text_module_create_failed')
  2332. raise 'text module creation failed'
  2333. end
  2334. =begin
  2335. signature_create(
  2336. browser: browser2,
  2337. data: {
  2338. name: 'some sla' + random,
  2339. body: 'some body',
  2340. },
  2341. )
  2342. =end
  2343. def signature_create(params = {})
  2344. switch_window_focus(params)
  2345. log('signature_create', params)
  2346. instance = params[:browser] || @browser
  2347. data = params[:data]
  2348. click(
  2349. browser: instance,
  2350. css: 'a[href="#manage"]',
  2351. mute_log: true,
  2352. )
  2353. click(
  2354. browser: instance,
  2355. css: '.content.active a[href="#channels/email"]',
  2356. mute_log: true,
  2357. )
  2358. click(
  2359. browser: instance,
  2360. css: '.content.active a[href="#c-signature"]',
  2361. mute_log: true,
  2362. )
  2363. sleep 4
  2364. click(
  2365. browser: instance,
  2366. css: '.content.active #c-signature a[data-type="new"]',
  2367. mute_log: true,
  2368. )
  2369. modal_ready(browser: instance)
  2370. set(
  2371. browser: instance,
  2372. css: '.modal input[name=name]',
  2373. value: data[:name],
  2374. )
  2375. set(
  2376. browser: instance,
  2377. css: '.modal [data-name=body]',
  2378. value: data[:body],
  2379. )
  2380. instance.find_elements(css: '.modal button.js-submit')[0].click
  2381. modal_disappear(browser: instance)
  2382. 11.times {
  2383. element = instance.find_elements(css: 'body')[0]
  2384. text = element.text
  2385. if text =~ /#{Regexp.quote(data[:name])}/
  2386. assert(true, 'signature created')
  2387. sleep 1
  2388. return true
  2389. end
  2390. sleep 1
  2391. }
  2392. screenshot(browser: instance, comment: 'signature_create_failed')
  2393. raise 'signature creation failed'
  2394. end
  2395. =begin
  2396. group_create(
  2397. browser: browser2,
  2398. data: {
  2399. name: 'some sla' + random,
  2400. signature: 'some signature bame',
  2401. member: [
  2402. 'some_user_login',
  2403. ],
  2404. },
  2405. )
  2406. =end
  2407. def group_create(params = {})
  2408. switch_window_focus(params)
  2409. log('group_create', params)
  2410. instance = params[:browser] || @browser
  2411. data = params[:data]
  2412. click(
  2413. browser: instance,
  2414. css: 'a[href="#manage"]',
  2415. mute_log: true,
  2416. )
  2417. click(
  2418. browser: instance,
  2419. css: '.content.active a[href="#manage/groups"]',
  2420. mute_log: true,
  2421. )
  2422. click(
  2423. browser: instance,
  2424. css: '.content.active a[data-type="new"]',
  2425. mute_log: true,
  2426. )
  2427. modal_ready(browser: instance)
  2428. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2429. element.clear
  2430. element.send_keys(data[:name])
  2431. element = instance.find_elements(css: '.modal select[name="email_address_id"]')[0]
  2432. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2433. dropdown.select_by(:index, 1)
  2434. #dropdown.select_by(:text, action[:group])
  2435. if data[:signature]
  2436. element = instance.find_elements(css: '.modal select[name="signature_id"]')[0]
  2437. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2438. dropdown.select_by(:text, data[:signature])
  2439. end
  2440. instance.find_elements(css: '.modal button.js-submit')[0].click
  2441. modal_disappear(browser: instance)
  2442. 11.times {
  2443. element = instance.find_elements(css: 'body')[0]
  2444. text = element.text
  2445. if text =~ /#{Regexp.quote(data[:name])}/
  2446. assert(true, 'group created')
  2447. modal_disappear(browser: instance) # wait until modal has gone
  2448. # add member
  2449. if data[:member]
  2450. data[:member].each { |login|
  2451. instance.find_elements(css: 'a[href="#manage"]')[0].click
  2452. sleep 1
  2453. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  2454. sleep 3
  2455. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  2456. element.clear
  2457. element.send_keys(login)
  2458. sleep 3
  2459. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  2460. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  2461. sleep 3
  2462. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  2463. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  2464. instance.find_elements(css: '.modal button.js-submit')[0].click
  2465. modal_disappear(browser: instance)
  2466. }
  2467. end
  2468. end
  2469. sleep 1
  2470. return true
  2471. }
  2472. screenshot(browser: instance, comment: 'group_create_failed')
  2473. raise 'group creation failed'
  2474. end
  2475. =begin
  2476. role_create(
  2477. browser: browser2,
  2478. data: {
  2479. name: 'some role' + random,
  2480. default_at_signup: false,
  2481. permission: [
  2482. 'admin.group',
  2483. 'preferences.password',
  2484. ],
  2485. member: [
  2486. 'some_user_login',
  2487. ],
  2488. },
  2489. )
  2490. =end
  2491. def role_create(params = {})
  2492. switch_window_focus(params)
  2493. log('role_create', params)
  2494. instance = params[:browser] || @browser
  2495. data = params[:data]
  2496. click(
  2497. browser: instance,
  2498. css: 'a[href="#manage"]',
  2499. mute_log: true,
  2500. )
  2501. click(
  2502. browser: instance,
  2503. css: '.content.active a[href="#manage/roles"]',
  2504. mute_log: true,
  2505. )
  2506. click(
  2507. browser: instance,
  2508. css: '.content.active a[data-type="new"]',
  2509. mute_log: true,
  2510. )
  2511. modal_ready(browser: instance)
  2512. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2513. element.clear
  2514. element.send_keys(data[:name])
  2515. if data.key?(:default_at_signup)
  2516. element = instance.find_elements(css: '.modal select[name="default_at_signup"]')[0]
  2517. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2518. if data[:default_at_signup] == true
  2519. dropdown.select_by(:text, 'yes')
  2520. else
  2521. dropdown.select_by(:text, 'no')
  2522. end
  2523. end
  2524. if data.key?(:permission)
  2525. data[:permission].each { |permission_name|
  2526. check(
  2527. browser: instance,
  2528. css: ".modal [data-permission-name=\"#{permission_name}\"]",
  2529. )
  2530. }
  2531. end
  2532. instance.find_elements(css: '.modal button.js-submit')[0].click
  2533. modal_disappear(browser: instance)
  2534. 11.times {
  2535. element = instance.find_elements(css: 'body')[0]
  2536. text = element.text
  2537. if text =~ /#{Regexp.quote(data[:name])}/
  2538. assert(true, 'role created')
  2539. modal_disappear(browser: instance) # wait until modal has gone
  2540. # add member
  2541. if data[:member]
  2542. data[:member].each { |login|
  2543. instance.find_elements(css: 'a[href="#manage"]')[0].click
  2544. sleep 1
  2545. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  2546. sleep 3
  2547. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  2548. element.clear
  2549. element.send_keys(login)
  2550. sleep 3
  2551. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  2552. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  2553. sleep 3
  2554. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  2555. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  2556. instance.find_elements(css: '.modal button.js-submit')[0].click
  2557. modal_disappear(browser: instance)
  2558. }
  2559. end
  2560. end
  2561. sleep 1
  2562. return true
  2563. }
  2564. screenshot(browser: instance, comment: 'role_create_failed')
  2565. raise 'role creation failed'
  2566. end
  2567. =begin
  2568. role_create(
  2569. browser: browser2,
  2570. data: {
  2571. name: 'some role' + random,
  2572. default_at_signup: false,
  2573. permission: [
  2574. 'admin.group',
  2575. 'preferences.password',
  2576. ],
  2577. member: [
  2578. 'some_user_login',
  2579. ],
  2580. },
  2581. )
  2582. =end
  2583. def role_edit(params = {})
  2584. switch_window_focus(params)
  2585. log('role_edit', params)
  2586. instance = params[:browser] || @browser
  2587. data = params[:data]
  2588. click(
  2589. browser: instance,
  2590. css: 'a[href="#manage"]',
  2591. mute_log: true,
  2592. )
  2593. click(
  2594. browser: instance,
  2595. css: '.content.active a[href="#manage/roles"]',
  2596. mute_log: true,
  2597. )
  2598. instance.execute_script('$(\'.content.active table tr td:contains(" ' + data[:name] + '")\').first().click()')
  2599. modal_ready(browser: instance)
  2600. element = instance.find_elements(css: '.modal input[name=name]')[0]
  2601. element.clear
  2602. element.send_keys(data[:name])
  2603. if data.key?(:default_at_signup)
  2604. element = instance.find_elements(css: '.modal select[name="default_at_signup"]')[0]
  2605. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2606. if data[:default_at_signup] == true
  2607. dropdown.select_by(:text, 'yes')
  2608. else
  2609. dropdown.select_by(:text, 'no')
  2610. end
  2611. end
  2612. if data.key?(:permission)
  2613. data[:permission].each { |permission_name|
  2614. check(
  2615. browser: instance,
  2616. css: ".modal [data-permission-name=\"#{permission_name}\"]",
  2617. )
  2618. }
  2619. end
  2620. if data.key?(:active)
  2621. element = instance.find_elements(css: '.modal select[name="active"]')[0]
  2622. dropdown = Selenium::WebDriver::Support::Select.new(element)
  2623. if data[:active] == true
  2624. dropdown.select_by(:text, 'active')
  2625. else
  2626. dropdown.select_by(:text, 'inactive')
  2627. end
  2628. end
  2629. instance.find_elements(css: '.modal button.js-submit')[0].click
  2630. modal_disappear(browser: instance)
  2631. 11.times {
  2632. element = instance.find_elements(css: 'body')[0]
  2633. text = element.text
  2634. if text =~ /#{Regexp.quote(data[:name])}/
  2635. assert(true, 'role created')
  2636. modal_disappear(browser: instance) # wait until modal has gone
  2637. # add member
  2638. if data[:member]
  2639. data[:member].each { |login|
  2640. instance.find_elements(css: 'a[href="#manage"]')[0].click
  2641. sleep 1
  2642. instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
  2643. sleep 3
  2644. element = instance.find_elements(css: '.content.active [name="search"]')[0]
  2645. element.clear
  2646. element.send_keys(login)
  2647. sleep 3
  2648. #instance.find_elements(:css => '.content.active table [data-id]')[0].click
  2649. instance.execute_script('$(".content.active table [data-id] td").first().click()')
  2650. sleep 3
  2651. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  2652. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  2653. instance.find_elements(css: '.modal button.js-submit')[0].click
  2654. modal_disappear(browser: instance)
  2655. }
  2656. end
  2657. end
  2658. sleep 1
  2659. return true
  2660. }
  2661. screenshot(browser: instance, comment: 'role_edit_failed')
  2662. raise 'role edit failed'
  2663. end
  2664. =begin
  2665. object_manager_attribute_create(
  2666. browser: browser2,
  2667. data: {
  2668. name: 'field_name' + random,
  2669. display: 'Display Name of Field',
  2670. data_type: 'Select',
  2671. data_option: {
  2672. options: {
  2673. 'aa' => 'AA',
  2674. 'bb' => 'BB',
  2675. },
  2676. default: 'abc',
  2677. },
  2678. },
  2679. error: 'already exists'
  2680. )
  2681. object_manager_attribute_create(
  2682. browser: browser2,
  2683. data: {
  2684. name: 'field_name' + random,
  2685. display: 'Display Name of Field',
  2686. data_type: 'Text',
  2687. data_option: {
  2688. default: 'abc',
  2689. },
  2690. },
  2691. error: 'already exists'
  2692. )
  2693. object_manager_attribute_create(
  2694. browser: browser2,
  2695. data: {
  2696. name: 'field_name' + random,
  2697. display: 'Display Name of Field',
  2698. data_type: 'Integer',
  2699. data_option: {
  2700. default: '15',
  2701. min: 1,
  2702. max: 999999,
  2703. },
  2704. },
  2705. error: 'already exists'
  2706. )
  2707. object_manager_attribute_create(
  2708. browser: browser2,
  2709. data: {
  2710. name: 'field_name' + random,
  2711. display: 'Display Name of Field',
  2712. data_type: 'Datetime',
  2713. data_option: {
  2714. future: true,
  2715. past: true,
  2716. diff: 24,
  2717. },
  2718. },
  2719. error: 'already exists'
  2720. )
  2721. object_manager_attribute_create(
  2722. browser: browser2,
  2723. data: {
  2724. name: 'field_name' + random,
  2725. display: 'Display Name of Field',
  2726. data_type: 'Date',
  2727. data_option: {
  2728. future: true,
  2729. past: true,
  2730. diff: 24,
  2731. },
  2732. },
  2733. error: 'already exists'
  2734. )
  2735. object_manager_attribute_create(
  2736. browser: browser2,
  2737. data: {
  2738. name: 'field_name' + random,
  2739. display: 'Display Name of Field',
  2740. data_type: 'Boolean',
  2741. data_option: {
  2742. options: {
  2743. true: 'YES',
  2744. false: 'NO',
  2745. }
  2746. default: undefined,
  2747. },
  2748. },
  2749. error: 'already exists'
  2750. )
  2751. =end
  2752. def object_manager_attribute_create(params = {})
  2753. switch_window_focus(params)
  2754. log('object_manager_attribute_create', params)
  2755. instance = params[:browser] || @browser
  2756. data = params[:data]
  2757. click(
  2758. browser: instance,
  2759. css: 'a[href="#manage"]',
  2760. mute_log: true,
  2761. )
  2762. click(
  2763. browser: instance,
  2764. css: '.content.active a[href="#system/object_manager"]',
  2765. mute_log: true,
  2766. )
  2767. sleep 4
  2768. click(
  2769. browser: instance,
  2770. css: '.content.active .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. element = instance.find_elements(css: '.modal input[name=display]')[0]
  2778. element.clear
  2779. element.send_keys(data[:display])
  2780. select(
  2781. browser: instance,
  2782. css: '.modal select[name="data_type"]',
  2783. value: data[:data_type],
  2784. mute_log: true,
  2785. )
  2786. if data[:data_option]
  2787. if data[:data_option][:options]
  2788. if data[:data_type] == 'Boolean'
  2789. element = instance.find_elements(css: '.modal .js-valueTrue').first
  2790. element.clear
  2791. element.send_keys(data[:data_option][:options][:true])
  2792. element = instance.find_elements(css: '.modal .js-valueFalse').first
  2793. element.clear
  2794. element.send_keys(data[:data_option][:options][:false])
  2795. else
  2796. data[:data_option][:options].each { |key, value|
  2797. element = instance.find_elements(css: '.modal .js-Table .js-key').last
  2798. element.clear
  2799. element.send_keys(key)
  2800. element = instance.find_elements(css: '.modal .js-Table .js-value').last
  2801. element.clear
  2802. element.send_keys(value)
  2803. element = instance.find_elements(css: '.modal .js-Table .js-add')[0]
  2804. element.click
  2805. }
  2806. end
  2807. end
  2808. [:default, :min, :max, :diff].each { |key|
  2809. next if !data[:data_option].key?(key)
  2810. element = instance.find_elements(css: ".modal [name=\"data_option::#{key}\"]").first
  2811. element.clear
  2812. element.send_keys(data[:data_option][key])
  2813. }
  2814. [:future, :past].each { |key|
  2815. next if !data[:data_option].key?(key)
  2816. select(
  2817. browser: instance,
  2818. css: ".modal select[name=\"data_option::#{key}\"]",
  2819. value: data[:data_option][key],
  2820. mute_log: true,
  2821. )
  2822. }
  2823. end
  2824. instance.find_elements(css: '.modal button.js-submit')[0].click
  2825. if params[:error]
  2826. sleep 4
  2827. watch_for(
  2828. css: '.modal',
  2829. value: params[:error],
  2830. )
  2831. click(
  2832. browser: instance,
  2833. css: '.modal .js-close',
  2834. )
  2835. modal_disappear(browser: instance)
  2836. return
  2837. end
  2838. 11.times {
  2839. element = instance.find_elements(css: 'body')[0]
  2840. text = element.text
  2841. if text =~ /#{Regexp.quote(data[:name])}/
  2842. assert(true, 'object manager attribute created')
  2843. sleep 1
  2844. return true
  2845. end
  2846. sleep 1
  2847. }
  2848. screenshot(browser: instance, comment: 'object_manager_attribute_create_failed')
  2849. raise 'object manager attribute creation failed'
  2850. end
  2851. =begin
  2852. object_manager_attribute_update(
  2853. browser: browser2,
  2854. data: {
  2855. name: 'field_name' + random,
  2856. display: 'Display Name of Field',
  2857. data_type: 'Select',
  2858. data_option: {
  2859. options: {
  2860. 'aa' => 'AA',
  2861. 'bb' => 'BB',
  2862. },
  2863. default: 'abc',
  2864. },
  2865. },
  2866. error: 'already exists'
  2867. )
  2868. =end
  2869. def object_manager_attribute_update(params = {})
  2870. switch_window_focus(params)
  2871. log('object_manager_attribute_update', params)
  2872. instance = params[:browser] || @browser
  2873. data = params[:data]
  2874. click(
  2875. browser: instance,
  2876. css: 'a[href="#manage"]',
  2877. mute_log: true,
  2878. )
  2879. click(
  2880. browser: instance,
  2881. css: '.content.active a[href="#system/object_manager"]',
  2882. mute_log: true,
  2883. )
  2884. sleep 4
  2885. instance.execute_script("$(\".content.active td:contains('#{data[:name]}')\").first().click()")
  2886. modal_ready(browser: instance)
  2887. element = instance.find_elements(css: '.modal input[name=display]')[0]
  2888. element.clear
  2889. element.send_keys(data[:display])
  2890. select(
  2891. browser: instance,
  2892. css: '.modal select[name="data_type"]',
  2893. value: data[:data_type],
  2894. mute_log: true,
  2895. )
  2896. if data[:data_option]
  2897. if data[:data_option][:options]
  2898. if data[:data_type] == 'Boolean'
  2899. element = instance.find_elements(css: '.modal .js-valueTrue').first
  2900. element.clear
  2901. element.send_keys(data[:data_option][:options][:true])
  2902. element = instance.find_elements(css: '.modal .js-valueFalse').first
  2903. element.clear
  2904. element.send_keys(data[:data_option][:options][:false])
  2905. else
  2906. data[:data_option][:options].each { |key, value|
  2907. element = instance.find_elements(css: '.modal .js-Table .js-key').last
  2908. element.clear
  2909. element.send_keys(key)
  2910. element = instance.find_elements(css: '.modal .js-Table .js-value').last
  2911. element.clear
  2912. element.send_keys(value)
  2913. element = instance.find_elements(css: '.modal .js-Table .js-add')[0]
  2914. element.click
  2915. }
  2916. end
  2917. end
  2918. [:default, :min, :max, :diff].each { |key|
  2919. next if !data[:data_option].key?(key)
  2920. element = instance.find_elements(css: ".modal [name=\"data_option::#{key}\"]").first
  2921. element.clear
  2922. element.send_keys(data[:data_option][key])
  2923. }
  2924. [:future, :past].each { |key|
  2925. next if !data[:data_option].key?(key)
  2926. select(
  2927. browser: instance,
  2928. css: ".modal select[name=\"data_option::#{key}\"]",
  2929. value: data[:data_option][key],
  2930. mute_log: true,
  2931. )
  2932. }
  2933. end
  2934. instance.find_elements(css: '.modal button.js-submit')[0].click
  2935. if params[:error]
  2936. sleep 4
  2937. watch_for(
  2938. css: '.modal',
  2939. value: params[:error],
  2940. )
  2941. click(
  2942. browser: instance,
  2943. css: '.modal .js-close',
  2944. )
  2945. modal_disappear(browser: instance)
  2946. return
  2947. end
  2948. 11.times {
  2949. element = instance.find_elements(css: 'body')[0]
  2950. text = element.text
  2951. if text =~ /#{Regexp.quote(data[:name])}/
  2952. assert(true, 'object manager attribute updated')
  2953. sleep 1
  2954. return true
  2955. end
  2956. sleep 1
  2957. }
  2958. screenshot(browser: instance, comment: 'object_manager_attribute_update_failed')
  2959. raise 'object manager attribute update failed'
  2960. end
  2961. =begin
  2962. object_manager_attribute_delete(
  2963. browser: browser2,
  2964. data: {
  2965. name: 'field_name' + random,
  2966. },
  2967. )
  2968. =end
  2969. def object_manager_attribute_delete(params = {})
  2970. switch_window_focus(params)
  2971. log('object_manager_attribute_delete', params)
  2972. instance = params[:browser] || @browser
  2973. data = params[:data]
  2974. click(
  2975. browser: instance,
  2976. css: 'a[href="#manage"]',
  2977. mute_log: true,
  2978. )
  2979. click(
  2980. browser: instance,
  2981. css: '.content.active a[href="#system/object_manager"]',
  2982. mute_log: true,
  2983. )
  2984. sleep 4
  2985. instance = params[:browser] || @browser
  2986. data = params[:data]
  2987. r = instance.execute_script("$(\".content.active td:contains('#{data[:name]}')\").first().closest('tr').find('.js-delete').click()")
  2988. #p "rrr #{r.inspect}"
  2989. end
  2990. =begin
  2991. object_manager_attribute_discard_changes(
  2992. browser: browser2,
  2993. )
  2994. =end
  2995. def object_manager_attribute_discard_changes(params = {})
  2996. switch_window_focus(params)
  2997. log('object_manager_attribute_discard_changes', params)
  2998. instance = params[:browser] || @browser
  2999. click(
  3000. browser: instance,
  3001. css: 'a[href="#manage"]',
  3002. mute_log: true,
  3003. )
  3004. click(
  3005. browser: instance,
  3006. css: '.content.active a[href="#system/object_manager"]',
  3007. mute_log: true,
  3008. )
  3009. sleep 4
  3010. element = instance.find_elements(css: '.content.active .js-discard').first
  3011. element.click
  3012. watch_for_disappear(
  3013. browser: instance,
  3014. css: '.content.active .js-discard',
  3015. )
  3016. end
  3017. =begin
  3018. tags_verify(
  3019. browser: browser2,
  3020. tags: {
  3021. 'tag 1' => true,
  3022. 'tag 2' => true,
  3023. 'tag 3' => false,
  3024. },
  3025. )
  3026. =end
  3027. def tags_verify(params = {})
  3028. switch_window_focus(params)
  3029. log('tags_verify', params)
  3030. instance = params[:browser] || @browser
  3031. tags = instance.find_elements({ css: '.content.active .js-tag' })
  3032. assert(tags)
  3033. assert(tags[0])
  3034. tags_found = {}
  3035. params[:tags].each { |key, _value|
  3036. tags_found[key] = false
  3037. }
  3038. tags.each { |element|
  3039. text = element.text
  3040. if tags_found.key?(text)
  3041. tags_found[text] = true
  3042. else
  3043. assert(false, "tag exists but is not in check to verify '#{text}'")
  3044. end
  3045. }
  3046. params[:tags].each { |key, value|
  3047. assert_equal(value, tags_found[key], "tag '#{key}'")
  3048. }
  3049. end
  3050. def quote(string)
  3051. string_quoted = string
  3052. string_quoted.gsub!(/&/, '&amp;')
  3053. string_quoted.gsub!(/</, '&lt;')
  3054. string_quoted.gsub!(/>/, '&gt;')
  3055. string_quoted
  3056. end
  3057. def switch_window_focus(params)
  3058. instance = params[:browser] || @browser
  3059. if instance != @last_used_browser
  3060. log('switch browser window focus', {})
  3061. instance.switch_to.window(instance.window_handles.first)
  3062. end
  3063. @last_used_browser = instance
  3064. end
  3065. def log(method, params = {})
  3066. begin
  3067. instance = params[:browser] || @browser
  3068. if instance
  3069. logs = instance.manage.logs.get(:browser)
  3070. logs.each { |log|
  3071. next if log.level == 'WARNING' && log.message =~ /Declaration\sdropped./ # ignore ff css warnings
  3072. time = Time.zone.parse(Time.zone.at(log.timestamp / 1000).to_datetime.to_s)
  3073. puts "#{time}/#{log.level}: #{log.message}"
  3074. }
  3075. end
  3076. rescue
  3077. # failed to get logs
  3078. end
  3079. return if !@@debug
  3080. return if params[:mute_log]
  3081. puts "#{Time.zone.now}/#{method}: #{params.inspect}"
  3082. end
  3083. end