1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522 |
- ENV['RAILS_ENV'] = 'test'
- # rubocop:disable HandleExceptions, NonLocalExitFromIterator, Style/GuardClause, Lint/MissingCopEnableDirective
- require File.expand_path('../config/environment', __dir__)
- require 'selenium-webdriver'
- require 'json'
- require 'net/http'
- require 'uri'
- class TestCase < Test::Unit::TestCase
- DEBUG = true
- def browser
- ENV['BROWSER'] || 'firefox'
- end
- def profile
- browser_profile = nil
- if browser == 'firefox'
- browser_profile = Selenium::WebDriver::Firefox::Profile.new
- browser_profile['intl.locale.matchOS'] = false
- browser_profile['intl.accept_languages'] = 'en-US'
- browser_profile['general.useragent.locale'] = 'en-US'
- # currently console log not working for firefox
- # https://github.com/SeleniumHQ/selenium/issues/1161
- #browser_profile['loggingPref'] = { browser: :all }
- elsif browser == 'chrome'
- # profile are only working on remote selenium
- if ENV['REMOTE_URL']
- browser_profile = Selenium::WebDriver::Chrome::Profile.new
- browser_profile['intl.accept_languages'] = 'en'
- browser_profile['loggingPref'] = { browser: :all }
- end
- end
- browser_profile
- end
- def browser_support_cookies
- if browser.match?(/(internet_explorer|ie)/i)
- return false
- end
- true
- end
- def browser_url
- ENV['BROWSER_URL'] || 'http://localhost:3000'
- end
- def browser_instance
- @browsers ||= {}
- if ENV['REMOTE_URL'].blank?
- params = {
- profile: profile,
- }
- if ENV['BROWSER_HEADLESS'].present?
- if browser == 'firefox'
- params[:options] = Selenium::WebDriver::Firefox::Options.new
- params[:options].add_argument('-headless')
- elsif browser == 'chrome'
- params[:options] = Selenium::WebDriver::Chrome::Options.new
- params[:options].add_argument('-headless')
- end
- end
- local_browser = Selenium::WebDriver.for(browser.to_sym, params)
- @browsers[local_browser.hash] = local_browser
- browser_instance_preferences(local_browser)
- return local_browser
- end
- # avoid "Cannot read property 'get_Current' of undefined" issues
- (1..5).each do |count|
- begin
- local_browser = browser_instance_remote
- break
- rescue
- wait_until_ready = rand(5..13)
- sleep wait_until_ready
- log('browser_instance', { rescure: true, count: count, sleep: wait_until_ready })
- end
- end
- local_browser
- end
- def browser_instance_remote
- caps = Selenium::WebDriver::Remote::Capabilities.send(browser)
- if ENV['BROWSER_OS']
- caps.platform = ENV['BROWSER_OS']
- end
- if ENV['BROWSER_VERSION']
- caps.version = ENV['BROWSER_VERSION']
- end
- local_browser = Selenium::WebDriver.for(
- :remote,
- url: ENV['REMOTE_URL'],
- desired_capabilities: caps,
- )
- @browsers[local_browser.hash] = local_browser
- browser_instance_preferences(local_browser)
- # upload files from remote dir
- local_browser.file_detector = lambda do |args|
- str = args.first.to_s
- str if File.file?(str)
- end
- local_browser
- end
- def browser_instance_close(local_browser)
- return if !@browsers[local_browser.hash]
- @browsers.delete(local_browser.hash)
- local_browser.quit
- end
- def browser_instance_preferences(local_browser)
- browser_width = ENV['BROWSER_WIDTH'] || 1024
- browser_height = ENV['BROWSER_HEIGHT'] || 800
- local_browser.manage.window.resize_to(browser_width, browser_height)
- if !ENV['REMOTE_URL']&.match?(/saucelabs|(grid|ci)\.(zammad\.org|znuny\.com)/i)
- if @browsers.count == 1
- local_browser.manage.window.move_to(0, 0)
- else
- local_browser.manage.window.move_to(browser_width, 0)
- end
- end
- local_browser.manage.timeouts.implicit_wait = 3 # seconds
- end
- def teardown
- return if !@browsers
- @browsers.each_value do |local_browser|
- screenshot(browser: local_browser, comment: 'teardown')
- browser_instance_close(local_browser)
- end
- end
- def screenshot(params = {})
- instance = params[:browser] || @browser
- comment = params[:comment] || ''
- filename = "tmp/#{Time.zone.now.strftime('screenshot_%Y_%m_%d__%H_%M_%S_%L')}_#{comment}#{instance.hash}.png"
- log('screenshot', { filename: filename })
- instance.save_screenshot(filename)
- end
- =begin
- username = login(
- browser: browser1,
- username: 'someuser',
- password: 'somepassword',
- url: 'some url', # optional, in case of aleady opened brower a reload is done because url is called again
- remember_me: true, # optional
- auto_wizard: false, # optional, in case of auto wizard, skip login
- success: false, #optional
- )
- =end
- def login(params)
- switch_window_focus(params)
- log('login', params)
- instance = params[:browser] || @browser
- if params[:url]
- instance.get(params[:url])
- end
- # submit logs anyway
- instance.execute_script('App.Track.force()')
- element = instance.find_elements(css: '#login input[name="username"]')[0]
- if !element
- if params[:auto_wizard]
- watch_for(
- browser: instance,
- css: 'body',
- value: 'auto wizard is enabled',
- timeout: 10,
- )
- location(url: "#{browser_url}/#getting_started/auto_wizard")
- sleep 10
- login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
- if login != params[:username]
- screenshot(browser: instance, comment: 'auto wizard login failed')
- raise 'auto wizard login failed'
- end
- assert(true, 'auto wizard login ok')
- clues_close(
- browser: instance,
- optional: true,
- )
- return
- end
- screenshot(browser: instance, comment: 'login_failed')
- raise 'No login box found'
- end
- element.clear
- element.send_keys(params[:username])
- element = instance.find_elements(css: '#login input[name="password"]')[0]
- element.clear
- element.send_keys(params[:password])
- if params[:remember_me]
- instance.find_elements(css: '#login .checkbox-replacement')[0].click
- end
- instance.find_elements(css: '#login button')[0].click
- sleep 4
- login_failed = false
- if !instance.find_elements(css: '.user-menu .user a')[0]
- login_failed = true
- else
- login = instance.find_elements(css: '.user-menu .user a')[0].attribute('title')
- if login != params[:username]
- login_failed = true
- end
- end
- if login_failed
- if params[:success] == false
- assert(true, 'login not successfull, like wanted')
- return true
- end
- screenshot(browser: instance, comment: 'login_failed')
- raise 'login failed'
- end
- if params[:success] == false
- raise 'login successfull but should not'
- end
- clues_close(
- browser: instance,
- optional: true,
- )
- assert(true, 'login ok')
- login
- end
- =begin
- logout(
- browser: browser1
- )
- =end
- def logout(params = {})
- switch_window_focus(params)
- log('logout', params)
- instance = params[:browser] || @browser
- click(
- browser: instance,
- css: 'a[href="#current_user"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: 'a[href="#logout"]',
- mute_log: true,
- )
- 5.times do
- sleep 1
- login = instance.find_elements(css: '#login')[0]
- next if !login
- assert(true, 'logout ok')
- return
- end
- screenshot(browser: instance, comment: 'logout_failed')
- raise 'no login box found, seems logout was not successfully!'
- end
- =begin
- clues_close(
- browser: browser1,
- optional: false,
- )
- =end
- def clues_close(params = {})
- switch_window_focus(params)
- log('clues_close', params)
- instance = params[:browser] || @browser
- clues = instance.find_elements(css: '.js-modal--clue .js-close')[0]
- if !params[:optional] && !clues
- screenshot(browser: instance, comment: 'no_clues')
- raise 'Unable to closes clues, no clues found!'
- end
- return if !clues
- instance.execute_script("$('.js-modal--clue .js-close').click()")
- assert(true, 'clues closed')
- sleep 1
- end
- =begin
- notify_close(
- browser: browser1,
- optional: false,
- )
- =end
- def notify_close(params = {})
- switch_window_focus(params)
- log('notify_close', params)
- instance = params[:browser] || @browser
- notify = instance.find_elements(css: '.noty_inline_layout_container.i-am-new')[0]
- if !params[:optional] && !notify
- screenshot(browser: instance, comment: 'no_notify')
- raise 'Unable to closes notify, no notify found!'
- end
- return if !notify
- notify.click
- assert(true, 'notify closed')
- sleep 1
- end
- =begin
- location(
- browser: browser1,
- url: 'http://someurl',
- )
- =end
- def location(params)
- switch_window_focus(params)
- log('location', params)
- instance = params[:browser] || @browser
- instance.get(params[:url])
- # check if reload was successfull
- if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
- instance.navigate.refresh
- end
- end
- =begin
- location_check(
- browser: browser1,
- url: 'http://someurl',
- )
- =end
- def location_check(params)
- switch_window_focus(params)
- log('location_check', params)
- instance = params[:browser] || @browser
- sleep 0.7
- current_url = instance.current_url
- if !current_url.match?(/#{Regexp.quote(params[:url])}/)
- screenshot(browser: instance, comment: 'location_check_failed')
- raise "url #{current_url} is not matching #{params[:url]}"
- end
- assert(true, "url #{current_url} is matching #{params[:url]}")
- end
- =begin
- reload(
- browser: browser1,
- )
- =end
- def reload(params = {})
- switch_window_focus(params)
- log('reload', params)
- instance = params[:browser] || @browser
- instance.navigate.refresh
- # check if reload was successfull
- if !instance.find_elements(css: 'body')[0] || instance.find_elements(css: 'body')[0].text =~ /unavailable or too busy/i
- instance.navigate.refresh
- end
- screenshot(browser: instance, comment: 'reload_after')
- end
- =begin
- click(
- browser: browser1,
- css: '.some_class',
- fast: false, # do not wait
- wait: 1, # wait 1 sec.
- )
- click(
- browser: browser1,
- xpath: '//a[contains(@class,".text-1")]',
- fast: false, # do not wait
- wait: 1, # wait 1 sec.
- )
- click(
- browser: browser1,
- text: '.partial_link_text',
- fast: false, # do not wait
- wait: 1, # wait 1 sec.
- )
- =end
- def click(params)
- switch_window_focus(params)
- log('click', params)
- instance = params[:browser] || @browser
- if params.include?(:css)
- param_key = :css
- find_element_key = :css
- elsif params.include?(:xpath)
- param_key = :xpath
- find_element_key = :xpath
- else
- param_key = :text
- find_element_key = :partial_link_text
- sleep 0.5
- end
- begin
- elements = instance.find_elements(find_element_key => params[param_key])
- .tap { |e| e.slice!(1..-1) unless params[:all] }
- if elements.empty?
- return if params[:only_if_exists] == true
- raise "No such element '#{params[param_key]}'"
- end
- # a clumsy substitute for elements.each(&:click)
- # (we need to refresh element references after each element.click
- # because if clicks alter page content,
- # subsequent element.clicks will raise a StaleElementReferenceError)
- elements.length.times do |i|
- instance.find_elements(find_element_key => params[param_key])[i].try(:click)
- end
- rescue => e
- raise e if (fail_count ||= 0).positive?
- fail_count += 1
- log('click', { rescure: true })
- sleep 0.5
- retry
- end
- sleep 0.2 if !params[:fast]
- sleep params[:wait] if params[:wait]
- end
- =begin
- perform_macro('Close & Tag as Spam')
- # or
- perform_macro(
- name: 'Close & Tag as Spam',
- browser: browser1,
- )
- =end
- def perform_macro(params)
- switch_window_focus(params)
- log('perform_macro', params)
- instance = params[:browser] || @browser
- click(
- browser: instance,
- css: '.active.content .js-submitDropdown .js-openDropdownMacro'
- )
- click(
- browser: instance,
- xpath: "//div[contains(@class, 'content') and contains(@class, 'active')]//li[contains(@class, 'js-dropdownActionMacro') and contains(text(), '#{params[:name]}')]"
- )
- end
- =begin
- scroll_to(
- browser: browser1,
- position: 'top', # botton
- css: '.some_class',
- )
- =end
- def scroll_to(params)
- switch_window_focus(params)
- log('scroll_to', params)
- instance = params[:browser] || @browser
- position = 'true'
- if params[:position] == 'botton'
- position = 'false'
- end
- execute(
- browser: instance,
- js: "\$('#{params[:css]}').get(0).scrollIntoView(#{position})",
- mute_log: params[:mute_log]
- )
- sleep 0.3
- screenshot(browser: instance, comment: 'scroll_to_after')
- end
- =begin
- modal_close(
- browser: browser1,
- )
- =end
- def modal_close(params = {})
- switch_window_focus(params)
- log('modal_close', params)
- instance = params[:browser] || @browser
- element = instance.find_elements(css: '.modal .js-close')[0]
- raise "No such modal to close #{params.inspect}" if !element
- element.click
- end
- =begin
- modal_ready(
- browser: browser1,
- )
- =end
- def modal_ready(params = {})
- switch_window_focus(params)
- log('modal_ready', params)
- instance = params[:browser] || @browser
- watch_for(
- browser: instance,
- css: '.modal.in',
- timeout: params[:timeout] || 4,
- )
- end
- =begin
- modal_disappear(
- browser: browser1,
- timeout: 12, # default 8
- )
- =end
- def modal_disappear(params = {})
- switch_window_focus(params)
- log('modal_disappear', params)
- instance = params[:browser] || @browser
- watch_for_disappear(
- browser: instance,
- css: '.modal',
- timeout: params[:timeout] || 8,
- )
- end
- =begin
- execute(
- browser: browser1,
- js: '.some_class',
- )
- =end
- def execute(params)
- switch_window_focus(params)
- log('js', params)
- instance = params[:browser] || @browser
- if params[:js]
- return instance.execute_script(params[:js])
- end
- raise "Invalid execute params #{params.inspect}"
- end
- =begin
- exists(
- browser: browser1,
- css: '.some_class',
- )
- exists(
- displayed: false, # true|false
- browser: browser1,
- css: '.some_class',
- displayed: true, # true|false
- )
- =end
- def exists(params)
- retries ||= 0
- switch_window_focus(params)
- log('exists', params)
- instance = params[:browser] || @browser
- if !instance.find_elements(css: params[:css])[0]
- screenshot(browser: instance, comment: 'exists_failed')
- raise "#{params[:css]} dosn't exist, but should"
- end
- if params.key?(:displayed)
- if params[:displayed] == true && !instance.find_elements(css: params[:css])[0].displayed?
- raise "#{params[:css]} is not displayed, but should"
- end
- if params[:displayed] == false && instance.find_elements(css: params[:css])[0].displayed?
- raise "#{params[:css]} is displayed, but should not"
- end
- end
- true
- rescue Selenium::WebDriver::Error::StaleElementReferenceError
- sleep retries
- retries += 1
- retry if retries < 3
- end
- =begin
- exists_not(
- browser: browser1,
- css: '.some_class',
- )
- =end
- def exists_not(params)
- switch_window_focus(params)
- log('exists_not', params)
- instance = params[:browser] || @browser
- if instance.find_elements(css: params[:css])[0]
- screenshot(browser: instance, comment: 'exists_not_failed')
- raise "#{params[:css]} exists but should not"
- end
- true
- end
- =begin
- set(
- browser: browser1,
- css: '.some_class',
- value: true,
- slow: false,
- blur: true, # default false
- clear: true, # todo | default: true
- no_click: true,
- )
- =end
- def set(params)
- switch_window_focus(params)
- log('set', params)
- instance = params[:browser] || @browser
- element = instance.find_elements(css: params[:css])[0]
- if !params[:no_click]
- element.click
- end
- element.clear
- begin
- if !params[:slow]
- element.send_keys(params[:value])
- else
- element.send_keys('')
- keys = params[:value].to_s.split('')
- keys.each do |key|
- instance.action.send_keys(key).perform
- end
- end
- rescue => e
- sleep 0.5
- # just try again
- log('set', { rescure: true })
- element = instance.find_elements(css: params[:css])[0]
- raise "No such element '#{params[:css]}'" if !element
- if !params[:slow]
- element.send_keys(params[:value])
- else
- element.send_keys('')
- keys = params[:value].to_s.split('')
- keys.each do |key|
- instance.action.send_keys(key).perform
- end
- end
- end
- # it's not working stable with ff via selenium, use js
- if browser =~ /firefox/i && params[:css] =~ /\[data-name=/
- log('set_ff_trigger_workaround', params)
- instance.execute_script("$('#{params[:css]}').trigger('focusout')")
- end
- if params[:blur]
- instance.execute_script("$('#{params[:css]}').blur()")
- end
- sleep 0.2
- end
- =begin
- select(
- browser: browser1,
- css: '.some_class',
- value: 'Some Value',
- deselect_all: false, # default false
- )
- =end
- def select(params)
- switch_window_focus(params)
- log('select', params)
- instance = params[:browser] || @browser
- # searchable select
- element = instance.find_elements(css: "#{params[:css]}.js-shadow")[0]
- if element
- element = instance.find_elements(css: "#{params[:css]}.js-shadow + .js-input")[0]
- element.click
- element.clear
- sleep 0.2
- element.send_keys(params[:value])
- sleep 0.2
- element.send_keys(:enter)
- sleep 0.2
- return
- end
- # native select
- begin
- element = instance.find_elements(css: params[:css])[0]
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- if params[:deselect_all]
- dropdown.deselect_all
- end
- dropdown.select_by(:text, params[:value])
- #puts "select - #{params.inspect}"
- rescue
- sleep 0.4
- # just try again
- log('select', { rescure: true })
- element = instance.find_elements(css: params[:css])[0]
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- if params[:deselect_all]
- dropdown.deselect_all
- end
- dropdown.select_by(:text, params[:value])
- #puts "select2 - #{params.inspect}"
- end
- end
- =begin
- switch(
- browser: browser1,
- css: '.some_class',
- type: 'on', # 'off'
- no_check: true, # do not check is switch has changed, in case if js alert
- )
- =end
- def switch(params)
- switch_window_focus(params)
- log('switch', params)
- instance = params[:browser] || @browser
- element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
- checked = element.attribute('checked')
- if !checked
- if params[:type] == 'on'
- instance.find_elements(css: "#{params[:css]} label")[0].click
- sleep 2
- if params[:no_check] != true
- element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
- checked = element.attribute('checked')
- raise 'Switch not on!' if !checked
- end
- end
- elsif params[:type] == 'off'
- instance.find_elements(css: "#{params[:css]} label")[0].click
- sleep 2
- if params[:no_check] != true
- element = instance.find_elements(css: "#{params[:css]} input[type=checkbox]")[0]
- checked = element.attribute('checked')
- raise 'Switch not off!' if checked
- end
- end
- end
- =begin
- check(
- browser: browser1,
- css: '.some_class',
- )
- =end
- def check(params)
- switch_window_focus(params)
- log('check', params)
- instance = params[:browser] || @browser
- instance.execute_script("$('#{params[:css]}:not(:checked)').click()")
- #element = instance.find_elements(css: params[:css])[0]
- #checked = element.attribute('checked')
- #element.click if !checked
- end
- =begin
- uncheck(
- browser: browser1,
- css: '.some_class',
- )
- =end
- def uncheck(params)
- switch_window_focus(params)
- log('uncheck', params)
- instance = params[:browser] || @browser
- instance.execute_script("$('#{params[:css]}:checked').click()")
- #element = instance.find_elements(css: params[:css])[0]
- #checked = element.attribute('checked')
- #element.click if checked
- end
- =begin
- sendkey(
- browser: browser1,
- value: :enter,
- slow: false, # default false
- )
- =end
- def sendkey(params)
- switch_window_focus(params)
- log('sendkey', params)
- instance = params[:browser] || @browser
- element = nil
- if params[:css]
- element = instance.find_elements(css: params[:css])[0]
- end
- if params[:value].class == Array
- params[:value].each do |key|
- if element
- element.send_keys(key)
- else
- instance.action.send_keys(key).perform
- end
- end
- return
- end
- if element
- element.send_keys(params[:value])
- else
- instance.action.send_keys(params[:value]).perform
- end
- if params[:slow]
- sleep 1.5
- else
- sleep 0.2
- end
- end
- =begin
- match(
- browser: browser1,
- css: '#content .text-1',
- value: 'some test for browser and some other for browser',
- attribute: 'some_attribute', # match on attribute
- should_not_match: true,
- no_quote: false, # use regex
- )
- =end
- def match(params, fallback = false)
- switch_window_focus(params)
- log('match', params)
- instance = params[:browser] || @browser
- element = instance.find_elements(css: params[:css])[0]
- if params[:css].match?(/select/)
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- success = false
- dropdown.selected_options&.each do |option|
- if option.text == params[:value]
- success = true
- end
- end
- if params[:should_not_match]
- if success
- screenshot(browser: instance, comment: 'match_failed')
- raise "should not match '#{params[:value]}' in select list, but is matching"
- end
- elsif !success
- screenshot(browser: instance, comment: 'match_failed')
- raise "not matching '#{params[:value]}' in select list"
- end
- return true
- end
- # match on attribute
- begin
- text = if params[:attribute]
- element.attribute(params[:attribute])
- elsif params[:css].match?(/(input|textarea)/i)
- element.attribute('value')
- else
- element.text
- end
- rescue => e
- # just try again
- if !fallback
- return match(params, true)
- end
- raise e.inspect
- end
- # do cleanups (needed for richtext tests)
- if params[:cleanup]
- text.gsub!(/\s+$/m, '')
- params[:value].gsub!(/\s+$/m, '')
- end
- match = false
- if params[:no_quote]
- #puts "aaaa #{text}/#{params[:value]}"
- if text =~ /#{params[:value]}/i
- match = $1 || true
- end
- elsif text.match?(/#{Regexp.quote(params[:value])}/i)
- match = true
- end
- if match
- if params[:should_not_match]
- screenshot(browser: instance, comment: 'match_failed')
- raise "matching '#{params[:value]}' in content '#{text}' but should not!"
- end
- elsif !params[:should_not_match]
- screenshot(browser: instance, comment: 'match_failed')
- raise "not matching '#{params[:value]}' in content '#{text}' but should!"
- end
- sleep 0.2
- match
- end
- =begin
- match_not(
- browser: browser1,
- css: '#content .text-1',
- value: 'some test for browser and some other for browser',
- attribute: 'some_attribute', # match on attribute
- should_not_match: true,
- no_quote: false, # use regex
- )
- =end
- def match_not(params)
- switch_window_focus(params)
- log('match_not', params)
- params[:should_not_match] = true
- match(params)
- end
- =begin
- Get the on-screen pixel coordinates of a given DOM element. Can be used to compare
- the relative location of table rows before and after sort, for example.
- Returns a Selenium::WebDriver::Point object. Use result.x and result.y to access
- its X and Y coordinates respectively.
- get_location(
- browser: browser1,
- css: '.some_class',
- )
- =end
- def get_location(params)
- switch_window_focus(params)
- log('exists', params)
- instance = params[:browser] || @browser
- if params[:css]
- query = { css: params[:css] }
- end
- if params[:xpath]
- query = { xpath: params[:xpath] }
- end
- if !instance.find_elements(query)[0]
- screenshot(browser: instance, comment: 'exists_failed')
- raise "#{query} dosn't exist, but should"
- end
- instance.find_elements(query)[0].location
- end
- =begin
- set type of task (closeTab, closeNextInOverview, stayOnTab)
- task_type(
- browser: browser1,
- type: 'stayOnTab',
- )
- =end
- def task_type(params)
- switch_window_focus(params)
- log('task_type', params)
- instance = params[:browser] || @browser
- if params[:type]
- instance.find_elements(css: '.content.active .js-secondaryActionButtonLabel')[0].click
- instance.find_elements(css: ".content.active .js-secondaryActionLabel[data-type=#{params[:type]}]")[0].click
- return
- end
- raise "Unknown params for task_type: #{params.inspect}"
- end
- =begin
- cookie(
- browser: browser1,
- name: '^_zammad.+?',
- value: '.+?',
- expires: nil,
- )
- cookie(
- browser: browser1,
- name: '^_zammad.+?',
- should_not_exist: true,
- )
- =end
- def cookie(params)
- switch_window_focus(params)
- log('cookie', params)
- instance = params[:browser] || @browser
- if !browser_support_cookies
- assert(true, "'#{params[:value]}' ups browser is not supporting reading cookies, go ahead")
- return true
- end
- cookies = instance.manage.all_cookies
- cookies.each do |cookie|
- # :name=>"_zammad_session_c25832f4de2", :value=>"adc31cd21615cb0a7ab269184ec8b76f", :path=>"/", :domain=>"localhost", :expires=>nil, :secure=>false}
- next if cookie[:name] !~ /#{params[:name]}/i
- if params.key?(:value) && cookie[:value].to_s =~ /#{params[:value]}/i
- assert(true, "matching value '#{params[:value]}' in cookie '#{cookie}'")
- else
- raise "not matching value '#{params[:value]}' in cookie '#{cookie}'"
- end
- if params.key?(:expires) && cookie[:expires].to_s =~ /#{params[:expires]}/i
- assert(true, "matching expires '#{params[:expires].inspect}' in cookie '#{cookie}'")
- else
- raise "not matching expires '#{params[:expires]}' in cookie '#{cookie}'"
- end
- return if !params[:should_not_exist]
- raise "cookie with name '#{params[:name]}' should not exist, but exists '#{cookies}'"
- end
- if params[:should_not_exist]
- assert(true, "cookie with name '#{params[:name]}' is not existing")
- return
- end
- raise "not matching name '#{params[:name]}' in cookie '#{cookies}'"
- end
- =begin
- verify_title(
- browser: browser1,
- value: 'some title',
- )
- =end
- def verify_title(params = {})
- switch_window_focus(params)
- log('verify_title', params)
- instance = params[:browser] || @browser
- title = instance.title
- if title.match?(/#{params[:value]}/i)
- assert(true, "matching '#{params[:value]}' in title '#{title}'")
- else
- raise "not matching '#{params[:value]}' in title '#{title}'"
- end
- end
- =begin
- verify_task(
- browser: browser1,
- data: {
- title: 'some title',
- modified: true, # optional
- }
- )
- =end
- def verify_task(params = {}, fallback = false)
- switch_window_focus(params)
- log('verify_task', params)
- instance = params[:browser] || @browser
- data = params[:data]
- sleep 1
- begin
- # verify title
- if data[:title]
- title = instance.find_elements(css: '.tasks .is-active')[0].text.strip
- if title.match?(/#{data[:title]}/i)
- assert(true, "matching '#{data[:title]}' in title '#{title}'")
- else
- screenshot(browser: instance, comment: 'verify_task_failed')
- raise "not matching '#{data[:title]}' in title '#{title}'"
- end
- end
- # verify modified
- if data.key?(:modified)
- exists = instance.find_elements(css: '.tasks .is-active')[0]
- is_modified = instance.find_elements(css: '.tasks .is-modified')[0]
- puts "m #{data[:modified].inspect}"
- if exists
- puts ' exists'
- end
- if is_modified
- puts ' is_modified'
- end
- if data[:modified] == true
- if is_modified
- assert(true, "task '#{data[:title]}' is modifed")
- elsif !exists
- screenshot(browser: instance, comment: 'verify_task_failed')
- raise "task '#{data[:title]}' not exists, should not modified"
- else
- screenshot(browser: instance, comment: 'verify_task_failed')
- raise "task '#{data[:title]}' is not modifed"
- end
- elsif !is_modified
- assert(true, "task '#{data[:title]}' is modifed")
- elsif !exists
- screenshot(browser: instance, comment: 'verify_task_failed')
- raise "task '#{data[:title]}' not exists, should be not modified"
- else
- screenshot(browser: instance, comment: 'verify_task_failed')
- raise "task '#{data[:title]}' is modifed, but should not"
- end
- end
- rescue => e
- # just try again
- if !fallback
- verify_task(params, true)
- end
- raise 'ERROR: ' + e.inspect
- end
- true
- end
- =begin
- open_task(
- browser: browser1,
- data: {
- title: 'some title',
- }
- )
- =end
- def open_task(params = {})
- switch_window_focus(params)
- log('open_task', params)
- instance = params[:browser] || @browser
- data = params[:data]
- element = instance.find_element(css: '#navigation').find_element(partial_link_text: data[:title])
- if !element
- screenshot(browser: instance, comment: 'open_task_failed')
- raise "no task with title '#{data[:title]}' found"
- end
- # firefix/marionette issue with Selenium::WebDriver::Error::ElementNotInteractableError: could not be scrolled into view
- # use js workaround instead of native click
- instance.execute_script("$('#navigation .tasks .task:contains(\"#{data[:title]}\") .nav-tab-name').click()")
- #element.click
- true
- end
- =begin
- close_task(
- browser: browser1,
- data: {
- title: 'some title',
- },
- discard_changes: true,
- )
- =end
- def close_task(params = {})
- switch_window_focus(params)
- log('close_task', params)
- instance = params[:browser] || @browser
- data = params[:data]
- element = instance.find_element(css: '#navigation').find_element(partial_link_text: data[:title])
- if !element
- screenshot(browser: instance, comment: 'close_task_failed')
- raise "no task with title '#{data[:title]}' found"
- end
- instance.action.move_to(element).release.perform
- sleep 0.1
- instance.execute_script("$('#navigation .tasks .task:contains(\"#{data[:title]}\") .js-close').click()")
- # accept task close warning
- if params[:discard_changes]
- modal_ready(browser: instance)
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- end
- true
- end
- =begin
- file_upload(
- browser: browser1,
- css: '.content.active .attachmentPlaceholder-inputHolder input'
- files: ['path/in/home/some_file.ext'], # 'test/data/pdf/test1.pdf'
- )
- =end
- def file_upload(params = {})
- switch_window_focus(params)
- log('file_upload', params)
- instance = params[:browser] || @browser
- params[:files].each do |file|
- instance.find_elements(css: params[:css])[0].send_keys(Rails.root.join(file))
- end
- return if params[:no_sleep]
- sleep 2 * params[:files].count
- end
- =begin
- watch_for(
- browser: browser1,
- container: element # optional, defaults to browser, must exist at the time of dispatch
- css: '#content .text-1', # xpath or css required
- xpath: '/content[contains(@class,".text-1")]', # xpath or css required
- value: 'some text',
- attribute: 'some_attribute' # optional
- timeout: 16, # in sec, default 16
- )
- =end
- def watch_for(params = {})
- switch_window_focus(params)
- log('watch_for', params)
- browser = params[:browser] || @browser
- instance = params[:container] || browser
- selector = params[:css] || params[:xpath]
- selector_type = if params.key?(:css)
- :css
- elsif params.key?(:xpath)
- :xpath
- end
- timeout = 16
- if params[:timeout]
- timeout = params[:timeout]
- end
- loops = timeout.to_i * 2
- text = ''
- (1..loops).each do
- element = instance.find_elements(selector_type => selector)[0]
- if element #&& element.displayed?
- begin
- # watch for selector
- if !params[:attribute] && !params[:value]
- assert(true, "'#{selector}' found")
- sleep 0.5
- return true
- # match an attribute
- else
- text = if params[:attribute]
- element.attribute(params[:attribute])
- elsif selector.match?(/(input|textarea)/i)
- element.attribute('value')
- else
- element.text
- end
- if text.match?(/#{params[:value]}/i)
- assert(true, "'#{params[:value]}' found in '#{text}'")
- sleep 0.5
- return true
- end
- end
- rescue
- # try again
- end
- end
- sleep 0.5
- end
- screenshot(browser: browser, comment: 'watch_for_failed')
- if !params[:attribute] && !params[:value]
- raise "'#{selector}' not found"
- end
- raise "'#{params[:value]}' not found in '#{text}'"
- end
- =begin
- wait untill selector disabppears
- watch_for_disappear(
- browser: browser1,
- css: '#content .text-1',
- timeout: 16, # in sec, default 16
- )
- wait untill text in selector disabppears
- watch_for_disappear(
- browser: browser1,
- css: '#content .text-1',
- value: 'some value as regexp',
- timeout: 16, # in sec, default 16
- )
- =end
- def watch_for_disappear(params = {})
- switch_window_focus(params)
- log('watch_for_disappear', params)
- instance = params[:browser] || @browser
- timeout = 16
- if params[:timeout]
- timeout = params[:timeout]
- end
- loops = timeout.to_i
- text = ''
- (1..loops).each do
- element = instance.find_elements(css: params[:css])[0]
- if !element #|| element.displayed?
- assert(true, 'not found')
- sleep 1
- return true
- end
- if params[:value]
- begin
- text = instance.find_elements(css: params[:css])[0].text
- if !text.match?(/#{params[:value]}/i)
- assert(true, "not matching '#{params[:value]}' in text '#{text}'")
- sleep 1
- return true
- end
- rescue
- # try again
- end
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'disappear_failed')
- raise "#{params[:css]}) still exsists"
- end
- =begin
- shortcut(
- browser: browser1,
- key: 'x',
- )
- =end
- def shortcut(params = {})
- switch_window_focus(params)
- log('shortcut', params)
- instance = params[:browser] || @browser
- screenshot(browser: instance, comment: 'shortcut_before')
- instance.action.key_down(:control)
- .key_down(:shift)
- .send_keys(params[:key])
- .key_up(:shift)
- .key_up(:control)
- .perform
- screenshot(browser: instance, comment: 'shortcut_after')
- end
- =begin
- window_keys(
- browser: browser1,
- value: 'x',
- )
- =end
- def window_keys(params = {})
- switch_window_focus(params)
- log('window_keys', params)
- instance = params[:browser] || @browser
- instance.action.send_keys(params[:value]).perform
- end
- =begin
- tasks_close_all(
- browser: browser1,
- )
- =end
- def tasks_close_all(params = {})
- switch_window_focus(params)
- log('tasks_close_all', params)
- instance = params[:browser] || @browser
- 99.times do
- #sleep 0.5
- begin
- if instance.find_elements(css: '#navigation .tasks .task:first-child')[0]
- instance.action.move_to(instance.find_elements(css: '#navigation .tasks .task:first-child')[0]).release.perform
- click_element = instance.find_elements(css: '#navigation .tasks .task:first-child .js-close')[0]
- if click_element
- click_element.click
- # accept task close warning
- if instance.find_elements(css: '.modal button.js-submit')[0]
- sleep 0.4
- instance.find_elements(css: '.modal button.js-submit')[0].click
- end
- end
- else
- break
- end
- rescue
- # try again
- end
- end
- assert(true, 'all tasks closed')
- end
- =begin
- close_online_notitifcation(
- browser: browser1,
- data: {
- #title: 'some title',
- position: 3,
- },
- )
- =end
- def close_online_notitifcation(params = {})
- switch_window_focus(params)
- log('close_online_notitifcation', params)
- instance = params[:browser] || @browser
- data = params[:data]
- if data[:title]
- element = instance.find_elements(partial_link_text: data[:title])[0]
- if !element
- screenshot(browser: instance, comment: 'close_online_notitifcation')
- raise "no online notification with title '#{data[:title]}' found"
- end
- instance.action.move_to(element).release.perform
- sleep 0.1
- instance.execute_script("$('.js-notificationsContainer .js-items .js-item .activity-text:contains(\"#{data[:title]}\") .js-remove').first().click()")
- else
- css = ".js-notificationsContainer .js-items .js-item:nth-child(#{data[:position]})"
- element = instance.find_elements(css: css)[0]
- if !element
- screenshot(browser: instance, comment: 'close_online_notitifcation')
- raise "no online notification with postion '#{css}' found"
- end
- instance.action.move_to(element).release.perform
- sleep 0.1
- instance.find_elements(css: "#{css} .js-remove")[0].click
- end
- true
- end
- =begin
- online_notitifcation_close_all(
- browser: browser1,
- )
- =end
- def online_notitifcation_close_all(params = {})
- switch_window_focus(params)
- log('online_notitifcation_close_all', params)
- instance = params[:browser] || @browser
- 99.times do
- sleep 0.5
- begin
- if instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0]
- instance.action.move_to(instance.find_elements(css: '.js-notificationsContainer .js-item:first-child')[0]).perform
- sleep 0.1
- click_element = instance.find_elements(css: '.js-notificationsContainer .js-item:first-child .js-remove')[0]
- click_element&.click
- else
- break
- end
- rescue
- # try again
- end
- end
- assert(true, 'all online notification closed')
- end
- =begin
- empty_search(
- browser: browser1,
- )
- =end
- def empty_search(params = {})
- switch_window_focus(params)
- log('empty_search', params)
- instance = params[:browser] || @browser
- # empty search box by x
- begin
- instance.find_elements(css: '.search .js-emptySearch')[0].click
- rescue
- # in issues with ff & selenium, sometimes exeption appears
- # "Element is not currently visible and so may not be interacted with"
- log('empty_search via js')
- instance.execute_script('$(".search .js-emptySearch").click()')
- end
- sleep 0.5
- text = instance.find_elements(css: '#global-search')[0].attribute('value')
- if !text
- raise '#global-search is not empty!'
- end
- true
- end
- =begin
- ticket_customer_select(
- browser: browser1,
- css: '#content .text-1',
- customer: '',
- )
- =end
- def ticket_customer_select(params = {})
- switch_window_focus(params)
- log('ticket_customer_select', params)
- instance = params[:browser] || @browser
- element = instance.find_elements(css: params[:css] + ' input[name="customer_id_completion"]')[0]
- element.click
- element.clear
- element.send_keys(params[:customer])
- sleep 2.5
- element.send_keys(:enter)
- #instance.find_elements(css: params[:css] + ' .recipientList-entry.js-object.is-active')[0].click
- sleep 0.4
- assert(true, 'ticket_customer_select')
- end
- =begin
- overview_create(
- browser: browser1,
- data: {
- name: name,
- roles: ['Agent'],
- selector: {
- 'Priority': '1 low',
- },
- 'order::direction' => 'down',
- }
- )
- =end
- def overview_create(params)
- switch_window_focus(params)
- log('overview_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/overviews"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[data-type="new"]',
- mute_log: true,
- )
- modal_ready(browser: instance)
- if data[:name]
- set(
- browser: instance,
- css: '.modal input[name=name]',
- value: data[:name],
- mute_log: true,
- )
- end
- if data[:roles]
- 99.times do
- begin
- element = instance.find_elements(css: '.modal .js-selected[data-name=role_ids] .js-option:not(.is-hidden)')[0]
- break if !element
- element.click
- sleep 0.1
- end
- end
- data[:roles].each do |role|
- instance.execute_script("$(\".modal [data-name=role_ids] .js-pool .js-option:not(.is-hidden):contains('#{role}')\").first().click()")
- end
- end
- data[:selector]&.each do |key, value|
- select(
- browser: instance,
- css: '.modal .ticket_selector .js-attributeSelector select',
- value: key,
- mute_log: true,
- )
- sleep 0.5
- if data.key?('text_input')
- set(
- browser: instance,
- css: '.modal .ticket_selector .js-value input',
- value: value,
- mute_log: true,
- )
- else
- select(
- browser: instance,
- css: '.modal .ticket_selector .js-value select',
- value: value,
- deselect_all: true,
- mute_log: true,
- )
- end
- end
- if data['order::direction']
- select(
- browser: instance,
- css: '.modal select[name="order::direction"]',
- value: data['order::direction'],
- mute_log: true,
- )
- end
- if data[:group_by]
- select(
- browser: instance,
- css: '.modal select[name="group_by"]',
- value: data[:group_by],
- mute_log: true,
- )
- end
- if data[:group_direction]
- select(
- browser: instance,
- css: '.modal select[name="group_direction"]',
- value: data[:group_direction],
- mute_log: true,
- )
- end
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 11.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'overview created')
- overview = {
- name: name,
- }
- sleep 1
- return overview
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'overview_create_failed')
- raise 'overview creation failed'
- end
- =begin
- overview_update(
- browser: browser1,
- data: {
- name: name,
- roles: ['Agent'],
- selector: {
- 'Priority': '1 low',
- },
- 'order::direction' => 'down',
- }
- )
- =end
- def overview_update(params)
- switch_window_focus(params)
- log('overview_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/overviews"]',
- mute_log: true,
- )
- instance.execute_script("$(\".content.active td:contains('#{data[:name]}')\").first().click()")
- sleep 2
- if data[:name]
- set(
- browser: instance,
- css: '.modal input[name=name]',
- value: data[:name],
- mute_log: true,
- )
- end
- if data[:roles]
- 99.times do
- begin
- element = instance.find_elements(css: '.modal .js-selected[data-name=role_ids] .js-option:not(.is-hidden)')[0]
- break if !element
- element.click
- sleep 0.1
- end
- end
- data[:roles].each do |role|
- instance.execute_script("$(\".modal [data-name=role_ids] .js-pool .js-option:not(.is-hidden):contains('#{role}')\").first().click()")
- end
- end
- data[:selector]&.each do |key, value|
- select(
- browser: instance,
- css: '.modal .ticket_selector .js-attributeSelector select',
- value: key,
- mute_log: true,
- )
- sleep 0.5
- select(
- browser: instance,
- css: '.modal .ticket_selector .js-value select',
- value: value,
- deselect_all: true,
- mute_log: true,
- )
- end
- if data['order::direction']
- select(
- browser: instance,
- css: '.modal select[name="order::direction"]',
- value: data['order::direction'],
- mute_log: true,
- )
- end
- if data[:group_direction]
- select(
- browser: instance,
- css: '.modal select[name="group_direction"]',
- value: data[:group_direction],
- mute_log: true,
- )
- end
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 11.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'overview updated')
- overview = {
- name: name,
- }
- sleep 1
- return overview
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'overview_update_failed')
- raise 'overview update failed'
- end
- =begin
- ticket = ticket_create(
- browser: browser1,
- data: {
- customer: 'nico',
- group: 'Users', # optional / '-NONE-' # if group selection should not be shown
- priority: '2 normal',
- state: 'open',
- title: 'overview #1',
- body: 'overview #1',
- },
- do_not_submit: true,
- )
- returns (in case of submitted)
- {
- id: 123,
- number: '100001',
- title: 'overview #1',
- }
- ticket = ticket_create(
- browser: browser1,
- data: {
- customer: 'nico',
- group: 'Users', # optional / '-NONE-' # if group selection should not be shown
- priority: '2 normal',
- state: 'open',
- title: 'overview #1',
- body: 'overview #1',
- },
- custom_data_select: {
- key1: 'some value',
- },
- custom_data_input: {
- key1: 'some value',
- },
- custom_data_date: {
- key!: '02/28/2018',
- }
- disable_group_check: true,
- )
- =end
- def ticket_create(params)
- switch_window_focus(params)
- log('ticket_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#new"]',
- mute_log: true,
- only_if_exists: true,
- )
- click(
- browser: instance,
- css: 'a[href="#ticket/create"]',
- mute_log: true,
- )
- found = false
- 7.times do
- element = instance.find_elements(css: '.content.active .newTicket')[0]
- if element
- found = true
- break
- end
- sleep 1
- end
- if !found
- screenshot(browser: instance, comment: 'ticket_create_failed')
- raise 'no ticket create screen found!'
- end
- if data[:group]
- if data[:group] == '-NONE-'
- # check if owner selection exists
- count = instance.find_elements(css: '.content.active .newTicket select[name="group_id"] option').count
- if count.nonzero?
- instance.find_elements(css: '.content.active .newTicket select[name="group_id"] option').each do |element|
- log('ticket_create invalid group count', text: element.text)
- end
- end
- assert_equal(0, count, 'owner selection should not be showm')
- # check count of agents, should be only 3 / - selection + master + agent on init screen
- count = instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').count
- if count != 3
- instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').each do |element|
- log('ticket_create invalid owner count', text: element.text)
- end
- end
- assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
- else
- # check count of agents, should be only 1 selection, the "-" selection on init screen
- if !params[:disable_group_check]
- count = instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').count
- if count != 1
- instance.find_elements(css: '.content.active .newTicket select[name="owner_id"] option').each do |element|
- log('ticket_create invalid owner count', text: element.text)
- end
- end
- assert_equal(1, count, 'check if owner selection is empty per default')
- end
- select(
- browser: instance,
- css: '.content.active .newTicket select[name="group_id"]',
- value: data[:group],
- mute_log: true,
- )
- end
- end
- if data[:priority]
- select(
- browser: instance,
- css: '.content.active .newTicket select[name="priority_id"]',
- value: data[:priority],
- mute_log: true,
- )
- end
- if data[:state]
- select(
- browser: instance,
- css: '.content.active .newTicket select[name="state_id"]',
- value: data[:state],
- mute_log: true,
- )
- end
- if data[:title]
- set(
- browser: instance,
- css: '.content.active .newTicket input[name="title"]',
- value: data[:title],
- clear: true,
- mute_log: true,
- )
- end
- if data[:body]
- set(
- browser: instance,
- css: '.content.active .newTicket div[data-name=body]',
- value: data[:body],
- clear: true,
- mute_log: true,
- )
- end
- if data[:customer]
- element = instance.find_elements(css: '.content.active .newTicket input[name="customer_id_completion"]')[0]
- element.click
- element.clear
- # ff issue, sometimes focus event gets dropped
- # if drowdown is not open, try it again
- if !instance.find_elements(css: '.content.active .newTicket .js-recipientDropdown.open')[0]
- instance.execute_script('$(".active .newTicket .js-recipientDropdown").addClass("open")')
- end
- element.send_keys(data[:customer])
- sleep 2.5
- element.send_keys(:enter)
- sleep 0.2
- # ff issue, sometimes enter event gets dropped
- # take user manually
- if instance.find_elements(css: '.content.active .newTicket .js-recipientDropdown.open')[0]
- instance.find_elements(css: '.content.active .newTicket .recipientList-entry.js-object.is-active')[0].click
- sleep 0.4
- end
- end
- params[:custom_data_select]&.each do |local_key, local_value|
- select(
- browser: instance,
- css: ".content.active .newTicket select[name=\"#{local_key}\"]",
- value: local_value,
- )
- end
- params[:custom_data_input]&.each do |local_key, local_value|
- set(
- browser: instance,
- css: ".content.active .newTicket input[name=\"#{local_key}\"]",
- value: local_value,
- clear: true,
- )
- end
- params[:custom_data_date]&.each do |local_key, local_value|
- set(
- browser: instance,
- css: ".content.active .newTicket div[data-name=\"#{local_key}\"] input[data-item=\"date\"]",
- value: local_value,
- clear: true,
- )
- end
- if data[:attachment]
- file_upload(
- browser: instance,
- css: '.content.active .text-1',
- value: 'some text',
- )
- end
- if params[:do_not_submit]
- assert(true, 'ticket created without submit')
- return
- end
- #instance.execute_script('$(".content.active .newTicket form").submit();')
- click(
- browser: instance,
- css: '.content.active .newTicket button.js-submit',
- mute_log: true,
- )
- sleep 1
- 9.times do
- if instance.current_url.match?(/#{Regexp.quote('#ticket/zoom/')}/)
- assert(true, 'ticket created')
- sleep 2
- id = instance.current_url
- id.gsub!(//,)
- id.gsub!(%r{^.+?/(\d+)$}, '\\1')
- element = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0]
- if element
- number = element.text
- ticket = {
- id: id,
- number: number,
- title: data[:title],
- }
- sleep 2 # wait until notify is gone
- return ticket
- end
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'ticket_create_failed')
- raise "ticket creation failed, can't get zoom url (current url is '#{instance.current_url}')"
- end
- =begin
- ticket_update(
- browser: browser1,
- data: {
- title: '',
- customer: 'some_customer@example.com',
- body: 'some body',
- group: 'some group', # optional
- priority: '1 low',
- state: 'closed',
- },
- do_not_submit: true,
- )
- ticket_update(
- browser: browser1,
- data: {
- title: '',
- customer: 'some_customer@example.com',
- body: 'some body',
- group: 'some group', # optional
- priority: '1 low',
- state: 'closed',
- },
- custom_data_select: {
- key1: 'some value',
- },
- custom_data_input: {
- key1: 'some value',
- },
- custom_data_date: {
- key1: '02/21/2018',
- },
- do_not_submit: true,
- task_type: 'stayOnTab', # default: stayOnTab / possible: closeTab, closeNextInOverview, stayOnTab
- )
- =end
- def ticket_update(params)
- switch_window_focus(params)
- log('ticket_update', params)
- instance = params[:browser] || @browser
- data = params[:data]
- if data[:title]
- #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
- #element.clear
- #sleep 0.5
- #element = instance.find_elements(:css => '.content.active .ticketZoom-header .js-objectTitle')[0]
- #element.send_keys(data[:title])
- #sleep 0.5
- #element.send_keys(:tab)
- instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").focus()')
- instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").text("' + data[:title] + '")')
- instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").blur()')
- instance.execute_script('$(".content.active .ticketZoom-header .js-objectTitle").trigger("blur")')
- # {
- # :where => :instance2,
- # :execute => 'sendkey',
- # :css => '.content.active .ticketZoom-header .js-objectTitle',
- # :value => 'TTT',
- # },
- # {
- # :where => :instance2,
- # :execute => 'sendkey',
- # :css => '.content.active .ticketZoom-header .js-objectTitle',
- # :value => :tab,
- # },
- end
- if data[:customer]
- # select tab
- click(browser: instance, css: '.content.active .tabsSidebar-tab[data-tab="customer"]')
- click(browser: instance, css: '.content.active div[data-tab="customer"] .js-actions .icon-arrow-down')
- click(browser: instance, css: '.content.active div[data-tab="customer"] .js-actions [data-type="customer-change"]')
- watch_for(
- browser: instance,
- css: '.modal',
- value: 'change',
- )
- element = instance.find_elements(css: '.modal input[name="customer_id_completion"]')[0]
- element.click
- element.clear
- element.send_keys(data[:customer])
- sleep 2.5
- element.send_keys(:enter)
- #instance.find_elements(css: '.modal .user_autocompletion .recipientList-entry.js-object.is-active')[0].click
- sleep 0.2
- click(browser: instance, css: '.modal .js-submit')
- modal_disappear(browser: instance)
- watch_for(
- browser: instance,
- css: '.content.active .tabsSidebar',
- value: data[:customer],
- )
- # select tab
- click(browser: instance, css: '.content.active .tabsSidebar-tab[data-tab="ticket"]')
- end
- if data[:body]
- set(
- browser: instance,
- css: '.content.active div[data-name=body]',
- value: data[:body],
- no_click: true,
- mute_log: true,
- )
- # it's not working stable via selenium, use js
- value = instance.find_elements(css: '.content.active div[data-name=body]')[0].text
- if value != data[:body]
- body_quoted = quote(data[:body])
- instance.execute_script("$('.content.active div[data-name=body]').html('#{body_quoted}').trigger('focusout')")
- end
- end
- if data[:group]
- if data[:group] == '-NONE-'
- # check if owner selection exists
- count = instance.find_elements(css: '.content.active .sidebar select[name="group_id"] option').count
- assert_equal(0, count, 'owner selection should not be showm')
- # check count of agents, should be only 3 / - selection + master + agent on init screen
- count = instance.find_elements(css: '.content.active .sidebar select[name="owner_id"] option').count
- assert_equal(3, count, 'check if owner selection is - selection + master + agent per default')
- else
- select(
- browser: instance,
- css: '.content.active .sidebar select[name="group_id"]',
- value: data[:group],
- mute_log: true,
- )
- sleep 0.2
- end
- end
- if data[:priority]
- select(
- browser: instance,
- css: '.content.active .sidebar select[name="priority_id"]',
- value: data[:priority],
- mute_log: true,
- )
- end
- if data[:state]
- select(
- browser: instance,
- css: '.content.active .sidebar select[name="state_id"]',
- value: data[:state],
- mute_log: true,
- )
- end
- if data[:files]
- file_upload(
- css: '.content.active .attachmentPlaceholder-inputHolder input',
- files: data[:files],
- )
- end
- params[:custom_data_select]&.each do |local_key, local_value|
- select(
- browser: instance,
- css: ".active .sidebar select[name=\"#{local_key}\"]",
- value: local_value,
- )
- end
- params[:custom_data_input]&.each do |local_key, local_value|
- set(
- browser: instance,
- css: ".active .sidebar input[name=\"#{local_key}\"]",
- value: local_value,
- clear: true,
- )
- end
- params[:custom_data_date]&.each do |local_key, local_value|
- click(
- browser: instance,
- css: ".active .sidebar div[data-name=\"#{local_key}\"] input[data-item=\"date\"]",
- mute_log: true,
- )
- # weird bug where you cannot "clear" for date/time input
- # this is specific chrome problem, chrome bug report: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1319#c2
- # indirect issue: https://github.com/angular/protractor/issues/562#issuecomment-47745263
- 11.times do
- sendkey(
- value: :backspace,
- )
- end
- set(
- browser: instance,
- css: ".active .sidebar div[data-name=\"#{local_key}\"] input[data-item=\"date\"]",
- value: local_value,
- )
- end
- if data[:state] || data[:group] || data[:body] || params[:custom_data_select].present? || params[:custom_data_input].present?
- found = nil
- 9.times do
- break if found
- begin
- text = instance.find_elements(css: '.content.active .js-reset')[0].text
- if text.match?(/(Discard your unsaved changes.|Verwerfen der)/)
- found = true
- end
- rescue
- # try again
- end
- sleep 1
- end
- if !found
- screenshot(browser: instance, comment: 'ticket_update_discard_message_failed')
- raise 'no discard message found'
- end
- end
- task_type(
- browser: instance,
- type: params[:task_type] || 'stayOnTab',
- )
- if params[:do_not_submit]
- assert(true, 'ticket updated without submit')
- return true
- end
- instance.find_elements(css: '.content.active .js-submit')[0].click
- # do not stay on tab
- if params[:task_type] == 'closeTab' || params[:task_type] == 'closeNextInOverview'
- sleep 1
- return
- end
- 9.times do
- begin
- text = instance.find_elements(css: '.content.active .js-reset')[0].text
- if text.blank?
- sleep 1
- return true
- end
- rescue
- # try again
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'ticket_update_failed')
- raise 'unable to update ticket'
- end
- =begin
- ticket_verify(
- browser: browser1,
- data: {
- title: 'some title',
- body: 'some body',
- ## group: 'some group',
- ## state: 'closed',
- custom_data_select: {
- key1: 'some value',
- },
- custom_data_input: {
- key1: 'some value',
- },
- },
- )
- =end
- def ticket_verify(params)
- switch_window_focus(params)
- log('ticket_verify', params)
- instance = params[:browser] || @browser
- data = params[:data]
- if data[:title]
- title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle').first.text.strip
- if title.match?(/#{data[:title]}/i)
- assert(true, "matching '#{data[:title]}' in title '#{title}'")
- else
- raise "not matching '#{data[:title]}' in title '#{title}'"
- end
- end
- if data[:body]
- body = instance.find_elements(css: '.content.active [data-name="body"]').first.text.strip
- if body.match?(/#{data[:body]}/i)
- assert(true, "matching '#{data[:body]}' in body '#{body}'")
- else
- raise "not matching '#{data[:body]}' in body '#{body}'"
- end
- end
- params[:custom_data_select]&.each do |local_key, local_value|
- element = instance.find_elements(css: ".active .sidebar select[name=\"#{local_key}\"] option[selected]").first
- value = element.text.strip
- if value.match?(/#{local_value}/i)
- assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
- else
- raise "not matching '#{value}' in #{local_key} '#{local_value}'"
- end
- end
- params[:custom_data_input]&.each do |local_key, local_value|
- element = instance.find_elements(css: ".active .sidebar input[name=\"#{local_key}\"]").first
- value = element.text.strip
- if value.match?(/#{local_value}/i)
- assert(true, "matching '#{value}' in #{local_key} '#{local_value}'")
- else
- raise "not matching '#{value}' in #{local_key} '#{local_value}'"
- end
- end
- true
- end
- =begin
- overview_open(
- browser: browser2,
- name: overview_name,
- )
- overview_open(
- browser: browser2,
- link: "#ticket/view/some_special_name",
- )
- =end
- def overview_open(params)
- switch_window_focus(params)
- log('overview_open', params)
- instance = params[:browser] || @browser
- # click on overview task in sidebar
- instance.find_elements(css: '.js-overviewsMenuItem')[0].click
- # show larger overview selection list
- sleep 0.5
- execute(
- browser: instance,
- js: '$(".content.active .sidebar").css("display", "block")',
- )
- link = if params[:link]
- params[:link]
- elsif params[:name]
- "\#ticket/view/#{params[:name]}"
- end
- # switch to overview
- instance.find_elements(css: ".content.active .sidebar a[href=\"#{link}\"]")[0].click
- # hide larger overview selection list again
- sleep 0.5
- execute(
- browser: instance,
- js: '$(".content.active .sidebar").css("display", "none")',
- )
- end
- =begin
- ticket_open_by_overview(
- browser: browser2,
- number: ticket1[:number],
- link: "#ticket/view/#{name}",
- )
- ticket_open_by_overview(
- browser: browser2,
- number: ticket1[:number],
- text: title,
- link: "#ticket/view/#{name}",
- )
- =end
- def ticket_open_by_overview(params)
- switch_window_focus(params)
- log('ticket_open_by_overview', params)
- instance = params[:browser] || @browser
- overview_open(params)
- if params[:title]
- element = instance.find_element(css: '.content.active').find_element(partial_link_text: params[:title])
- if !element
- screenshot(browser: instance, comment: 'ticket_open_by_overview_no_ticket_failed')
- raise "unable to find ticket #{params[:title]} in overview #{params[:link]}!"
- end
- else
- element = instance.find_elements(partial_link_text: params[:number])[0]
- if !element
- screenshot(browser: instance, comment: 'ticket_open_by_overview_no_ticket_failed')
- raise "unable to find ticket #{params[:number]} in overview #{params[:link]}!"
- end
- end
- element.click
- sleep 1
- number = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0].text
- if !number.match?(/#{params[:number]}/)
- screenshot(browser: instance, comment: 'ticket_open_by_overview_open_failed_failed')
- raise "unable to open ticket #{params[:number]}!"
- end
- assert(true, "ticket #{params[:number]} found")
- true
- end
- =begin
- ticket_open_by_search(
- browser: browser2,
- number: ticket1[:number],
- )
- =end
- def ticket_open_by_search(params)
- switch_window_focus(params)
- log('ticket_open_by_search', params)
- instance = params[:browser] || @browser
- # search by number
- element = instance.find_elements(css: '#global-search')[0]
- element.click
- element.clear
- element.send_keys(params[:number])
- sleep 3
- empty_search(browser: instance)
- # search by number again
- element = instance.find_elements(css: '#global-search')[0]
- element.click
- element.clear
- element.send_keys(params[:number])
- sleep 1
- # open ticket
- #instance.find_element(partial_link_text: params[:number] } ).click
- instance.execute_script("$(\".js-global-search-result a:contains('#{params[:number]}') .nav-tab-icon\").first().click()")
- sleep 1
- number = instance.find_elements(css: '.content.active .ticketZoom-header .ticket-number')[0].text
- if !number.match?(/#{params[:number]}/)
- screenshot(browser: instance, comment: 'ticket_open_by_search_failed')
- raise "unable to search/find ticket #{params[:number]}!"
- end
- true
- end
- =begin
- ticket_open_by_title(
- browser: browser2,
- title: ticket1[:title],
- )
- =end
- def ticket_open_by_title(params)
- switch_window_focus(params)
- log('ticket_open_by_title', params)
- instance = params[:browser] || @browser
- # search by number
- element = instance.find_elements(css: '#global-search')[0]
- element.click
- element.clear
- element.send_keys(params[:title])
- sleep 3
- # open ticket
- #instance.find_element(partial_link_text: params[:title] } ).click
- instance.execute_script("$(\".js-global-search-result a:contains('#{params[:title]}') .nav-tab-icon\").click()")
- sleep 1
- title = instance.find_elements(css: '.content.active .ticketZoom-header .js-objectTitle')[0].text
- if !title.match?(/#{params[:title]}/)
- screenshot(browser: instance, comment: 'ticket_open_by_title_failed')
- raise "unable to search/find ticket #{params[:title]}!"
- end
- true
- end
- =begin
- overview_count = overview_counter(
- browser: browser2,
- )
- returns
- {
- '#ticket/view/all_unassigned' => 42,
- }
- =end
- def overview_counter(params = {})
- switch_window_focus(params)
- log('overview_counter', params)
- instance = params[:browser] || @browser
- instance.find_elements(css: '.js-overviewsMenuItem')[0].click
- sleep 2
- execute(
- browser: instance,
- js: '$(".content.active .sidebar").css("display", "block")',
- )
- #execute(
- # browser: instance,
- # js: '$(".content.active .overview-header").css("display", "none")',
- #)
- overviews = {}
- instance.find_elements(css: '.content.active .sidebar a[href]').each do |element|
- url = element.attribute('href')
- url.gsub!(%r{(http|https)://.+?/(.+?)$}, '\\2')
- overviews[url] = 0
- #puts url.inspect
- #puts element.inspect
- end
- overviews.each_key do |url|
- count = instance.find_elements(css: ".content.active .sidebar a[href=\"#{url}\"] .badge")[0].text
- overviews[url] = count.to_i
- end
- log('overview_counter', overviews)
- overviews
- end
- =begin
- organization_open_by_search(
- browser: browser2,
- value: 'some value',
- )
- =end
- def organization_open_by_search(params = {})
- switch_window_focus(params)
- log('organization_open_by_search', params)
- instance = params[:browser] || @browser
- element = instance.find_elements(css: '#global-search')[0]
- element.click
- element.clear
- element.send_keys(params[:value])
- sleep 3
- empty_search(browser: instance)
- element = instance.find_elements(css: '#global-search')[0]
- element.click
- element.clear
- element.send_keys(params[:value])
- sleep 2
- #instance.find_element(partial_link_text: params[:value] } ).click
- instance.execute_script("$(\".js-global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
- sleep 1
- name = instance.find_elements(css: '.content.active h1')[0].text
- if !name.match?(/#{params[:value]}/)
- screenshot(browser: instance, comment: 'organization_open_by_search_failed')
- raise "unable to search/find org #{params[:value]}!"
- end
- assert(true, "org #{params[:value]} found")
- true
- end
- =begin
- user_open_by_search(
- browser: browser2,
- value: 'some value',
- )
- =end
- def user_open_by_search(params = {})
- switch_window_focus(params)
- log('user_open_by_search', params)
- instance = params[:browser] || @browser
- element = instance.find_elements(css: '#global-search')[0]
- element.click
- element.clear
- element.send_keys(params[:value])
- sleep 3
- #instance.find_element(partial_link_text: params[:value]).click
- instance.execute_script("$(\".js-global-search-result a:contains('#{params[:value]}') .nav-tab-icon\").click()")
- sleep 1
- name = instance.find_elements(css: '.content.active h1')[0].text
- if !name.match?(/#{params[:value]}/)
- screenshot(browser: instance, comment: 'user_open_by_search_failed')
- raise "unable to search/find user #{params[:value]}!"
- end
- assert(true, "user #{params[:term]} found")
- true
- end
- =begin
- user_create(
- browser: browser2,
- data: {
- #login: 'some login' + random,
- firstname: 'Manage Firstname' + random,
- lastname: 'Manage Lastname' + random,
- email: user_email,
- password: 'some-pass',
- role: 'Admin', # optional, choose among [Admin, Agent, Customer]
- # defaults to Customer if not provided
- },
- )
- user_create(
- browser: browser2,
- data: {
- #login: 'some login' + random,
- firstname: 'Manage Firstname' + random,
- lastname: 'Manage Lastname' + random,
- email: user_email,
- password: 'some-pass',
- role: 'Agent', # when the role is Agent an array of permissions for each group is optionally accepted
- permissions: { 1 => %w[read create overview],
- 2 => ['full'], }
- },
- )
- =end
- def user_create(params = {})
- switch_window_focus(params)
- log('user_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/users"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[data-type="new"]',
- mute_log: true,
- )
- modal_ready(browser: instance)
- element = instance.find_elements(css: '.modal input[name=firstname]')[0]
- element.clear
- element.send_keys(data[:firstname])
- element = instance.find_elements(css: '.modal input[name=lastname]')[0]
- element.clear
- element.send_keys(data[:lastname])
- element = instance.find_elements(css: '.modal input[name=email]')[0]
- element.clear
- element.send_keys(data[:email])
- element = instance.find_elements(css: '.modal input[name=password]')[0]
- element.clear
- element.send_keys(data[:password])
- element = instance.find_elements(css: '.modal input[name=password_confirm]')[0]
- element.clear
- element.send_keys(data[:password])
- element = instance.find_elements(css: '.modal input[name=phone]')[0]
- element.clear
- element.send_keys(data[:phone])
- if data[:active] == false
- select(css: 'select[name="active"]', value: 'inactive')
- end
- if data[:organization]
- element = instance.find_elements(css: '.modal input.searchableSelect-main')[0]
- element.clear
- element.send_keys(data[:organization])
- begin
- retries ||= 0
- target = nil
- until target
- sleep 0.5
- target = instance.find_elements(css: ".modal li[title='#{data[:organization]}']")[0]
- end
- target.click()
- rescue Selenium::WebDriver::Error::StaleElementReferenceError
- sleep retries
- retries += 1
- retry if retries < 3
- end
- end
- if data[:role]
- if data[:role] == 'Admin'
- check(
- browser: instance,
- css: '.modal input[name=role_ids][value=1]',
- )
- elsif data[:role] == 'Customer'
- check(
- browser: instance,
- css: '.modal input[name=role_ids][value=3]',
- )
- elsif data[:role] == 'Agent'
- check(
- browser: instance,
- css: '.modal input[name=role_ids][value=2]',
- )
- data[:permissions].each do |key, value|
- value.each do |permission|
- check(
- browser: instance,
- css: ".modal input[name=\"group_ids::#{key}\"][value=\"#{permission}\"]",
- )
- end
- end
- else
- raise "Unknown :role \"#{data[:role]}\" in user_create()"
- end
- else
- check(
- browser: instance,
- css: '.modal input[name=role_ids][value=3]',
- )
- end
- click(
- browser: instance,
- css: '.modal .js-submit',
- )
- modal_disappear(
- browser: instance,
- timeout: 10,
- )
- set(
- browser: instance,
- css: '.content .js-search',
- value: data[:email],
- )
- watch_for(
- browser: instance,
- css: 'body',
- value: data[:lastname],
- )
- assert(true, 'user created')
- end
- =begin
- user_edit(
- browser: browser2,
- data: {
- login: 'some login' + random,
- firstname: 'Manage Firstname' + random,
- lastname: 'Manage Lastname' + random,
- email: user_email,
- password: 'some-pass',
- role: 'Agent', # when the role is Agent an array of permissions for each group is optionally accepted
- permissions: { 1 => %w[read create overview],
- 2 => ['full'], }
- },
- )
- =end
- def user_edit(params = {})
- switch_window_focus(params)
- log('user_edit', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/users"]',
- mute_log: true,
- )
- instance.find_elements(css: '.content.active .user-list td:first-child').each do |element|
- next if element.text.strip != data[:login]
- element.click
- break
- end
- modal_ready(browser: instance)
- if data[:firstname]
- element = instance.find_elements(css: '.modal input[name=firstname]')[0]
- element.clear
- element.send_keys(data[:firstname])
- end
- if data[:lastname]
- element = instance.find_elements(css: '.modal input[name=lastname]')[0]
- element.clear
- element.send_keys(data[:lastname])
- end
- if data[:email]
- element = instance.find_elements(css: '.modal input[name=email]')[0]
- element.clear
- element.send_keys(data[:email])
- end
- if data[:password]
- element = instance.find_elements(css: '.modal input[name=password]')[0]
- element.clear
- element.send_keys(data[:password])
- element = instance.find_elements(css: '.modal input[name=password_confirm]')[0]
- element.clear
- element.send_keys(data[:password])
- end
- if data[:phone]
- element = instance.find_elements(css: '.modal input[name=phone]')[0]
- element.clear
- element.send_keys(data[:phone])
- end
- if data[:active].present?
- select(css: 'select[name="active"]', value: data[:active] ? 'active' : 'inactive' )
- end
- if data[:organization]
- element = instance.find_elements(css: '.modal input.searchableSelect-main')[0]
- element.clear
- element.send_keys(data[:organization])
- begin
- retries ||= 0
- target = nil
- until target
- sleep 0.5
- target = instance.find_elements(css: ".modal li[title='#{data[:organization]}']")[0]
- end
- target.click()
- rescue Selenium::WebDriver::Error::StaleElementReferenceError
- sleep retries
- retries += 1
- retry if retries < 3
- end
- end
- if data[:role]
- if data[:role] == 'Admin'
- check(
- browser: instance,
- css: '.modal input[name=role_ids][value=1]',
- )
- elsif data[:role] == 'Customer'
- check(
- browser: instance,
- css: '.modal input[name=role_ids][value=3]',
- )
- elsif data[:role] == 'Agent'
- check(
- browser: instance,
- css: '.modal input[name=role_ids][value=2]',
- )
- else
- raise "Unknown :role \"#{data[:role]}\" in user_create()"
- end
- end
- if data[:permissions].present?
- data[:permissions].each do |key, value|
- value.each do |permission|
- check(
- browser: instance,
- css: ".modal input[name=\"group_ids::#{key}\"][value=\"#{permission}\"]",
- )
- end
- end
- end
- click(
- browser: instance,
- css: '.modal .js-submit',
- )
- modal_disappear(
- browser: instance,
- timeout: 10,
- )
- assert(true, 'user updated')
- end
- =begin
- organization_create(
- browser: browser2,
- data: {
- name: 'Test Organization',
- }
- )
- =end
- def organization_create(params = {})
- switch_window_focus(params)
- log('organization_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/organizations"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[data-type="new"]',
- mute_log: true,
- )
- modal_ready(browser: instance)
- element = instance.find_elements(css: '.modal input[name=name]')[0]
- element.clear
- element.send_keys(data[:name])
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(
- browser: instance,
- timeout: 5,
- )
- watch_for(
- browser: instance,
- css: 'body',
- value: data[:name],
- )
- end
- =begin
- calendar_create(
- browser: browser2,
- data: {
- name: 'some calendar' + random,
- first_response_time_in_text: 61
- },
- )
- =end
- def calendar_create(params = {})
- switch_window_focus(params)
- log('calendar_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/calendars"]',
- mute_log: true,
- )
- sleep 4
- click(
- browser: instance,
- css: '.content.active a.js-new',
- mute_log: true,
- )
- modal_ready(browser: instance)
- element = instance.find_elements(css: '.content.active .modal input[name=name]')[0]
- element.clear
- element.send_keys(data[:name])
- element = instance.find_elements(css: '.content.active .modal .js-input')[0]
- element.clear
- element.send_keys(data[:timezone])
- element.send_keys(:enter)
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 7.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'calendar created')
- sleep 1
- return true
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'calendar_create_failed')
- raise 'calendar creation failed'
- end
- =begin
- sla_create(
- browser: browser2,
- data: {
- name: 'some sla' + random,
- calendar: 'some calendar name',
- first_response_time_in_text: 61
- },
- )
- =end
- def sla_create(params = {})
- switch_window_focus(params)
- log('sla_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/slas"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a.js-new',
- mute_log: true,
- )
- modal_ready(browser: instance)
- element = instance.find_elements(css: '.modal input[name=name]')[0]
- element.clear
- element.send_keys(data[:name])
- if data[:calendar].present?
- element = instance.find_elements(css: '.modal select[name="calendar_id"]')[0]
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- dropdown.select_by(:text, data[:calendar])
- end
- element = instance.find_elements(css: '.modal input[name=first_response_time_in_text]')[0]
- element.clear
- element.send_keys(data[:first_response_time_in_text])
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 7.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'sla created')
- sleep 1
- return true
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'sla_create_failed')
- raise 'sla creation failed'
- end
- =begin
- text_module_create(
- browser: browser2,
- data: {
- name: 'some sla' + random,
- keywords: 'some keywords',
- content: 'some content',
- },
- )
- =end
- def text_module_create(params = {})
- switch_window_focus(params)
- log('text_module_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/text_modules"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[data-type="new"]',
- mute_log: true,
- )
- modal_ready(browser: instance)
- set(
- browser: instance,
- css: '.modal input[name=name]',
- value: data[:name],
- )
- set(
- browser: instance,
- css: '.modal input[name=keywords]',
- value: data[:keywords],
- )
- set(
- browser: instance,
- css: '.modal [data-name=content]',
- value: data[:content],
- )
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 7.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'text module created')
- sleep 1
- return true
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'text_module_create_failed')
- raise 'text module creation failed'
- end
- =begin
- signature_create(
- browser: browser2,
- data: {
- name: 'some sla' + random,
- body: 'some body',
- },
- )
- =end
- def signature_create(params = {})
- switch_window_focus(params)
- log('signature_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#channels/email"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#c-signature"]',
- mute_log: true,
- )
- sleep 4
- click(
- browser: instance,
- css: '.content.active #c-signature a[data-type="new"]',
- mute_log: true,
- )
- modal_ready(browser: instance)
- set(
- browser: instance,
- css: '.modal input[name=name]',
- value: data[:name],
- )
- set(
- browser: instance,
- css: '.modal [data-name=body]',
- value: data[:body],
- )
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 11.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'signature created')
- sleep 1
- return true
- end
- sleep 1
- end
- screenshot(browser: instance, comment: 'signature_create_failed')
- raise 'signature creation failed'
- end
- =begin
- group_create(
- browser: browser2,
- data: {
- name: 'some sla' + random,
- signature: 'some signature bame',
- member: [
- {
- login: 'some_user_login',
- access: 'all',
- },
- ],
- },
- )
- =end
- def group_create(params = {})
- switch_window_focus(params)
- log('group_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/groups"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[data-type="new"]',
- mute_log: true,
- )
- modal_ready(browser: instance)
- element = instance.find_elements(css: '.modal input[name=name]')[0]
- element.clear
- element.send_keys(data[:name])
- element = instance.find_elements(css: '.modal select[name="email_address_id"]')[0]
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- dropdown.select_by(:index, 1)
- #dropdown.select_by(:text, action[:group])
- if data[:signature]
- element = instance.find_elements(css: '.modal select[name="signature_id"]')[0]
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- dropdown.select_by(:text, data[:signature])
- end
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 11.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'group created')
- modal_disappear(browser: instance) # wait until modal has gone
- # add member
- data[:member]&.each do |member|
- instance.find_elements(css: 'a[href="#manage"]')[0].click
- sleep 1
- instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
- sleep 3
- element = instance.find_elements(css: '.content.active [name="search"]')[0]
- element.clear
- element.send_keys(member[:login])
- sleep 3
- #instance.find_elements(:css => '.content.active table [data-id]')[0].click
- instance.execute_script('$(".content.active table [data-id] td").first().click()')
- modal_ready(browser: instance)
- #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
- instance.execute_script('$(".js-groupList tr:contains(\"' + data[:name] + '\") .js-groupListItem[value=' + member[:access] + ']").prop("checked", true)')
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- end
- end
- sleep 1
- return true
- end
- screenshot(browser: instance, comment: 'group_create_failed')
- raise 'group creation failed'
- end
- =begin
- macro_create(
- browser: browser1,
- name: 'Emmanuel Macro',
- ux_flow_next_up: 'Stay on tab', # possible: 'Stay on tab', 'Close tab', 'Advance to next ticket from overview'
- actions: {
- 'Tags' => { # currently only 'Tags' is supported
- operator: 'add',
- value: 'spam',
- }
- }
- )
- =end
- def macro_create(params)
- switch_window_focus(params)
- log('macro_create', params)
- instance = params[:browser] || @browser
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.sidebar a[href="#manage/macros"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.page-header-meta > a[data-type="new"]'
- )
- sendkey(
- browser: instance,
- css: '.modal-body input[name="name"]',
- value: params[:name]
- )
- params[:actions]&.each do |attribute, changes|
- select(
- browser: instance,
- css: '.modal .ticket_perform_action .js-filterElement .js-attributeSelector select',
- value: attribute,
- mute_log: true,
- )
- next if attribute != 'Tags'
- select(
- browser: instance,
- css: '.modal .ticket_perform_action .js-filterElement .js-operator select',
- value: changes[:operator],
- mute_log: true,
- )
- sendkey(
- browser: instance,
- css: '.modal .ticket_perform_action .js-filterElement .js-value .token-input',
- value: changes[:value],
- mute_log: true,
- )
- sendkey(
- browser: instance,
- value: :enter,
- )
- end
- select(
- browser: instance,
- css: '.modal-body select[name="ux_flow_next_up"]',
- value: params[:ux_flow_next_up]
- )
- click(
- browser: instance,
- css: '.modal-footer button[type="submit"]'
- )
- watch_for(
- browser: instance,
- css: 'body',
- value: params[:name],
- )
- assert(true, 'macro created')
- end
- =begin
- role_create(
- browser: browser2,
- data: {
- name: 'some role' + random,
- default_at_signup: false,
- permission: {
- 'admin.group' => true,
- 'preferences.password' => true,
- },
- member: [
- 'some_user_login',
- ],
- },
- )
- =end
- def role_create(params = {})
- switch_window_focus(params)
- log('role_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/roles"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[data-type="new"]',
- mute_log: true,
- )
- modal_ready(browser: instance)
- element = instance.find_elements(css: '.modal input[name=name]')[0]
- element.clear
- element.send_keys(data[:name])
- if data.key?(:default_at_signup)
- element = instance.find_elements(css: '.modal select[name="default_at_signup"]')[0]
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- if data[:default_at_signup] == true
- dropdown.select_by(:text, 'yes')
- else
- dropdown.select_by(:text, 'no')
- end
- end
- if data.key?(:permission)
- data[:permission].each do |permission_name, permission_value|
- if permission_value == false
- uncheck(
- browser: instance,
- css: ".modal [data-permission-name=\"#{permission_name}\"]",
- )
- else
- check(
- browser: instance,
- css: ".modal [data-permission-name=\"#{permission_name}\"]",
- )
- end
- end
- end
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 11.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'role created')
- modal_disappear(browser: instance) # wait until modal has gone
- # add member
- data[:member]&.each do |login|
- instance.find_elements(css: 'a[href="#manage"]')[0].click
- sleep 1
- instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
- sleep 3
- element = instance.find_elements(css: '.content.active [name="search"]')[0]
- element.clear
- element.send_keys(login)
- sleep 3
- #instance.find_elements(:css => '.content.active table [data-id]')[0].click
- instance.execute_script('$(".content.active table [data-id] td").first().click()')
- sleep 3
- #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
- instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- end
- end
- sleep 1
- return true
- end
- screenshot(browser: instance, comment: 'role_create_failed')
- raise 'role creation failed'
- end
- =begin
- role_create(
- browser: browser2,
- data: {
- name: 'some role' + random,
- default_at_signup: false,
- permission: {
- 'admin.group' => true,
- 'preferences.password' => true,
- },
- member: [
- 'some_user_login',
- ],
- },
- )
- =end
- def role_edit(params = {})
- switch_window_focus(params)
- log('role_edit', params)
- instance = params[:browser] || @browser
- data = params[:data]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#manage/roles"]',
- mute_log: true,
- )
- instance.execute_script('$(\'.content.active table tr td:contains(" ' + data[:name] + '")\').first().click()')
- modal_ready(browser: instance)
- element = instance.find_elements(css: '.modal input[name=name]')[0]
- element.clear
- element.send_keys(data[:name])
- if data.key?(:default_at_signup)
- element = instance.find_elements(css: '.modal select[name="default_at_signup"]')[0]
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- if data[:default_at_signup] == true
- dropdown.select_by(:text, 'yes')
- else
- dropdown.select_by(:text, 'no')
- end
- end
- if data.key?(:permission)
- data[:permission].each do |permission_name, permission_value|
- if permission_value == false
- uncheck(
- browser: instance,
- css: ".modal [data-permission-name=\"#{permission_name}\"]",
- )
- else
- check(
- browser: instance,
- css: ".modal [data-permission-name=\"#{permission_name}\"]",
- )
- end
- end
- end
- if data.key?(:group_permissions)
- data[:group_permissions].each do |key, value|
- value.each do |permission|
- check(
- browser: instance,
- css: ".modal input[name=\"group_ids::#{key}\"][value=\"#{permission}\"]",
- )
- end
- end
- end
- if data.key?(:active)
- element = instance.find_elements(css: '.modal select[name="active"]')[0]
- dropdown = Selenium::WebDriver::Support::Select.new(element)
- if data[:active] == true
- dropdown.select_by(:text, 'active')
- else
- dropdown.select_by(:text, 'inactive')
- end
- end
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- 11.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'role created')
- modal_disappear(browser: instance) # wait until modal has gone
- # add member
- data[:member]&.each do |login|
- instance.find_elements(css: 'a[href="#manage"]')[0].click
- sleep 1
- instance.find_elements(css: '.content.active a[href="#manage/users"]')[0].click
- sleep 3
- element = instance.find_elements(css: '.content.active [name="search"]')[0]
- element.clear
- element.send_keys(login)
- sleep 3
- #instance.find_elements(:css => '.content.active table [data-id]')[0].click
- instance.execute_script('$(".content.active table [data-id] td").first().click()')
- sleep 3
- #instance.find_elements(:css => 'label:contains(" ' + action[:name] + '")')[0].click
- instance.execute_script('$(\'label:contains(" ' + data[:name] + '")\').first().click()')
- instance.find_elements(css: '.modal button.js-submit')[0].click
- modal_disappear(browser: instance)
- end
- end
- sleep 1
- return true
- end
- screenshot(browser: instance, comment: 'role_edit_failed')
- raise 'role edit failed'
- end
- =begin
- object_manager_attribute_create(
- browser: browser2,
- data: {
- object: 'Ticket', # optional, defaults to Ticket
- name: 'field_name' + random,
- display: 'Display Name of Field',
- data_type: 'Select',
- data_option: {
- options: {
- 'aa' => 'AA',
- 'bb' => 'BB',
- },
- default: 'abc',
- },
- },
- error: 'already exists'
- )
- object_manager_attribute_create(
- browser: browser2,
- data: {
- object: 'Ticket', # optional, defaults to Ticket
- name: 'field_name' + random,
- display: 'Display Name of Field',
- data_type: 'Text',
- data_option: {
- default: 'abc',
- maxlength: 20,
- },
- },
- error: 'already exists'
- )
- object_manager_attribute_create(
- browser: browser2,
- data: {
- object: 'Ticket', # optional, defaults to Ticket
- name: 'field_name' + random,
- display: 'Display Name of Field',
- data_type: 'Integer',
- data_option: {
- default: '15',
- min: 1,
- max: 999999,
- },
- },
- error: 'already exists'
- )
- object_manager_attribute_create(
- browser: browser2,
- data: {
- object: 'Ticket', # optional, defaults to Ticket
- name: 'field_name' + random,
- display: 'Display Name of Field',
- data_type: 'Datetime',
- data_option: {
- future: true,
- past: true,
- diff: 24,
- },
- },
- error: 'already exists'
- )
- object_manager_attribute_create(
- browser: browser2,
- data: {
- object: 'Ticket', # optional, defaults to Ticket
- name: 'field_name' + random,
- display: 'Display Name of Field',
- data_type: 'Date',
- data_option: {
- future: true,
- past: true,
- diff: 24,
- },
- },
- error: 'already exists'
- )
- object_manager_attribute_create(
- browser: browser2,
- data: {
- object: 'Ticket', # optional, defaults to Ticket
- name: 'field_name' + random,
- display: 'Display Name of Field',
- data_type: 'Boolean',
- data_option: {
- options: {
- true: 'YES',
- false: 'NO',
- }
- default: undefined,
- },
- },
- error: 'already exists'
- )
- =end
- def object_manager_attribute_create(params = {})
- switch_window_focus(params)
- log('object_manager_attribute_create', params)
- instance = params[:browser] || @browser
- data = params[:data]
- data[:object] = data[:object] || 'Ticket'
- raise 'invalid object parameter in object_manager_attribute_create' if %w[Ticket User Organization Group].exclude? data[:object]
- # make sure that required params are supplied
- %i[name display data_type].each do |s|
- next if data.key? s
- raise "missing required param #{s} in object_manager_attribute_create()"
- end
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#system/object_manager"]',
- mute_log: true,
- )
- watch_for(
- browser: instance,
- css: '.content.active .js-new',
- )
- click(
- browser: instance,
- css: ".content.active a[href='#c-#{data[:object]}']",
- mute_log: true,
- )
- click(
- browser: instance,
- css: ".content.active #c-#{data[:object]} .js-new",
- mute_log: true,
- )
- object_manager_attribute_perform('create', params)
- end
- =begin
- object_manager_attribute_update(
- browser: browser2,
- data: {
- object: 'Ticket', # optional, defaults to Ticket
- name: 'field_name' + random,
- display: 'Display Name of Field',
- data_type: 'Select',
- data_option: {
- options: {
- 'aa' => 'AA',
- 'bb' => 'BB',
- },
- default: 'abc',
- },
- },
- error: 'already exists'
- )
- =end
- def object_manager_attribute_update(params = {})
- switch_window_focus(params)
- log('object_manager_attribute_update', params)
- instance = params[:browser] || @browser
- data = params[:data]
- data[:object] = data[:object] || 'Ticket'
- raise 'invalid object parameter in object_manager_attribute_update' if %w[Ticket User Organization Group].exclude? data[:object]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#system/object_manager"]',
- mute_log: true,
- )
- watch_for(
- browser: instance,
- css: '.content.active .js-new',
- )
- click(
- browser: instance,
- css: ".content.active a[href='#c-#{data[:object]}']",
- mute_log: true,
- )
- instance.execute_script("$(\".content.active #c-#{data[:object]} td:contains('#{data[:name]}')\").first().click()")
- object_manager_attribute_perform('update', params)
- end
- =begin
- object_manager_attribute_delete(
- browser: browser2,
- data: {
- object: 'Ticket', # optional, defaults to Ticket
- name: 'field_name' + random,
- },
- )
- =end
- def object_manager_attribute_delete(params = {})
- switch_window_focus(params)
- log('object_manager_attribute_delete', params)
- instance = params[:browser] || @browser
- data = params[:data]
- data[:object] = data[:object] || 'Ticket'
- raise 'invalid object parameter in object_manager_attribute_delete' if %w[Ticket User Organization Group].exclude? data[:object]
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#system/object_manager"]',
- mute_log: true,
- )
- watch_for(
- browser: instance,
- css: '.content.active .js-new',
- )
- click(
- browser: instance,
- css: ".content.active a[href='#c-#{data[:object]}']",
- mute_log: true,
- )
- sleep 4
- r = instance.execute_script("$(\".content.active #c-#{data[:object]} td:contains('#{data[:name]}')\").first().closest('tr').find('.js-delete').click()")
- end
- =begin
- object_manager_attribute_discard_changes(
- browser: browser2,
- )
- =end
- def object_manager_attribute_discard_changes(params = {})
- switch_window_focus(params)
- log('object_manager_attribute_discard_changes', params)
- instance = params[:browser] || @browser
- click(
- browser: instance,
- css: 'a[href="#manage"]',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active a[href="#system/object_manager"]',
- mute_log: true,
- )
- sleep 4
- element = instance.find_elements(css: '.content.active .js-discard').first
- element.click
- watch_for_disappear(
- browser: instance,
- css: '.content.active .js-discard',
- )
- end
- =begin
- Execute any pending migrations in the object attribute manager
- object_manager_attribute_migrate(
- browser: browser2,
- )
- =end
- def object_manager_attribute_migrate(params = {})
- switch_window_focus(params)
- log('object_manager_attribute_migrate', params)
- instance = params[:browser] || @browser
- watch_for(
- browser: instance,
- css: '.content.active',
- value: 'Database Update required',
- mute_log: true,
- )
- click(
- browser: instance,
- css: '.content.active .tab-pane.active div.js-execute',
- mute_log: true,
- )
- modal_ready(
- browser: instance,
- )
- title_text = instance.find_elements(css: '.modal .modal-title').first.text
- if title_text == 'Zammad is restarting...'
- # in the complex case, wait for server to restart
- modal_disappear(
- browser: instance,
- timeout: 7.minutes,
- )
- elsif title_text == 'Config has changed'
- # in the simple case, just click the submit button
- click(
- browser: instance,
- css: '.modal .js-submit',
- mute_log: true,
- )
- else
- raise "Unknown title text \"#{title_text}\" found when trying to update database"
- end
- sleep 5
- watch_for(
- browser: instance,
- css: '.content.active',
- mute_log: true,
- )
- end
- =begin
- tags_verify(
- browser: browser2,
- tags: {
- 'tag 1' => true,
- 'tag 2' => true,
- 'tag 3' => false,
- },
- )
- =end
- def tags_verify(params = {})
- switch_window_focus(params)
- log('tags_verify', params)
- instance = params[:browser] || @browser
- tags = instance.find_elements({ css: '.content.active .js-tag' })
- assert(tags)
- assert(tags[0])
- tags_found = {}
- params[:tags].each_key do |key|
- tags_found[key] = false
- end
- tags.each do |element|
- text = element.text
- if tags_found.key?(text)
- tags_found[text] = true
- else
- assert(false, "tag exists but is not in check to verify '#{text}'")
- end
- end
- params[:tags].each do |key, value|
- assert_equal(value, tags_found[key], "tag '#{key}'")
- end
- end
- def quote(string)
- string_quoted = string
- string_quoted.gsub!(/&/, '&')
- string_quoted.gsub!(/</, '<')
- string_quoted.gsub!(/>/, '>')
- string_quoted
- end
- def switch_window_focus(params)
- instance = params[:browser] || @browser
- if instance != @last_used_browser
- log('switch browser window focus', {})
- instance.switch_to.window(instance.window_handles.first)
- end
- @last_used_browser = instance
- end
- def log(method, params = {})
- begin
- instance = params[:browser] || @browser
- if instance
- logs = instance.manage.logs.get(:browser)
- logs.each do |log|
- next if log.level == 'WARNING' && log.message =~ /Declaration\sdropped./ # ignore ff css warnings
- time = Time.zone.parse(Time.zone.at(log.timestamp / 1000).to_datetime.to_s)
- puts "#{time}/#{log.level}: #{log.message}"
- end
- end
- rescue
- # failed to get logs
- end
- return if !DEBUG
- return if params[:mute_log]
- puts "#{Time.zone.now}/#{method}: #{params.inspect}"
- end
- private
- def add_tree_options(instance:, options:)
- # first level entries have to get added in regular order
- options.each_key.with_index do |option, index|
- if index != 0
- element = instance.find_elements(css: '.modal .js-treeTable .js-addRow')[index - 1]
- element.click
- end
- element = instance.find_elements(css: '.modal .js-treeTable .js-key')[index]
- element.clear
- element.send_keys(option)
- end
- add_sub_tree_recursion(
- instance: instance,
- options: options,
- )
- end
- def add_sub_tree_recursion(instance:, options:, offset: 0)
- options.each_value.inject(offset) do |child_offset, children|
- child_offset += 1
- # put your recursion glasses on 8-)
- add_sub_tree_options(
- instance: instance,
- options: children,
- offset: child_offset,
- )
- end
- end
- def add_sub_tree_options(instance:, options:, offset:)
- # sub level entries have to get added in reversed order
- level_options = options.to_a.reverse.to_h.keys
- level_options.each do |option|
- # sub level entries have to get added via 'add child row' link
- click_index = offset - 1
- element = instance.find_elements(css: '.modal .js-treeTable .js-addChild')[click_index]
- element.click
- element = instance.find_elements(css: '.modal .js-treeTable .js-key')[offset]
- element.clear
- element.send_keys(option)
- sleep 0.25
- end
- add_sub_tree_recursion(
- instance: instance,
- options: options,
- offset: offset,
- )
- end
- def token_verify(css, value)
- original_element = @browser.find_element(:css, css)
- elem = original_element.find_element(xpath: '../input[contains(@class, "token-input")]')
- elem.send_keys value
- elem.send_keys :enter
- watch_for(
- xpath: '../*/span[contains(@class,"token-label")]',
- value: value,
- container: original_element
- )
- end
- def toggle_checkbox(scope, value)
- checkbox = scope.find_element(css: "input[value=#{value}]")
- @browser
- .action
- .move_to(checkbox)
- .click
- .perform
- end
- def checkbox_is_selected(scope, value)
- scope.find_element(css: "input[value=#{value}]").property('checked')
- end
- =begin
- Retrieve a hash of all the avaiable Zammad settings and their current values.
- settings = fetch_settings()
- =end
- def fetch_settings
- url = URI.parse(browser_url)
- req = Net::HTTP::Get.new(browser_url + '/api/v1/settings/')
- req.basic_auth('master@example.com', 'test')
- res = Net::HTTP.start(url.host, url.port) do |http|
- http.request(req)
- end
- raise "HTTP error #{res.code} while fetching #{browser_url}/api/v1/settings/" if res.code != '200'
- JSON.parse(res.body)
- end
- =begin
- Enable or disable Zammad experiemental features remotely.
- set_setting('ui_ticket_zoom_attachments_preview', true)
- =end
- def set_setting(name, value)
- name_to_id = fetch_settings.map { |s| [s['name'], s['id']] }.to_h
- id = name_to_id[name]
- url = URI.parse(browser_url)
- req = Net::HTTP::Put.new("#{browser_url}/api/v1/settings/#{id}")
- req['Content-Type'] = 'application/json'
- req.basic_auth('master@example.com', 'test')
- req.body = { 'state_current' => { 'value' => value } }.to_json
- res = Net::HTTP.start(url.host, url.port) do |http|
- http.request(req)
- end
- raise "HTTP error #{res.code} while POSTing to #{browser_url}/api/v1/settings/" if res.code != '200'
- end
- =begin
- Helper method for both object_manager_attribute_create and object_manager_attribute_update
- =end
- def object_manager_attribute_perform(action = 'create', params = {})
- instance = params[:browser] || @browser
- data = params[:data]
- modal_ready(browser: instance)
- if action == 'create'
- set(
- browser: instance,
- css: '.modal input[name=name]',
- value: data[:name],
- mute_log: true,
- )
- end
- if data[:display]
- set(
- browser: instance,
- css: '.modal input[name=display]',
- value: data[:display],
- mute_log: true,
- )
- end
- if data[:data_type]
- select(
- browser: instance,
- css: '.modal select[name="data_type"]',
- value: data[:data_type],
- mute_log: true,
- )
- end
- if data[:data_option]
- if data[:data_option][:options]
- if data[:data_type] == 'Boolean'
- # rubocop:disable Lint/BooleanSymbol
- element = instance.find_elements(css: '.modal .js-valueTrue').first
- element.clear
- element.send_keys(data[:data_option][:options][:true])
- element = instance.find_elements(css: '.modal .js-valueFalse').first
- element.clear
- element.send_keys(data[:data_option][:options][:false])
- # rubocop:enable Lint/BooleanSymbol
- elsif data[:data_type] == 'Tree Select'
- add_tree_options(
- instance: instance,
- options: data[:data_option][:options],
- )
- else
- if action == 'update'
- # first clear all existing entries
- loop do
- target = {
- browser: instance,
- css: '.modal .js-Table .js-remove',
- mute_log: true,
- }
- break if !instance.find_elements(css: target[:css])[0]
- click(target)
- end
- sleep 1
- end
- # then populate the table with the new values
- data[:data_option][:options].each do |key, value|
- element = instance.find_elements(css: '.modal .js-Table .js-key').last
- element.clear
- element.send_keys(key)
- element = instance.find_elements(css: '.modal .js-Table .js-value').last
- element.clear
- element.send_keys(value)
- element = instance.find_elements(css: '.modal .js-Table .js-add')[0]
- element.click
- end
- end
- end
- %i[default min max diff].each do |key|
- next if !data[:data_option].key?(key)
- element = instance.find_elements(css: ".modal [name=\"data_option::#{key}\"]").first
- element.clear
- element.send_keys(data[:data_option][key])
- end
- %i[future past].each do |key|
- next if !data[:data_option].key?(key)
- select(
- browser: instance,
- css: ".modal select[name=\"data_option::#{key}\"]",
- value: data[:data_option][key],
- mute_log: true,
- )
- end
- %i[maxlength].each do |key|
- next if !data[:data_option].key?(key)
- set(
- browser: instance,
- css: ".modal input[name=\"data_option::#{key}\"]",
- value: data[:data_option][key],
- mute_log: true,
- )
- end
- end
- if params[:do_not_submit]
- assert(true, "attribute #{action}d without submit")
- return true
- end
- instance.find_elements(css: '.modal button.js-submit')[0].click
- if params[:error]
- sleep 4
- watch_for(
- css: '.modal',
- value: params[:error],
- )
- click(
- browser: instance,
- css: '.modal .js-close',
- )
- modal_disappear(browser: instance)
- return
- end
- 11.times do
- element = instance.find_elements(css: 'body')[0]
- text = element.text
- if text.match?(/#{Regexp.quote(data[:name])}/)
- assert(true, 'object manager attribute updated')
- sleep 1
- return true
- end
- sleep 1
- end
- screenshot(browser: instance, comment: "object_manager_attribute_#{action}_failed")
- raise "object_manager_attribute_#{action}_failed"
- end
- end
|