loader_detector 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4b9069f03bb3197d38a351062d1f35f90e2cba16b20a4e51ed25a288fb731973
4
+ data.tar.gz: 15b6a7a4bf4a8c26d57e17adf96c8cf3d4370f3d23e48b3c488d8f275819075a
5
+ SHA512:
6
+ metadata.gz: 71c8b3d62b4c07d1695fc959998fb02e7ed5f2eabfe17498c03164ebd77724249afeafbe06d6b572b74e5c05a6f09d2526c05ede6abd2737424c148abde40b88
7
+ data.tar.gz: f427e85564680415793fabccb826cdda10c9b2e7d7e288072f5ccfabca41d0e816cfcf1b873407e36231cb648686a50c802e750bd8d7d59a2ed7b8300060dc6c
@@ -0,0 +1,4 @@
1
+ require "mkmf"
2
+
3
+ $LIBS = '-lnetpbm'
4
+ create_makefile "loader_detector/loader_detector"
@@ -0,0 +1,117 @@
1
+ require "loader_detector/loader_detector"
2
+ require 'shotgun_ruby'
3
+
4
+
5
+ module LoaderDetector
6
+ class Detector
7
+ class IDnotSetError < StandardError
8
+ def initialize(msg="the id of the window to be screenshotted isn't set. Call initialize(id) first.")
9
+ super(msg)
10
+ end
11
+ end
12
+ class ThresholdNegativeError < StandardError
13
+ def initialize(msg="The pixel difference threshold is negative. Please set a positive value or use the default value (0)")
14
+ super(msg)
15
+ end
16
+ end
17
+
18
+ @@logger = Logger.new(STDOUT)
19
+
20
+ attr_reader :id, :threshold
21
+
22
+ ##
23
+ # Sets the id of the window of which the program will take screenshots and creates the two initial screenshots.
24
+ #
25
+ # @param id [String] id of the window to be screenshotted
26
+ # @param threshold [Integer] threshold for the pixel difference between two screenshots. If the difference is below the threshold the website is assumed to be loaded.
27
+ # @param frame_count [Integer] number of times the pixel difference has to be below the threshold to assume that the website is loaded.
28
+ # @param timeout [Integer] number of seconds the program will wait for the website to load.
29
+ # @raise [IDnotSetError] if the id of the window to be screenshotted isn't set
30
+ # @raise [ThresholdNegativeError] if the pixel difference threshold is negative
31
+ def initialize( driver, threshold = 0, frame_count = 10, timeout = 10 )
32
+ raise IDnotSetError.new if id == ""
33
+ raise ThresholdNegativeError.new if threshold < 0
34
+
35
+ @driver = driver
36
+ @threshold = threshold
37
+ @frame_count = frame_count
38
+ @timeout = timeout
39
+ end
40
+
41
+ # Checks if a website has finished loading already. Calls the comparison algorithm, if the pixel difference is below the threshold 10 times in a row it assumes that the website doesn't change anymore.
42
+ def wait_until_content_loaded
43
+ imagefile1 = Tempfile.new(['image1', '.pnm'])
44
+ imagefile2 = Tempfile.new(['image2', '.pnm'])
45
+
46
+ @driver.pam_screenshot_file(imagefile1.path)
47
+
48
+ count = 0
49
+ t0 = Time.now
50
+
51
+ while Time.now - t0 < @timeout do
52
+
53
+ @driver.pam_screenshot_file(imagefile2.path)
54
+
55
+ count = difference_detected?(imagefile1, imagefile2) ? 0 : count + 1
56
+
57
+ begin
58
+ File.rename(imagefile2.path, imagefile1.path)
59
+ rescue Errno::ENOENT
60
+ logger.warn("Can't rename imagefile2")
61
+ end
62
+
63
+ if count >= @frame_count
64
+ yield if block_given?
65
+ return true
66
+ end
67
+ end
68
+
69
+ logger.warn("check_loading timed out. Couldn't detect a loaded website.")
70
+
71
+ return false
72
+ end
73
+
74
+ private
75
+
76
+ #Compares two images. Takes screenshots and compares them using netpbm.
77
+ def difference_detected?(imagefile1, imagefile2)
78
+ pixeldiff = LoaderDetector.compare_pamfiles( imagefile1.path, imagefile2.path )
79
+
80
+ case pixeldiff
81
+ when -1
82
+ logger.warn("Window size changed")
83
+ return true
84
+ when -2
85
+ logger.error("Can't open image file")
86
+ return true
87
+ when -3
88
+ logger.error("Some image dimension = 0")
89
+ return true
90
+ end
91
+
92
+ return pixeldiff > @threshold
93
+ end
94
+
95
+ ##
96
+ # @return [Logger] Logger instance
97
+ def logger
98
+ @@logger
99
+ end
100
+ end
101
+
102
+ class ScreenshotDriver
103
+ def pam_screenshot_file( file_path )
104
+ raise 'not implemented'
105
+ end
106
+ end
107
+
108
+ class ShotgunRubyDriver < ScreenshotDriver
109
+ def initialize( window_id )
110
+ @window_id = window_id.strip
111
+ end
112
+
113
+ def pam_screenshot_file( file_path )
114
+ ShotgunRuby.screenshot( @window_id, file_path )
115
+ end
116
+ end
117
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: loader_detector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Moesicke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shotgun_ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.3
27
+ description: loader_detector was developed to efficiently detect changes on websites.
28
+ It does this by using fast screenshots and counting the pixel differences between
29
+ these screenshots. It can be used in automated test scenarios to determine whether,
30
+ for example, a loader is still running on the tested website or whether the loading
31
+ process has been completed.
32
+ email:
33
+ executables: []
34
+ extensions:
35
+ - ext/loader_detector/extconf.rb
36
+ extra_rdoc_files: []
37
+ files:
38
+ - ext/loader_detector/extconf.rb
39
+ - lib/loader_detector.rb
40
+ homepage: https://github.com/jm591/loader_detector
41
+ licenses: []
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ - ext
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.3.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.3.26
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Loader detection for automated website testing
63
+ test_files: []