browserly 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f53fa92854c720723ff9cffdbb2e082b0119319b0e1be774d7c1fb8935c48d5c
4
- data.tar.gz: 5b7e46847076cde7def8f31aebd8088e9d1eb372d855b0e40876fe3d947e35b8
3
+ metadata.gz: 864ffcdb27ce74199ab63cacc930eff3581253a1fabfbcbf8c98310eeeff2fbe
4
+ data.tar.gz: c10ed260d1d352e36b603226fd955b13850f009ce9205b0f20f38a240327a2fc
5
5
  SHA512:
6
- metadata.gz: 312c8944b2adaf49f4e957c19037a088d664a6d57fce281447bf9be861e781940a0d88d01c8958616bb11c7d3a468107fcb843874dd3748f5d000c3b42172498
7
- data.tar.gz: ba7099286f393e71685fd43112c8df6a9508a6ace23098d84fa254cc2750a288c062c3407c00f73027f238d924318e2ac0179a088a7193578a6b81fc0e9f52b8
6
+ metadata.gz: 941e32fcf720d90e5caa20013c84b2547d4206706853b82ee4ec1928983bb952eb39bbb31a0ae6159682fbfe2486c20b1448e76f5ec131a5769aa2239f1ba37f
7
+ data.tar.gz: 3903c22df8d2df3f215396bb43f9c712a79d6d580b1aecc7eceaf1db0bbe2f216be34c2a8838e3362d564ca73aa5091762fd79c4ef63d861b62fe13ca0bdba32
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # Browserly
2
- Is a simple tool that helps to create website screenshots using selenium + chrome driver. This gem creates pool of browsers thus increases speed of taking webpage screenshot.
2
+ Is a simple tool that helps to create website screenshots using selenium + chrome driver. It also can work with remote selenium webdriver.
3
3
 
4
4
  ## Installation
5
+
5
6
  Add this line to your application's Gemfile:
6
7
 
7
8
  ```ruby
@@ -22,8 +23,21 @@ $ gem install browserly
22
23
 
23
24
  ```ruby
24
25
  Browserly.configure do |config|
25
- config.tmp_path = 'path/to/store/screenshots'
26
- config.pool_size = 1 # count of browsers instance to start
26
+ config.output_dir = Rails.root.join('tmp', 'screenshots')
27
+ config.width = 1280
28
+ config.height = 780
29
+ config.remote_driver = true # default
30
+ config.remote_driver_url = "http://localhost:4444/wd/hub"
31
+ config.chrome_args = [
32
+ '--headless',
33
+ '--hide-scrollbars',
34
+ '--ignore-certificate-errors',
35
+ '--disable-popup-blocking',
36
+ '--disable-translate',
37
+ "--window-size=#{config.width},#{config.height}",
38
+ '--enable-font-antialiasing',
39
+ '--font-cache-shared-handle[6]'
40
+ ]
27
41
  end
28
42
  ```
29
43
 
@@ -34,8 +48,22 @@ screenshot = Browserly::Screenshot.new('http://example.com').perform
34
48
  screenshot.file #=> screenshot image file
35
49
  ```
36
50
 
51
+ Also you can pass DOM ID as second parameter to get it height on the page. This height will be used as screenshot height.
52
+
53
+ ```ruby
54
+ screenshot = Browserly::Screenshot.new('http://example.com', 'DOM_ID').perform
55
+ ```
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to rubygems.org.
62
+
37
63
  ## Contributing
38
- Contribution directions go here.
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/softeamco/browserly. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
39
66
 
40
67
  ## License
68
+
41
69
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -1,8 +1,8 @@
1
- require 'singleton'
2
1
  require 'comandor'
3
2
  require 'selenium-webdriver'
3
+ require 'browserly/errors/configuration_error'
4
4
  require 'browserly/configuration'
5
- require 'browserly/pool'
5
+ require 'browserly/driver'
6
6
  require 'browserly/screenshot'
7
7
  require 'browserly/version'
8
8
 
@@ -1,9 +1,33 @@
1
1
  module Browserly
2
2
  class Configuration
3
- attr_accessor :tmp_path
3
+ attr_accessor :output_dir,
4
+ :width,
5
+ :height,
6
+ :remote_driver,
7
+ :remote_driver_url,
8
+ :chrome_args
4
9
 
5
10
  def initialize
6
- @tmp_path = '../../tmp/screenshots'
11
+ @output_dir = File.join(Dir.tmpdir, 'siter_screenshots')
12
+
13
+ unless File.directory?(@output_dir)
14
+ FileUtils.mkdir_p(@output_dir)
15
+ end
16
+
17
+ @width = 1280
18
+ @height = 780
19
+ @remote_driver = true
20
+ @remote_driver_url = "http://localhost:4444/wd/hub"
21
+ @chrome_args = [
22
+ '--headless',
23
+ '--hide-scrollbars',
24
+ '--ignore-certificate-errors',
25
+ '--disable-popup-blocking',
26
+ '--disable-translate',
27
+ "--window-size=#{@width},#{@height}",
28
+ '--enable-font-antialiasing',
29
+ '--font-cache-shared-handle[6]'
30
+ ]
7
31
  end
8
32
  end
9
33
  end
@@ -0,0 +1,33 @@
1
+ module Browserly
2
+ class Driver
3
+ attr_reader :chrome_args, :driver
4
+
5
+ def initialize(remote, chrome_args)
6
+ @remote = remote
7
+ @chrome_args = chrome_args
8
+
9
+ return remote_driver if remote
10
+ local_driver
11
+ end
12
+
13
+ private
14
+
15
+ def local_driver
16
+ @driver = Selenium::WebDriver.for :chrome, options: options
17
+ end
18
+
19
+ def remote_driver
20
+ raise ConfigurationError, :remote_driver_url unless Browserly.configuration.remote_driver_url
21
+
22
+ caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => options)
23
+
24
+ @driver = Selenium::WebDriver.for :remote,
25
+ url: Browserly.configuration.remote_driver_url,
26
+ desired_capabilities: caps
27
+ end
28
+
29
+ def options
30
+ { args: chrome_args }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ module Browserly
2
+ class ConfigurationError < StandardError
3
+ def message
4
+ "Config failed: #{super}"
5
+ end
6
+ end
7
+ end
@@ -3,36 +3,44 @@
3
3
  module Browserly
4
4
  class Screenshot
5
5
  extend Comandor
6
+ attr_reader :file
6
7
 
7
- attr_reader :browser, :pool, :file, :selector
8
-
9
- def initialize(url, selector = '')
8
+ def initialize(url, height_selector = '')
10
9
  @url = url
11
- @pool = Browserly::Pool.instance
12
- @browser = pool.take_browser
13
- @selector = selector
10
+ @height_selector = height_selector
14
11
  end
15
12
 
16
13
  def perform
17
- take_screenshot! && release_browser!
14
+ take! && close_session!
15
+ end
16
+
17
+ def driver
18
+ @driver ||= Browserly::Driver.new(Browserly.configuration.remote_driver, Browserly.configuration.chrome_args).driver
18
19
  end
19
20
 
20
21
  private
21
22
 
22
- def take_screenshot!
23
- browser.navigate.to @url
24
- height = browser.execute_script(js) || 780
25
- browser.manage.window.resize_to(1280, height)
26
- @file = browser.save_screenshot(filename)
23
+ def take!
24
+ driver.navigate.to @url
25
+ @height = driver.execute_script(js) unless @height_selector.nil? || @height_selector == ''
26
+ @height ||= Browserly.configuration.height
27
+ driver.manage.window.resize_to(Browserly.configuration.width, @height)
28
+ @file = driver.save_screenshot(filename)&.path
29
+ return true if File.exist?(@file)
30
+ error(:file, 'Unable to save file')
31
+ false
27
32
  end
28
33
 
29
- def release_browser!
30
- pool.release!(browser)
34
+ def close_session!
35
+ return true if driver.close && driver.quit
36
+ rescue => e
37
+ error(:session, e.message)
38
+ false
31
39
  end
32
40
 
33
41
  def js
34
42
  <<-JAVASCRIPT
35
- element = document.getElementById('#{selector}');
43
+ element = document.getElementById('#{@height_selector}');
36
44
 
37
45
  if(element != null) {
38
46
  return element.clientHeight;
@@ -43,7 +51,7 @@ module Browserly
43
51
  end
44
52
 
45
53
  def filename
46
- File.join(Browserly.configuration.tmp_path, "#{Time.now.to_i}.png")
54
+ File.join(Browserly.configuration.output_dir, "#{Time.now.to_i}.png")
47
55
  end
48
56
  end
49
57
  end
@@ -1,3 +1,3 @@
1
1
  module Browserly
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browserly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Selivanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-05 00:00:00.000000000 Z
11
+ date: 2018-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -28,30 +28,30 @@ dependencies:
28
28
  name: comandor
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.1.5
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.1.5
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: selenium-webdriver
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 3.141.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 3.141.0
55
55
  description: Website screenshot for Ruby uses Selenium with Chrome Driver
56
56
  email:
57
57
  - selivandex@gmail.com
@@ -64,7 +64,8 @@ files:
64
64
  - Rakefile
65
65
  - lib/browserly.rb
66
66
  - lib/browserly/configuration.rb
67
- - lib/browserly/pool.rb
67
+ - lib/browserly/driver.rb
68
+ - lib/browserly/errors/configuration_error.rb
68
69
  - lib/browserly/screenshot.rb
69
70
  - lib/browserly/version.rb
70
71
  homepage: http://github.com/softeamco/browserly
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Browserly
4
- class Pool
5
- include ::Singleton
6
- attr_reader :browsers
7
-
8
- def initialize
9
- @browsers = SizedQueue.new(Browserly.configuration.pool_size)
10
- Browserly.configuration.pool_size.times { @browsers.push(new_browser) }
11
- end
12
-
13
- def take_browser
14
- @browsers.pop
15
- end
16
-
17
- def release!(browser)
18
- @browsers.push(browser)
19
- end
20
-
21
- def terminate_all!
22
- @browsers.each(&:quit)
23
- end
24
-
25
- private
26
-
27
- def new_browser
28
- options = Selenium::WebDriver::Chrome::Options.new(
29
- args: [
30
- '--headless',
31
- '--hide-scrollbars',
32
- '--ignore-certificate-errors',
33
- '--disable-popup-blocking',
34
- '--disable-translate',
35
- '--window-size=1280,780',
36
- '--enable-font-antialiasing',
37
- '--font-cache-shared-handle[6]'
38
- ]
39
- )
40
- Selenium::WebDriver.for :chrome, options: options
41
- end
42
- end
43
- end