browser_test_helper.rb 101 KB

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