Class: Kontrast::PageRunner

Inherits:
Object
  • Object
show all
Includes:
ImageUploader
Defined in:
lib/kontrast/page_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImageUploader

#upload_images

Constructor Details

#initializePageRunner

Returns a new instance of PageRunner.



7
8
9
10
11
# File 'lib/kontrast/page_runner.rb', line 7

def initialize
    @diffs = {}
    @selenium_handler = nil
    @page_comparator = nil
end

Instance Attribute Details

#diffsObject (readonly)

Returns the value of attribute diffs.



5
6
7
# File 'lib/kontrast/page_runner.rb', line 5

def diffs
  @diffs
end

Instance Method Details

#parallel_run(suite, current_node) ⇒ Object

Runs tests, handles all image operations, creates manifest for current node



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kontrast/page_runner.rb', line 43

def parallel_run(suite, current_node)
    # Load test handlers
    @selenium_handler = SeleniumHandler.new
    @page_comparator = PageComparator.new

    # Run per-page tasks
    suite.each do |test|
        begin
            print "Processing #{test.name} @ #{test.width}... "

            # Run the browser and take screenshots
            @selenium_handler.run_comparison(test)

            # Compare images
            print "Diffing... "
            @page_comparator.diff(test)

            # Create thumbnails for gallery
            print "Creating thumbnails... "
            ThumbnailCreator.create_thumbnails(test, ['test.png', 'production.png', 'diff.png'])

            # Upload to S3
            if Kontrast.configuration.run_parallel
                print "Uploading... "
                upload_images(test)
            end

            puts "\n", ("=" * 85)
        rescue Net::ReadTimeout => e
            puts "Test timed out. Message: #{e.inspect}"
            if Kontrast.configuration.fail_build
                raise e
            end
        rescue StandardError => e
            puts "Exception: #{e.inspect}"
            puts e.backtrace.inspect
            if Kontrast.configuration.fail_build
                raise e
            end
        end
    end
ensure
    # Log diffs
    puts @page_comparator.diffs

    # We need the diff at the runner level to create the manifest
    @diffs = @page_comparator.diffs

    @selenium_handler.cleanup
end

#run(total_nodes, current_node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kontrast/page_runner.rb', line 13

def run(total_nodes, current_node)
    # Nothing to run
    return if Kontrast.page_test_suite.nil?

    # Load & bind specs
    Kontrast.page_test_suite.bind_specs

    # Assign tests and run them
    suite = split_run(total_nodes, current_node)
    parallel_run(suite, current_node)
end

#split_run(total_nodes, current_node) ⇒ Object

Given the total number of nodes and the index of the current node, we determine which tests the current node will run



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kontrast/page_runner.rb', line 27

def split_run(total_nodes, current_node)
    test_suite = Kontrast.page_test_suite
    tests_to_run = []

    index = 0
    test_suite.tests.each do |test|
        if index % total_nodes == current_node
            tests_to_run << test
        end
        index += 1
    end

    return tests_to_run
end