browser_test_helper.rb 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  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. elsif browser == 'chrome'
  18. # profile are only working on remote selenium
  19. if ENV['REMOTE_URL']
  20. browser_profile = Selenium::WebDriver::Chrome::Profile.new
  21. browser_profile['intl.accept_languages'] = 'en'
  22. end
  23. end
  24. browser_profile
  25. end
  26. def browser_support_cookies
  27. if browser =~ /(internet_explorer|ie)/i
  28. return false
  29. end
  30. true
  31. end
  32. def browser_url
  33. ENV['BROWSER_URL'] || 'http://localhost:3000'
  34. end
  35. def browser_instance
  36. if !@browsers
  37. @browsers = {}
  38. end
  39. if !ENV['REMOTE_URL'] || ENV['REMOTE_URL'].empty?
  40. local_browser = Selenium::WebDriver.for(browser.to_sym, profile: profile)
  41. @browsers[local_browser.hash] = local_browser
  42. browser_instance_preferences(local_browser)
  43. return local_browser
  44. end
  45. caps = Selenium::WebDriver::Remote::Capabilities.send(browser)
  46. if ENV['BROWSER_OS']
  47. caps.platform = ENV['BROWSER_OS']
  48. end
  49. if ENV['BROWSER_VERSION']
  50. caps.version = ENV['BROWSER_VERSION']
  51. end
  52. local_browser = Selenium::WebDriver.for(
  53. :remote,
  54. url: ENV['REMOTE_URL'],
  55. desired_capabilities: caps,
  56. )
  57. @browsers[local_browser.hash] = local_browser
  58. # avoid "Cannot read property 'get_Current' of undefined" issues
  59. begin
  60. browser_instance_preferences(local_browser)
  61. rescue
  62. # just try again
  63. sleep 10
  64. browser_instance_preferences(local_browser)
  65. end
  66. # upload files from remote dir
  67. local_browser.file_detector = lambda do |args|
  68. str = args.first.to_s
  69. str if File.file?(str)
  70. end
  71. local_browser
  72. end
  73. def browser_instance_close(local_browser)
  74. return if !@browsers[local_browser.hash]
  75. @browsers.delete(local_browser.hash)
  76. local_browser.quit
  77. end
  78. def browser_instance_preferences(local_browser)
  79. local_browser.manage.window.resize_to(1024, 800)
  80. if ENV['REMOTE_URL'] !~ /saucelabs|(grid|ci)\.(zammad\.org|znuny\.com)/i
  81. if @browsers.count == 1
  82. local_browser.manage.window.move_to(0, 0)
  83. else
  84. local_browser.manage.window.move_to(1024, 0)
  85. end
  86. end
  87. local_browser.manage.timeouts.implicit_wait = 3 # seconds
  88. end
  89. def teardown
  90. return if !@browsers
  91. @browsers.each { |_hash, local_browser|
  92. screenshot(browser: local_browser, comment: 'teardown')
  93. browser_instance_close(local_browser)
  94. }
  95. end
  96. def screenshot(params)
  97. instance = params[:browser] || @browser
  98. comment = params[:comment] || ''
  99. filename = "tmp/#{Time.zone.now.strftime('screenshot_%Y_%m_%d__%H_%M_%S')}_#{comment}_#{instance.hash}.png"
  100. log('screenshot', { filename: filename })
  101. instance.save_screenshot(filename)
  102. end
  103. =begin
  104. username = login(
  105. browser: browser1,
  106. username: 'someuser',
  107. password: 'somepassword',
  108. url: 'some url', # optional
  109. remember_me: true, # optional
  110. auto_wizard: false, # optional, in case of auto wizard, skip login
  111. )
  112. =end
  113. def login(params)
  114. switch_window_focus(params)
  115. log('login', params)
  116. instance = params[:browser] || @browser
  117. if params[:url]
  118. instance.get(params[:url])
  119. end
  120. element = instance.find_elements(css: '#login input[name="username"]')[0]
  121. if !element
  122. if params[:auto_wizard]
  123. watch_for(
  124. browser: instance,
  125. css: 'body',
  126. value: 'auto wizard is enabled',
  127. timeout: 10,
  128. )
  129. location( url: "#{browser_url}/#getting_started/auto_wizard" )
  130. sleep 10
  131. login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
  132. if login != params[:username]
  133. screenshot(browser: instance, comment: 'auto wizard login failed')
  134. fail 'auto wizard login failed'
  135. end
  136. assert(true, 'auto wizard login ok')
  137. clues_close(
  138. browser: instance,
  139. optional: true,
  140. )
  141. return
  142. end
  143. screenshot(browser: instance, comment: 'login_failed')
  144. fail 'No login box found'
  145. end
  146. screenshot(browser: instance, comment: 'login')
  147. element.clear
  148. element.send_keys(params[:username])
  149. element = instance.find_elements(css: '#login input[name="password"]')[0]
  150. element.clear
  151. element.send_keys(params[:password])
  152. if params[:remember_me]
  153. instance.find_elements(css: '#login .checkbox-replacement')[0].click
  154. end
  155. instance.find_elements(css: '#login button')[0].click
  156. sleep 5
  157. login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
  158. if login != params[:username]
  159. screenshot(browser: instance, comment: 'login_failed')
  160. fail 'login failed'
  161. end
  162. clues_close(
  163. browser: instance,
  164. optional: true,
  165. )
  166. screenshot(browser: instance, comment: 'login_ok')
  167. assert(true, 'login ok')
  168. login
  169. end
  170. =begin
  171. logout(
  172. browser: browser1
  173. )
  174. =end
  175. def logout(params = {})
  176. switch_window_focus(params)
  177. log('logout', params)
  178. instance = params[:browser] || @browser
  179. instance.find_elements(css: 'a[href="#current_user"]')[0].click
  180. sleep 0.1
  181. instance.find_elements(css: 'a[href="#logout"]')[0].click
  182. (1..6).each {
  183. sleep 1
  184. login = instance.find_elements(css: '#login')[0]
  185. next if !login
  186. screenshot(browser: instance, comment: 'logout_ok')
  187. assert(true, 'logout ok')
  188. return
  189. }
  190. screenshot(browser: instance, comment: 'logout_failed')
  191. fail 'no login box found, seems logout was not successfully!'
  192. end
  193. =begin
  194. clues_close(
  195. browser: browser1,
  196. optional: false,
  197. )
  198. =end
  199. def clues_close(params = {})
  200. switch_window_focus(params)
  201. log('clues_close', params)
  202. instance = params[:browser] || @browser
  203. clues = instance.find_elements(css: '.js-modal--clue .js-close')[0]
  204. if !params[:optional] && !clues
  205. screenshot(browser: instance, comment: 'no_clues')
  206. fail 'Unable to closes clues, no clues found!'
  207. end
  208. return if !clues
  209. instance.execute_script("$('.js-modal--clue .js-close').click()")
  210. assert(true, 'clues closed')
  211. sleep 4
  212. end
  213. =begin
  214. location(
  215. browser: browser1,
  216. url: 'http://someurl',
  217. )
  218. =end
  219. def location(params)
  220. switch_window_focus(params)
  221. log('location', params)
  222. instance = params[:browser] || @browser
  223. instance.get(params[:url])
  224. # check if reload was successfull
  225. if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
  226. instance.navigate.refresh
  227. end
  228. screenshot(browser: instance, comment: 'location')
  229. end
  230. =begin
  231. location_check(
  232. browser: browser1,
  233. url: 'http://someurl',
  234. )
  235. =end
  236. def location_check(params)
  237. switch_window_focus(params)
  238. log('location_check', params)
  239. instance = params[:browser] || @browser
  240. sleep 0.7
  241. current_url = instance.current_url
  242. if current_url !~ /#{Regexp.quote(params[:url])}/
  243. screenshot(browser: instance, comment: 'location_check_failed')
  244. fail "url #{current_url} is not matching #{params[:url]}"
  245. end
  246. assert(true, "url #{current_url} is matching #{params[:url]}")
  247. end
  248. =begin
  249. reload(
  250. browser: browser1,
  251. )
  252. =end
  253. def reload(params = {})
  254. switch_window_focus(params)
  255. log('reload', params)
  256. instance = params[:browser] || @browser
  257. screenshot(browser: instance, comment: 'reload_before')
  258. instance.navigate.refresh
  259. # check if reload was successfull
  260. if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
  261. instance.navigate.refresh
  262. end
  263. screenshot(browser: instance, comment: 'reload_after')
  264. end
  265. =begin
  266. click(
  267. browser: browser1,
  268. css: '.some_class',
  269. fast: false, # do not wait
  270. wait: 1, # wait 1 sec.
  271. )
  272. click(
  273. browser: browser1,
  274. text: '.partial_link_text',
  275. fast: false, # do not wait
  276. wait: 1, # wait 1 sec.
  277. )
  278. =end
  279. def click(params)
  280. switch_window_focus(params)
  281. log('click', params)
  282. instance = params[:browser] || @browser
  283. if params[:css]
  284. element = instance.find_elements(css: params[:css])[0]
  285. #instance.mouse.move_to(element)
  286. #sleep 0.2
  287. element.click
  288. # trigger also focus on input/select and textarea fields
  289. #if params[:css] =~ /(input|select|textarea)/
  290. # instance.execute_script("$('#{params[:css]}').trigger('focus')")
  291. # sleep 0.2
  292. #end
  293. else
  294. instance.find_elements(partial_link_text: params[:text])[0].click
  295. end
  296. sleep 0.4 if !params[:fast]
  297. sleep params[:wait] if params[:wait]
  298. end
  299. =begin
  300. scroll_to(
  301. browser: browser1,
  302. position: 'top', # botton
  303. css: '.some_class',
  304. )
  305. =end
  306. def scroll_to(params)
  307. switch_window_focus(params)
  308. log('scroll_to', params)
  309. instance = params[:browser] || @browser
  310. position = 'true'
  311. if params[:position] == 'botton'
  312. position = 'false'
  313. end
  314. execute(
  315. browser: instance,
  316. js: "\$('#{params[:css]}').get(0).scrollIntoView(#{position})",
  317. mute_log: params[:mute_log]
  318. )
  319. sleep 0.2
  320. end
  321. =begin
  322. execute(
  323. browser: browser1,
  324. js: '.some_class',
  325. )
  326. =end
  327. def execute(params)
  328. switch_window_focus(params)
  329. log('js', params)
  330. instance = params[:browser] || @browser
  331. if params[:js]
  332. return instance.execute_script(params[:js])
  333. end
  334. fail "Invalid execute params #{params.inspect}"
  335. end
  336. =begin
  337. exists(
  338. browser: browser1,
  339. css: '.some_class',
  340. )
  341. =end
  342. def exists(params)
  343. switch_window_focus(params)
  344. log('exists', params)
  345. instance = params[:browser] || @browser
  346. if !instance.find_elements(css: params[:css])[0]
  347. screenshot(browser: instance, comment: 'exists_failed')
  348. fail "#{params[:css]} dosn't exist, but should"
  349. end
  350. true
  351. end
  352. =begin
  353. exists_not(
  354. browser: browser1,
  355. css: '.some_class',
  356. )
  357. =end
  358. def exists_not(params)
  359. switch_window_focus(params)
  360. log('exists_not', params)
  361. instance = params[:browser] || @browser
  362. if instance.find_elements(css: params[:css])[0]
  363. screenshot(browser: instance, comment: 'exists_not_failed')
  364. fail "#{params[:css]} exists but should not"
  365. end
  366. true
  367. end
  368. =begin
  369. set(
  370. browser: browser1,
  371. css: '.some_class',
  372. value: true,
  373. slow: false,
  374. blur: true,
  375. clear: true, # todo | default: true
  376. no_click: true,
  377. )
  378. =end
  379. def set(params)
  380. switch_window_focus(params)
  381. log('set', params)
  382. instance = params[:browser] || @browser
  383. element = instance.find_elements(css: params[:css])[0]
  384. if !params[:no_click]
  385. element.click
  386. end
  387. element.clear
  388. if !params[:slow]
  389. element.send_keys(params[:value])
  390. else
  391. element.send_keys('')
  392. keys = params[:value].to_s.split('')
  393. keys.each {|key|
  394. instance.action.send_keys(key).perform
  395. }
  396. end
  397. if params[:blur]
  398. instance.execute_script("$('#{params[:css]}').blur()")
  399. end
  400. sleep 0.5
  401. end
  402. =begin
  403. select(
  404. browser: browser1,
  405. css: '.some_class',
  406. value: 'Some Value',
  407. )
  408. =end
  409. def select(params)
  410. switch_window_focus(params)
  411. log('select', params)
  412. instance = params[:browser] || @browser
  413. # searchable select
  414. element = instance.find_elements(css: "#{params[:css]}.js-shadow")[0]
  415. if element
  416. element = instance.find_elements(css: "#{params[:css]}.js-shadow + .js-input")[0]
  417. element.click
  418. element.clear
  419. sleep 1
  420. element.send_keys(params[:value])
  421. sleep 0.5
  422. element.send_keys(:enter)
  423. sleep 0.5
  424. return
  425. end
  426. # native select
  427. begin
  428. element = instance.find_elements(css: params[:css])[0]
  429. dropdown = Selenium::WebDriver::Support::Select.new(element)
  430. dropdown.select_by(:text, params[:value])
  431. puts "select - #{params.inspect}"
  432. rescue
  433. # just try again
  434. element = instance.find_elements(css: params[:css])[0]
  435. dropdown = Selenium::WebDriver::Support::Select.new(element)
  436. dropdown.select_by(:text, params[:value])
  437. puts "select2 - #{params.inspect}"
  438. end
  439. end
  440. =begin
  441. switch(
  442. browser: browser1,
  443. css: '.some_class',
  444. type: 'on', # 'off'
  445. )
  446. =end
  447. def switch(params)
  448. switch_window_focus(params)
  449. log('switch', params)
  450. instance = params[:browser] || @browser
  451. element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
  452. checked = element.attribute('checked')
  453. if !checked
  454. if params[:type] == 'on'
  455. instance.find_elements(css: "#{params[:css]} label")[0].click
  456. end
  457. elsif params[:type] == 'off'
  458. instance.find_elements(css: "#{params[:css]} label")[0].click
  459. end
  460. end
  461. =begin
  462. check(
  463. browser: browser1,
  464. css: '.some_class',
  465. )
  466. =end
  467. def check(params)
  468. switch_window_focus(params)
  469. log('check', params)
  470. instance = params[:browser] || @browser
  471. element = instance.find_elements(css: params[:css])[0]
  472. checked = element.attribute('checked')
  473. element.click if !checked
  474. end
  475. =begin
  476. uncheck(
  477. browser: browser1,
  478. css: '.some_class',
  479. )
  480. =end
  481. def uncheck(params)
  482. switch_window_focus(params)
  483. log('uncheck', params)
  484. instance = params[:browser] || @browser
  485. element = instance.find_elements(css: params[:css])[0]
  486. checked = element.attribute('checked')
  487. element.click if checked
  488. end
  489. =begin
  490. sendkey(
  491. browser: browser1,
  492. value: :enter,
  493. slow: false,
  494. )
  495. =end
  496. def sendkey(params)
  497. switch_window_focus(params)
  498. log('sendkey', params)
  499. instance = params[:browser] || @browser
  500. if params[:value].class == Array
  501. params[:value].each {|key|
  502. instance.action.send_keys(key).perform
  503. }
  504. return
  505. end
  506. instance.action.send_keys(params[:value]).perform
  507. if params[:slow]
  508. sleep 2
  509. else
  510. sleep 0.6
  511. end
  512. end
  513. =begin
  514. match(
  515. browser: browser1,
  516. css: '#content .text-1',
  517. value: 'some test for browser and some other for browser',
  518. attribute: 'some_attribute', # match on attribute
  519. should_not_match: true,
  520. no_quote: false, # use regex
  521. )
  522. =end
  523. def match(params, fallback = false)
  524. switch_window_focus(params)
  525. log('match', params)
  526. instance = params[:browser] || @browser
  527. element = instance.find_elements(css: params[:css])[0]
  528. if params[:css] =~ /select/
  529. dropdown = Selenium::WebDriver::Support::Select.new(element)
  530. success = false
  531. if dropdown.selected_options
  532. dropdown.selected_options.each {|option|
  533. if option.text == params[:value]
  534. success = true
  535. end
  536. }
  537. end
  538. if params[:should_not_match]
  539. if success
  540. fail "should not match '#{params[:value]}' in select list, but is matching"
  541. end
  542. elsif !success
  543. fail "not matching '#{params[:value]}' in select list"
  544. end
  545. return true
  546. end
  547. # match on attribute
  548. begin
  549. text = if params[:attribute]
  550. element.attribute(params[:attribute])
  551. elsif params[:css] =~ /(input|textarea)/i
  552. element.attribute('value')
  553. else
  554. element.text
  555. end
  556. rescue => e
  557. # just try again
  558. if !fallback
  559. return match(params, true)
  560. end
  561. raise e.inspect
  562. end
  563. # do cleanups (needed for richtext tests)
  564. if params[:cleanup]
  565. text.gsub!(/\s+$/m, '')
  566. params[:value].gsub!(/\s+$/m, '')
  567. end
  568. match = false
  569. if params[:no_quote]
  570. #puts "aaaa #{text}/#{params[:value]}"
  571. if text =~ /#{params[:value]}/i
  572. match = $1 || true
  573. end
  574. elsif text =~ /#{Regexp.quote(params[:value])}/i
  575. match = true
  576. end
  577. if match
  578. if params[:should_not_match]
  579. fail "matching '#{params[:value]}' in content '#{text}' but should not!"
  580. end
  581. elsif !params[:should_not_match]
  582. fail "not matching '#{params[:value]}' in content '#{text}' but should!"
  583. end
  584. sleep 0.8
  585. match
  586. end
  587. =begin
  588. match_not(
  589. browser: browser1,
  590. css: '#content .text-1',
  591. value: 'some test for browser and some other for browser',
  592. attribute: 'some_attribute', # match on attribute
  593. should_not_match: true,
  594. no_quote: false, # use regex
  595. )
  596. =end
  597. def match_not(params)
  598. switch_window_focus(params)
  599. log('match_not', params)
  600. params[:should_not_match] = true
  601. match(params)
  602. end
  603. =begin
  604. task_type(
  605. browser: browser1,
  606. type: 'stayOnTab',
  607. )
  608. =end
  609. def task_type(params)
  610. switch_window_focus(params)
  611. log('task_type', params)
  612. instance = params[:browser] || @browser
  613. if params[:type]
  614. instance.find_elements(css: '.content.active .js-secondaryActionButtonLabel')[0].click
  615. instance.find_elements(css: ".content.active .js-secondaryActionLabel[data-type=#{params[:type]}]")[0].click
  616. return
  617. end
  618. fail "Unknown params for task_type: #{params.inspect}"
  619. end
  620. =begin
  621. cookie(
  622. browser: browser1,
  623. name: '^_zammad.+?',
  624. value: '.+?',
  625. expires: nil,
  626. )
  627. cookie(
  628. browser: browser1,
  629. name: '^_zammad.+?',
  630. should_not_exist: true,
  631. )
  632. =end
  633. def cookie(params)
  634. switch_window_focus(params)
  635. log('cookie', params)
  636. instance = params[:browser] || @browser
  637. if !browser_support_cookies
  638. assert(true, "'#{params[:value]}' ups browser is not supporting reading cookies, go ahead")
  639. return true
  640. end
  641. cookies = instance.manage.all_cookies
  642. cookies.each {|cookie|
  643. #puts "CCC #{cookie.inspect}"
  644. # :name=>"_zammad_session_c25832f4de2", :value=>"adc31cd21615cb0a7ab269184ec8b76f", :path=>"/", :domain=>"localhost", :expires=>nil, :secure=>false}
  645. next if cookie[:name] !~ /#{params[:name]}/i
  646. if params.key?(:value ) && cookie[:value].to_s =~ /#{params[:value]}/i
  647. assert(true, "matching value '#{params[:value]}' in cookie '#{cookie}'")
  648. else
  649. fail "not matching value '#{params[:value]}' in cookie '#{cookie}'"
  650. end
  651. if params.key?(:expires) && cookie[:expires].to_s =~ /#{params[:expires]}/i
  652. assert(true, "matching expires '#{params[:expires].inspect}' in cookie '#{cookie}'")
  653. else
  654. fail "not matching expires '#{params[:expires]}' in cookie '#{cookie}'"
  655. end
  656. return if !params[:should_not_exist]
  657. fail "cookie with name '#{params[:name]}' should not exist, but exists '#{cookies}'"
  658. }
  659. if params[:should_not_exist]
  660. assert(true, "cookie with name '#{params[:name]}' is not existing")
  661. return
  662. end
  663. fail "not matching name '#{params[:name]}' in cookie '#{cookies}'"
  664. end
  665. =begin
  666. verify_title(
  667. browser: browser1,
  668. value: 'some title',
  669. )
  670. =end
  671. def verify_title(params = {})
  672. switch_window_focus(params)
  673. log('verify_title', params)
  674. instance = params[:browser] || @browser
  675. title = instance.title
  676. if title =~ /#{params[:value]}/i
  677. assert(true, "matching '#{params[:value]}' in title '#{title}'")
  678. else
  679. fail "not matching '#{params[:value]}' in title '#{title}'"
  680. end
  681. end
  682. =begin
  683. verify_task(
  684. browser: browser1,
  685. data: {
  686. title: 'some title',
  687. modified: true, # optional
  688. }
  689. )
  690. =end
  691. def verify_task(params = {}, fallback = false)
  692. switch_window_focus(params)
  693. log('verify_task', params)
  694. instance = params[:browser] || @browser
  695. data = params[:data]
  696. sleep 1
  697. begin
  698. # verify title
  699. if data[:title]
  700. title = instance.find_elements(css: '.tasks .is-active')[0].text.strip
  701. if title =~ /#{data[:title]}/i
  702. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  703. else
  704. fail "not matching '#{data[:title]}' in title '#{title}'"
  705. end
  706. end
  707. puts "tv #{params.inspect}"
  708. # verify modified
  709. if data.key?(:modified)
  710. exists = instance.find_elements(css: '.tasks .is-active')[0]
  711. is_modified = instance.find_elements(css: '.tasks .is-modified')[0]
  712. puts "m #{data[:modified].inspect}"
  713. if exists
  714. puts ' exists'
  715. end
  716. if is_modified
  717. puts ' is_modified'
  718. end
  719. if data[:modified] == true
  720. if is_modified
  721. assert(true, "task '#{data[:title]}' is modifed")
  722. elsif !exists
  723. fail "task '#{data[:title]}' not exists, should not modified"
  724. else
  725. fail "task '#{data[:title]}' is not modifed"
  726. end
  727. elsif !is_modified
  728. assert(true, "task '#{data[:title]}' is modifed")
  729. elsif !exists
  730. fail "task '#{data[:title]}' not exists, should be not modified"
  731. else
  732. fail "task '#{data[:title]}' is modifed, but should not"
  733. end
  734. end
  735. rescue => e
  736. # just try again
  737. if !fallback
  738. verify_task(params, true)
  739. end
  740. raise 'ERROR: ' + e.inspect
  741. end
  742. true
  743. end
  744. =begin
  745. open_task(
  746. browser: browser1,
  747. data: {
  748. title: 'some title',
  749. }
  750. )
  751. =end
  752. def open_task(params = {}, _fallback = false)
  753. switch_window_focus(params)
  754. log('open_task', params)
  755. instance = params[:browser] || @browser
  756. data = params[:data]
  757. element = instance.find_elements(partial_link_text: data[:title])[0]
  758. if !element
  759. screenshot(browser: instance, comment: 'open_task_failed')
  760. fail "no task with title '#{data[:title]}' found"
  761. end
  762. element.click
  763. true
  764. end
  765. =begin
  766. file_upload(
  767. browser: browser1,
  768. css: '.active .attachmentPlaceholder-inputHolder input'
  769. files: ['path/in/home/some_file.ext'], # 'test/fixtures/test1.pdf'
  770. )
  771. =end
  772. def file_upload(params = {})
  773. switch_window_focus(params)
  774. log('file_upload', params)
  775. instance = params[:browser] || @browser
  776. params[:files].each {|file|
  777. instance.find_elements(css: params[:css])[0].send_keys "#{Rails.root}/#{file}"
  778. }
  779. sleep 2 * params[:files].count
  780. end
  781. =begin
  782. watch_for(
  783. browser: browser1,
  784. css: '#content .text-1',
  785. value: 'some text',
  786. attribute: 'some_attribute' # optional
  787. timeout: 16, # in sec, default 16
  788. )
  789. =end
  790. def watch_for(params = {})
  791. switch_window_focus(params)
  792. log('watch_for', params)
  793. instance = params[:browser] || @browser
  794. timeout = 16
  795. if params[:timeout]
  796. timeout = params[:timeout]
  797. end
  798. loops = timeout.to_i * 2
  799. text = ''
  800. (1..loops).each {
  801. element = instance.find_elements(css: params[:css])[0]
  802. if element #&& element.displayed?
  803. begin
  804. # match pn attribute
  805. text = if params[:attribute]
  806. element.attribute(params[:attribute])
  807. elsif params[:css] =~ /(input|textarea)/i
  808. element.attribute('value')
  809. else
  810. element.text
  811. end
  812. if text =~ /#{params[:value]}/i
  813. assert(true, "'#{params[:value]}' found in '#{text}'")
  814. sleep 0.5
  815. return true
  816. end
  817. rescue
  818. # try again
  819. end
  820. end
  821. sleep 0.5
  822. }
  823. screenshot(browser: instance, comment: 'watch_for_failed')
  824. fail "'#{params[:value]}' found in '#{text}'"
  825. end
  826. =begin
  827. wait untill selector disabppears
  828. watch_for_disappear(
  829. browser: browser1,
  830. css: '#content .text-1',
  831. timeout: 16, # in sec, default 16
  832. )
  833. wait untill text in selector disabppears
  834. watch_for_disappear(
  835. browser: browser1,
  836. css: '#content .text-1',
  837. value: 'some value as regexp',
  838. timeout: 16, # in sec, default 16
  839. )
  840. =end
  841. def watch_for_disappear(params = {})
  842. switch_window_focus(params)
  843. log('watch_for_disappear', params)
  844. instance = params[:browser] || @browser
  845. timeout = 16
  846. if params[:timeout]
  847. timeout = params[:timeout]
  848. end
  849. loops = timeout.to_i
  850. text = ''
  851. (1..loops).each {
  852. element = instance.find_elements(css: params[:css])[0]
  853. if !element #|| element.displayed?
  854. assert(true, 'not found')
  855. sleep 1
  856. return true
  857. end
  858. if params[:value]
  859. begin
  860. text = instance.find_elements(css: params[:css])[0].text
  861. if text !~ /#{params[:value]}/i
  862. assert(true, "not matching '#{params[:value]}' in text '#{text}'")
  863. sleep 1
  864. return true
  865. end
  866. rescue
  867. # try again
  868. end
  869. end
  870. sleep 1
  871. }
  872. screenshot(browser: instance, comment: 'disappear_failed')
  873. fail "#{params[:css]}) still exsists"
  874. end
  875. =begin
  876. shortcut(
  877. browser: browser1,
  878. key: 'x',
  879. )
  880. =end
  881. def shortcut(params = {})
  882. switch_window_focus(params)
  883. log('shortcut', params)
  884. instance = params[:browser] || @browser
  885. instance.action.key_down(:control)
  886. .key_down(:alt)
  887. .send_keys(params[:key])
  888. .key_up(:alt)
  889. .key_up(:control)
  890. .perform
  891. end
  892. =begin
  893. window_keys(
  894. browser: browser1,
  895. value: 'x',
  896. )
  897. =end
  898. def window_keys(params = {})
  899. switch_window_focus(params)
  900. log('window_keys', params)
  901. instance = params[:browser] || @browser
  902. instance.action.send_keys(params[:value]).perform
  903. end
  904. =begin
  905. tasks_close_all(
  906. browser: browser1,
  907. discard_changes: true,
  908. )
  909. =end
  910. def tasks_close_all(params = {})
  911. switch_window_focus(params)
  912. log('tasks_close_all', params)
  913. instance = params[:browser] || @browser
  914. (1..100).each do
  915. sleep 1
  916. begin
  917. if instance.find_elements(css: '.navigation .tasks .task:first-child')[0]
  918. instance.mouse.move_to(instance.find_elements(css: '.navigation .tasks .task:first-child')[0])
  919. sleep 0.1
  920. click_element = instance.find_elements(css: '.navigation .tasks .task:first-child .js-close')[0]
  921. if click_element
  922. click_element.click
  923. # accept task close warning
  924. if params[:discard_changes]
  925. sleep 1
  926. instance.find_elements(css: '.modal button.js-submit')[0].click
  927. end
  928. end
  929. else
  930. break
  931. end
  932. rescue
  933. # try again
  934. end
  935. end
  936. sleep 1
  937. assert(true, 'all tasks closed')
  938. end
  939. =begin
  940. ticket_customer_select(
  941. browser: browser1,
  942. css: '#content .text-1',
  943. customer: '',
  944. )
  945. =end
  946. def ticket_customer_select(params = {})
  947. switch_window_focus(params)
  948. log('ticket_customer_select', params)
  949. instance = params[:browser] || @browser
  950. element = instance.find_elements(css: params[:css] + ' input[name="customer_id_completion"]')[0]
  951. element.click
  952. element.clear
  953. # workaround, sometimes focus is not triggered
  954. element.send_keys(params[:customer] + '*')
  955. sleep 3.5
  956. # check if pulldown is open, it's not working stable via selenium
  957. #instance.execute_script("$('#{params[:css]} .js-recipientDropdown').addClass('open')")
  958. #sleep 0.5
  959. element.send_keys(:arrow_down)
  960. sleep 0.3
  961. element.send_keys(:enter)
  962. #instance.find_elements(css: params[:css] + ' .recipientList-entry.js-user.is-active')[0].click
  963. sleep 0.6
  964. assert(true, 'ticket_customer_select')
  965. end
  966. =begin
  967. username = overview_create(
  968. browser: browser1,
  969. data: {
  970. name: name,
  971. role: 'Agent',
  972. selector: {
  973. 'Priority': '1 low',
  974. },
  975. prio: 1000,
  976. 'order::direction' => 'down',
  977. }
  978. )
  979. =end
  980. def overview_create(params)
  981. switch_window_focus(params)
  982. log('overview_create', params)
  983. instance = params[:browser] || @browser
  984. data = params[:data]
  985. instance.find_elements(css: 'a[href="#manage"]')[0].click
  986. instance.find_elements(css: 'a[href="#manage/overviews"]')[0].click
  987. sleep 0.2
  988. instance.find_elements(css: '#content a[data-type="new"]')[0].click
  989. sleep 2
  990. if data[:name]
  991. element = instance.find_elements(css: '.modal input[name=name]')[0]
  992. element.clear
  993. element.send_keys(data[:name])
  994. end
  995. if data[:role]
  996. element = instance.find_elements(css: '.modal select[name="role_id"]')[0]
  997. dropdown = Selenium::WebDriver::Support::Select.new(element)
  998. dropdown.select_by(:text, data[:role])
  999. end
  1000. if data[:selector]
  1001. data[:selector].each {|key, value|
  1002. element = instance.find_elements(css: '.modal .ticket_selector .js-attributeSelector select')[0]
  1003. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1004. dropdown.select_by(:text, key)
  1005. element = instance.find_elements(css: '.modal .ticket_selector .js-value select')[0]
  1006. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1007. dropdown.deselect_all
  1008. dropdown.select_by(:text, value)
  1009. }
  1010. end
  1011. if data[:prio]
  1012. element = instance.find_elements(css: '.modal input[name=prio]')[0]
  1013. element.clear
  1014. element.send_keys(data[:prio])
  1015. end
  1016. if data['order::direction']
  1017. element = instance.find_elements(css: '.modal select[name="order::direction"]')[0]
  1018. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1019. dropdown.select_by(:text, data['order::direction'])
  1020. end
  1021. instance.find_elements(css: '.modal button.js-submit')[0].click
  1022. (1..12).each {
  1023. element = instance.find_elements(css: 'body')[0]
  1024. text = element.text
  1025. if text =~ /#{Regexp.quote(data[:name])}/
  1026. assert(true, 'overview created')
  1027. overview = {
  1028. name: name,
  1029. }
  1030. return overview
  1031. end
  1032. sleep 1
  1033. }
  1034. screenshot(browser: instance, comment: 'overview_create_failed')
  1035. fail 'overview creation failed'
  1036. end
  1037. =begin
  1038. ticket = ticket_create(
  1039. browser: browser1,
  1040. data: {
  1041. customer: 'nico',
  1042. group: 'Users',
  1043. priority: '2 normal',
  1044. title: 'overview #1',
  1045. body: 'overview #1',
  1046. },
  1047. do_not_submit: true,
  1048. )
  1049. returns (in case of submitted)
  1050. {
  1051. id: 123,
  1052. number: '100001',
  1053. title: 'overview #1',
  1054. }
  1055. =end
  1056. def ticket_create(params)
  1057. switch_window_focus(params)
  1058. log('ticket_create', params)
  1059. instance = params[:browser] || @browser
  1060. data = params[:data]
  1061. instance.find_elements(css: 'a[href="#new"]')[0].click
  1062. instance.find_elements(css: 'a[href="#ticket/create"]')[0].click
  1063. element = instance.find_elements(css: '.active .newTicket')[0]
  1064. if !element
  1065. screenshot(browser: instance, comment: 'ticket_create_failed')
  1066. fail 'no ticket create screen found!'
  1067. end
  1068. sleep 1
  1069. # check count of agents, should be only 1 / - selection on init screen
  1070. count = instance.find_elements(css: '.active .newTicket select[name="owner_id"] option').count
  1071. assert_equal(1, count, 'check if owner selection is empty per default' )
  1072. if data[:group]
  1073. element = instance.find_elements(css: '.active .newTicket select[name="group_id"]')[0]
  1074. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1075. dropdown.select_by(:text, data[:group])
  1076. sleep 0.2
  1077. end
  1078. if data[:priority]
  1079. element = instance.find_elements(css: '.active .newTicket select[name="priority_id"]')[0]
  1080. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1081. dropdown.select_by(:text, data[:priority])
  1082. sleep 0.2
  1083. end
  1084. if data[:title]
  1085. element = instance.find_elements(css: '.active .newTicket input[name="title"]')[0]
  1086. element.clear
  1087. element.send_keys(data[:title])
  1088. sleep 0.2
  1089. end
  1090. if data[:body]
  1091. #instance.execute_script('$(".active .newTicket div[data-name=body]").focus()')
  1092. sleep 0.5
  1093. element = instance.find_elements(css: '.active .newTicket div[data-name=body]')[0]
  1094. element.clear
  1095. element.send_keys(data[:body])
  1096. # it's not working stable via selenium, use js
  1097. value = instance.find_elements(css: '.content .newTicket div[data-name=body]')[0].text
  1098. #puts "V #{value.inspect}"
  1099. if value != data[:body]
  1100. body_quoted = quote(data[:body])
  1101. instance.execute_script("$('.content.active div[data-name=body]').html('#{body_quoted}').trigger('focusout')")
  1102. end
  1103. end
  1104. if data[:customer]
  1105. element = instance.find_elements(css: '.active .newTicket input[name="customer_id_completion"]')[0]
  1106. element.click
  1107. element.clear
  1108. # workaround, sometimes focus is not triggered
  1109. element.send_keys(data[:customer] + '*')
  1110. sleep 3.5
  1111. # check if pulldown is open, it's not working stable via selenium
  1112. #instance.execute_script("$('.active .newTicket .js-recipientDropdown').addClass('open')")
  1113. #sleep 0.5
  1114. element.send_keys(:arrow_down)
  1115. sleep 0.3
  1116. element.send_keys(:enter)
  1117. #instance.find_elements(css: '.active .newTicket .recipientList-entry.js-user.is-active')[0].click
  1118. sleep 0.6
  1119. end
  1120. if data[:attachment]
  1121. file_upload(
  1122. browser: instance,
  1123. css: '#content .text-1',
  1124. value: 'some text',
  1125. )
  1126. end
  1127. if params[:do_not_submit]
  1128. assert(true, 'ticket created without submit')
  1129. return
  1130. end
  1131. sleep 0.8
  1132. #instance.execute_script('$(".content.active .newTicket form").submit();')
  1133. instance.find_elements(css: '.active .newTicket button.js-submit')[0].click
  1134. sleep 1
  1135. (1..10).each {
  1136. if instance.current_url =~ /#{Regexp.quote('#ticket/zoom/')}/
  1137. assert(true, 'ticket created')
  1138. sleep 2.5
  1139. id = instance.current_url
  1140. id.gsub!(//,)
  1141. id.gsub!(%r{^.+?/(\d+)$}, '\\1')
  1142. element = instance.find_elements(css: '.active .ticketZoom-header .ticket-number')[0]
  1143. if element
  1144. number = element.text
  1145. ticket = {
  1146. id: id,
  1147. number: number,
  1148. title: data[:title],
  1149. }
  1150. sleep 3 # wait until notify is gone
  1151. screenshot(browser: instance, comment: 'ticket_create_ok')
  1152. return ticket
  1153. end
  1154. end
  1155. sleep 1
  1156. }
  1157. screenshot(browser: instance, comment: 'ticket_create_failed')
  1158. fail "ticket creation failed, can't get zoom url (current url is '#{instance.current_url}')"
  1159. end
  1160. =begin
  1161. ticket_update(
  1162. browser: browser1,
  1163. data: {
  1164. title: '',
  1165. customer: 'some_customer@example.com',
  1166. body: 'some body',
  1167. group: 'some group',
  1168. priority: '1 low',
  1169. state: 'closed',
  1170. },
  1171. do_not_submit: true,
  1172. )
  1173. =end
  1174. def ticket_update(params)
  1175. switch_window_focus(params)
  1176. log('ticket_update', params)
  1177. instance = params[:browser] || @browser
  1178. data = params[:data]
  1179. if data[:title]
  1180. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .ticket-title-update')[0]
  1181. #element.clear
  1182. #sleep 0.5
  1183. #element = instance.find_elements(:css => '.content.active .ticketZoom-header .ticket-title-update')[0]
  1184. #element.send_keys(data[:title])
  1185. #sleep 0.5
  1186. #element.send_keys(:tab)
  1187. instance.execute_script('$(".content.active .ticketZoom-header .ticket-title-update").focus()')
  1188. instance.execute_script('$(".content.active .ticketZoom-header .ticket-title-update").text("' + data[:title] + '")')
  1189. instance.execute_script('$(".content.active .ticketZoom-header .ticket-title-update").blur()')
  1190. instance.execute_script('$(".content.active .ticketZoom-header .ticket-title-update").trigger("blur")')
  1191. # {
  1192. # :where => :instance2,
  1193. # :execute => 'sendkey',
  1194. # :css => '.content.active .ticketZoom-header .ticket-title-update',
  1195. # :value => 'TTT',
  1196. # },
  1197. # {
  1198. # :where => :instance2,
  1199. # :execute => 'sendkey',
  1200. # :css => '.content.active .ticketZoom-header .ticket-title-update',
  1201. # :value => :tab,
  1202. # },
  1203. end
  1204. if data[:customer]
  1205. # select tab
  1206. click(browser: instance, css: '.active .tabsSidebar-tab[data-tab="customer"]')
  1207. click(browser: instance, css: '.active div[data-tab="customer"] .js-actions .icon-arrow-down')
  1208. click(browser: instance, css: '.active div[data-tab="customer"] .js-actions [data-type="customer-change"]')
  1209. watch_for(
  1210. browser: instance,
  1211. css: '.modal',
  1212. value: 'change',
  1213. )
  1214. element = instance.find_elements(css: '.modal input[name="customer_id_completion"]')[0]
  1215. element.click
  1216. element.clear
  1217. # workaround, sometimes focus is not triggered
  1218. element.send_keys(data[:customer])
  1219. sleep 3.5
  1220. # check if pulldown is open, it's not working stable via selenium
  1221. #instance.execute_script("$('.modal .user_autocompletion .js-recipientDropdown').addClass('open')")
  1222. #sleep 0.5
  1223. element.send_keys(:arrow_down)
  1224. sleep 0.6
  1225. element.send_keys(:enter)
  1226. #instance.find_elements(css: '.modal .user_autocompletion .recipientList-entry.js-user.is-active')[0].click
  1227. sleep 0.3
  1228. click(browser: instance, css: '.modal .js-submit')
  1229. watch_for_disappear(
  1230. browser: instance,
  1231. css: '.modal',
  1232. )
  1233. watch_for(
  1234. browser: instance,
  1235. css: '.active .tabsSidebar',
  1236. value: data[:customer],
  1237. )
  1238. # select tab
  1239. click(browser: instance, css: '.active .tabsSidebar-tab[data-tab="ticket"]')
  1240. end
  1241. if data[:body]
  1242. #instance.execute_script('$(".content.active div[data-name=body]").focus()')
  1243. sleep 0.5
  1244. element = instance.find_elements(css: '.content.active div[data-name=body]')[0]
  1245. element.clear
  1246. element.send_keys(data[:body])
  1247. # it's not working stable via selenium, use js
  1248. value = instance.find_elements(css: '.content.active div[data-name=body]')[0].text
  1249. if value != data[:body]
  1250. body_quoted = quote(data[:body])
  1251. instance.execute_script("$('.content.active div[data-name=body]').html('#{body_quoted}').trigger('focusout')")
  1252. end
  1253. end
  1254. if data[:group]
  1255. element = instance.find_elements(css: '.active .sidebar select[name="group_id"]')[0]
  1256. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1257. dropdown.select_by(:text, data[:group])
  1258. sleep 0.2
  1259. end
  1260. if data[:priority]
  1261. element = instance.find_elements(css: '.active .sidebar select[name="priority_id"]')[0]
  1262. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1263. dropdown.select_by(:text, data[:priority])
  1264. sleep 0.2
  1265. end
  1266. if data[:state]
  1267. element = instance.find_elements(css: '.active .sidebar select[name="state_id"]')[0]
  1268. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1269. dropdown.select_by(:text, data[:state])
  1270. sleep 0.2
  1271. end
  1272. if data[:state] || data[:group] || data[:body]
  1273. found = nil
  1274. (1..10).each {
  1275. break if found
  1276. begin
  1277. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  1278. if text =~ /(Discard your unsaved changes.|Verwerfen der)/
  1279. found = true
  1280. end
  1281. rescue
  1282. # try again
  1283. end
  1284. sleep 1
  1285. }
  1286. if !found
  1287. screenshot(browser: instance, comment: 'ticket_update_discard_message_failed')
  1288. fail 'no discard message found'
  1289. end
  1290. end
  1291. task_type(
  1292. browser: instance,
  1293. type: 'stayOnTab',
  1294. )
  1295. if params[:do_not_submit]
  1296. assert(true, 'ticket updated without submit')
  1297. return true
  1298. end
  1299. instance.find_elements(css: '.content.active .js-submit')[0].click
  1300. (1..10).each {
  1301. begin
  1302. text = instance.find_elements(css: '.content.active .js-reset')[0].text
  1303. if !text || text.empty?
  1304. screenshot(browser: instance, comment: 'ticket_update_ok')
  1305. return true
  1306. end
  1307. rescue
  1308. # try again
  1309. end
  1310. sleep 1
  1311. }
  1312. screenshot(browser: instance, comment: 'ticket_update_failed')
  1313. fail 'unable to update ticket'
  1314. end
  1315. =begin
  1316. ticket_verify(
  1317. browser: browser1,
  1318. data: {
  1319. title: 'some title',
  1320. body: 'some body',
  1321. ## group: 'some group',
  1322. ## state: 'closed',
  1323. },
  1324. )
  1325. =end
  1326. def ticket_verify(params)
  1327. switch_window_focus(params)
  1328. log('ticket_verify', params)
  1329. instance = params[:browser] || @browser
  1330. data = params[:data]
  1331. if data[:title]
  1332. title = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-title-update')[0].text.strip
  1333. if title =~ /#{data[:title]}/i
  1334. assert(true, "matching '#{data[:title]}' in title '#{title}'")
  1335. else
  1336. fail "not matching '#{data[:title]}' in title '#{title}'"
  1337. end
  1338. end
  1339. if data[:body]
  1340. body = instance.find_elements(css: '.content.active [data-name="body"]')[0].text.strip
  1341. if body =~ /#{data[:body]}/i
  1342. assert(true, "matching '#{data[:body]}' in body '#{body}'")
  1343. else
  1344. fail "not matching '#{data[:body]}' in body '#{body}'"
  1345. end
  1346. end
  1347. true
  1348. end
  1349. =begin
  1350. ticket_open_by_overview(
  1351. browser: browser2,
  1352. number: ticket1[:number],
  1353. link: "#ticket/view/#{name}",
  1354. )
  1355. =end
  1356. def ticket_open_by_overview(params)
  1357. switch_window_focus(params)
  1358. log('ticket_open_by_overview', params)
  1359. instance = params[:browser] || @browser
  1360. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  1361. sleep 1
  1362. execute(
  1363. browser: instance,
  1364. js: '$(".content.active .sidebar").css("display", "block")',
  1365. )
  1366. instance.find_elements(css: ".content.active .sidebar a[href=\"#{params[:link]}\"]")[0].click
  1367. sleep 1
  1368. execute(
  1369. browser: instance,
  1370. js: '$(".content.active .sidebar").css("display", "none")',
  1371. )
  1372. instance.find_elements(partial_link_text: params[:number])[0].click
  1373. sleep 1
  1374. number = instance.find_elements(css: '.active .ticketZoom-header .ticket-number')[0].text
  1375. if number !~ /#{params[:number]}/
  1376. screenshot(browser: instance, comment: 'ticket_open_by_overview_failed')
  1377. fail "unable to search/find ticket #{params[:number]}!"
  1378. end
  1379. sleep 1
  1380. assert(true, "ticket #{params[:number]} found")
  1381. true
  1382. end
  1383. =begin
  1384. ticket_open_by_search(
  1385. browser: browser2,
  1386. number: ticket1[:number],
  1387. )
  1388. =end
  1389. def ticket_open_by_search(params)
  1390. switch_window_focus(params)
  1391. log('ticket_open_by_search', params)
  1392. instance = params[:browser] || @browser
  1393. # search by number
  1394. element = instance.find_elements(css: '#global-search')[0]
  1395. element.click
  1396. element.clear
  1397. element.send_keys(params[:number])
  1398. sleep 3
  1399. # empty search box by x
  1400. instance.find_elements(css: '.search .empty-search')[0].click
  1401. sleep 0.5
  1402. text = instance.find_elements(css: '#global-search')[0].attribute('value')
  1403. if !text
  1404. fail '#global-search is not empty!'
  1405. end
  1406. # search by number again
  1407. element = instance.find_elements(css: '#global-search')[0]
  1408. element.click
  1409. element.clear
  1410. element.send_keys(params[:number])
  1411. sleep 1
  1412. # open ticket
  1413. #instance.find_element(partial_link_text: params[:number] } ).click
  1414. instance.execute_script("$(\"#global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  1415. number = instance.find_elements(css: '.active .ticketZoom-header .ticket-number')[0].text
  1416. if number !~ /#{params[:number]}/
  1417. screenshot(browser: instance, comment: 'ticket_open_by_search_failed')
  1418. fail "unable to search/find ticket #{params[:number]}!"
  1419. end
  1420. sleep 1
  1421. true
  1422. end
  1423. =begin
  1424. ticket_open_by_title(
  1425. browser: browser2,
  1426. title: ticket1[:title],
  1427. )
  1428. =end
  1429. def ticket_open_by_title(params)
  1430. switch_window_focus(params)
  1431. log('ticket_open_by_title', params)
  1432. instance = params[:browser] || @browser
  1433. # search by number
  1434. element = instance.find_elements(css: '#global-search')[0]
  1435. element.click
  1436. element.clear
  1437. element.send_keys(params[:title])
  1438. sleep 3
  1439. # open ticket
  1440. #instance.find_element(partial_link_text: params[:title] } ).click
  1441. instance.execute_script("$(\"#global-search-result a:contains('#{params[:title]}') .nav-tab-icon\").click()")
  1442. title = instance.find_elements(css: '.active .ticketZoom-header .ticket-title-update')[0].text
  1443. if title !~ /#{params[:title]}/
  1444. screenshot(browser: instance, comment: 'ticket_open_by_title_failed')
  1445. fail "unable to search/find ticket #{params[:title]}!"
  1446. end
  1447. sleep 1
  1448. true
  1449. end
  1450. =begin
  1451. overview_count = overview_counter(
  1452. browser: browser2,
  1453. )
  1454. returns
  1455. {
  1456. '#ticket/view/all_unassigned' => 42,
  1457. }
  1458. =end
  1459. def overview_counter(params = {})
  1460. switch_window_focus(params)
  1461. log('overview_counter', params)
  1462. instance = params[:browser] || @browser
  1463. instance.find_elements(css: '.js-overviewsMenuItem')[0].click
  1464. sleep 2
  1465. execute(
  1466. browser: instance,
  1467. js: '$(".content.active .sidebar").css("display", "block")',
  1468. )
  1469. #execute(
  1470. # browser: instance,
  1471. # js: '$(".content.active .overview-header").css("display", "none")',
  1472. #)
  1473. overviews = {}
  1474. instance.find_elements(css: '.content.active .sidebar a[href]').each {|element|
  1475. url = element.attribute('href')
  1476. url.gsub!(%r{(http|https)://.+?/(.+?)$}, '\\2')
  1477. overviews[url] = 0
  1478. #puts url.inspect
  1479. #puts element.inspect
  1480. }
  1481. overviews.each {|url, _value|
  1482. count = instance.find_elements(css: ".content.active .sidebar a[href=\"#{url}\"] .badge")[0].text
  1483. overviews[url] = count.to_i
  1484. }
  1485. log('overview_counter', overviews)
  1486. overviews
  1487. end
  1488. =begin
  1489. organization_open_by_search(
  1490. browser: browser2,
  1491. value: 'some value',
  1492. )
  1493. =end
  1494. def organization_open_by_search(params = {})
  1495. switch_window_focus(params)
  1496. log('organization_open_by_search', params)
  1497. instance = params[:browser] || @browser
  1498. element = instance.find_elements(css: '#global-search')[0]
  1499. element.click
  1500. element.clear
  1501. element.send_keys(params[:value])
  1502. sleep 3
  1503. instance.find_elements(css: '.search .empty-search')[0].click
  1504. sleep 0.5
  1505. text = instance.find_elements(css: '#global-search')[0].attribute('value')
  1506. if !text
  1507. fail '#global-search is not empty!'
  1508. end
  1509. element = instance.find_elements(css: '#global-search')[0]
  1510. element.click
  1511. element.clear
  1512. element.send_keys(params[:value])
  1513. sleep 2
  1514. #instance.find_element(partial_link_text: params[:value] } ).click
  1515. instance.execute_script("$(\"#global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  1516. name = instance.find_elements(css: '.active h1')[0].text
  1517. if name !~ /#{params[:value]}/
  1518. screenshot(browser: instance, comment: 'organization_open_by_search_failed')
  1519. fail "unable to search/find org #{params[:value]}!"
  1520. end
  1521. assert(true, "org #{params[:value]} found")
  1522. sleep 2
  1523. true
  1524. end
  1525. =begin
  1526. user_open_by_search(
  1527. browser: browser2,
  1528. value: 'some value',
  1529. )
  1530. =end
  1531. def user_open_by_search(params = {})
  1532. switch_window_focus(params)
  1533. log('user_open_by_search', params)
  1534. instance = params[:browser] || @browser
  1535. element = instance.find_elements(css: '#global-search')[0]
  1536. element.click
  1537. element.clear
  1538. element.send_keys(params[:value])
  1539. sleep 3
  1540. #instance.find_element(partial_link_text: params[:value]).click
  1541. instance.execute_script("$(\"#global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
  1542. name = instance.find_elements(css: '.active h1')[0].text
  1543. if name !~ /#{params[:value]}/
  1544. screenshot(browser: instance, comment: 'user_open_by_search_failed')
  1545. fail "unable to search/find user #{params[:value]}!"
  1546. end
  1547. assert(true, "user #{params[:term]} found")
  1548. sleep 2
  1549. true
  1550. end
  1551. =begin
  1552. user_create(
  1553. browser: browser2,
  1554. data: {
  1555. #login: 'some login' + random,
  1556. firstname: 'Manage Firstname' + random,
  1557. lastname: 'Manage Lastname' + random,
  1558. email: user_email,
  1559. password: 'some-pass',
  1560. },
  1561. )
  1562. =end
  1563. def user_create(params = {})
  1564. switch_window_focus(params)
  1565. log('user_create', params)
  1566. instance = params[:browser] || @browser
  1567. data = params[:data]
  1568. instance.find_elements(css: 'a[href="#manage"]')[0].click
  1569. sleep 1
  1570. instance.find_elements(css: 'a[href="#manage/users"]')[0].click
  1571. sleep 2
  1572. instance.find_elements(css: 'a[data-type="new"]')[0].click
  1573. sleep 2
  1574. element = instance.find_elements(css: '.modal input[name=firstname]')[0]
  1575. element.clear
  1576. element.send_keys(data[:firstname])
  1577. element = instance.find_elements(css: '.modal input[name=lastname]')[0]
  1578. element.clear
  1579. element.send_keys(data[:lastname])
  1580. element = instance.find_elements(css: '.modal input[name=email]')[0]
  1581. element.clear
  1582. element.send_keys(data[:email])
  1583. element = instance.find_elements(css: '.modal input[name=password]')[0]
  1584. element.clear
  1585. element.send_keys(data[:password])
  1586. element = instance.find_elements(css: '.modal input[name=password_confirm]')[0]
  1587. element.clear
  1588. element.send_keys(data[:password])
  1589. instance.find_elements(css: '.modal input[name="role_ids"][value="3"]')[0].click
  1590. instance.find_elements(css: '.modal button.js-submit')[0].click
  1591. sleep 3.5
  1592. set(
  1593. browser: instance,
  1594. css: '.content .js-search',
  1595. value: data[:email],
  1596. )
  1597. watch_for(
  1598. browser: instance,
  1599. css: 'body',
  1600. value: data[:lastname],
  1601. )
  1602. assert(true, 'user created')
  1603. end
  1604. =begin
  1605. sla_create(
  1606. browser: browser2,
  1607. data: {
  1608. name: 'some sla' + random,
  1609. first_response_time_in_text: 61
  1610. },
  1611. )
  1612. =end
  1613. def sla_create(params = {})
  1614. switch_window_focus(params)
  1615. log('sla_create', params)
  1616. instance = params[:browser] || @browser
  1617. data = params[:data]
  1618. instance.find_elements(css: 'a[href="#manage"]')[0].click
  1619. sleep 1
  1620. instance.find_elements(css: 'a[href="#manage/slas"]')[0].click
  1621. sleep 2
  1622. instance.find_elements(css: 'a.js-new')[0].click
  1623. sleep 2
  1624. element = instance.find_elements(css: '.modal input[name=name]')[0]
  1625. element.clear
  1626. element.send_keys(data[:name])
  1627. element = instance.find_elements(css: '.modal input[name=first_response_time_in_text]')[0]
  1628. element.clear
  1629. element.send_keys(data[:first_response_time_in_text])
  1630. instance.find_elements(css: '.modal button.js-submit')[0].click
  1631. (1..8).each {
  1632. element = instance.find_elements(css: 'body')[0]
  1633. text = element.text
  1634. if text =~ /#{Regexp.quote(data[:name])}/
  1635. assert(true, 'sla created')
  1636. return true
  1637. end
  1638. sleep 1
  1639. }
  1640. screenshot(browser: instance, comment: 'sla_create_failed')
  1641. fail 'sla creation failed'
  1642. end
  1643. =begin
  1644. text_module_create(
  1645. browser: browser2,
  1646. data: {
  1647. name: 'some sla' + random,
  1648. keywords: 'some keywords',
  1649. content: 'some content',
  1650. },
  1651. )
  1652. =end
  1653. def text_module_create(params = {})
  1654. switch_window_focus(params)
  1655. log('text_module_create', params)
  1656. instance = params[:browser] || @browser
  1657. data = params[:data]
  1658. instance.find_elements(css: 'a[href="#manage"]')[0].click
  1659. sleep 1
  1660. instance.find_elements(css: 'a[href="#manage/text_modules"]')[0].click
  1661. sleep 2
  1662. instance.find_elements(css: 'a[data-type="new"]')[0].click
  1663. sleep 2
  1664. element = instance.find_elements(css: '.modal input[name=name]')[0]
  1665. element.clear
  1666. element.send_keys(data[:name])
  1667. element = instance.find_elements(css: '.modal input[name=keywords]')[0]
  1668. element.clear
  1669. element.send_keys(data[:keywords])
  1670. element = instance.find_elements(css: '.modal [data-name=content]')[0]
  1671. element.clear
  1672. element.send_keys(data[:content])
  1673. instance.find_elements(css: '.modal button.js-submit')[0].click
  1674. (1..8).each {
  1675. element = instance.find_elements(css: 'body')[0]
  1676. text = element.text
  1677. if text =~ /#{Regexp.quote(data[:name])}/
  1678. assert(true, 'text module created')
  1679. return true
  1680. end
  1681. sleep 1
  1682. }
  1683. screenshot(browser: instance, comment: 'text_module_create_failed')
  1684. fail 'text module creation failed'
  1685. end
  1686. =begin
  1687. signature_create(
  1688. browser: browser2,
  1689. data: {
  1690. name: 'some sla' + random,
  1691. body: 'some body',
  1692. },
  1693. )
  1694. =end
  1695. def signature_create(params = {})
  1696. switch_window_focus(params)
  1697. log('signature_create', params)
  1698. instance = params[:browser] || @browser
  1699. data = params[:data]
  1700. instance.find_elements(css: 'a[href="#manage"]')[0].click
  1701. sleep 1
  1702. instance.find_elements(css: 'a[href="#channels/email"]')[0].click
  1703. sleep 1
  1704. instance.find_elements(css: 'a[href="#c-signature"]')[0].click
  1705. sleep 8
  1706. instance.find_elements(css: '#content #c-signature a[data-type="new"]')[0].click
  1707. sleep 2
  1708. element = instance.find_elements(css: '.modal input[name=name]')[0]
  1709. element.clear
  1710. element.send_keys(data[:name])
  1711. element = instance.find_elements(css: '.modal [data-name=body]')[0]
  1712. element.clear
  1713. element.send_keys(data[:body])
  1714. instance.find_elements(css: '.modal button.js-submit')[0].click
  1715. (1..12).each {
  1716. element = instance.find_elements(css: 'body')[0]
  1717. text = element.text
  1718. if text =~ /#{Regexp.quote(data[:name])}/
  1719. assert(true, 'signature created')
  1720. return true
  1721. end
  1722. sleep 1
  1723. }
  1724. screenshot(browser: instance, comment: 'signature_create_failed')
  1725. fail 'signature creation failed'
  1726. end
  1727. =begin
  1728. group_create(
  1729. browser: browser2,
  1730. data: {
  1731. name: 'some sla' + random,
  1732. signature: 'some signature bame',
  1733. member: [
  1734. 'some_user_login',
  1735. ],
  1736. },
  1737. )
  1738. =end
  1739. def group_create(params = {})
  1740. switch_window_focus(params)
  1741. log('group_create', params)
  1742. instance = params[:browser] || @browser
  1743. data = params[:data]
  1744. instance.find_elements(css: 'a[href="#manage"]')[0].click
  1745. sleep 0.5
  1746. instance.find_elements(css: 'a[href="#manage/groups"]')[0].click
  1747. sleep 2
  1748. instance.find_elements(css: 'a[data-type="new"]')[0].click
  1749. sleep 2
  1750. element = instance.find_elements(css: '.modal input[name=name]')[0]
  1751. element.clear
  1752. element.send_keys(data[:name])
  1753. element = instance.find_elements(css: '.modal select[name="email_address_id"]')[0]
  1754. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1755. dropdown.select_by(:index, 1)
  1756. #dropdown.select_by(:text, action[:group])
  1757. if data[:signature]
  1758. element = instance.find_elements(css: '.modal select[name="signature_id"]')[0]
  1759. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1760. dropdown.select_by(:text, data[:signature])
  1761. end
  1762. instance.find_elements(css: '.modal button.js-submit')[0].click
  1763. (1..12).each {
  1764. element = instance.find_elements(css: 'body')[0]
  1765. text = element.text
  1766. if text =~ /#{Regexp.quote(data[:name])}/
  1767. assert(true, 'group created')
  1768. # add member
  1769. if data[:member]
  1770. data[:member].each {|login|
  1771. instance.find_elements(css: 'a[href="#manage"]')[0].click
  1772. sleep 0.5
  1773. instance.find_elements(css: 'a[href="#manage/users"]')[0].click
  1774. sleep 3
  1775. element = instance.find_elements(css: '#content [name="search"]')[0]
  1776. element.clear
  1777. element.send_keys(login)
  1778. sleep 3
  1779. #instance.find_elements(:css => '#content table [data-id]')[0].click
  1780. instance.execute_script('$("#content table [data-id] td").first().click()')
  1781. sleep 3
  1782. #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
  1783. instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
  1784. instance.find_elements(css: '.modal button.js-submit')[0].click
  1785. }
  1786. end
  1787. end
  1788. sleep 1
  1789. return true
  1790. }
  1791. screenshot(browser: instance, comment: 'group_create_failed')
  1792. fail 'group creation failed'
  1793. end
  1794. def quote(string)
  1795. string_quoted = string
  1796. string_quoted.gsub!(/&/, '&amp;')
  1797. string_quoted.gsub!(/</, '&lt;')
  1798. string_quoted.gsub!(/>/, '&gt;')
  1799. string_quoted
  1800. end
  1801. def switch_window_focus(params)
  1802. instance = params[:browser] || @browser
  1803. if instance != @last_used_browser
  1804. log('switch browser window focus', {})
  1805. instance.switch_to.window(instance.window_handles.first)
  1806. end
  1807. @last_used_browser = instance
  1808. end
  1809. def log(method, params)
  1810. return if !@@debug
  1811. return if params[:mute_log]
  1812. puts "#{Time.zone.now}/#{method}: #{params.inspect}"
  1813. end
  1814. end