Class: LoaderDetector::Detector
- Inherits:
-
Object
- Object
- LoaderDetector::Detector
- Defined in:
- lib/loader_detector.rb
Defined Under Namespace
Classes: IDnotSetError, ThresholdNegativeError
Constant Summary collapse
- @@logger =
Logger.new(STDOUT)
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
Instance Method Summary collapse
-
#initialize(driver, threshold = 0, frame_count = 10, timeout = 10) ⇒ Detector
constructor
Sets the id of the window of which the program will take screenshots and creates the two initial screenshots.
-
#wait_until_content_loaded ⇒ Object
Checks if a website has finished loading already.
Constructor Details
#initialize(driver, threshold = 0, frame_count = 10, timeout = 10) ⇒ Detector
Sets the id of the window of which the program will take screenshots and creates the two initial screenshots.
31 32 33 34 35 36 37 38 39 |
# File 'lib/loader_detector.rb', line 31 def initialize( driver, threshold = 0, frame_count = 10, timeout = 10 ) raise IDnotSetError.new if id == "" raise ThresholdNegativeError.new if threshold < 0 @driver = driver @threshold = threshold @frame_count = frame_count @timeout = timeout end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
20 21 22 |
# File 'lib/loader_detector.rb', line 20 def id @id end |
#threshold ⇒ Object (readonly)
Returns the value of attribute threshold.
20 21 22 |
# File 'lib/loader_detector.rb', line 20 def threshold @threshold end |
Instance Method Details
#wait_until_content_loaded ⇒ Object
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/loader_detector.rb', line 42 def wait_until_content_loaded imagefile1 = Tempfile.new(['image1', '.pnm']) imagefile2 = Tempfile.new(['image2', '.pnm']) @driver.pam_screenshot_file(imagefile1.path) count = 0 t0 = Time.now while Time.now - t0 < @timeout do @driver.pam_screenshot_file(imagefile2.path) count = difference_detected?(imagefile1, imagefile2) ? 0 : count + 1 begin File.rename(imagefile2.path, imagefile1.path) rescue Errno::ENOENT logger.warn("Can't rename imagefile2") end if count >= @frame_count yield if block_given? return true end end logger.warn("check_loading timed out. Couldn't detect a loaded website.") return false end |