├── .gitignore ├── .rspec ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console ├── flick └── setup ├── flick.gemspec ├── lib ├── flick.rb └── flick │ ├── android.rb │ ├── applitools.rb │ ├── checker.rb │ ├── info.rb │ ├── ios.rb │ ├── log.rb │ ├── manager.rb │ ├── screenshot.rb │ ├── simple_daemon.rb │ ├── system.rb │ ├── version.rb │ ├── video.rb │ └── vitals.rb └── spec ├── android_spec.rb ├── flick_spec.rb ├── ios_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | .byebug_history 11 | *.mp4 12 | *.swp 13 | *.png 14 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.2.1 4 | before_install: gem install bundler -v 1.10.6 5 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in flick.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Justin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FLICK 2 | A CLI to capture screenshots, video, logs, device info and device vitals(memory, cpu) for Android (Devices & Emulators) and iOS (Devices). 3 | 4 | 5 | 6 | Features 7 | -------- 8 | 9 | * Easily capture screenshots for Android and iOS. 10 | * Video record real android devices. (OS > 4.4) 11 | * Extend recording past the 180 seconds SDK limit. 12 | * Save video formats in mp4 or gif. 13 | * Flick auto detects if a device is recordable. 14 | * Falls back to screenshot recording if video record is not available. 15 | * Video record android emulators and **real** iOS devices. 16 | * Takes a screenshot every 0.5 seconds (default), then combines the screenshots into a single mp4 or gif. 17 | * Android pulls only unique (default) screenshots from devices/emulators. e.g. A 1 minute test run might convert to only 30 seconds of video based on unique images. You can change this by passing `-q false` to pull all images instead. 18 | * iOS example [here](https://www.dropbox.com/s/4pjhhmnsx9gj5pi/ios-flick-example.mp4?dl=0) 19 | * Android Emulator example [here](https://www.dropbox.com/s/gwunrvgzxkny13z/android-flick-example.mp4?dl=0) 20 | * Flick auto selects device when only one device is connected, per platform. 21 | * Save log output for Android or iOS. 22 | * Display device information or save it to a file. 23 | * Install or Uninstall applications from devices. 24 | * Capture device vitals (Android Only) for app and system performance. 25 | * If anyone knows of a tool to capture performance for iOS please let me know and I'll add it to Flick. 26 | * Checkout the latest release notes [here](https://github.com/isonic1/flick/releases). 27 | 28 | Reason 29 | ------ 30 | I wanted an easy way to video record my automation tests for mobile, and I didn't need the video quality to be perfect. Unfortunately, you cannot video record on android emulators, but you can take screenshots! You also cannot video record iOS without using QuickTime, or doing what [this](https://github.com/appium/screen_recording) did, but it's not maintained anymore. 31 | 32 | So I created Flick to work for my needs, and included a couple other tools I use frequently. Hopefully this will be as helpful for others too. It's also a CLI and language-agnostic, it can be used with any framework where you can make a system call. See examples [here](https://github.com/isonic1/appium-mobile-grid/blob/flick/ios/spec/spec_helper.rb#L15-L16) and [here](https://github.com/isonic1/appium-mobile-grid/blob/flick/android/spec/spec_helper.rb#L22-L23). I suppose there are use cases for this outside of test automation. I'd love to hear them if so. 33 | 34 | If you're looking for high-quality video, then this wouldn't be the tool for you. Take a look at this great tool [androidtool-mac](https://github.com/mortenjust/androidtool-mac) instead. 35 | 36 | Prerequisites 37 | ------------- 38 | #### System Tools 39 | * Install ffmpeg. [OSX](https://trac.ffmpeg.org/wiki/CompilationGuide/MacOSX) 40 | * ```$ brew install ffmpeg``` 41 | * Install mp4box. [OSX](http://hunterford.me/compiling-mp4box-on-mac-os-x/) 42 | * ```$ brew install mp4box``` 43 | 44 | #### Android 45 | * Install [SDK Tools](http://developer.android.com/sdk/installing/index.html?pkg=tools). 46 | * SDK tools are added to your $PATH. [OSX](http://stackoverflow.com/questions/5526470/trying-to-add-adb-to-path-variable-osx) 47 | * Enable [USB Debugging](https://www.kingoapp.com/root-tutorials/how-to-enable-usb-debugging-mode-on-android.htm) on your device(s). 48 | * Emulator or Devices have approximately 1GB of [sdcard space](http://developer.android.com/tools/help/mksdcard.html). 49 | 50 | #### iOS 51 | * Install [Xcode](https://developer.apple.com/xcode/download/). 52 | * Install Xcode [Command Line Tools](http://railsapps.github.io/xcode-command-line-tools.html). 53 | * Enable [Developer Mode](http://apple.stackexchange.com/questions/159196/enable-developer-inside-the-settings-app-on-ios) on iPhone or iPad devices. 54 | * Install [libimobiledevice](http://www.libimobiledevice.org/). 55 | * ```$ brew install libimobiledevice``` 56 | 57 | #### Known Issues 58 | * IOS: 59 | * Sometimes ideviceinstaller gets out of sync with your OS due to various reasons. When this happens, screenshots and video will not work for iOS. 60 | * Follow steps in [here](https://github.com/isonic1/flick/issues/10) to uninstall and reinstall dependencies. 61 | * Make sure xcode and command line tools are up to date. 62 | * Make sure iOS device is up to date with latest iOS version. 63 | * Validate your device is paired to your machine. `$ idevicepair -u validate` 64 | * Additionally, try to unpair and pair again. 65 | * `$ idevicepair -u unpair` 66 | * `$ idevicepair -u pair` Accept the Trust popup on the device. 67 | 68 | * Android: 69 | * Some devices/emulators are read-only. Flick requires access to internal storage of the device/emulator to write files to. If the device is read-only it cannot do this so it will Abort. There is probably a solution to this but I, unfortunately, don't have a lot of time to figure it out. Pull Requests are highly encouragaged if you find this tool useful! 70 | 71 | Installation 72 | ------------ 73 | 74 | $ gem install flick 75 | 76 | 77 | Usage: 78 | ------ 79 | $ flick --help 80 | 81 | DESCRIPTION: 82 | 83 | A CLI to capture screenshots, video, logs, and device info for Android (Devices & Emulators) and iOS (Devices). 84 | 85 | COMMANDS: 86 | 87 | help Display global or [command] help documentation 88 | info Get device information 89 | log Get device log output 90 | manager Manage apps on devices 91 | screenshot Take a screenshot 92 | video Record video 93 | vitals Android Only! Get apps and device vitals - App Memory Used, App CPU %. System Stats: (User, System, IQW, IRQ) 94 | applitools Validate images with Applitools 95 | 96 | GLOBAL OPTIONS: 97 | 98 | -h, --help 99 | Display help documentation 100 | 101 | -v, --version 102 | Display version information 103 | 104 | -t, --trace 105 | Display backtrace when an error occurs 106 | 107 | $ flick info --help 108 | 109 | $ flick info -p (ios or android) 110 | $ flick info -p (ios or android) -s true -o $HOME 111 | 112 | 113 | $ flick log --help 114 | 115 | $ flick log -a start -p (ios or android) -o $HOME -n iosLog 116 | $ flick log -a stop -p (ios or android) 117 | 118 | $ flick screenshot --help 119 | 120 | $ flick screenshot -p (ios or android) -o $HOME -n myImage 121 | 122 | $ flick video --help 123 | 124 | $ flick video -a start -p (ios or android) 125 | $ flick video -a stop -p (ios or android) -o /output -n myVideo -f gif 126 | $ flick video -a start -p android -u emulator-5554 -c 1000 127 | $ flick video -a stop -p android -u emulator-5554 128 | 129 | $ flick manager --help 130 | $ flick manager -a install -p (ios or android) -f ~/myApp/my-awesome-app.apk or .app 131 | $ flick maanger -a uninstall -p (ios or android) -n com.package.name 132 | 133 | $ flick vitals --help 134 | $ flick vitals -p android -n com.awsome-package.name 135 | 136 | $ flick applitools --help 137 | $ flick applitools -p android --appName Twitter --testName Messages 138 | 139 | ##Demo 140 | 141 | 142 | ## Contributing 143 | 144 | Bug reports and pull requests are welcome on GitHub at https://github.com/isonic1/flick. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://github.com/isonic1/Appium-Native-Crawler/blob/master/CODE_OF_CONDUCT.md) code of conduct. 145 | 146 | ## TODO 147 | * Dry the code a bit. 148 | * Setup Flick android for cross platform os's (windows & linux) 149 | * Add screenshot capture for iOS Simulators. 150 | * Multithread the screenshot and pull process. 151 | * Look into capturing video for iOS similar to [this](https://github.com/mortenjust/androidtool-mac/blob/9347cd9aeca9e7370e323d12f862bc5d8beacc25/AndroidTool/IOSDeviceHelper.swift#L56) 152 | 153 | ## License 154 | 155 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 156 | 157 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "flick" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | require 'json' 15 | IRB.start 16 | -------------------------------------------------------------------------------- /bin/flick: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative '../lib/flick' 3 | require 'commander/import' 4 | require 'digest' 5 | 6 | program :version, '0.4.6' 7 | program :description, 'A CLI to capture screenshots, video, logs, and device info for Android (Devices & Emulators) and iOS (Devices).' 8 | 9 | command :video do |c| 10 | c.syntax = 'flick video [options]' 11 | c.description = 'Record video' 12 | c.example 'description', "flick video -a start -p android -e true -u TA64300B9C\n flick video -a stop -p android -n my-video -o /mydir" 13 | c.option '-a', '--action ACTION', String, 'Set action: start or stop' 14 | c.option '-p', '--platform PLATFORM', String, 'Set platform: android or ios' 15 | c.option '-u', '--udid UDID', String, 'Set device UDID. Defaults to only connected device per platform' 16 | c.option '-s', '--seconds SECONDS', String, 'Set the seconds per screenshot. Default: 0.5' 17 | c.option '-c', '--count COUNT', Integer, 'Set maximum number of screenshots. Default: 500' 18 | c.option '-e', '--extend EXTEND', 'Extend android video past 180 seconds SDK limit. e.g. -e true .Default: false' 19 | #The options below are relavent to the stop action... 20 | c.option '-n', '--name NAME', String, 'Set name of output file, Default: UDID' 21 | c.option '-q', '--unique UNIQUE', 'Pull only unique screenshots. Significantly speeds up the pulling process. Default: true' 22 | c.option '-f', '--format FORMAT', String, 'Set output format (mp4 or gif). Default: mp4' 23 | c.option '-r', '--rate RATE', String, 'Set the framerate (in seconds) when converting screenshots to video. Default: 1' 24 | c.option '-o', '--outdir OUTDIR', String, "Set output directory. Default is #{Dir.pwd}" 25 | c.option '--use_screenshots', "Use screenshot instead of screenrecord to create final video. Use it on devices with limited space. Default: false" 26 | c.action do |args, options| 27 | options.default \ 28 | :seconds => 0.5, #Seconds per screenshot. 29 | :rate => 1, #Seconds per framerate for screenshot to video conversion. 30 | :count => 500, #This is a safety check to stop runaway screenshot process. 31 | :extend => false, #Extend android video recording (for REAL devices and os >= 4.4) past 180 seconds. 32 | :unique => true, #defaults to pulling only unique screenshots. Speeds up pulling process from device. 33 | :format => "mp4", 34 | :outdir => Dir.pwd, #defaults to save in current directory. 35 | :use_screenshots => false # By default use screenrecord to record Android activity 36 | Video.new(options.default).run 37 | end 38 | end 39 | 40 | command :screenshot do |c| 41 | c.syntax = 'flick screenshot [options]' 42 | c.summary = 'Take a screenshot' 43 | c.description = c.summary 44 | c.example 'description', 'flick screenshot -p ios' 45 | c.option '-p', '--platform PLATFORM', String, 'Set platform: android or ios' 46 | c.option '-u', '--udid UDID', String, 'Set device UDID.' 47 | c.option '-n', '--name NAME', String, "Set name of output file. Default: UDID" 48 | c.option '-o', '--outdir OUTDIR', String, "Set output directory. Default is #{Dir.pwd}" 49 | c.action do |args, options| 50 | options.default \ 51 | :outdir => Dir.pwd #defaults to save in current directory. 52 | Screenshot.new(options.default).screenshot 53 | end 54 | end 55 | 56 | command :applitools do |c| 57 | c.syntax = 'flick applitools [options]' 58 | c.summary = 'Validate images with Applitools' 59 | c.description = c.summary 60 | c.example 'description', "flick applitools -p android --appName Twitter --testName Messages" 61 | c.option '-p', '--platform PLATFORM', String, 'Set platform: android or ios' 62 | c.option '-u', '--udid UDID', String, 'Set device UDID.' 63 | c.option '-o', '--outdir OUTDIR', String, "Set output directory. Default: #{Dir.pwd}" 64 | c.option '--keep', "Keep image after capture." 65 | c.option '--appName appName', String, "Set your App Name. Example: --appName Twitter. Default is the platform value" 66 | c.option '--testName testName', String, "Set Test Name. Example: --testName 'Home View'" 67 | c.option '--autoSave autoSave', String, "Automatically save failed tests." 68 | c.option '--apiKey apiKey', String, "Applitools API Key" 69 | c.option '--showLogs showLogs', String, "Display Applitools Logs" 70 | c.option '--matchLevel matchLevel', String, "Set match level to one of 'strict, layout, or content'. Default: strict" 71 | c.option '--server Server', String, "Set Applitools server url. Default: https://eyes.applitools.com" 72 | c.option '--proxy Proxy', String, "Set proxy address" 73 | c.option '--baseline Baseline', String, "Set your baseline name" 74 | c.option '--batchName BatchName', String, "Set your batch (folder) name" 75 | c.option '--batchId BatchId', String, "Set a batch ID. If you want to put images into an existing batch you can set this ID. e.g. 1234" 76 | c.option '--hostApp', String, "Set Host-app identifier for the screens under test" 77 | c.option '--hostOs', String, "Set OS identifier for the screens under test" 78 | # c.option '--branch', String, "Set branch name" 79 | # c.option '--parentBranch', String, "Set parent branch name, optional when working with branches" 80 | c.action do |args, options| 81 | options.default \ 82 | :outdir => Dir.pwd, 83 | :keep => false, 84 | :matchLevel => 'strict', 85 | :server => 'https://eyes.applitools.com', 86 | :showLogs => false, 87 | :apiKey => ENV['APPLITOOLS_API_KEY'], 88 | :appName => options.platform, 89 | :baseline => nil, 90 | :batchName => options.appName, 91 | :hostApp => nil, 92 | :hostOs => nil 93 | 94 | [:testName, :apiKey].each do |o| 95 | if options.default[o].nil? 96 | puts "\n--#{o.to_s} is required... Please set it before running!\n".red 97 | abort 98 | end 99 | end 100 | 101 | match_levels = ['strict', 'layout', 'content'] 102 | unless match_levels.include? options.matchLevel 103 | puts "You entered an incorrect match level. Please choose any of these: #{match_levels}" 104 | abort 105 | end 106 | 107 | options.batchId = options.batchId.to_i if options.batchId 108 | 109 | Applitoolz.new(options.default).upload_to_applitools 110 | end 111 | end 112 | 113 | command :log do |c| 114 | c.syntax = 'flick log [options]' 115 | c.summary = 'Get device log output' 116 | c.description = c.summary 117 | c.example 'description', "flick log -a start -p ios -o /logs -n myLog\n flick log -a stop -p ios" 118 | c.option '-a', '--action ACTION', String, 'Set action: start or stop' 119 | c.option '-p', '--platform PLATFORM', String, 'Set platform: android or ios' 120 | c.option '-u', '--udid UDID', String, 'Set device UDID.' 121 | c.option '-n', '--name NAME', String, "Set name of output file. Default: UDID" 122 | c.option '-o', '--outdir OUTDIR', String, "Set output directory. Default is #{Dir.pwd}" 123 | c.action do |args, options| 124 | options.default \ 125 | :outdir => Dir.pwd #defaults to save in current directory. 126 | Log.new(options.default).run 127 | end 128 | end 129 | 130 | command :info do |c| 131 | c.syntax = 'flick info [options]' 132 | c.summary = 'Get device information' 133 | c.description = c.summary 134 | c.example 'description', "flick info -p android\n flick info -p ios" 135 | c.option '-p', '--platform PLATFORM', String, 'Set platform: android or ios' 136 | c.option '-u', '--udid UDID', String, 'Set device UDID.' 137 | c.option '-s', '--save SAVE', 'Save device info to a file.' 138 | c.option '-o', '--outdir OUTDIR', String, "Save device info to file. Default is #{Dir.pwd}" 139 | c.action do |args, options| 140 | options.default \ 141 | :save => false, 142 | :outdir => Dir.pwd #defaults to save in current directory. 143 | Info.new(options.default).info 144 | end 145 | end 146 | 147 | command :manager do |c| 148 | c.syntax = 'flick manager [options]' 149 | c.summary = 'Manage apps on devices' 150 | c.description = c.summary 151 | c.example 'description', "flick manager -a install -p android -f ~/my-awesome-app.apk\n flick manager -a uninstall -p ios -n com.viber" 152 | c.option '-a', '--action ACTION', String, 'Set action: install or uninstall' 153 | c.option '-p', '--platform PLATFORM', String, 'Set platform: android or ios' 154 | c.option '-u', '--udid UDID', String, 'Set device UDID.' 155 | c.option '-f', '--file FILE', String, 'Set the apk or app file location path.' 156 | c.option '-n', '--name NAME', String, 'Set the package name.' 157 | c.action { |args, options| Manager.new(options.default).run } 158 | end 159 | 160 | command :vitals do |c| 161 | c.syntax = 'flick vitals [options]' 162 | c.summary = 'Android Only! Get apps and device vitals - App Memory Used, App CPU %. System Stats: (User, System, IQW, IRQ)' 163 | c.description = c.summary 164 | c.example 'description', "flick vitals -p android -n com.viber" 165 | c.option '-p', '--platform PLATFORM', String, 'Set platform: android or ios' 166 | c.option '-n', '--name NAME', String, 'Set the package name.' 167 | c.option '-u', '--udid UDID', String, 'Set device UDID.' 168 | c.action { |args, options| Vitals.new(options.default).vitals } 169 | end 170 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /flick.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'flick/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "flick" 8 | spec.version = Flick::VERSION 9 | spec.authors = ["Justin"] 10 | spec.email = ["justin.ison@gmail.com"] 11 | 12 | spec.summary = %q{A CLI to capture screenshots, video, logs, and device information for Android (Devices & Emulators) and iOS (Devices).} 13 | spec.description = %q{A CLI with helpful QA tools for iOS and Android from the command line} 14 | spec.homepage = "https://github.com/isonic1/flick" 15 | spec.license = "MIT" 16 | 17 | if spec.respond_to?(:metadata) 18 | spec.metadata['allowed_push_host'] = "https://rubygems.org" 19 | else 20 | raise "RubyGems 2.0 or newer is required to protect against public gem pushes." 21 | end 22 | 23 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 24 | spec.bindir = "bin" 25 | spec.executables = ["flick"] 26 | spec.require_paths = ["lib"] 27 | 28 | spec.add_development_dependency "bundler", "~> 1.9" 29 | spec.add_development_dependency "rake", "~> 10.0" 30 | spec.add_development_dependency "rspec", '~> 3.4', '>= 3.4.0' 31 | spec.add_dependency "parallel", '~> 1.6', '>= 1.6.2' 32 | spec.add_dependency "colorize", "~> 0.8.1" 33 | spec.add_dependency "commander" 34 | spec.add_dependency "json" 35 | spec.add_dependency "wannabe_bool", "~> 0.5.0" 36 | spec.add_dependency "awesome_print", '~> 1.6', '>= 1.6.1' 37 | spec.add_dependency "os", "~> 0.9.6" 38 | spec.add_dependency "sys-proctable", '~> 1.1', '>= 1.1.1' 39 | spec.add_dependency "apktools", '~> 0.7.1', '>= 0.7.1' 40 | spec.add_dependency "ffi" 41 | spec.add_dependency "eyes_images" 42 | spec.add_dependency "pry" 43 | end -------------------------------------------------------------------------------- /lib/flick.rb: -------------------------------------------------------------------------------- 1 | require 'colorize' 2 | require 'json' 3 | require 'parallel' 4 | require 'wannabe_bool' 5 | require 'awesome_print' 6 | require 'os' 7 | require 'tempfile' 8 | require 'sys/proctable' 9 | require 'apktools/apkxml' 10 | require 'open3' 11 | require 'eyes_images' 12 | require 'pry' 13 | require 'openssl' 14 | original_verbose = $VERBOSE 15 | $VERBOSE = nil 16 | OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE 17 | $VERBOSE = original_verbose 18 | 19 | require "flick/version" 20 | require_relative "./flick/android" 21 | require_relative "./flick/checker" 22 | require_relative "./flick/info" 23 | require_relative "./flick/ios" 24 | require_relative "./flick/log" 25 | require_relative "./flick/screenshot" 26 | require_relative "./flick/simple_daemon" 27 | require_relative "./flick/system" 28 | require_relative "./flick/video" 29 | require_relative "./flick/manager" 30 | require_relative "./flick/vitals" 31 | require_relative "./flick/applitools" -------------------------------------------------------------------------------- /lib/flick/android.rb: -------------------------------------------------------------------------------- 1 | module Flick 2 | class Android 3 | attr_accessor :udid, :flick_dir, :dir_name, :name, :outdir, :unique, :limit, :specs, :size, :use_screenshots 4 | 5 | def initialize options 6 | Flick::Checker.system_dependency "adb" 7 | if options[:udid].nil? 8 | self.udid = get_device_udid 9 | else 10 | self.udid = options[:udid] 11 | end 12 | self.flick_dir = "#{Dir.home}/.flick/#{udid}" 13 | self.dir_name = "sdcard/flick" 14 | self.name = remove_bad_characters(options.fetch(:name, self.udid)) 15 | self.outdir = options.fetch(:outdir, Dir.pwd) 16 | self.unique = options.fetch(:unique, true).to_b 17 | self.limit = options.fetch(:limit, 180) 18 | self.specs = options.fetch(:specs, false) 19 | self.size = options.fetch(:size, "720x1280") 20 | self.use_screenshots = options.fetch(:use_screenshots, false) 21 | create_flick_dirs 22 | end 23 | 24 | def remove_bad_characters string 25 | string.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') 26 | end 27 | 28 | def create_flick_dirs 29 | Flick::System.setup_system_dir "#{Dir.home}/.flick" 30 | Flick::System.setup_system_dir flick_dir 31 | message = %x(adb -s #{udid} shell 'mkdir #{dir_name}').split(":").last.strip rescue nil 32 | if message == "Read-only file system" 33 | puts "\nDevice: '#{udid}' is a 'Read-only file system'! Flick cannot write to the sdcard folder. Aborting...\n".red 34 | abort 35 | end 36 | end 37 | 38 | def clear_files 39 | Flick::System.clean_system_dir flick_dir, udid 40 | %x(adb -s #{udid} shell rm '#{dir_name}/*') 41 | end 42 | 43 | def devices 44 | (`adb devices`).scan(/\n(.*)\t/).flatten 45 | end 46 | 47 | def devices_connected? 48 | devices.any? 49 | end 50 | 51 | def check_for_devices 52 | unless devices_connected? 53 | puts "\nNo Devices Connected or Authorized!!!\nMake sure at least one device (emulator/simulator) is connected!\n".red 54 | abort 55 | end 56 | end 57 | 58 | def get_device_udid 59 | check_for_devices 60 | if devices.size == 1 61 | devices.first 62 | else 63 | puts "\nMultiple android devices '#{devices}' found.\nSpecify a single UDID. e.g. -u #{devices.sample}\n".red 64 | abort unless specs 65 | end 66 | end 67 | 68 | def info 69 | specs = { os: "ro.build.version.release", manufacturer: "ro.product.manufacturer", model: "ro.product.model", sdk: "ro.build.version.sdk" } 70 | hash = { udid: udid } 71 | specs.each do |key, spec| 72 | value = `adb -s #{udid} shell getprop "#{spec}"`.strip 73 | hash.merge!({key=> "#{value}"}) 74 | end 75 | hash 76 | end 77 | 78 | def system_stats 79 | x = %x(adb -s #{udid} shell top -n 1 -d 1 | grep System).split(",") 80 | user = x.find { |x| x.include? "User" }.match(/User (.*)%/)[1].to_i 81 | sys = x.find { |x| x.include? "System" }.match(/System (.*)%/)[1].to_i 82 | iow = x.find { |x| x.include? "IOW" }.match(/IOW (.*)%/)[1].to_i 83 | irq = x.find { |x| x.include? "IRQ" }.match(/IRQ (.*)%/)[1].to_i 84 | { user: user, system: sys, iow: iow, irq: irq } 85 | end 86 | 87 | def memory package 88 | (%x(adb -s #{udid} shell dumpsys meminfo | grep #{package} | awk '{print $1}').strip.split.last.to_i * 0.001).round(2) 89 | end 90 | 91 | def cpu package 92 | %x(adb -s #{udid} shell top -n 1 -d 1 | grep #{package} | awk '{print $3}').strip.chomp("%").to_i 93 | end 94 | 95 | def get_vitals package 96 | if app_installed? package 97 | stats = JSON.generate({ app_stats: { memory_mb: memory(package), cpu_per: cpu(package) }, system_stats: system_stats }) 98 | json = JSON.parse stats 99 | puts json 100 | json 101 | else 102 | puts packages 103 | puts "\n#{package} was not found on device #{udid}! Please choose one from above. e.g. #{packages.sample}\n".red 104 | end 105 | end 106 | 107 | def install app_path 108 | %x(adb -s #{udid} install -r #{app_path}) 109 | end 110 | 111 | def uninstall package 112 | if app_installed? package 113 | %x(adb -s #{udid} shell pm uninstall #{package}) 114 | else 115 | puts packages 116 | puts "\n#{package} was not found on device #{udid}! Please choose one from above. e.g. #{packages.sample}\n".red 117 | end 118 | end 119 | 120 | def app_version app_path 121 | manifest(app_path).find {|x| x.name == "versionName" }["value"] 122 | end 123 | 124 | def package_name app_path 125 | manifest(app_path).find {|x| x.name == "package" }["value"] 126 | end 127 | 128 | def manifest app_path 129 | data = ApkXml.new app_path 130 | data.parse_xml("AndroidManifest.xml", false, true) 131 | data.xml_elements[0].attributes 132 | end 133 | 134 | def app_installed? package 135 | packages.include? "package:#{package}" 136 | end 137 | 138 | def packages 139 | %x(adb -s #{udid} shell pm list packages).split 140 | end 141 | 142 | def os_version 143 | %x(adb -s #{udid} shell getprop "ro.build.version.release").strip.to_f 144 | end 145 | 146 | def screenshot name 147 | %x(adb -s #{udid} shell screencap #{dir_name}/#{name}.png) 148 | end 149 | 150 | def log name 151 | %x(adb -s #{udid} logcat -c) 152 | %x(adb -s #{udid} logcat -v long > #{outdir}/#{name}.log) 153 | end 154 | 155 | def recordable? 156 | if genymotion? || use_screenshots 157 | return false 158 | else 159 | %x(adb -s #{udid} shell "ls /system/bin/screenrecord").strip == "/system/bin/screenrecord" 160 | end 161 | end 162 | 163 | def genymotion? 164 | info[:manufacturer] == "Genymotion" 165 | end 166 | 167 | def screenrecord name 168 | %x(adb -s #{udid} shell screenrecord --time-limit #{limit} --size #{size} #{dir_name}/#{name}.mp4) 169 | end 170 | 171 | def pull_file file, dir 172 | %x(adb -s #{udid} pull #{file} #{dir}) 173 | end 174 | 175 | def unique_files type 176 | if os_version < 6.0 177 | command = "md5" 178 | else 179 | command = "md5sum" 180 | end 181 | files = %x(adb -s #{udid} shell "#{command} #{dir_name}/#{type}*") 182 | hash = files.split(/[\r\n]+/).map { |file| { md5: file.match(/(.*) /)[1].strip, file: file.match(/ (.*)/)[1].strip } } 183 | hash.uniq! { |e| e[:md5] } 184 | hash.map { |file| file[:file] } 185 | end 186 | 187 | def pull_files type 188 | if unique 189 | files = unique_files type 190 | else 191 | files = %x(adb -s #{udid} shell "ls #{dir_name}/#{type}*").split("\r\n") 192 | end 193 | return if files.empty? 194 | Parallel.map(files, in_threads: 10) do |file| 195 | pull_file file, flick_dir 196 | #Flick::System.wait_for_file 10, "#{flick_dir}/#{file.split("/").last}" 197 | end 198 | end 199 | end 200 | end 201 | -------------------------------------------------------------------------------- /lib/flick/applitools.rb: -------------------------------------------------------------------------------- 1 | require 'date' 2 | 3 | class Applitoolz 4 | 5 | attr_accessor :platform, :driver, :eyes, :app_name, :test_name, :keep 6 | 7 | def initialize options 8 | self.platform = options[:platform] 9 | self.driver = Screenshot.new options 10 | self.eyes = Applitools::Images::Eyes.new 11 | eyes.api_key = options[:apiKey] 12 | eyes.server_url = options[:server] 13 | eyes.log_handler = Logger.new(STDOUT) if options[:showLogs] 14 | eyes.proxy = Applitools::Connectivity::Proxy.new options[:proxy] if options[:proxy] 15 | eyes.match_level = options[:matchLevel].downcase.to_sym 16 | eyes.baseline_name = options[:baseline] 17 | self.app_name = options[:appName] 18 | self.test_name = options[:testName] 19 | batch_info = Applitools::BatchInfo.new(options[:batchName]) 20 | batch_info.id = options[:batchId] #Date.today.to_time.to_i 21 | eyes.batch = batch_info 22 | eyes.host_app = options[:hostApp] 23 | eyes.host_os = options[:hostOs] 24 | self.keep = options[:keep] 25 | end 26 | 27 | def upload_to_applitools 28 | image_info = driver.screenshot 29 | eyes.test(app_name: app_name, test_name: test_name) do 30 | eyes.check_image(image_path: image_info[:path], tag: image_info[:udid]) 31 | remove_image image_info[:path] unless keep 32 | end 33 | end 34 | 35 | private 36 | 37 | def remove_image image 38 | File.delete image if File.exists? image 39 | end 40 | end -------------------------------------------------------------------------------- /lib/flick/checker.rb: -------------------------------------------------------------------------------- 1 | module Flick 2 | module Checker 3 | def self.which(cmd) 4 | exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] 5 | ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| 6 | exts.each { |ext| 7 | exe = File.join(path, "#{cmd}#{ext}") 8 | return exe if File.executable?(exe) && !File.directory?(exe) 9 | } 10 | end 11 | return nil 12 | end 13 | 14 | def self.system_dependency dep 15 | program = self.which dep 16 | if program.nil? || program.empty? 17 | puts "\n#{dep} was not found. Please ensure you have installed #{dep} and it's in your $PATH\n".red 18 | abort 19 | end 20 | end 21 | 22 | def self.platform platform 23 | platforms = ["android","ios"] 24 | unless platforms.include? platform 25 | puts "\nPlease specify a valid platform #{platforms}. e.g. flick -a start -p #{platforms.sample}\n".red 26 | abort 27 | end 28 | end 29 | 30 | def self.action action 31 | actions = ["start","stop"] 32 | unless actions.include? action 33 | puts "\nPlease specify a valid action #{actions}. e.g. flick -a #{actions.sample} -p ios\n".red 34 | abort 35 | end 36 | end 37 | 38 | def self.manager option 39 | options = ["install","uninstall"] 40 | unless options.include? option 41 | puts "\nPlease specify a valid option #{options}. e.g. flick -a #{options.sample} -p ios\n".red 42 | abort 43 | end 44 | end 45 | 46 | def self.file_exists? file 47 | unless File.exists? file 48 | puts "\n#{file} does not exist! Please specify a valid file path.".red 49 | abort 50 | end 51 | end 52 | 53 | def self.format format 54 | formats = ["mp4","gif"] 55 | unless formats.include? format 56 | puts "\nPlease specify a valid format #{formats}. e.g. flick -a stop -p ios -f #{formats.sample}\n".red 57 | abort 58 | end 59 | end 60 | end 61 | end -------------------------------------------------------------------------------- /lib/flick/info.rb: -------------------------------------------------------------------------------- 1 | class Info 2 | 3 | attr_accessor :platform, :driver, :save 4 | 5 | def initialize options 6 | Flick::Checker.platform options[:platform] 7 | self.platform = options[:platform] 8 | case @platform 9 | when "ios" 10 | options[:todir] = options[:outdir] 11 | self.driver = Flick::Ios.new options 12 | when "android" 13 | self.driver = Flick::Android.new options 14 | end 15 | self.save = options[:save].to_b 16 | end 17 | 18 | def info 19 | ap driver.info 20 | if save 21 | puts "Saving to #{driver.outdir}/info-#{driver.name}.log" 22 | save_device_data driver.info 23 | end 24 | end 25 | 26 | private 27 | 28 | def save_device_data info 29 | file = "#{driver.outdir}/info-#{driver.name}.log" 30 | File.delete file if File.exists? file 31 | info.each do |k,v| 32 | open(file, 'a') do |file| 33 | file << "#{k}: #{v}\n" 34 | end 35 | end 36 | end 37 | end -------------------------------------------------------------------------------- /lib/flick/ios.rb: -------------------------------------------------------------------------------- 1 | module Flick 2 | class Ios 3 | attr_accessor :flick_dir, :udid, :name, :outdir, :todir, :specs 4 | 5 | def initialize options 6 | self.udid = options.fetch(:udid, get_device_udid(options)) 7 | self.flick_dir = "#{Dir.home}/.flick/#{udid}" 8 | self.name = remove_bad_characters(options.fetch(:name, self.udid)) 9 | self.todir = options.fetch(:todir, self.flick_dir) 10 | self.outdir = options.fetch(:outdir, Dir.pwd) 11 | self.specs = options.fetch(:specs, false) 12 | create_flick_dirs 13 | is_paired? 14 | end 15 | 16 | def remove_bad_characters string 17 | string.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') 18 | end 19 | 20 | def create_flick_dirs 21 | Flick::System.setup_system_dir "#{Dir.home}/.flick" 22 | Flick::System.setup_system_dir flick_dir 23 | end 24 | 25 | def is_paired? 26 | Flick::Checker.system_dependency "idevicename" 27 | if Open3.capture3("idevicename -u #{udid}")[1].split[0] == "ERROR:" 28 | puts "\nUDID: #{udid} - Is not paired with your machine!\nOr make sure the device is not locked!\n".red 29 | puts "Run: idevicepair -u pair\nIf not working still, see: https://github.com/isonic1/flick/issues/10\n".red 30 | puts "Read more information about libmobiledevice libraries, see: http://libimobiledevice.org".green 31 | abort 32 | end 33 | end 34 | 35 | def devices 36 | Flick::Checker.system_dependency "idevice_id" 37 | (`idevice_id -l`).split.uniq.map { |d| d } 38 | end 39 | 40 | def devices_connected? 41 | devices.any? 42 | end 43 | 44 | def check_for_devices 45 | unless devices_connected? 46 | puts "\nNo iPhone or iPad Devices Connected!!!\nMake sure at least one REAL device is connected!\n".red 47 | abort 48 | end 49 | end 50 | 51 | def get_device_udid opts_hash 52 | check_for_devices 53 | return unless opts_hash[:udid].nil? 54 | if devices.size == 1 55 | devices[0] 56 | else 57 | puts "\nMultiple iOS devices '#{devices}' found.\nSpecify a single UDID. e.g. -u #{devices.sample}\n".red 58 | abort unless specs 59 | end 60 | end 61 | 62 | def info 63 | specs = { os: "ProductVersion", name: "DeviceName", arc: "CPUArchitecture", type: "DeviceClass", sdk: "ProductType" } 64 | hash = { udid: udid } 65 | specs.each do |key, spec| 66 | value = (`ideviceinfo -u #{udid} | grep #{spec} | awk '{$1=""; print $0}'`).strip 67 | hash.merge!({key=> "#{value}"}) 68 | end 69 | hash 70 | end 71 | 72 | def recordable? 73 | false 74 | end 75 | 76 | def clear_files 77 | Flick::System.clean_system_dir flick_dir, udid 78 | end 79 | 80 | def screenshot name 81 | Flick::Checker.system_dependency "idevicescreenshot" 82 | %x(idevicescreenshot -u #{udid} #{todir}/#{name}.png) 83 | end 84 | 85 | def log name 86 | Flick::Checker.system_dependency "idevicesyslog" 87 | system("idevicesyslog -u #{udid} > #{outdir}/#{name}.log") 88 | #file = File.open("#{outdir}/#{name}.log", 'a') { |f| f.puts "\n<<<<<<<<<<<<<<<<<<<<<<<<< FLICK LOG START >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" } 89 | #file.close 90 | end 91 | 92 | def install app_path 93 | Flick::Checker.system_dependency "ideviceinstaller" 94 | %x(ideviceinstaller -u #{udid} -i #{app_path}) 95 | end 96 | 97 | def uninstall package 98 | Flick::Checker.system_dependency "ideviceinstaller" 99 | if app_installed? package 100 | %x(ideviceinstaller -u #{udid} -U #{package}) 101 | else 102 | puts packages 103 | puts "\n#{package} was not found on device #{udid}! Please choose one from above. e.g. #{packages.sample}\n".red 104 | end 105 | end 106 | 107 | def app_installed? package 108 | packages.include? "#{package}" 109 | end 110 | 111 | def packages 112 | %x(ideviceinstaller -u #{udid} -l -o list_user).split("\n")[1..100000].map { |p| p.match(/(.*) -/)[1] } 113 | end 114 | end 115 | end -------------------------------------------------------------------------------- /lib/flick/log.rb: -------------------------------------------------------------------------------- 1 | class Log 2 | 3 | attr_accessor :action, :platform, :driver, :udid 4 | 5 | def initialize options 6 | Flick::Checker.action options[:action] 7 | Flick::Checker.platform options[:platform] 8 | self.action = options[:action] 9 | self.platform = options[:platform] 10 | case @platform 11 | when "ios" 12 | options[:todir] = options[:outdir] 13 | self.driver = Flick::Ios.new options 14 | when "android" 15 | self.driver = Flick::Android.new options 16 | end 17 | self.udid = self.driver.udid 18 | end 19 | 20 | def run 21 | self.send(action) 22 | end 23 | 24 | def start 25 | puts "Saving to #{driver.outdir}/#{driver.name}.log" 26 | log 27 | end 28 | 29 | def stop 30 | Flick::System.kill_process "log", udid 31 | Flick::System.kill "idevicesyslog -u #{udid}" if ios 32 | Flick::System.kill "adb -s #{udid} logcat" if android 33 | end 34 | 35 | def log 36 | stop 37 | $0 = "flick-log-#{udid}" 38 | SimpleDaemon.daemonize! 39 | command = -> do 40 | driver.log "#{driver.name}" 41 | end 42 | command.call 43 | end 44 | 45 | private 46 | 47 | def android 48 | platform == "android" 49 | end 50 | 51 | def ios 52 | platform == "ios" 53 | end 54 | end -------------------------------------------------------------------------------- /lib/flick/manager.rb: -------------------------------------------------------------------------------- 1 | class Manager 2 | 3 | attr_accessor :action, :platform, :driver, :udid, :file, :name 4 | 5 | def initialize options 6 | Flick::Checker.manager options[:action] 7 | Flick::Checker.platform options[:platform] 8 | self.action = options[:action] 9 | self.platform = options[:platform] 10 | case platform 11 | when "ios" 12 | self.driver = Flick::Ios.new options 13 | when "android" 14 | self.driver = Flick::Android.new options 15 | end 16 | self.udid = self.driver.udid 17 | self.file = options[:file] 18 | self.name = options[:name] 19 | end 20 | 21 | def run 22 | self.send(action) 23 | end 24 | 25 | def install 26 | if file.nil? 27 | puts "Specify a file path. e.g. -f #{Dir.home}/myApp/amazing-app.apk or .app".red; abort 28 | else 29 | Flick::Checker.file_exists? file 30 | driver.install file 31 | end 32 | end 33 | 34 | def uninstall 35 | if name.nil? 36 | puts "Specify a Package Name or Bundle ID. e.g. -n ".red; abort 37 | else 38 | driver.uninstall name 39 | end 40 | end 41 | end -------------------------------------------------------------------------------- /lib/flick/screenshot.rb: -------------------------------------------------------------------------------- 1 | class Screenshot 2 | 3 | attr_accessor :platform, :driver 4 | 5 | def initialize options 6 | Flick::Checker.platform options[:platform] 7 | self.platform = options[:platform] 8 | case platform 9 | when "ios" 10 | options[:todir] = options[:outdir] 11 | self.driver = Flick::Ios.new options 12 | when "android" 13 | self.driver = Flick::Android.new options 14 | end 15 | setup 16 | end 17 | 18 | def screenshot 19 | driver.screenshot driver.name 20 | driver.pull_file "#{driver.dir_name}/#{driver.name}.png", driver.outdir if android 21 | if File.exists? "#{driver.outdir}/#{driver.name}.png" 22 | puts "Saved image to: #{driver.outdir}/#{driver.name}.png" 23 | return { path: "#{driver.outdir}/#{driver.name}.png", udid: driver.udid } 24 | else 25 | puts "\nThere appears to be an issue capturing the #{platform} image. Run flick with --trace for more details.\n".red 26 | abort 27 | end 28 | end 29 | 30 | private 31 | 32 | def android 33 | platform == "android" 34 | end 35 | 36 | def setup 37 | driver.clear_files 38 | end 39 | end -------------------------------------------------------------------------------- /lib/flick/simple_daemon.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # 3 | # https://gist.githubusercontent.com/mynameisrufus/1372491/raw/b76b60fb1842bf0507f47869ab19ad50a045b214/simple_daemon.rb 4 | # 5 | # == Double-forking Unix daemon 6 | # 7 | # Author:: Rufus Post (mailto:rufuspost@gmail.com) 8 | # 9 | # === How does it work? 10 | # 11 | # According to Stevens's Advanced Programming in the UNIX Environment 12 | # chapter 13, this is the procedure to make a well-behaved Unix daemon: 13 | # 14 | # Fork and have the parent exit. This makes the shell or boot script 15 | # think the command is done. Also, the child process is guaranteed not 16 | # to be a process group leader (a prerequisite for setsid next) 17 | # Call setsid to create a new session. This does three things: 18 | # * The process becomes a session leader of a new session 19 | # * The process becomes the process group leader of a new process group 20 | # * The process has no controlling terminal 21 | # 22 | # Optionally fork again and have the parent exit. This guarantes that 23 | # the daemon is not a session leader nor can it acquire a controlling 24 | # terminal (under SVR4) 25 | # 26 | # grandparent - the current process 27 | # \_ parent - exits immediately 28 | # \_ simple daemon - writes out its pid to file 29 | # 30 | # Change the current working directory to / to avoid interfering with 31 | # mounting and unmounting. By default don't bother to chdir("/") here 32 | # because we might to run inside APP_ROOT. 33 | # 34 | # Set file mode creation mask to 000 to allow creation of files with any 35 | # required permission later. By default umask is whatever was set by the 36 | # parent process at startup and can be set in config.ru and config_file, 37 | # so making it 0000 and potentially exposing sensitive log data can be 38 | # bad policy. 39 | # 40 | # Close unneeded file descriptors inherited from the parent (there is no 41 | # controlling terminal anyway): stdout, stderr, and stdin. 42 | # 43 | # Nowadays there is a file to track the PID which is used heavily by 44 | # Linux distribution boot scripts. Be sure to write out the PID of the 45 | # grandchild, either the return value of the second fork (step 3) or the 46 | # value of getpid() after step 3. 47 | # 48 | 49 | class SimpleDaemon 50 | 51 | # In the directory where you want your daemon add a git submodule to 52 | # your project and create a daemon launcher script: 53 | # 54 | # #!/usr/bin/env ruby 55 | # 56 | # require 'simple_daemon' 57 | # 58 | # $0 = "my daemon" 59 | # 60 | # SimpleDaemon.daemonize! ARGV[0], ARGV[1], ARGV[2] 61 | # 62 | # loop do 63 | # sleep 5 64 | # puts "tick" 65 | # sleep 5 66 | # puts "tock" 67 | # end 68 | # 69 | # make your script executable and run: 70 | # 71 | # $ chmod +x launcher 72 | # $ launcher tmp/daemon.pid log/daemon.stdout.log log/daemon.stderr.log 73 | # 74 | # check that it is running by with the following: 75 | # 76 | # $ ps aux | grep "my daemon" 77 | # 78 | # 79 | def self.daemonize! out = '/dev/null', err = '/dev/null', safe = true 80 | raise 'First fork failed' if (pid = fork) == -1 81 | exit unless pid.nil? 82 | Process.setsid 83 | raise 'Second fork failed' if (pid = fork) == -1 84 | exit unless pid.nil? 85 | kill_pid 86 | @file = Tempfile.new("flick-temp") 87 | @file.write Process.pid 88 | @file.rewind 89 | unless safe 90 | Dir.chdir '/' 91 | File.umask 0000 92 | end 93 | redirect out, err 94 | end 95 | 96 | # Attempts to write the pid of the forked process to the pid file. 97 | # Kills process if write unsuccesfull. 98 | def self.write pid, pidfile 99 | File.open pidfile, "w" do |f| 100 | f.write pid 101 | f.close 102 | end 103 | $stdout.puts "Daemon running with pid: #{pid}" 104 | rescue ::Exception => e 105 | raise "While writing the PID to file, unexpected #{e.class}: #{e}" 106 | end 107 | 108 | # Try and read the existing pid from the pid file and signal HUP to 109 | # process. 110 | def self.kill_pid 111 | unless @file.nil? 112 | opid = @file.read 113 | Process.kill "HUP", opid 114 | @file.unlink 115 | end 116 | rescue TypeError 117 | $stdout.puts "#{pidfile} was empty: TypeError" 118 | rescue Errno::ENOENT 119 | $stdout.puts "#{pidfile} did not exist: Errno::ENOENT" 120 | rescue Errno::ESRCH 121 | $stdout.puts "The process #{opid} did not exist: Errno::ESRCH" 122 | rescue Errno::EPERM 123 | raise "Lack of privileges to manage the process #{opid}: Errno::EPERM" 124 | rescue ::Exception => e 125 | raise "While signaling the PID, unexpected #{e.class}: #{e}" 126 | end 127 | 128 | # Redirect file descriptors inherited from the parent. 129 | def self.redirect out, err 130 | $stdin.reopen '/dev/null' 131 | $stdout.reopen File.new(out, "a") 132 | $stderr.reopen File.new(err, "a") 133 | $stdout.sync = $stderr.sync = true 134 | end 135 | end -------------------------------------------------------------------------------- /lib/flick/system.rb: -------------------------------------------------------------------------------- 1 | module Flick 2 | module System 3 | 4 | include Sys #load sys-proctable methods 5 | 6 | def self.setup_system_dir dir_name 7 | Dir.mkdir dir_name unless File.exists? dir_name 8 | end 9 | 10 | def self.clean_system_dir dir_name, udid 11 | Dir.glob("#{dir_name}/*#{udid}*").each do |file| 12 | File.delete file 13 | end 14 | end 15 | 16 | def self.find_pid string 17 | processes = ProcTable.ps.find_all { |x| x.cmdline.include? string } 18 | processes.map { |p| p.pid } rescue [] 19 | end 20 | 21 | def self.kill_pids pid_array 22 | return if pid_array.empty? 23 | pid_array.each { |p| Process.kill 'SIGKILL', p } 24 | end 25 | 26 | def self.process_running? string 27 | pid = self.find_pid string 28 | unless pid.empty? 29 | puts "PROCESSING IS RUNNING!!!" 30 | true 31 | else 32 | false 33 | end 34 | end 35 | 36 | def self.kill_process type, udid 37 | pids = self.find_pid "#{type}-#{udid}" 38 | self.kill_pids pids 39 | end 40 | 41 | def self.kill string 42 | pids = self.find_pid string 43 | self.kill_pids pids 44 | end 45 | 46 | def self.video_length file 47 | (`ffmpeg -i #{file} 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//`).strip 48 | end 49 | end 50 | end -------------------------------------------------------------------------------- /lib/flick/version.rb: -------------------------------------------------------------------------------- 1 | module Flick 2 | VERSION = "0.4.6" 3 | end 4 | -------------------------------------------------------------------------------- /lib/flick/video.rb: -------------------------------------------------------------------------------- 1 | class Video 2 | 3 | attr_accessor :action, :platform, :driver, :image_count, :seconds, :rate, :extended, :udid, :format 4 | 5 | def initialize options 6 | Flick::Checker.action options[:action] 7 | Flick::Checker.platform options[:platform] 8 | Flick::Checker.format options[:format] 9 | self.action = options[:action] 10 | self.platform = options[:platform] 11 | case self.platform 12 | when "ios" 13 | self.driver = Flick::Ios.new options 14 | when "android" 15 | self.driver = Flick::Android.new options 16 | end 17 | self.image_count = options[:count] 18 | self.seconds = options[:seconds].to_f 19 | self.rate = options[:rate].to_f 20 | self.extended = options[:extend].to_b 21 | self.udid = self.driver.udid 22 | self.format = options[:format] 23 | end 24 | 25 | def android 26 | platform == "android" 27 | end 28 | 29 | def ios 30 | platform == "ios" 31 | end 32 | 33 | def run 34 | self.send(action) 35 | end 36 | 37 | def start 38 | driver.clear_files 39 | puts "\nStarting Recoder!!!" 40 | if driver.recordable? 41 | if extended 42 | puts "In extended mode." 43 | Flick::Checker.system_dependency "mp4box" 44 | loop_record 45 | else 46 | start_record 47 | end 48 | else 49 | Flick::Checker.system_dependency "ffmpeg" 50 | start_screenshot_record 51 | end 52 | end 53 | 54 | def stop 55 | puts "\nStopping Recorder!!!" 56 | if driver.recordable? 57 | stop_record 58 | else 59 | stop_screenshot_recording 60 | end 61 | sleep 1 62 | driver.clear_files 63 | end 64 | 65 | private 66 | 67 | def start_record 68 | Flick::System.kill_process "video", udid 69 | $0 = "flick-video-#{udid}" 70 | SimpleDaemon.daemonize! 71 | command = -> do 72 | driver.screenrecord "video-#{udid}-single" 73 | end 74 | command.call 75 | end 76 | 77 | def loop_record 78 | Flick::System.kill_process "video", udid 79 | $0 = "flick-video-#{udid}" 80 | SimpleDaemon.daemonize! 81 | command = -> do 82 | count = "%03d" % 1 83 | loop do 84 | unless Flick::System.process_running? "#{udid}-" 85 | driver.screenrecord "video-#{udid}-#{count}" 86 | count.next! 87 | end 88 | end 89 | end 90 | command.call 91 | end 92 | 93 | def stop_record 94 | Flick::System.kill_process "video", udid 95 | sleep 5 #wait for video process to finish 96 | driver.pull_files "video" 97 | files = Dir.glob("#{driver.flick_dir}/video-#{udid}*.mp4").sort 98 | return if files.empty? 99 | files.each { |file| system("mp4box -cat #{file} #{driver.flick_dir}/#{driver.name}.mp4") } 100 | puts "Saving to #{driver.outdir}/#{driver.name}.#{format}" 101 | if format == "gif" 102 | gif 103 | else 104 | File.rename "#{driver.flick_dir}/#{driver.name}.mp4", "#{driver.outdir}/#{driver.name}.mp4" 105 | end 106 | end 107 | 108 | def start_screenshot_record 109 | Flick::System.kill_process "screenshot", udid 110 | puts "Process will stop after #{image_count} screenshots.\n" 111 | $0 = "flick-screenshot-#{udid}" 112 | SimpleDaemon.daemonize! 113 | command = -> do 114 | count = "%03d" % 1 115 | loop do 116 | if count.to_i <= image_count 117 | driver.screenshot "screenshot-#{udid}-#{count}" 118 | count.next!; sleep seconds 119 | else 120 | stop_screenshot_recording 121 | break 122 | end 123 | end 124 | end 125 | command.call 126 | end 127 | 128 | def stop_screenshot_recording 129 | driver.pull_files "screenshot" if android 130 | Flick::System.kill_process "screenshot", udid 131 | puts "Saving to #{driver.outdir}/#{driver.name}.#{format}" 132 | self.send(format) 133 | end 134 | 135 | def gif 136 | convert_images_to_mp4 unless driver.recordable? 137 | %x(ffmpeg -loglevel quiet -i #{driver.flick_dir}/#{driver.name}.mp4 -pix_fmt rgb24 #{driver.outdir}/#{driver.name}.gif) 138 | end 139 | 140 | def mp4 141 | convert_images_to_mp4 142 | File.rename "#{driver.flick_dir}/#{driver.name}.mp4", "#{driver.outdir}/#{driver.name}.mp4" unless format == "gif" 143 | end 144 | 145 | def convert_images_to_mp4 146 | remove_zero_byte_images 147 | %x(ffmpeg -loglevel quiet -framerate #{rate} -pattern_type glob -i '#{driver.flick_dir}/screenshot-#{udid}*.png' -c:v libx264 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" #{driver.flick_dir}/#{driver.name}.mp4) 148 | end 149 | 150 | def remove_zero_byte_images 151 | Dir.glob("#{driver.flick_dir}/screenshot-#{udid}*.png").each do |f| 152 | File.delete f if File.zero? f 153 | end 154 | end 155 | end 156 | -------------------------------------------------------------------------------- /lib/flick/vitals.rb: -------------------------------------------------------------------------------- 1 | class Vitals 2 | 3 | attr_accessor :platform, :driver, :name 4 | 5 | def initialize options 6 | Flick::Checker.platform options[:platform] 7 | self.platform = options[:platform] 8 | case @platform 9 | when "ios" 10 | options[:todir] = options[:outdir] 11 | self.driver = Flick::Ios.new options 12 | when "android" 13 | self.driver = Flick::Android.new options 14 | end 15 | self.name = options[:name] 16 | end 17 | 18 | def vitals 19 | if platform == "ios" 20 | puts "\nAndroid only for now. If you know of a tool to get this info for iOS please let me know or add a PR :)\n".yellow; abort 21 | end 22 | if name.nil? 23 | puts "Specify a Package Name. e.g. -n com.viber".red; abort 24 | else 25 | driver.get_vitals name 26 | end 27 | end 28 | end -------------------------------------------------------------------------------- /spec/android_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe "Flick Android Validations" do 4 | 5 | before :all do 6 | androids = ["TA64300B9C", "emulator-5554"] #set one real device and one emulator 7 | @options = {platform: "android", flick_dir: "#{Dir.home}/.flick", dir_name: "sdcard/flick", outdir: Dir.pwd, specs: true} 8 | Flick::Android.new(@options).check_for_devices 9 | abort unless (Flick::Android.new(@options).devices - androids).empty? 10 | `rm *.png *.mp4 >> /dev/null 2>&1` 11 | end 12 | 13 | after :each do 14 | `rm *.png *.mp4 >> /dev/null 2>&1` 15 | end 16 | 17 | it 'capture a screenshot on device' do 18 | @options.merge!({udid: "TA64300B9C", name: "android-device-screenshot"}) 19 | flick = Screenshot.new(@options) 20 | file = "#{@options[:name]}.png" 21 | flick.screenshot 22 | expect(File.file? file).to eq true 23 | end 24 | 25 | it 'capture a screenshot on emulator' do 26 | @options.merge!({udid: "emulator-5554", name: "android-emulator-screenshot"}) 27 | flick = Screenshot.new(@options) 28 | file = "#{@options[:name]}.png" 29 | flick.screenshot 30 | expect(File.file? file).to eq true 31 | end 32 | 33 | it 'capture a video on device' do 34 | @options.merge!({udid: "TA64300B9C", name: "android-device-video"}) 35 | flick = Video.new(@options) 36 | file = "#{@options[:name]}.mp4" 37 | flick.start; sleep 3 #need to wait for video to start 38 | flick.stop 39 | expect(File.file? file).to eq true 40 | end 41 | 42 | it 'capture extended video on device' do 43 | #must have something activily running on the screen. mp4box will limit video to uniqueness otherwise. 44 | @options.merge!({udid: "TA64300B9C", name: "android-device-video-extended", extend: true, limit: 5}) 45 | flick = Video.new(@options) 46 | file = "#{@options[:name]}.mp4" 47 | flick.start; sleep 10 #Wait for SDK limit to exceed. 48 | flick.stop 49 | expect(File.file? file).to eq true 50 | runtime = Flick::System.video_length(file) 51 | expect(DateTime.parse(runtime).sec).to be > 5 52 | end 53 | 54 | it 'capture a video on emulator' do 55 | @options.merge!({ count: 500, seconds: 0.5, udid: "emulator-5554", name: "android-emulator-video"}) 56 | flick = Video.new(@options) 57 | file = "#{@options[:name]}.mp4" 58 | flick.start; sleep 3 59 | flick.stop 60 | start = Time.now 61 | until File.file? file 62 | sleep 1; break if Time.now - start > 30 63 | end 64 | expect(File.file? file).to eq true 65 | end 66 | 67 | it 'screenshot count exceeded on emulator' do 68 | @options.merge!({ count: 10, seconds: 0.5, udid: "emulator-5554", name: "android-emulator-video-exceeded"}) 69 | flick = Video.new(@options) 70 | file = "#{@options[:name]}.mp4" 71 | flick.start 72 | start = Time.now 73 | until File.file? file 74 | sleep 1; break if Time.now - start > 30 75 | end 76 | expect(File.file? file).to eq true 77 | end 78 | 79 | xit 'pull all files' do 80 | @options.merge!({ count: 10, seconds: 0.5, udid: "emulator-5554", name: "android-emulator-video-all-files", unique: false}) 81 | flick = Video.new(@options) 82 | file = "#{@options[:name]}.mp4" 83 | flick.start 84 | start = Time.now 85 | until File.file? file 86 | sleep 1; break if Time.now - start > 30 87 | end 88 | expect(File.file? file).to eq true 89 | end 90 | end -------------------------------------------------------------------------------- /spec/flick_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Flick do 4 | it 'has a version number' do 5 | expect(Flick::VERSION).not_to be nil 6 | end 7 | 8 | it 'does something useful' do 9 | expect(false).to eq(true) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /spec/ios_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe "Flick iOS Validations" do 4 | 5 | before :all do 6 | @options = {platform: "ios", flick_dir: "#{Dir.home}/.flick", outdir: Dir.pwd} 7 | Flick::Ios.new(@options).check_for_devices 8 | `rm *.png *.mp4 >> /dev/null 2>&1` 9 | end 10 | 11 | after :each do 12 | `rm *.png *.mp4 >> /dev/null 2>&1` 13 | end 14 | 15 | it 'capture a screenshot on device' do 16 | @options.merge!({name: "ios-screenshot"}) 17 | flick = Screenshot.new(@options) 18 | file = "#{@options[:name]}.png" 19 | flick.screenshot 20 | expect(File.file? file).to eq true 21 | end 22 | 23 | it 'capture a video on device' do 24 | @options.merge!({ count: 500, seconds: 0.5, name: "ios-video", todir: "#{Dir.home}/.flick"}) 25 | flick = Video.new(@options) 26 | file = "#{@options[:name]}.mp4" 27 | flick.start; sleep 3 28 | flick.stop 29 | start = Time.now 30 | until File.file? file 31 | sleep 1; break if Time.now - start > 30 32 | end 33 | expect(File.file? file).to eq true 34 | end 35 | 36 | it 'screenshot count exceeded on device' do 37 | @options.merge!({ count: 10, seconds: 0.5, name: "ios-video", todir: "#{Dir.home}/.flick"}) 38 | flick = Video.new(@options) 39 | file = "#{@options[:name]}.mp4" 40 | flick.start 41 | start = Time.now 42 | until File.file? file 43 | sleep 1; break if Time.now - start > 30 44 | end 45 | expect(File.file? file).to eq true 46 | end 47 | end -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'flick' 3 | 4 | puts "\nSETUP:" 5 | puts "Connect an Android device.\nStart an Android emulator.\nConnect an iOS device." --------------------------------------------------------------------------------