browser_test_helper.rb 53 KB

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