diffity 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2f6cdaa452dd137feeb1cd7d70a5072ee4fb473b6d0a2ff0fb2a6de86b824b52
4
+ data.tar.gz: ca85943b5e9c40f92309b903f3ed8f3a45b03656b2ec1ec6665ffbd8496fe96c
5
+ SHA512:
6
+ metadata.gz: 0e3f0dca85f3539b12fac404a2f5f8e7e2f8595f46d07a94b6f00189951bb3e6a2d4d77ed03bd4ead3f9bbd4f5aca6d0ed6602d5d260e5c8f9761700f5025398
7
+ data.tar.gz: 2ef2ca1f6de652a7f701c850056f1e73a3ea94ec67e3789628a58f3223f175d571a1276f1f26d81e52fbccbcdb0ead06c2845fe3eee5868e9613ef851170b60a
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Yuva
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Diffity'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,124 @@
1
+ # Diffity
2
+
3
+ Currently this supports only RSpec.
4
+
5
+ ### Installation
6
+
7
+ ```rb
8
+ gem 'diffity'
9
+ ```
10
+
11
+ ### Configuration
12
+
13
+ Include `diffity` in your rspec `spec_helper` and configure 6 variables
14
+ which will be used while taking screenshots. Make sure that `enable_service` is
15
+ set to true if images need to be uploaded.
16
+
17
+ **NOTE:** Make sure that that project exists in service with `project_name`. Also
18
+ api key can be obtained by loggin into service and visiting `/api_key`.
19
+
20
+
21
+ ```rb
22
+ Diffity.configure do |config|
23
+ # configure domain to which all images have to be uploaded.
24
+ config.base_uri = "http://idf.dev"
25
+
26
+ # configure project name to which images belong to.
27
+ config.project_name = "idf"
28
+
29
+ # configure api_key required to authorize api access
30
+ config.api_key = ENV["DIFFITY_API_KEY"]
31
+
32
+ # configure js driver which is used for taking screenshots.
33
+ config.javascript_driver = "poltergeist"
34
+
35
+ # configure service to mock capturing and uploading screenshots
36
+ config.enable_service = !!ENV["DIFFITY_ENABLE"]
37
+
38
+ # configure logger to log messages. optional.
39
+ config.logger = Rails.logger
40
+ end
41
+ ```
42
+
43
+ After configuration, include `Diffity::Dsl` in your `spec_helper` and
44
+ configure before and after suite so that suite interacts with the service.
45
+
46
+
47
+ ```rb
48
+ RSpec.configure do |config|
49
+ config.include Diffity::Dsl
50
+
51
+ config.before(:suite) do
52
+ Diffity.start_run
53
+ end
54
+
55
+ config.after(:suite) do
56
+ Diffity.wrap_run
57
+ end
58
+ end
59
+ ```
60
+
61
+ ### Usage
62
+
63
+ In your specs, simply use `diffity` helper which has bunch of config utilities.
64
+
65
+ First, you should specify environment details under which screenshots are
66
+ taken. There are 6 parameters which can be configured.
67
+
68
+ Parameter|Explanation
69
+ ---------|-----------
70
+ browser | which browser is used to take screenshots. default: 'firefox'
71
+ | supported: firefox, chrome, safari, ie, opera
72
+ device | which device is used to take screenshots. default: 'desktop'
73
+ | supported: desktop, laptop, tablet, phone
74
+ os | which os is used to take screenshots. default: 'linux'
75
+ | supported: android, ios, windows, osx, linux
76
+ browser_version | (optional) version of browser used, for eg: '46' for firefox
77
+ device_name | (optional) name of device, for eg: 'MacBook Air'
78
+ os_version | (optional) version of os used, for eg: '10.11'
79
+
80
+
81
+ They can be configured using `diffity` helper while running specs. For eg:
82
+
83
+ ```rb
84
+ diffity.browser = 'firefox'
85
+ diffity.device = 'laptop'
86
+ diffity.os = 'osx'
87
+ diffity.browser_version = '46'
88
+ diffity.device_name = 'MBA'
89
+ diffity.os_version = '10.11.5'
90
+ ```
91
+
92
+ Also, `diffity` can used to take screenshots also. Make sure that you pass
93
+ unique identifier to screenshots that you take. unique identifier helps
94
+ in differentiating this screenshot taken from other screenshots for a
95
+ given set of `browser`, `device`, and `os`.
96
+
97
+
98
+ ```rb
99
+ describe "Landing page" do
100
+ it "has a big banner" do
101
+ visit root_path
102
+
103
+ diffity.browser = 'chrome'
104
+ diffity.screenshot("unique-identifier")
105
+ end
106
+ end
107
+ ```
108
+
109
+ Since there is flexibility to specify `browser`, `device`, and `os` while
110
+ running specs dynamically (unlike specifying `project_name`), you can run
111
+ all your specs in a loop by changing `browser`, `device` and `os` by
112
+ changing selenium driver, or changing viewport etc. Flexibility for your
113
+ service!
114
+
115
+
116
+ ### Concurrency
117
+
118
+ By default, when all the screenshots are collected, and before suite ends, this
119
+ gem will upload all the screenshots taken. `Diffity.wrap_run` is the method
120
+ responsible for the same.
121
+
122
+ However, if you want to upload screenshots as and when they are taken, this gem
123
+ has soft dependency on `concurrent-ruby` gem. Make sure that this gem is
124
+ **required** before capturing screenshots, and see the magic yourself :)
@@ -0,0 +1,77 @@
1
+ require 'faraday'
2
+ require 'diffity/dummy_runner'
3
+ require 'diffity/runner'
4
+ require 'diffity/dsl'
5
+ require 'logger'
6
+
7
+ module Diffity
8
+ # configure domain to which all images have to be uploaded.
9
+ @@base_uri = "http://diffity.com"
10
+ def self.base_uri=(uri)
11
+ @@base_uri = uri
12
+ end
13
+ def self.base_uri
14
+ @@base_uri
15
+ end
16
+
17
+ # configure project name to which images belong to.
18
+ @@project_name = "idf"
19
+ def self.project_name=(name)
20
+ @@project_name = name
21
+ end
22
+ def self.project_name
23
+ @@project_name
24
+ end
25
+
26
+ # configure api_key required to authorize api access
27
+ @@api_key = ''
28
+ def self.api_key=(key)
29
+ @@api_key = key
30
+ end
31
+ def self.api_key
32
+ @@api_key
33
+ end
34
+
35
+ # configure js driver which is used for taking screenshots.
36
+ @@javascript_driver = "poltergeist"
37
+ def self.javascript_driver=(driver)
38
+ @@javascript_driver = driver
39
+ end
40
+ def self.javascript_driver
41
+ @@javascript_driver
42
+ end
43
+
44
+ # configure service to be mocked so that no screenshots are
45
+ # taken, and uploaded to service.
46
+ @@enable_service = false
47
+ def self.enable_service=(enable)
48
+ @@enable_service = enable
49
+ end
50
+ def self.enable_service
51
+ @@enable_service
52
+ end
53
+
54
+ # configure logger, which will be used to log issues if any
55
+ @@logger = Logger.new(STDOUT)
56
+ def self.logger=(new_logger)
57
+ @@logger = new_logger
58
+ end
59
+ def self.logger
60
+ @@logger
61
+ end
62
+
63
+ # helper to configure above variables.
64
+ def self.configure
65
+ yield(self)
66
+ end
67
+
68
+ # helps in setting up the run
69
+ def self.start_run
70
+ Diffity::Dsl.diffity.start_run
71
+ end
72
+
73
+ # helps in wrapping up run by uploading images
74
+ def self.wrap_run
75
+ Diffity::Dsl.diffity.wrap_run
76
+ end
77
+ end
@@ -0,0 +1,22 @@
1
+ module Diffity
2
+ module Dsl
3
+ def self.diffity
4
+ @diffity ||=
5
+ begin
6
+ klass =
7
+ if Diffity.enable_service
8
+ Diffity::Runner
9
+ else
10
+ Diffity::DummyRunner
11
+ end
12
+
13
+ Diffity.logger.info "Using runner #{klass}"
14
+ klass.instance
15
+ end
16
+ end
17
+
18
+ def diffity
19
+ Diffity::Dsl.diffity
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module Diffity
2
+ class DummyRunner
3
+ def self.instance
4
+ @runner ||= DummyRunner.new
5
+ end
6
+
7
+ attr_accessor :browser, :device, :os
8
+ attr_accessor :browser_version, :device_name, :os_version
9
+
10
+ def start_run
11
+ end
12
+
13
+ def wrap_run
14
+ end
15
+
16
+ def screenshot(_)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,55 @@
1
+ module Diffity
2
+ class RunDetails
3
+ class Jenkins
4
+ def branch
5
+ ENV.fetch('GIT_BRANCH').split('/').last
6
+ end
7
+
8
+ def author
9
+ 'Jenkins'.freeze
10
+ end
11
+ end
12
+
13
+ class Travis
14
+ def branch
15
+ ENV.fetch('TRAVIS_BRANCH')
16
+ end
17
+
18
+ def author
19
+ 'Travis'.freeze
20
+ end
21
+ end
22
+
23
+ class GitRepo
24
+ def branch
25
+ `git rev-parse --abbrev-ref HEAD`.strip
26
+ end
27
+
28
+ def author
29
+ `git config user.name`.strip
30
+ end
31
+ end
32
+
33
+ class Default
34
+ def branch
35
+ 'HEAD'.freeze
36
+ end
37
+
38
+ def author
39
+ 'None'.freeze
40
+ end
41
+ end
42
+
43
+ def details
44
+ if !!ENV['JENKINS_HOME']
45
+ Jenkins.new
46
+ elsif !!ENV['TRAVIS']
47
+ Travis.new
48
+ elsif system('git branch')
49
+ GitRepo.new
50
+ else
51
+ Default.new
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,87 @@
1
+ require 'time'
2
+ require 'json'
3
+ require 'diffity/run_details'
4
+ require 'diffity/uploader'
5
+ require 'diffity/utils'
6
+
7
+ module Diffity
8
+ class Runner
9
+ include Capybara::DSL
10
+
11
+ def self.instance
12
+ @runner ||= Runner.new(Diffity.project_name,
13
+ Diffity.javascript_driver)
14
+ end
15
+
16
+ attr_accessor :browser, :device, :os
17
+ attr_accessor :browser_version, :device_name, :os_version
18
+
19
+ def initialize(project_name, javascript_driver)
20
+ @project_name = project_name
21
+ @javascript_driver = javascript_driver
22
+
23
+ dir = Diffity::Utils.images_dir
24
+ Dir.mkdir('tmp') unless Dir.exist?('tmp')
25
+ Dir.mkdir(dir) unless Dir.exist?(dir)
26
+
27
+ self.browser = 'firefox'
28
+ self.device = 'desktop'
29
+ self.os = 'linux'
30
+ end
31
+
32
+ # TODO: Improve error handling here for network timeouts
33
+ def start_run
34
+ draft_run
35
+ @uploader = Diffity::Uploader.build(@run_id)
36
+ rescue StandardError => e
37
+ Diffity.logger.fatal e.message
38
+ raise e
39
+ end
40
+
41
+ # TODO: Improve error handling here for network timeouts
42
+ def wrap_run
43
+ @uploader.wrapup
44
+
45
+ complete_run if @run_id
46
+ rescue StandardError => e
47
+ Diffity.logger.fatal e.message
48
+ raise e
49
+ end
50
+
51
+ def screenshot(identifier)
52
+ raise 'no browser information provided' if browser.nil?
53
+ raise 'no device information provided' if device.nil?
54
+ raise 'no os information provided' if os.nil?
55
+
56
+ screenshot_name = Diffity::Utils.image_file(identifier)
57
+ page.save_screenshot(screenshot_name, full: true)
58
+ @uploader.enqueue(identifier, browser, device, os, browser_version,
59
+ device_name, os_version)
60
+ end
61
+
62
+ private
63
+
64
+ def draft_run
65
+ run_name = @project_name + "-" + Time.now.iso8601
66
+
67
+ details = Diffity::RunDetails.new.details
68
+ branch = details.branch
69
+ author = details.author
70
+ project = @project_name
71
+
72
+ response = connection.post('/api/v1/runs',
73
+ name: run_name, project: project, group: branch,
74
+ author: author, js_driver: @javascript_driver)
75
+
76
+ @run_id = JSON.parse(response.body)["id"]
77
+ end
78
+
79
+ def complete_run
80
+ connection.put("/api/v1/runs/#{@run_id}/status", status: "completed")
81
+ end
82
+
83
+ def connection
84
+ @connection ||= Diffity::Utils.connection
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,13 @@
1
+ module Diffity
2
+ class Uploader
3
+ def self.build(run_id)
4
+ if defined?(::Concurrent)
5
+ require 'diffity/uploaders/concurrent'
6
+ Diffity::Uploaders::Concurrent.new(run_id)
7
+ else
8
+ require 'diffity/uploaders/sequential'
9
+ Diffity::Uploaders::Sequential.new(run_id)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,41 @@
1
+ require 'diffity/utils'
2
+
3
+ module Diffity
4
+ module Uploaders
5
+ class Concurrent
6
+ MAX_NO_OF_THREADS = 20
7
+
8
+ def initialize(run_id)
9
+ @run_id = run_id
10
+ @pool = ::Concurrent::FixedThreadPool.new(MAX_NO_OF_THREADS)
11
+ @screenshots_taken = 0
12
+ end
13
+
14
+ def enqueue(identifier, browser, device, os, browser_version,
15
+ device_name, os_version)
16
+ @screenshots_taken += 1
17
+
18
+ @pool.post do
19
+ Diffity::Utils
20
+ .upload_image(@run_id, identifier, browser, device, os,
21
+ browser_version, device_name, os_version)
22
+ end
23
+ end
24
+
25
+ def wrapup
26
+ retries = 180 # 30 mins
27
+
28
+ # if all screenshots are not uploaded, wait.
29
+ until @screenshots_taken == @pool.completed_task_count
30
+ retries -= 1
31
+ break if retries.zero?
32
+
33
+ sleep 10
34
+ end
35
+
36
+ @pool.shutdown
37
+ @pool.wait_for_termination
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,28 @@
1
+ require 'diffity/utils'
2
+
3
+ module Diffity
4
+ module Uploaders
5
+ class Sequential
6
+ def initialize(run_id)
7
+ @run_id = run_id
8
+ @identifiers_with_env = []
9
+ end
10
+
11
+ def enqueue(identifier, browser, device, os, browser_version,
12
+ device_name, os_version)
13
+ @identifiers_with_env << [identifier, browser, device, os,
14
+ browser_version, device_name,
15
+ os_version]
16
+ end
17
+
18
+ def wrapup
19
+ @identifiers_with_env
20
+ .each do |identifier, browser, device, os, browser_version, device_name, os_version|
21
+ Diffity::Utils
22
+ .upload_image(@run_id, identifier, browser, device, os,
23
+ browser_version, device_name, os_version)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ module Diffity
2
+ module Utils
3
+ # http connection that will be used for uploading images
4
+ def self.connection
5
+ base_uri = Diffity.base_uri
6
+ Faraday.new(base_uri, request: { timeout: 120, open_timeout: 120 }) do |f|
7
+ f.request :basic_auth, Diffity.api_key, 'X'
8
+ f.request :multipart
9
+ f.request :url_encoded
10
+ f.adapter :net_http
11
+ end
12
+ end
13
+
14
+ def self.images_dir
15
+ 'tmp/diffity'.freeze
16
+ end
17
+
18
+ def self.upload_image(run_id, identifier, browser, device, os, browser_version,
19
+ device_name, os_version)
20
+ Diffity.logger.fatal "uploading #{identifier}"
21
+ image_io = Faraday::UploadIO.new(image_file(identifier), 'image/png')
22
+ connection.post("/api/v1/runs/#{run_id}/run_images",
23
+ identifier: identifier, image: image_io, browser: browser,
24
+ device: device, os: os, browser_version: browser_version,
25
+ device_name: device_name, os_version: os_version)
26
+ end
27
+
28
+ def self.image_file(identifier)
29
+ "#{Dir.pwd}/#{images_dir}/#{identifier}.png"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Diffity
2
+ VERSION = "0.3.0"
3
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diffity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Rails integration for diffity service
28
+ email:
29
+ - yuva@codemancers.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - Rakefile
36
+ - Readme.md
37
+ - lib/diffity.rb
38
+ - lib/diffity/dsl.rb
39
+ - lib/diffity/dummy_runner.rb
40
+ - lib/diffity/run_details.rb
41
+ - lib/diffity/runner.rb
42
+ - lib/diffity/uploader.rb
43
+ - lib/diffity/uploaders/concurrent.rb
44
+ - lib/diffity/uploaders/sequential.rb
45
+ - lib/diffity/utils.rb
46
+ - lib/diffity/version.rb
47
+ homepage: http://diffity.com
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.7.3
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Rails integration for diffity service
71
+ test_files: []