Class: Kontrast::SeleniumHandler

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

Instance Method Summary collapse

Constructor Details

#initializeSeleniumHandler

Returns a new instance of SeleniumHandler.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kontrast/selenium_handler.rb', line 6

def initialize
    @path = Kontrast.path

    # Configure profile
    driver_name = Kontrast.configuration.browser_driver
    profile = Selenium::WebDriver.const_get(driver_name.capitalize)::Profile.new
    Kontrast.configuration.browser_profile.each do |option, value|
        profile[option] = value
    end

    # Get drivers with profile
    @test_driver = {
        name: "test",
        driver: Selenium::WebDriver.for(driver_name.to_sym, profile: profile)
    }
    @production_driver = {
        name: "production",
        driver: Selenium::WebDriver.for(driver_name.to_sym, profile: profile)
    }
end

Instance Method Details

#cleanupObject



27
28
29
30
31
32
# File 'lib/kontrast/selenium_handler.rb', line 27

def cleanup
    # Make sure windows are closed
    Workers.map([@test_driver, @production_driver]) do |driver|
        driver[:driver].quit
    end
end

#run_comparison(test) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kontrast/selenium_handler.rb', line 34

def run_comparison(test)
    # Create folder for this test
    current_output = FileUtils.mkdir_p("#{@path}/#{test}").join('')

    # Open test host tabs
    navigate(test.path)

    # Resize to given width and total height
    resize(test.width)

    screenshot_args = [@test_driver[:driver], @production_driver[:driver], { width: test.width, name: test.name }]

    # Take screenshot
    begin
        # Global callback
        Kontrast.configuration.before_screenshot(*screenshot_args)

        # Spec callback
        test.run_callback(:before_screenshot, *screenshot_args)

        screenshot(current_output)
    ensure
        # Global callback
        Kontrast.configuration.after_screenshot(*screenshot_args)

        # Spec callback
        test.run_callback(:after_screenshot, *screenshot_args)
    end
end