Class: LoaderDetector::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/loader_detector.rb

Defined Under Namespace

Classes: IDnotSetError, ThresholdNegativeError

Constant Summary collapse

@@logger =
Logger.new(STDOUT)

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • id (String)

    id of the window to be screenshotted

  • threshold (Integer) (defaults to: 0)

    threshold for the pixel difference between two screenshots. If the difference is below the threshold the website is assumed to be loaded.

  • frame_count (Integer) (defaults to: 10)

    number of times the pixel difference has to be below the threshold to assume that the website is loaded.

  • timeout (Integer) (defaults to: 10)

    number of seconds the program will wait for the website to load.

Raises:



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

#idObject (readonly)

Returns the value of attribute id.



20
21
22
# File 'lib/loader_detector.rb', line 20

def id
  @id
end

#thresholdObject (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_loadedObject

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