browser_test_helper.rb 53 KB

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