browser_test_helper.rb 51 KB

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