ui-delta 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f63d66c2d5cef64287cc988cf52a98b42d429ea7c7069cf1dc307962526e97eb
4
+ data.tar.gz: 453388fb69e832e4c1edd49dd2fd2c5e90e6e280530868db8740b48ea0b6c315
5
+ SHA512:
6
+ metadata.gz: 3a17e3efdac19225c5ed36e70048eee2a24d6134d067cc8fcc694e020012894eb7767f234cc9839f115ec1c141a483c6045de09d6f78177a44ea2c1ce26925ed
7
+ data.tar.gz: 8bf9d6b4bd7304bce8c44b69a6ae11c2182e6aa4a1660c6b82f35ff6400b2e17aabcfd701f6167e3dc4b0077fdbab3ae360d103a0194a3af4aff36dba3fa0451
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015-2024 Codemancers
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.
data/Rakefile ADDED
@@ -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 = 'UI Delta'
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
data/Readme.md ADDED
@@ -0,0 +1,124 @@
1
+ # UI Delta
2
+
3
+ Currently this supports only RSpec.
4
+
5
+ ### Installation
6
+
7
+ ```rb
8
+ gem 'ui-delta'
9
+ ```
10
+
11
+ ### Configuration
12
+
13
+ Include `ui-delta` 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
+ UiDelta.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["UI_DELTA_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["UI_DELTA_ENABLE"]
37
+
38
+ # configure logger to log messages. optional.
39
+ config.logger = Rails.logger
40
+ end
41
+ ```
42
+
43
+ After configuration, include `UiDelta::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 UiDelta::Dsl
50
+
51
+ config.before(:suite) do
52
+ UiDelta.start_run
53
+ end
54
+
55
+ config.after(:suite) do
56
+ UiDelta.wrap_run
57
+ end
58
+ end
59
+ ```
60
+
61
+ ### Usage
62
+
63
+ In your specs, simply use `ui-delta` 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 `ui-delta` helper while running specs. For eg:
82
+
83
+ ```rb
84
+ ui_delta.browser = 'firefox'
85
+ ui_delta.device = 'laptop'
86
+ ui_delta.os = 'osx'
87
+ ui_delta.browser_version = '46'
88
+ ui_delta.device_name = 'MBA'
89
+ ui_delta.os_version = '10.11.5'
90
+ ```
91
+
92
+ Also, `ui-delta` 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
+ ui_delta.browser = 'chrome'
104
+ ui_delta.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. `UiDelta.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 :)
data/lib/ui-delta.rb ADDED
@@ -0,0 +1 @@
1
+ require 'ui_delta'
@@ -0,0 +1,22 @@
1
+ module UiDelta
2
+ module Dsl
3
+ def self.ui_delta
4
+ @ui_delta ||=
5
+ begin
6
+ klass =
7
+ if UiDelta.enable_service
8
+ UiDelta::Runner
9
+ else
10
+ UiDelta::DummyRunner
11
+ end
12
+
13
+ UiDelta.logger.info "Using runner #{klass}"
14
+ klass.instance
15
+ end
16
+ end
17
+
18
+ def ui_delta
19
+ UiDelta::Dsl.ui_delta
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module UiDelta
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 UiDelta
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,95 @@
1
+ require 'time'
2
+ require 'json'
3
+ require 'ui_delta/run_details'
4
+ require 'ui_delta/uploader'
5
+ require 'ui_delta/utils'
6
+
7
+ module UiDelta
8
+ class Runner
9
+ include Capybara::DSL
10
+
11
+ def self.instance
12
+ @runner ||= Runner.new(UiDelta.project_name,
13
+ UiDelta.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 = UiDelta::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 = UiDelta::Uploader.build(@run_id)
36
+ rescue StandardError => e
37
+ UiDelta.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
+ UiDelta.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 = UiDelta::Utils.image_file(identifier)
57
+ # set windows size equal to page width and height
58
+ # TODO: may support portrait resolutions
59
+ # assuming the device is in landscape orientation
60
+ width = page.driver.execute_script('return document.body.parentNode.scrollWidth')
61
+ height = page.driver.execute_script('return document.body.parentNode.scrollHeight')
62
+ page.driver.browser.manage.window.resize_to(width, height)
63
+
64
+ # save the screenshot
65
+ page.save_screenshot(screenshot_name, full_page: true)
66
+ @uploader.enqueue(identifier, browser, device, os, browser_version,
67
+ device_name, os_version, width, height)
68
+ end
69
+
70
+ private
71
+
72
+ def draft_run
73
+ run_name = @project_name + "-" + Time.now.iso8601
74
+
75
+ details = Ui-Delta::RunDetails.new.details
76
+ branch = details.branch
77
+ author = details.author
78
+ project = @project_name
79
+
80
+ response = connection.post('/api/v1/runs',
81
+ name: run_name, project: project, group: branch,
82
+ author: author, js_driver: @javascript_driver)
83
+
84
+ @run_id = JSON.parse(response.body)["id"]
85
+ end
86
+
87
+ def complete_run
88
+ connection.put("/api/v1/runs/#{@run_id}/status", status: "completed")
89
+ end
90
+
91
+ def connection
92
+ @connection ||= Ui-Delta::Utils.connection
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,13 @@
1
+ module UiDelta
2
+ class Uploader
3
+ def self.build(run_id)
4
+ if defined?(::Concurrent)
5
+ require 'ui_delta/uploaders/concurrent'
6
+ UiDelta::Uploaders::Concurrent.new(run_id)
7
+ else
8
+ require 'ui_delta/uploaders/sequential'
9
+ UiDelta::Uploaders::Sequential.new(run_id)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,41 @@
1
+ require 'uidelta/utils'
2
+
3
+ module UiDelta
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, image_width, image_height)
16
+ @screenshots_taken += 1
17
+
18
+ @pool.post do
19
+ UiDelta::Utils
20
+ .upload_image(@run_id, identifier, browser, device, os,
21
+ browser_version, device_name, os_version, image_width, image_height)
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 'ui_delta/utils'
2
+
3
+ module UiDelta
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, image_width, image_height)
13
+ @identifiers_with_env << [identifier, browser, device, os,
14
+ browser_version, device_name,
15
+ os_version, image_width, image_height]
16
+ end
17
+
18
+ def wrapup
19
+ @identifiers_with_env
20
+ .each do |identifier, browser, device, os, browser_version, device_name, os_version, image_width, image_height|
21
+ UiDelta::Utils
22
+ .upload_image(@run_id, identifier, browser, device, os,
23
+ browser_version, device_name, os_version, image_width, image_height)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,49 @@
1
+ module UiDelta
2
+ module Utils
3
+ # http connection that will be used for uploading images
4
+ def self.connection
5
+ base_uri = UiDelta.base_uri
6
+ Faraday.new(base_uri, request: { timeout: 120, open_timeout: 120 }) do |f|
7
+ f.request :basic_auth, UiDelta.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/ui_delta'.freeze
16
+ end
17
+
18
+ def self.upload_image(run_id, identifier, browser, device, os, browser_version,
19
+ device_name, os_version, image_width, image_height)
20
+ image_content = File.open(image_file(identifier), 'rb').read
21
+ data = get_presigned_url(run_id, identifier)
22
+
23
+ # upload to S3 directly
24
+ resp = Faraday.put(data["presign_url"], image_content)
25
+ unless resp.status == 200
26
+ raise "Failed to complete UiDelta image [#{identifier}.png] upload"
27
+ end
28
+
29
+ # create the run image on UiDelta
30
+ connection.post("/api/v1/runs/#{run_id}/run_images",
31
+ image: data["image_url"], image_width: image_width, image_height: image_height,
32
+ identifier: identifier, browser: browser,
33
+ device: device, os: os, browser_version: browser_version,
34
+ device_name: device_name, os_version: os_version)
35
+ end
36
+
37
+ def self.image_file(identifier)
38
+ "#{Dir.pwd}/#{images_dir}/#{identifier}.png"
39
+ end
40
+
41
+ def self.get_presigned_url(run_id, identifier)
42
+ resp = connection.post(
43
+ "/api/v1/runs/#{run_id}/run_images/presigned-url",
44
+ filename: "#{identifier}.png"
45
+ )
46
+ JSON.parse resp.body
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module UiDelta
2
+ VERSION = "0.5.0"
3
+ end
data/lib/ui_delta.rb ADDED
@@ -0,0 +1,77 @@
1
+ require 'faraday'
2
+ require 'ui_delta/dummy_runner'
3
+ require 'ui_delta/runner'
4
+ require 'ui_delta/dsl'
5
+ require 'logger'
6
+
7
+ module UiDelta
8
+ # configure domain to which all images have to be uploaded.
9
+ @@base_uri = "https://ui-delta.fly.dev"
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
+ UiDelta::Dsl.ui_delta.start_run
71
+ end
72
+
73
+ # helps in wrapping up run by uploading images
74
+ def self.wrap_run
75
+ UiDelta::Dsl.ui_delta.wrap_run
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ui-delta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-15 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 ui-delta 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/ui-delta.rb
38
+ - lib/ui_delta.rb
39
+ - lib/ui_delta/dsl.rb
40
+ - lib/ui_delta/dummy_runner.rb
41
+ - lib/ui_delta/run_details.rb
42
+ - lib/ui_delta/runner.rb
43
+ - lib/ui_delta/uploader.rb
44
+ - lib/ui_delta/uploaders/concurrent.rb
45
+ - lib/ui_delta/uploaders/sequential.rb
46
+ - lib/ui_delta/utils.rb
47
+ - lib/ui_delta/version.rb
48
+ homepage: https://ui-delta.fly.dev
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.4.10
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Rails integration for ui-delta service
71
+ test_files: []