Class: BrowserStackCucumber::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



9
10
11
# File 'lib/browserstack/browserstack.rb', line 9

def browser
  @browser
end

Class Method Details

._scenario_and_feature_name(scenario) ⇒ Object



32
33
34
35
36
# File 'lib/browserstack/browserstack.rb', line 32

def self._scenario_and_feature_name(scenario)
  scenario_name = scenario.name.split("\n").first
  feature_name = scenario.feature.short_name
  return scenario_name, feature_name
end

.browserObject



195
196
197
# File 'lib/browserstack/browserstack.rb', line 195

def self.browser
  @browser
end

.call_api(url_part) ⇒ Object



170
171
172
173
174
# File 'lib/browserstack/browserstack.rb', line 170

def self.call_api(url_part)
  url = "https://#{selenium_username}:#{selenium_apikey}@www.browserstack.com/automate/#{url_part}"
  r = RestClient.get(url)
  JSON.parse(r.body)
end

.capabilitiesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/browserstack/browserstack.rb', line 72

def self.capabilities

  capabilities = ::Selenium::WebDriver::Remote::Capabilities.new
  capabilities[:name] = 'Testing Selenium 2 with Ruby on BrowserStack'
  capabilities['os'] = selenium_os
  capabilities['os_version'] = selenium_os_version
  capabilities['browser'] = selenium_browser
  capabilities['browser_version'] = selenium_browser_version
  capabilities['browserstack.user'] = selenium_username
  capabilities['browserstack.key'] = selenium_apikey
  capabilities['browserstack.debug'] = 'true'
  capabilities['acceptSslCerts'] = 'true'
  capabilities['Idle timeout'] = 30
  capabilities['build'] = ENV['BUILD_NUMBER']
  capabilities['project'] = ENV['JOB_NAME']
  capabilities
end

.close_browserObject



199
200
201
202
# File 'lib/browserstack/browserstack.rb', line 199

def self.close_browser
  @browser.quit if !@browser.nil?
  @browser = nil
end

.close_browser_forceObject



204
205
206
207
208
209
210
211
# File 'lib/browserstack/browserstack.rb', line 204

def self.close_browser_force
  unless @browser.nil?
    puts @browser.title
    STDERR.puts 'Something went wrong and selenium session was not closed. Closing it now.'
    ::BrowserStackCucumber::Config.close_browser
  end

end

.get_session_info(session_id) ⇒ Object



183
184
185
# File 'lib/browserstack/browserstack.rb', line 183

def self.get_session_info(session_id)
  call_api("sessions/#{session_id}.json")
end

.get_session_limitsObject



164
165
166
167
168
# File 'lib/browserstack/browserstack.rb', line 164

def self.get_session_limits
  url = "https://#{selenium_username}:#{selenium_apikey}@api.browserstack.com/3/status"
  r = RestClient.get(url)
  JSON.parse(r.body)
end

.init_browser(scenario) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
# File 'lib/browserstack/browserstack.rb', line 94

def self.init_browser(scenario)
  my_capabilities = capabilities()
  my_url = url()
  my_capabilities[:name] = name_from_scenario(scenario)
  @browser = nil
  count=0
  try_count=0

  #check for session limit before starting the test
  wait_till_session_is_available()

  WaitUntil::wait_until(500) do
    count+=1
    puts "try #{count} to init browser" if count>1

    begin
      try_count+=1
      @browser = Selenium::WebDriver.for(:remote, :url => my_url, :desired_capabilities => my_capabilities)
      @browser.get @test_url
      raise "Network connection issue. Failed to open #{@test_url} and find '#{@test_substring}' substring" if !@browser.page_source.downcase.include? @test_substring
      @browser
    rescue => e
      if e.message.include? 'sessions are currently being used. Please upgrade to add more parallel sessions'
        puts "Run out of sessions: '#{e.message}'"
      else
        puts "Exception while initializing Selenium session: #{e.class }#{e}"
        puts e.backtrace
        @browser.quit if !@browser.nil?
        raise if try_count>30
      end
    end
  end

  if @browser.nil?
    puts 'failed to init browser'
    raise 'failed to initiate remote browser session after 300 seconds'
  end

  unless @browser.is_a? ::Selenium::WebDriver::Driver
    puts 'invalid browser'
    raise 'invalid browser'
  end

  @browser.instance_variable_get(:'@bridge').maximizeWindow
  #puts my_capabilities['name']
  #puts @browser.session_id
  ENV['browser_session_id'] = @browser.session_id
  @last_session_id = @browser.session_id
  ENV['browser_session_url'] = session_url()
  @browser
end

.name_from_scenario(scenario) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/browserstack/browserstack.rb', line 21

def self.name_from_scenario(scenario)
  # Special behavior to handle Scenario Outlines
  if scenario.instance_of? ::Cucumber::Ast::OutlineTable::ExampleRow
    table = scenario.instance_variable_get(:@table)
    outline = table.instance_variable_get(:@scenario_outline)
    return "#{outline.feature.file} - #{outline.title} - #{table.headers} -> #{scenario.name}"
  end
  scenario, feature = _scenario_and_feature_name(scenario)
  return "#{feature} - #{scenario}"
end

.put_api(url_part, payload) ⇒ Object



176
177
178
179
180
# File 'lib/browserstack/browserstack.rb', line 176

def self.put_api(url_part, payload)
  url = "https://#{selenium_username}:#{selenium_apikey}@www.browserstack.com/automate/#{url_part}"
  r = RestClient.put(url, payload.to_json, :content_type => :json)
  JSON.parse(r.body)
end

.selenium_apikeyObject



42
43
44
# File 'lib/browserstack/browserstack.rb', line 42

def self.selenium_apikey
  ENV['BROWSER_STACK_API_KEY']
end

.selenium_browserObject



61
62
63
64
# File 'lib/browserstack/browserstack.rb', line 61

def self.selenium_browser
  #firefox
  ENV['SELENIUM_BROWSER']
end

.selenium_browser_versionObject



66
67
68
69
# File 'lib/browserstack/browserstack.rb', line 66

def self.selenium_browser_version
  #21
  ENV['SELENIUM_VERSION']
end

.selenium_osObject



46
47
48
49
50
# File 'lib/browserstack/browserstack.rb', line 46

def self.selenium_os
  #TODO: Windows 8 doesn't match BS
  #'Windows XP' => Windows
  ENV['SELENIUM_PLATFORM'].split(' ')[0]
end

.selenium_os_versionObject



52
53
54
55
56
57
58
59
# File 'lib/browserstack/browserstack.rb', line 52

def self.selenium_os_version
  #'Windows XP' => XP
  r = ENV['SELENIUM_PLATFORM'].split(' ')[1]
  return 'XP' if (r=='2003')
  return '7' if (r=='2008')
  return '8' if (r=='2012')
  r
end

.selenium_usernameObject



38
39
40
# File 'lib/browserstack/browserstack.rb', line 38

def self.selenium_username
  ENV['BROWSER_STACK_USER_NAME']
end

.session_urlObject



146
147
148
149
150
151
# File 'lib/browserstack/browserstack.rb', line 146

def self.session_url
  @session_url = get_session_info(@browser.session_id)
  @session_url['automation_session']['browser_url']
rescue => e
  return "failed to obtain session url #{e}"
end

.update_session_status(success) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/browserstack/browserstack.rb', line 187

def self.update_session_status(success)
  unless success
    put_api("sessions/#{@last_session_id}.json", {'status' => 'error'})
  end
rescue => e
  puts e
end

.urlObject



90
91
92
# File 'lib/browserstack/browserstack.rb', line 90

def self.url
  'http://hub.browserstack.com/wd/hub'
end

.wait_till_session_is_availableObject



153
154
155
156
157
158
159
160
161
162
# File 'lib/browserstack/browserstack.rb', line 153

def self.wait_till_session_is_available
  ::BrowserStackCucumber::WaitUntil::wait_until(500) do
    parsed_r = get_session_limits()
    puts "no free BrowserStack session available now#{parsed_r}" if (parsed_r['sessions_limit']==parsed_r['running_sessions'])
    parsed_r['sessions_limit']-parsed_r['running_sessions']>0
  end
rescue RestClient::Unauthorized => e
  puts "Error: Failed to access BrowserStack account, please check username and api key: #{e}"
  raise
end

Instance Method Details

#test_url=(url) ⇒ Object



17
18
19
# File 'lib/browserstack/browserstack.rb', line 17

def test_url=(url)
  @test_url = url
end