browser_test_helper.rb 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. :subject => '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. # Add more helper methods to be used by all tests here...
  743. def browser_login(data)
  744. all_tests = [
  745. {
  746. :name => 'login',
  747. :instance => data[:instance] || browser_instance,
  748. :url => data[:url] || browser_url,
  749. :action => [
  750. {
  751. :execute => 'login',
  752. :username => data[:username] || 'nicole.braun@zammad.org',
  753. :password => data[:password] || 'test'
  754. },
  755. ],
  756. },
  757. ];
  758. return all_tests
  759. end
  760. def browser_signle_test_with_login(tests, login = {})
  761. all_tests = browser_login( login )
  762. all_tests = all_tests.concat( tests )
  763. browser_single_test(all_tests)
  764. end
  765. def browser_double_test(tests)
  766. instance1 = browser_single_test( browser_login({
  767. :instance => tests[0][:instance1],
  768. :username => tests[0][:instance1_username],
  769. :password => tests[0][:instance1_password],
  770. :url => tests[0][:url],
  771. }), true )
  772. instance2 = browser_single_test( browser_login({
  773. :instance => tests[0][:instance2],
  774. :username => tests[0][:instance2_username],
  775. :password => tests[0][:instance2_password],
  776. :url => tests[0][:url],
  777. }), true )
  778. tests.each { |test|
  779. if test[:action]
  780. test[:action].each { |action|
  781. # wait
  782. if action[:execute] == 'wait'
  783. sleep action[:value]
  784. next
  785. end
  786. # ignore no browser defined action
  787. next if !action[:where]
  788. # set current browser
  789. if action[:where] == :instance1
  790. instance = instance1
  791. else
  792. instance = instance2
  793. end
  794. browser_element_action(test, action, instance)
  795. }
  796. end
  797. }
  798. browser_instance_close(instance1)
  799. browser_instance_close(instance2)
  800. end
  801. def browser_single_test(tests, keep_connection = false)
  802. instance = nil
  803. @stack = nil
  804. tests.each { |test|
  805. if test[:instance]
  806. instance = test[:instance]
  807. end
  808. if test[:url]
  809. instance.get( test[:url] )
  810. end
  811. if test[:action]
  812. test[:action].each { |action|
  813. if action[:execute] == 'wait'
  814. sleep action[:value]
  815. next
  816. end
  817. browser_element_action(test, action, instance)
  818. }
  819. end
  820. }
  821. if keep_connection
  822. return instance
  823. end
  824. browser_instance_close(instance)
  825. end
  826. def browser_element_action(test, action, instance)
  827. puts "NOTICE #{Time.now.to_s}: " + action.inspect
  828. if action[:execute] !~ /accept|dismiss/i
  829. #cookies = instance.manage.all_cookies
  830. #cookies.each {|cookie|
  831. # puts " COOKIE " + cookie.to_s
  832. #}
  833. end
  834. sleep 0.1
  835. if action[:css]
  836. if action[:css].match '###stack###'
  837. action[:css].gsub! '###stack###', @stack
  838. end
  839. begin
  840. if action[:range] == 'all'
  841. element = instance.find_elements( { :css => action[:css] } )
  842. else
  843. element = instance.find_element( { :css => action[:css] } )
  844. end
  845. rescue
  846. element = nil
  847. end
  848. if action[:result] == false
  849. assert( !element, "(#{test[:name]}) Element with css '#{action[:css]}' exists" )
  850. else
  851. assert( element, "(#{test[:name]}) Element with css '#{action[:css]}' doesn't exist" )
  852. end
  853. elsif action[:element] == :url
  854. if instance.current_url =~ /#{Regexp.quote(action[:result])}/
  855. assert( true, "(#{test[:name]}) url #{instance.current_url} is matching #{action[:result]}" )
  856. else
  857. assert( false, "(#{test[:name]}) url #{instance.current_url} is not matching #{action[:result]}" )
  858. end
  859. elsif action[:element] == :title
  860. title = instance.title
  861. if title =~ /#{action[:value]}/i
  862. assert( true, "(#{test[:name]}) matching '#{action[:value]}' in title '#{title}'" )
  863. else
  864. assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in title '#{title}'" )
  865. end
  866. return
  867. elsif action[:element] == :alert
  868. element = instance.switch_to.alert
  869. elsif action[:execute] == 'login'
  870. element = instance.find_elements( { :css => '#login input[name="username"]' } )[0]
  871. if !element
  872. assert( false, "(#{test[:name]}) no login box found!" )
  873. return
  874. end
  875. element.clear
  876. element.send_keys( action[:username] )
  877. element = instance.find_elements( { :css => '#login input[name="password"]' } )[0]
  878. element.clear
  879. element.send_keys( action[:password] )
  880. if action[:remember_me]
  881. instance.find_elements( { :css => '#login [name="remember_me"]' } )[0].click
  882. end
  883. instance.find_elements( { :css => '#login button' } )[0].click
  884. sleep 4
  885. login = instance.find_elements( { :css => '.user-menu .user a' } )[0].attribute('title')
  886. if login != action[:username]
  887. assert( false, "(#{test[:name]}) login failed" )
  888. return
  889. end
  890. assert( true, "(#{test[:name]}) login success" )
  891. return
  892. elsif action[:execute] == 'logout'
  893. instance.find_elements( { :css => 'a[href="#current_user"]' } )[0].click
  894. sleep 0.1
  895. instance.find_elements( { :css => 'a[href="#logout"]' } )[0].click
  896. (1..6).each {|loop|
  897. login = instance.find_elements( { :css => '#login' } )[0]
  898. if login
  899. assert( true, "(#{test[:name]}) logout" )
  900. return
  901. end
  902. }
  903. assert( false, "(#{test[:name]}) no login box found!" )
  904. return
  905. elsif action[:execute] == 'watch_for'
  906. timeout = 16
  907. if action[:timeout]
  908. timeout = action[:timeout]
  909. end
  910. loops = (timeout).to_i
  911. text = ''
  912. (1..loops).each { |loop|
  913. element = instance.find_elements( { :css => action[:area] } )[0]
  914. if element #&& element.displayed?
  915. begin
  916. text = element.text
  917. if text =~ /#{action[:value]}/i
  918. assert( true, "(#{test[:name]}) '#{action[:value]}' found in '#{text}'" )
  919. return
  920. end
  921. rescue
  922. # just try again
  923. end
  924. end
  925. sleep 1
  926. }
  927. assert( false, "(#{test[:name]}) '#{action[:value]}' found in '#{text}'" )
  928. return
  929. elsif action[:execute] == 'watch_for_disappear'
  930. timeout = 16
  931. if action[:timeout]
  932. timeout = action[:timeout]
  933. end
  934. loops = (timeout / 2).to_i
  935. text = ''
  936. (1..loops).each { |loop|
  937. element = instance.find_elements( { :css => action[:area] } )[0]
  938. if !element #|| element.displayed?
  939. assert( true, "(#{test[:name]}) not found" )
  940. sleep 0.2
  941. return
  942. end
  943. sleep 2
  944. }
  945. assert( false, "(#{test[:name]} / #{test[:area]}) still exsists" )
  946. return
  947. # create user
  948. elsif action[:execute] == 'create_user'
  949. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  950. instance.find_elements( { :css => 'a[href="#manage/users"]' } )[0].click
  951. sleep 2
  952. instance.find_elements( { :css => 'a[data-type="new"]' } )[0].click
  953. sleep 2
  954. #element = instance.find_element( { :css => '.modal input[name=login]' } )
  955. #element.clear
  956. #element.send_keys( action[:login] )
  957. element = instance.find_elements( { :css => '.modal input[name=firstname]' } )[0]
  958. element.clear
  959. element.send_keys( action[:firstname] )
  960. element = instance.find_elements( { :css => '.modal input[name=lastname]' } )[0]
  961. element.clear
  962. element.send_keys( action[:lastname] )
  963. element = instance.find_elements( { :css => '.modal input[name=email]' } )[0]
  964. element.clear
  965. element.send_keys( action[:email] )
  966. element = instance.find_elements( { :css => '.modal input[name=password]' } )[0]
  967. element.clear
  968. element.send_keys( action[:password] )
  969. element = instance.find_elements( { :css => '.modal input[name=password_confirm]' } )[0]
  970. element.clear
  971. element.send_keys( action[:password] )
  972. instance.find_elements( { :css => '.modal input[name="role_ids"][value="3"]' } )[0].click
  973. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  974. (1..14).each {|loop|
  975. element = instance.find_elements( { :css => 'body' } )[0]
  976. text = element.text
  977. if text =~ /#{Regexp.quote(action[:lastname])}/
  978. assert( true, "(#{test[:name]}) user created" )
  979. return
  980. end
  981. sleep 0.5
  982. }
  983. assert( true, "(#{test[:name]}) user creation failed" )
  984. return
  985. # create signature
  986. elsif action[:execute] == 'create_signature'
  987. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  988. instance.find_elements( { :css => 'a[href="#channels/email"]' } )[0].click
  989. instance.find_elements( { :css => 'a[href="#c-signature"]' } )[0].click
  990. sleep 8
  991. instance.find_elements( { :css => '#content #c-signature a[data-type="new"]' } )[0].click
  992. sleep 2
  993. element = instance.find_elements( { :css => '.modal input[name=name]' } )[0]
  994. element.clear
  995. element.send_keys( action[:name] )
  996. element = instance.find_elements( { :css => '.modal textarea[name=body]' } )[0]
  997. element.clear
  998. element.send_keys( action[:body] )
  999. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1000. (1..12).each {|loop|
  1001. element = instance.find_elements( { :css => 'body' } )[0]
  1002. text = element.text
  1003. if text =~ /#{Regexp.quote(action[:name])}/
  1004. assert( true, "(#{test[:name]}) signature created" )
  1005. return
  1006. end
  1007. sleep 1
  1008. }
  1009. assert( true, "(#{test[:name]}) signature creation failed" )
  1010. return
  1011. # create group
  1012. elsif action[:execute] == 'create_group'
  1013. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  1014. instance.find_elements( { :css => 'a[href="#manage/groups"]' } )[0].click
  1015. sleep 2
  1016. instance.find_elements( { :css => 'a[data-type="new"]' } )[0].click
  1017. sleep 2
  1018. element = instance.find_elements( { :css => '.modal input[name=name]' } )[0]
  1019. element.clear
  1020. element.send_keys( action[:name] )
  1021. element = instance.find_elements( { :css => '.modal select[name="email_address_id"]' } )[0]
  1022. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1023. dropdown.select_by( :index, 1 )
  1024. #dropdown.select_by( :text, action[:group])
  1025. if action[:signature]
  1026. element = instance.find_elements( { :css => '.modal select[name="signature_id"]' } )[0]
  1027. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1028. dropdown.select_by( :text, action[:signature])
  1029. end
  1030. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1031. (1..12).each {|loop|
  1032. element = instance.find_elements( { :css => 'body' } )[0]
  1033. text = element.text
  1034. if text =~ /#{Regexp.quote(action[:name])}/
  1035. assert( true, "(#{test[:name]}) group created" )
  1036. # add member
  1037. if action[:member]
  1038. action[:member].each {|login|
  1039. instance.find_elements( { :css => 'a[href="#manage"]' } )[0].click
  1040. instance.find_elements( { :css => 'a[href="#manage/users"]' } )[0].click
  1041. sleep 2
  1042. element = instance.find_elements( { :css => '#content [name="search"]' } )[0]
  1043. element.clear
  1044. element.send_keys( login )
  1045. sleep 2
  1046. #instance.find_elements( { :css => '#content table [data-id]' } )[0].click
  1047. instance.execute_script( '$("#content table [data-id] td").first().click()' )
  1048. sleep 2
  1049. #instance.find_elements( { :css => 'label:contains(" ' + action[:name] + '")' } )[0].click
  1050. instance.execute_script( '$(\'label:contains(" ' + action[:name] + '")\').first().click()' )
  1051. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1052. }
  1053. end
  1054. return
  1055. end
  1056. sleep 1
  1057. }
  1058. assert( true, "(#{test[:name]}) group creation failed" )
  1059. return
  1060. elsif action[:execute] == 'verify_task_attributes'
  1061. if action[:title]
  1062. text = instance.find_elements( { :css => '.tasks .active' } )[0].text.strip
  1063. assert_equal( action[:title], text )
  1064. end
  1065. return
  1066. elsif action[:execute] == 'verify_ticket_attributes'
  1067. if action[:title]
  1068. text = instance.find_elements( { :css => '.content.active .page-header .ticket-title-update' } )[0].text.strip
  1069. assert_equal( action[:title], text )
  1070. end
  1071. if action[:body]
  1072. text = instance.find_elements( { :css => '.content.active [data-name="body"]' } )[0].text.strip
  1073. assert_equal( action[:body], text )
  1074. end
  1075. return
  1076. elsif action[:execute] == 'set_ticket_attributes'
  1077. if action[:title]
  1078. #element = instance.find_elements( { :css => '.content.active .page-header .ticket-title-update' } )[0]
  1079. #element.clear
  1080. #sleep 0.5
  1081. #element = instance.find_elements( { :css => '.content.active .page-header .ticket-title-update' } )[0]
  1082. #element.send_keys( action[:title] )
  1083. #sleep 0.5
  1084. #element.send_keys( :tab )
  1085. instance.execute_script( '$(".content.active .page-header .ticket-title-update").focus()' )
  1086. instance.execute_script( '$(".content.active .page-header .ticket-title-update").text("' + action[:title] + '")' )
  1087. instance.execute_script( '$(".content.active .page-header .ticket-title-update").blur()' )
  1088. instance.execute_script( '$(".content.active .page-header .ticket-title-update").trigger("blur")' )
  1089. # {
  1090. # :where => :instance2,
  1091. # :execute => 'sendkey',
  1092. # :css => '.content.active .page-header .ticket-title-update',
  1093. # :value => 'TTT',
  1094. # },
  1095. # {
  1096. # :where => :instance2,
  1097. # :execute => 'sendkey',
  1098. # :css => '.content.active .page-header .ticket-title-update',
  1099. # :value => :tab,
  1100. # },
  1101. end
  1102. if action[:body]
  1103. #instance.execute_script( '$(".content.active div[data-name=body]").focus()' )
  1104. sleep 0.5
  1105. element = instance.find_elements( { :css => '.content.active div[data-name=body]' } )[0]
  1106. element.clear
  1107. element.send_keys( action[:body] )
  1108. end
  1109. return
  1110. # create ticket
  1111. elsif action[:execute] == 'create_ticket'
  1112. instance.find_elements( { :css => 'a[href="#new"]' } )[0].click
  1113. instance.find_elements( { :css => 'a[href="#ticket/create"]' } )[0].click
  1114. element = instance.find_elements( { :css => '.active .newTicket' } )[0]
  1115. if !element
  1116. assert( false, "(#{test[:name]}) no ticket create screen found!" )
  1117. return
  1118. end
  1119. sleep 2
  1120. # check count of agents, should be only 1 / - selection on init screen
  1121. count = instance.find_elements( { :css => '.active .newTicket select[name="owner_id"] option' } ).count
  1122. assert_equal( 1, count, 'check if owner selection is empty per default' )
  1123. if action[:group]
  1124. element = instance.find_elements( { :css => '.active .newTicket select[name="group_id"]' } )[0]
  1125. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1126. dropdown.select_by( :text, action[:group])
  1127. sleep 0.2
  1128. end
  1129. if action[:subject]
  1130. element = instance.find_elements( { :css => '.active .newTicket input[name="title"]' } )[0]
  1131. element.clear
  1132. element.send_keys( action[:subject] )
  1133. sleep 0.2
  1134. end
  1135. if action[:body]
  1136. #instance.execute_script( '$(".active .newTicket div[data-name=body]").focus()' )
  1137. sleep 0.5
  1138. element = instance.find_elements( { :css => '.active .newTicket div[data-name=body]' } )[0]
  1139. element.clear
  1140. element.send_keys( action[:body] )
  1141. end
  1142. if action[:customer] == nil
  1143. element = instance.find_elements( { :css => '.active .newTicket input[name="customer_id_completion"]' } )[0]
  1144. element.click
  1145. element.clear
  1146. element.send_keys( 'nico*' )
  1147. sleep 4
  1148. element.send_keys( :arrow_down )
  1149. sleep 0.1
  1150. instance.find_elements( { :css => '.active .newTicket .recipientList-entry.js-user.is-active' } )[0].click
  1151. sleep 0.3
  1152. end
  1153. if action[:do_not_submit]
  1154. assert( true, "(#{test[:name]}) ticket created without submit" )
  1155. return
  1156. end
  1157. sleep 0.8
  1158. #instance.execute_script( '$(".content.active .newTicket form").submit();' )
  1159. instance.find_elements( { :css => '.active .newTicket button.submit' } )[0].click
  1160. sleep 1
  1161. (1..16).each {|loop|
  1162. if instance.current_url =~ /#{Regexp.quote('#ticket/zoom/')}/
  1163. assert( true, "(#{test[:name]}) ticket created" )
  1164. sleep 1
  1165. return
  1166. end
  1167. sleep 0.5
  1168. }
  1169. assert( false, "(#{test[:name]}) ticket creation failed, can't get zoom url" )
  1170. return
  1171. # search user
  1172. elsif action[:execute] == 'search_user'
  1173. element = instance.find_elements( { :css => '#global-search' } )[0]
  1174. element.click
  1175. element.clear
  1176. if @stack
  1177. action[:term].gsub! '###stack###', @stack
  1178. end
  1179. element.send_keys( action[:term] )
  1180. sleep 3
  1181. element = instance.find_element( { :partial_link_text => action[:term] } ).click
  1182. name = instance.find_elements( { :css => '.active h1' } )[0].text
  1183. if name !~ /#{action[:term]}/
  1184. assert( false, "(#{test[:name]}) unable to search/find user #{action[:term]}!" )
  1185. return
  1186. end
  1187. assert( true, "(#{test[:name]}) user #{action[:term]} found" )
  1188. return
  1189. # search org
  1190. elsif action[:execute] == 'search_organization'
  1191. element = instance.find_elements( { :css => '#global-search' } )[0]
  1192. element.click
  1193. element.clear
  1194. if @stack
  1195. action[:term].gsub! '###stack###', @stack
  1196. end
  1197. element.send_keys( action[:term] )
  1198. sleep 3
  1199. instance.find_elements( { :css => '.search .empty-search' } )[0].click
  1200. sleep 0.5
  1201. text = instance.find_elements( { :css => '#global-search' } )[0].attribute('value')
  1202. if !text
  1203. assert( false, "(#{test[:name]}) #global-search is not empty!" )
  1204. return
  1205. end
  1206. element = instance.find_elements( { :css => '#global-search' } )[0]
  1207. element.click
  1208. element.clear
  1209. if @stack
  1210. action[:term].gsub! '###stack###', @stack
  1211. end
  1212. element.send_keys( action[:term] )
  1213. sleep 2
  1214. element = instance.find_element( { :partial_link_text => action[:term] } ).click
  1215. name = instance.find_elements( { :css => '.active h1' } )[0].text
  1216. if name !~ /#{action[:term]}/
  1217. assert( false, "(#{test[:name]}) unable to search/find org #{action[:term]}!" )
  1218. return
  1219. end
  1220. assert( true, "(#{test[:name]}) org #{action[:term]} found" )
  1221. return
  1222. # search ticket
  1223. elsif action[:execute] == 'search_ticket'
  1224. element = instance.find_elements( { :css => '#global-search' } )[0]
  1225. element.click
  1226. element.clear
  1227. action[:number].gsub! '###stack###', @stack
  1228. element.send_keys( action[:number] )
  1229. sleep 3
  1230. instance.find_elements( { :css => '.search .empty-search' } )[0].click
  1231. sleep 0.5
  1232. text = instance.find_elements( { :css => '#global-search' } )[0].attribute('value')
  1233. if !text
  1234. assert( false, "(#{test[:name]}) #global-search is not empty!" )
  1235. return
  1236. end
  1237. element = instance.find_elements( { :css => '#global-search' } )[0]
  1238. element.click
  1239. element.clear
  1240. action[:number].gsub! '###stack###', @stack
  1241. element.send_keys( action[:number] )
  1242. sleep 2
  1243. element = instance.find_element( { :partial_link_text => action[:number] } ).click
  1244. number = instance.find_elements( { :css => '.active .page-header .ticket-number' } )[0].text
  1245. if number !~ /#{action[:number]}/
  1246. assert( false, "(#{test[:name]}) unable to search/find ticket #{action[:number]}!" )
  1247. return
  1248. end
  1249. assert( true, "(#{test[:name]}) ticket #{action[:number]} found" )
  1250. return
  1251. # close all tasks
  1252. elsif action[:execute] == 'close_all_tasks'
  1253. for i in 1..100
  1254. sleep 1
  1255. if instance.find_elements( { :css => '.navigation .tasks .task:first-child' } )[0]
  1256. instance.mouse.move_to( instance.find_elements( { :css => '.navigation .tasks .task:first-child' } )[0] )
  1257. sleep 0.2
  1258. click_element = instance.find_elements( { :css => '.navigation .tasks .task:first-child .js-close' } )[0]
  1259. if click_element
  1260. sleep 0.1
  1261. click_element.click
  1262. # accept task close warning
  1263. if action[:discard_changes]
  1264. sleep 1
  1265. instance.find_elements( { :css => '.modal button.js-submit' } )[0].click
  1266. end
  1267. end
  1268. else
  1269. break
  1270. end
  1271. end
  1272. assert( true, "(#{test[:name]}) all tasks closed" )
  1273. return
  1274. elsif action[:execute] == 'navigate'
  1275. instance.navigate.to( action[:to] )
  1276. return
  1277. elsif action[:execute] == 'reload'
  1278. instance.navigate.refresh
  1279. return
  1280. elsif action[:execute] == 'js'
  1281. result = instance.execute_script( action[:value] )
  1282. elsif action[:execute] == 'sendkey'
  1283. if action[:value].class == Array
  1284. action[:value].each {|key|
  1285. instance.action.send_keys(key).perform
  1286. }
  1287. else
  1288. instance.action.send_keys(action[:value]).perform
  1289. #instance.action.send_keys(:enter).perform
  1290. end
  1291. # element.send_keys( action[:value] )
  1292. elsif action[:link]
  1293. if action[:link].match '###stack###'
  1294. action[:link].gsub! '###stack###', @stack
  1295. end
  1296. element = instance.find_element( { :partial_link_text => action[:link] } )
  1297. else
  1298. assert( false, "(#{test[:name]}) unknow selector for '#{action[:element]}'" )
  1299. end
  1300. if action[:execute] == 'setCheck'
  1301. checked = element.attribute('checked')
  1302. if !checked
  1303. element.click
  1304. end
  1305. elsif action[:execute] == 'setUncheck'
  1306. checked = element.attribute('checked')
  1307. if checked
  1308. element.click
  1309. end
  1310. elsif action[:execute] == 'set'
  1311. element.clear
  1312. if action[:value] == '###stack###'
  1313. element.send_keys( @stack )
  1314. else
  1315. if !action[:slow]
  1316. element.send_keys( action[:value] )
  1317. else
  1318. element.send_keys( '' )
  1319. keys = action[:value].to_s.split('')
  1320. keys.each {|key|
  1321. instance.action.send_keys(key).perform
  1322. }
  1323. end
  1324. sleep 0.3
  1325. end
  1326. elsif action[:execute] == 'select'
  1327. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1328. dropdown.select_by(:text, action[:value])
  1329. elsif action[:execute] == 'click'
  1330. if element.class == Array
  1331. element.each {|item|
  1332. item.click
  1333. }
  1334. else
  1335. element.click
  1336. end
  1337. elsif action[:execute] == 'accept'
  1338. element.accept
  1339. elsif action[:execute] == 'dismiss'
  1340. element.dismiss
  1341. elsif action[:execute] == 'send_key'
  1342. element.send_keys action[:value]
  1343. elsif action[:execute] == 'match'
  1344. if action[:css] =~ /select/
  1345. dropdown = Selenium::WebDriver::Support::Select.new(element)
  1346. success = false
  1347. if dropdown.selected_options
  1348. dropdown.selected_options.each {|option|
  1349. if option.text == action[:value]
  1350. success = true
  1351. end
  1352. }
  1353. end
  1354. if action[:match_result]
  1355. if success
  1356. assert( true, "(#{test[:name]}) matching '#{action[:value]}' in select list" )
  1357. else
  1358. assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in select list" )
  1359. end
  1360. else
  1361. if success
  1362. assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in select list" )
  1363. else
  1364. assert( true, "(#{test[:name]}) matching '#{action[:value]}' in select list" )
  1365. end
  1366. end
  1367. else
  1368. if action[:attribute]
  1369. text = element.attribute( action[:attribute] )
  1370. elsif action[:css] =~ /(input|textarea)/i
  1371. text = element.attribute('value')
  1372. else
  1373. text = element.text
  1374. end
  1375. if action[:value] == '###stack###'
  1376. action[:value] = @stack
  1377. end
  1378. match = false
  1379. if action[:no_quote]
  1380. #puts "aaaa #{text}/#{action[:value]}"
  1381. if text =~ /#{action[:value]}/i
  1382. if $1
  1383. @stack = $1
  1384. end
  1385. match = $1 || true
  1386. end
  1387. else
  1388. if text =~ /#{Regexp.quote(action[:value])}/i
  1389. match = true
  1390. end
  1391. end
  1392. if match
  1393. if action[:match_result]
  1394. assert( true, "(#{test[:name]}) matching '#{action[:value]}' in content '#{text}'" )
  1395. else
  1396. assert( false, "(#{test[:name]}) matching '#{action[:value]}' in content '#{text}' but should not!" )
  1397. end
  1398. else
  1399. if !action[:match_result]
  1400. assert( true, "(#{test[:name]}) not matching '#{action[:value]}' in content '#{text}'" )
  1401. else
  1402. assert( false, "(#{test[:name]}) not matching '#{action[:value]}' in content '#{text}' but should not!" )
  1403. end
  1404. end
  1405. end
  1406. elsif action[:execute] == 'check'
  1407. elsif action[:execute] == 'js'
  1408. elsif action[:execute] == 'sendkey'
  1409. else
  1410. assert( false, "(#{test[:name]}) unknow action '#{action[:execute]}'" )
  1411. end
  1412. end
  1413. end