browser_test_helper.rb 55 KB

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