browser_test_helper.rb 86 KB

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