browser_test_helper.rb 55 KB

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