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



30
31
32
33
34
# File 'lib/browserstack/browserstack.rb', line 30

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



154
155
156
# File 'lib/browserstack/browserstack.rb', line 154

def self.browser
  @browser
end

.capabilitiesObject



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

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



158
159
160
161
# File 'lib/browserstack/browserstack.rb', line 158

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

.close_browser_forceObject



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

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

.init_browser(scenario) ⇒ Object



91
92
93
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
# File 'lib/browserstack/browserstack.rb', line 91

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
  @browser
end

.name_from_scenario(scenario) ⇒ Object



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

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

.selenium_apikeyObject



40
41
42
# File 'lib/browserstack/browserstack.rb', line 40

def self.selenium_apikey
  ENV['BROWSER_STACK_API_KEY']
end

.selenium_browserObject



59
60
61
62
# File 'lib/browserstack/browserstack.rb', line 59

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

.selenium_browser_versionObject



64
65
66
67
# File 'lib/browserstack/browserstack.rb', line 64

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

.selenium_osObject



44
45
46
47
48
# File 'lib/browserstack/browserstack.rb', line 44

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

.selenium_os_versionObject



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

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



36
37
38
# File 'lib/browserstack/browserstack.rb', line 36

def self.selenium_username
  ENV['BROWSER_STACK_USER_NAME']
end

.urlObject



87
88
89
# File 'lib/browserstack/browserstack.rb', line 87

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

.wait_till_session_is_availableObject



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/browserstack/browserstack.rb', line 141

def self.wait_till_session_is_available
  ::BrowserStackCucumber::WaitUntil::wait_until(500) do
    url = "https://#{selenium_username}:#{selenium_apikey}@api.browserstack.com/3/status"
    r = RestClient.get(url)
    parsed_r = JSON.parse(r.body)
    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



15
16
17
# File 'lib/browserstack/browserstack.rb', line 15

def test_url=(url)
  @test_url = url
end