Class: SeleniumShots

Inherits:
ActionController::IntegrationTest
  • Object
show all
Defined in:
lib/selenium_shots/test_selenium_shots.rb

Constant Summary collapse

PICS_WINDOWS_PATH =
"Z:"
PICS_LINUX_PATH =
''
PICS_MACOS_PATH =
''
HOST =
"127.0.0.1"
PORT =
"4444"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



23
24
25
# File 'lib/selenium_shots/test_selenium_shots.rb', line 23

def agent
  @agent
end

#driverObject (readonly)

Returns the value of attribute driver.



23
24
25
# File 'lib/selenium_shots/test_selenium_shots.rb', line 23

def driver
  @driver
end

#take_screenshotObject (readonly)

Returns the value of attribute take_screenshot.



23
24
25
# File 'lib/selenium_shots/test_selenium_shots.rb', line 23

def take_screenshot
  @take_screenshot
end

Class Method Details

.core_test(description, take_screenshot = true, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/selenium_shots/test_selenium_shots.rb', line 57

def self.core_test(description, take_screenshot = true, &block)
  @@group = (@group || "Default")
  test_name = "test_#{description.gsub(/\s+/,'_')}".to_sym
  defined = instance_method(test_name) rescue false
  raise "#{test_name} is already defined in #{self}" if defined
  if block_given?
    define_method(test_name) do
      @description     = description
      @take_screenshot = take_screenshot
      run_in_all_browsers do
        instance_eval &block
      end
    end
  else
    define_method(test_name) do
      flunk "No implementation provided for #{name}"
    end
  end
end

.selenium_shot(description, &block) ⇒ Object



81
82
83
# File 'lib/selenium_shots/test_selenium_shots.rb', line 81

def self.selenium_shot(description, &block)
  core_test(description, true, &block)
end

.selenium_test(description, &block) ⇒ Object



77
78
79
# File 'lib/selenium_shots/test_selenium_shots.rb', line 77

def self.selenium_test(description, &block)
  core_test(description, nil, &block)
end

Instance Method Details

#capture_screenshot_on(src) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/selenium_shots/test_selenium_shots.rb', line 157

def capture_screenshot_on(src)
  browser.window_focus
  browser.window_maximize
  sleep(2)
  if @driver.browser.to_s.match(/XP/)
    @driver.capture_entire_page_screenshot("#{PICS_WINDOWS_PATH}\\#{src}", "background=#FFFFFF")
  elsif @driver.browser.to_s.match(/SnowLeopard/)
    @driver.capture_entire_page_screenshot("#{PICS_MACOS_PATH}/#{src}", "background=#FFFFFF")
  elsif @driver.browser.to_s.match(/Linux/)
    @driver.capture_entire_page_screenshot("#{PICS_LINUX_PATH}/#{src}", "background=#FFFFFF")
  end
end

#local_browsersObject



41
42
43
# File 'lib/selenium_shots/test_selenium_shots.rb', line 41

def local_browsers
  ["firefox", "ie", "chrome"]
end

#pid_fileObject



37
38
39
# File 'lib/selenium_shots/test_selenium_shots.rb', line 37

def pid_file
  "/tmp/selenium_shots.pid"
end

#run_in_all_browsers(&block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/selenium_shots/test_selenium_shots.rb', line 85

def run_in_all_browsers(&block)
  @error = nil
  browsers = (@selected_browser || selected_browsers)
  browsers.each do |browser_spec|
    begin
      run_webdriver(browser_spec, block)
    rescue  => error
      @driver.quit if @driver
      @error = error.message
      if @error.match(/Failed to start new browser session/) && SeleniumConfig.mode == "local"
        @tmp_browsers ||= local_browsers
        @tmp_browsers.delete(browser_spec)
        @selected_browser  = [@tmp_browsers.shift]
        unless @selected_browser.empty?
          puts "The browser #{browser_spec} is not available, selenium_shots going to try with #{@selected_browser} browser"
          run_in_all_browsers(&block)
        end
      end
    end
  end
  assert @error.nil?, "Expected zero failures or errors, but got #{@error}\n"
end

#run_webdriver(browser_spec, block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/selenium_shots/test_selenium_shots.rb', line 108

def run_webdriver(browser_spec, block)
  
  client = Selenium::WebDriver::Remote::Http::Default.new
  client.timeout = 20 # seconds
  
  if SeleniumConfig.mode == "local"
    if /(firefox)/i.match(browser_spec)
      profile = Selenium::WebDriver::Firefox::Profile.new
      profile.native_events = false
      @driver = Selenium::WebDriver.for(:firefox, :profile => profile, :http_client => client)
    elsif /(chrome)/i.match(browser_spec)
      @driver = Selenium::WebDriver.for(:chrome, :http_client => client)
    elsif /(ie)/i.match(browser_spec)
      @driver = Selenium::WebDriver.for(:ie, :http_client => client)
    end
  else
    caps = nil
    if /(firefox)/i.match(browser_spec)
      caps = WebDriver::Remote::Capabilities.firefox
    elsif /(chrome)/i.match(browser_spec)
      caps = WebDriver::Remote::Capabilities.chrome
    elsif /(ie)/i.match(browser_spec)
      caps = WebDriver::Remote::Capabilities.internet_explorer
    elsif /(safari)/i.match(browser_spec)
      caps = WebDriver::Remote::Capabilities.safari
    elsif /(htmlunit)/i.match(browser_spec)
      caps = WebDriver::Remote::Capabilities.htmlunit
      caps.javascript_enabled = true
    end

    @driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps, :http_client => client) if caps
  end
  
  @driver.manage.timeouts.implicit_wait = 2 #seconds
  @driver.navigate.to SeleniumConfig.default_browser_url

  begin
    block.call
  rescue  => error
    @error = error.message
    puts error.message
    puts error.backtrace
  ensure
    save_test({:selenium_test_group_name => @@group, :selenium_test_name => @name,
              :description => @description}) if SeleniumConfig.mode == "remote"
    @driver.quit
  end 
end

#save_test(params) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'lib/selenium_shots/test_selenium_shots.rb', line 170

def save_test(params)
  src = "#{SeleniumConfig.application_name}_#{params[:selenium_test_group_name]}_#{params[:selenium_test_name]}_" +
         "#{@driver.browser.to_s.gsub(/\s+/,"_").downcase}.png"

  capture_screenshot_on(src)

  SeleniumTest.create(:selenium_test_name => params[:selenium_test_name], :description => params[:description],
    :url => @driver.current_url, :error_message => @error, :is_error => !@error.nil?, :environment => @driver.browser.to_s,
    :selenium_test_group_name => params[:selenium_test_group_name], :application_name => SeleniumConfig.application_name)
end

#selected_browsersObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/selenium_shots/test_selenium_shots.rb', line 45

def selected_browsers
  if SeleniumConfig.mode == "remote"
    SeleniumConfig.browsers
  else
    if defined?(SeleniumConfig.local_browser)
      [SeleniumConfig.local_browser]
   else
      [local_browsers.first]
   end
  end
end