screenshot 0.0.1 → 0.0.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
  SHA1:
3
- metadata.gz: 463f6b75a51edc417e5e3b3f4e6af7cf1b0991a8
4
- data.tar.gz: 55e25638ce41681938342f35b4285d3e1b5d6889
3
+ metadata.gz: 3da4c0f81e3a99c26ad33877274e7e160a096d9d
4
+ data.tar.gz: 0e36a57cd73ec600435f516682293297a2d5eafb
5
5
  SHA512:
6
- metadata.gz: 95483fa46626177a0502dea6d19974040d9f45d04f625eee3e23f8543870e6b034f989e3e066ee3edbcc801a6299c3ace14cb7097930320583cdc12977f4ee44
7
- data.tar.gz: bdb0d61c094336bb8d297050bb1cac0e17ed9b9163de16443bf941ca886de8ee50180afc02413b43754632de8e75b34415a1604ad0aa4955512047a1bb51bd72
6
+ metadata.gz: 543c4140553429600008c8f4723a12c4b2fca0f745d9cbb50fd351c735d0cbeae8c2706f4f68c7330df42d302851c7e89c26cbea981faa416794fde684c87ccd
7
+ data.tar.gz: 22efffd640d47a48e31663a344da57029586352646d4c14dcf378a41dea64c8d47d97f6480d1358a56610b972c7d2f82117456f570edcb16314a3326981c77cc
data/lib/screenshot.rb CHANGED
@@ -1,5 +1,106 @@
1
1
  require "screenshot/version"
2
2
 
3
3
  module Screenshot
4
- # Your code goes here...
4
+ def capture_screenshot(file_name, page_elements)
5
+ screenshot_directory = ENV['LANGUAGE_SCREENSHOT_PATH'] || 'screenshots'
6
+ FileUtils.mkdir_p screenshot_directory
7
+ screenshot_path = "#{screenshot_directory}/#{file_name}"
8
+
9
+ @browser.screenshot.save screenshot_path
10
+ crop_image screenshot_path, page_elements, nil
11
+ end
12
+
13
+ def zoom_browser(rate)
14
+ rate.abs.times do
15
+ direction = rate > 0 ? :add : :subtract
16
+ @browser.send_keys [:control, direction]
17
+ end
18
+ end
19
+
20
+ def crop_image(path, page_elements, offset_element)
21
+ if offset_element
22
+ offset_rectangle = coordinates_from_page_element(offset_element)
23
+ else
24
+ offset_rectangle = [0, 0, 0, 0]
25
+ end
26
+ rectangles = coordinates_from_page_elements(page_elements)
27
+ crop_rectangle = rectangle(rectangles, offset_rectangle)
28
+
29
+ top_left_x = crop_rectangle[0]
30
+ top_left_y = crop_rectangle[1]
31
+ width = crop_rectangle[2]
32
+ height = crop_rectangle[3]
33
+
34
+ require 'chunky_png'
35
+ image = ChunkyPNG::Image.from_file path
36
+
37
+ # It happens with some elements that an image goes off the screen a bit,
38
+ # and chunky_png fails when this happens
39
+ width = image.width - top_left_x if image.width < top_left_x + width
40
+
41
+ image.crop!(top_left_x, top_left_y, width, height)
42
+ image.save path
43
+ end
44
+
45
+ def self.rectangle(rectangles, offset_rectangle = [0, 0, 0, 0])
46
+ top_left_x, top_left_y = top_left_x_y rectangles
47
+ bottom_right_x, bottom_right_y = bottom_right_x_y rectangles
48
+
49
+ # Finding width and height
50
+ width = bottom_right_x - top_left_x
51
+ height = bottom_right_y - top_left_y
52
+
53
+ # We are calculating the offset co-ordinates
54
+ x_offset = offset_rectangle[0]
55
+ y_offset = offset_rectangle[1]
56
+
57
+ # The new rectangle is constructed with all the co-ordinates calculated above
58
+ [top_left_x + x_offset, top_left_y + y_offset, width, height]
59
+ end
60
+
61
+ def coordinates_from_page_elements(page_elements)
62
+ page_elements.collect do |page_element|
63
+ coordinates_from_page_element page_element
64
+ end
65
+ end
66
+
67
+ def coordinates_from_page_element(page_element)
68
+ [page_element.element.wd.location.x, page_element.element.wd.location.y, page_element.element.wd.size.width, page_element.element.wd.size.height]
69
+ end
70
+
71
+ def self.top_left_x_coordinates(input_rectangles)
72
+ input_rectangles.collect do |rectangle|
73
+ rectangle[0]
74
+ end
75
+ end
76
+
77
+ def self.top_left_y_coordinates(input_rectangles)
78
+ input_rectangles.collect do |rectangle|
79
+ rectangle[1]
80
+ end
81
+ end
82
+
83
+ def self.bottom_right_x_coordinates(input_rectangles)
84
+ input_rectangles.collect do |rectangle|
85
+ rectangle[0] + rectangle[2]
86
+ end
87
+ end
88
+
89
+ def self.bottom_right_y_coordinates(input_rectangles)
90
+ input_rectangles.collect do |rectangle|
91
+ rectangle[1] + rectangle[3]
92
+ end
93
+ end
94
+
95
+ def self.bottom_right_x_y(input_rectangles)
96
+ [bottom_right_x_coordinates(input_rectangles).max, bottom_right_y_coordinates(input_rectangles).max]
97
+ end
98
+
99
+ def self.top_left_x_y(input_rectangles)
100
+ [top_left_x_coordinates(input_rectangles).min, top_left_y_coordinates(input_rectangles).min]
101
+ end
102
+
103
+ def highlight(element, color = '#FF00FF')
104
+ @current_page.execute_script("arguments[0].style.border = 'thick solid #{color}'", element)
105
+ end
5
106
  end
@@ -1,3 +1,3 @@
1
1
  module Screenshot
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/screenshot.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", "~> 3.2.0"
25
26
  end
@@ -0,0 +1,204 @@
1
+ require_relative "../lib/screenshot.rb"
2
+
3
+ # Rectangle is defined as set of co-ordinates represented by top left x, top left y, width, height
4
+ describe 'Rectangle' do
5
+ it 'should return the co-ordinates of provided 1 rectangle' do
6
+ input_rectangle = [0, 0, 1, 1]
7
+ input_rectangles = [input_rectangle]
8
+ expect(Screenshot.rectangle(input_rectangles)).to eq(input_rectangle)
9
+ end
10
+
11
+ it 'should return the co-ordinates of the rectangle which is inside a iframe' do
12
+ input_rectangle = [50, 50, 10, 10]
13
+ iframe_rectangle = [100, 100, 20, 20]
14
+ input_rectangles = [input_rectangle]
15
+ output_rectangle = [150, 150, 10, 10]
16
+ expect(Screenshot.rectangle(input_rectangles, iframe_rectangle)).to eq(output_rectangle)
17
+ end
18
+
19
+ it 'if we provide 2 rectangles and if one contains the other then it should return co-ordinates of bigger rectangle' do
20
+ input_rectangle_1 = [0, 0, 1, 1]
21
+ input_rectangle_2 = [0, 0, 2, 2]
22
+ input_rectangles = [input_rectangle_1, input_rectangle_2]
23
+ expect(Screenshot.rectangle(input_rectangles)).to eq(input_rectangle_2)
24
+ end
25
+
26
+ it 'if we provide 2 rectangles it should return co-ordinates of third rectangle which contains both' do
27
+ input_rectangle_1 = [0, 0, 1, 1]
28
+ input_rectangle_2 = [1, 0, 1, 1]
29
+ input_rectangles_1 = [input_rectangle_1, input_rectangle_2]
30
+ output_rectangle_1 = [0, 0, 2, 1]
31
+ expect(Screenshot.rectangle(input_rectangles_1)).to eq(output_rectangle_1)
32
+
33
+ input_rectangle_3 = [1, 1, 1, 1]
34
+ input_rectangle_4 = [3, 3, 1, 1]
35
+ input_rectangles_2 = [input_rectangle_3, input_rectangle_4]
36
+ output_rectangle_2 = [1, 1, 3, 3]
37
+ expect(Screenshot.rectangle(input_rectangles_2)).to eq(output_rectangle_2)
38
+ end
39
+
40
+ it 'if we provide 3 rectangles it should return co-ordinates the rectangle which contains all the input rectangles' do
41
+ input_rectangle_1 = [1, 1, 1, 1]
42
+ input_rectangle_2 = [2, 2, 1, 1]
43
+ input_rectangle_3 = [3, 3, 1, 1]
44
+ input_rectangles = [input_rectangle_1, input_rectangle_2, input_rectangle_3]
45
+ output_rectangle_1 = [1, 1, 3, 3]
46
+ expect(Screenshot.rectangle(input_rectangles)).to eq(output_rectangle_1)
47
+ end
48
+ end
49
+
50
+ describe 'Calculate topleft co-ordinates' do
51
+ it 'if we provide 1 rectangle then it should return top left co-ordinates of the input rectangle' do
52
+ input_rectangle = [2, 2, 1, 1]
53
+ input_rectangles = [input_rectangle]
54
+ output_coordinates = [2, 2]
55
+ expect(Screenshot.top_left_x_y(input_rectangles)).to eq(output_coordinates)
56
+ end
57
+
58
+ it 'if we provide 2 rectangles then it should return top left co-ordinates of the biggest rectangle containing both rectangles' do
59
+ input_rectangle_1 = [1, 0, 1, 1]
60
+ input_rectangle_2 = [0, 0, 1, 1]
61
+ input_rectangles = [input_rectangle_1, input_rectangle_2]
62
+ output_coordinates = [0, 0]
63
+ expect(Screenshot.top_left_x_y(input_rectangles)).to eq(output_coordinates)
64
+ end
65
+
66
+ it 'if we provide 3 rectangles then it should return top left co-ordinates of the biggest rectangle containing both rectangles' do
67
+ input_rectangle_1 = [3, 3, 1, 1]
68
+ input_rectangle_2 = [2, 2, 1, 1]
69
+ input_rectangle_3 = [1, 1, 1, 1]
70
+ input_rectangles = [input_rectangle_1, input_rectangle_2, input_rectangle_3]
71
+ output_coordinates = [1, 1]
72
+ expect(Screenshot.top_left_x_y(input_rectangles)).to eq(output_coordinates)
73
+ end
74
+ end
75
+
76
+ describe 'Topleft co-ordinates x' do
77
+ it 'if we provide 1 rectangle then it should return array of top left x co-ordinate of the input rectangle' do
78
+ input_rectangle = [2, 2, 1, 1]
79
+ input_rectangles = [input_rectangle]
80
+ output_coordinates = [2]
81
+ expect(Screenshot.top_left_x_coordinates(input_rectangles)).to eq(output_coordinates)
82
+ end
83
+
84
+ it 'if we provide 2 rectangles then it should return array top left x co-ordinates' do
85
+ input_rectangle_1 = [0, 0, 1, 1]
86
+ input_rectangle_2 = [1, 0, 1, 1]
87
+ input_rectangles = [input_rectangle_1, input_rectangle_2]
88
+ output_coordinates = [0, 1]
89
+ expect(Screenshot.top_left_x_coordinates(input_rectangles)).to eq(output_coordinates)
90
+ end
91
+
92
+ it 'if we provide 3 rectangles then it should return array of top left x co-ordinates' do
93
+ input_rectangle_1 = [3, 3, 1, 1]
94
+ input_rectangle_2 = [2, 2, 1, 1]
95
+ input_rectangle_3 = [1, 1, 1, 1]
96
+ input_rectangles = [input_rectangle_1, input_rectangle_2, input_rectangle_3]
97
+ output_coordinates = [3, 2, 1]
98
+ expect(Screenshot.top_left_x_coordinates(input_rectangles)).to eq(output_coordinates)
99
+ end
100
+ end
101
+
102
+ describe 'Topleft co-ordinates y' do
103
+ it 'if we provide 1 rectangle then it should return array of top left y co-ordinate of the input rectangle' do
104
+ input_rectangle = [2, 2, 1, 1]
105
+ input_rectangles = [input_rectangle]
106
+ output_coordinates = [2]
107
+ expect(Screenshot.top_left_y_coordinates(input_rectangles)).to eq(output_coordinates)
108
+ end
109
+
110
+ it 'if we provide 2 rectangles then it should return array top left y co-ordinates' do
111
+ input_rectangle_1 = [0, 0, 1, 1]
112
+ input_rectangle_2 = [1, 0, 1, 1]
113
+ input_rectangles = [input_rectangle_1, input_rectangle_2]
114
+ output_coordinates = [0, 0]
115
+ expect(Screenshot.top_left_y_coordinates(input_rectangles)).to eq(output_coordinates)
116
+ end
117
+
118
+ it 'if we provide 3 rectangles then it should return array of top left y co-ordinates' do
119
+ input_rectangle_1 = [3, 3, 1, 1]
120
+ input_rectangle_2 = [2, 2, 1, 1]
121
+ input_rectangle_3 = [1, 1, 1, 1]
122
+ input_rectangles = [input_rectangle_1, input_rectangle_2, input_rectangle_3]
123
+ output_coordinates = [3, 2, 1]
124
+ expect(Screenshot.top_left_y_coordinates(input_rectangles)).to eq(output_coordinates)
125
+ end
126
+ end
127
+
128
+ describe 'Calculate bottomright co-ordinates' do
129
+ it 'if we provide 1 rectangle then it should return bottom right co-ordinates of the input rectangle' do
130
+ input_rectangle = [2, 2, 1, 1]
131
+ input_rectangles = [input_rectangle]
132
+ output_coordinates = [3, 3]
133
+ expect(Screenshot.bottom_right_x_y(input_rectangles)).to eq(output_coordinates)
134
+ end
135
+
136
+ it 'if we provide 2 rectangles then it should return bottom right co-ordinates of the biggest rectangle containing both rectangles' do
137
+ input_rectangle_1 = [1, 0, 1, 1]
138
+ input_rectangle_2 = [0, 0, 1, 1]
139
+ input_rectangles = [input_rectangle_1, input_rectangle_2]
140
+ output_coordinates = [2, 1]
141
+ expect(Screenshot.bottom_right_x_y(input_rectangles)).to eq(output_coordinates)
142
+ end
143
+
144
+ it 'if we provide 3 rectangles then it should return bottom right co-ordinates of the biggest rectangle containing both rectangles' do
145
+ input_rectangle_1 = [3, 3, 1, 1]
146
+ input_rectangle_2 = [2, 2, 1, 1]
147
+ input_rectangle_3 = [1, 1, 1, 1]
148
+ input_rectangles = [input_rectangle_1, input_rectangle_2, input_rectangle_3]
149
+ output_coordinates = [4, 4]
150
+ expect(Screenshot.bottom_right_x_y(input_rectangles)).to eq(output_coordinates)
151
+ end
152
+ end
153
+
154
+ describe 'Bottom right co-ordinates x' do
155
+ it 'if we provide 1 rectangle then it should return array of bottom right x co-ordinate of the input rectangle' do
156
+ input_rectangle = [2, 2, 1, 1]
157
+ input_rectangles = [input_rectangle]
158
+ output_coordinates = [3]
159
+ expect(Screenshot.bottom_right_x_coordinates(input_rectangles)).to eq(output_coordinates)
160
+ end
161
+
162
+ it 'if we provide 2 rectangles then it should return array bottom right x co-ordinates' do
163
+ input_rectangle_1 = [0, 0, 1, 1]
164
+ input_rectangle_2 = [1, 0, 1, 1]
165
+ input_rectangles = [input_rectangle_1, input_rectangle_2]
166
+ output_coordinates = [1, 2]
167
+ expect(Screenshot.bottom_right_x_coordinates(input_rectangles)).to eq(output_coordinates)
168
+ end
169
+
170
+ it 'if we provide 3 rectangles then it should return array of bottom right x co-ordinates' do
171
+ input_rectangle_1 = [3, 3, 1, 1]
172
+ input_rectangle_2 = [2, 2, 1, 1]
173
+ input_rectangle_3 = [1, 1, 1, 1]
174
+ input_rectangles = [input_rectangle_1, input_rectangle_2, input_rectangle_3]
175
+ output_coordinates = [4, 3, 2]
176
+ expect(Screenshot.bottom_right_x_coordinates(input_rectangles)).to eq(output_coordinates)
177
+ end
178
+ end
179
+
180
+ describe 'Bottom right co-ordinates y' do
181
+ it 'if we provide 1 rectangle then it should return array of bottom right y co-ordinate of the input rectangle' do
182
+ input_rectangle = [2, 2, 1, 1]
183
+ input_rectangles = [input_rectangle]
184
+ output_coordinates = [3]
185
+ expect(Screenshot.bottom_right_y_coordinates(input_rectangles)).to eq(output_coordinates)
186
+ end
187
+
188
+ it 'if we provide 2 rectangles then it should return array bottom right y co-ordinates' do
189
+ input_rectangle_1 = [0, 0, 1, 1]
190
+ input_rectangle_2 = [1, 0, 1, 1]
191
+ input_rectangles = [input_rectangle_1, input_rectangle_2]
192
+ output_coordinates = [1, 1]
193
+ expect(Screenshot.bottom_right_y_coordinates(input_rectangles)).to eq(output_coordinates)
194
+ end
195
+
196
+ it 'if we provide 3 rectangles then it should return array of bottom right y co-ordinates' do
197
+ input_rectangle_1 = [3, 3, 1, 1]
198
+ input_rectangle_2 = [2, 2, 1, 1]
199
+ input_rectangle_3 = [1, 1, 1, 1]
200
+ input_rectangles = [input_rectangle_1, input_rectangle_2, input_rectangle_3]
201
+ output_coordinates = [4, 3, 2]
202
+ expect(Screenshot.bottom_right_y_coordinates(input_rectangles)).to eq(output_coordinates)
203
+ end
204
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: screenshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vikas Yaligar
@@ -54,6 +54,20 @@ dependencies:
54
54
  - - '>='
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rspec
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: 3.2.0
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: 3.2.0
57
71
  description: A library for taking and cropping screenshots of web pages. It uses Selenium
58
72
  and chunky_png. It was originally built for taking screenshots in many languages
59
73
  for the translations of the Wikimedia VisualEditor user manual, but now it can be
@@ -72,6 +86,7 @@ files:
72
86
  - lib/screenshot.rb
73
87
  - lib/screenshot/version.rb
74
88
  - screenshot.gemspec
89
+ - spec/rectangle_spec.rb
75
90
  homepage: https://github.com/amire80/screenshot
76
91
  licenses:
77
92
  - MIT
@@ -96,4 +111,5 @@ rubygems_version: 2.0.3
96
111
  signing_key:
97
112
  specification_version: 4
98
113
  summary: A library for taking and cropping screenshots of web pages.
99
- test_files: []
114
+ test_files:
115
+ - spec/rectangle_spec.rb