Class: CloudTest::Core

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

Constant Summary collapse

CONFIG_NAME =
'cloud_test'

Class Method Summary collapse

Class Method Details

.check_if_input_is_valid?(str) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/cloud_test/core.rb', line 17

def self.check_if_input_is_valid?(str)
  #for all relevant config user input only allow an optional '@' for the cucumber_tag, then some letters, followed by
  # optional digits
  # this should enhance security
  Regexp.new('^@?[A-z]+\d*$') =~ str
end

.copy_keys(caps, config, keys = config.keys) ⇒ Object

a small helper method, to copy keys



95
96
97
98
99
# File 'lib/cloud_test/core.rb', line 95

def self.copy_keys(caps, config, keys=config.keys) # a small helper method, to copy keys
  keys.each do |key|
    caps[key] = config[key]
  end
end

.get_default_capsObject



9
10
11
12
13
14
15
# File 'lib/cloud_test/core.rb', line 9

def self.get_default_caps
  @caps = Hash.new
  @caps['project'] = File.split(Dir.getwd)[-1] # folder name
  @caps['build'] =   `git rev-parse HEAD || echo buildname` # HEAD commit hash
  @caps['name'] =    `git log -1 --pretty=%B || echo testname` # HEAD commit message
  return @caps
end

.get_provider_class(config = load_config) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cloud_test/core.rb', line 127

def self.get_provider_class(config=load_config)
  case config.delete('provider').to_s.downcase
  when 'browserstack', 'bs', 'b'
    require 'cloud_test/browserstack'
    return Browserstack
  when 'lambdatest', 'lt', 'l'
    require 'cloud_test/lambdatest'
    return Lambdatest
  when 'crossbrowsertesting', 'cbs', 'ct', 'cbt', 'c'
    require 'cloud_test/cross_browser_testing'
    return CrossBrowserTesting
  when 'saucelabs', 'sauce', 'sc', 'sl', 's'
    require 'cloud_test/saucelabs'
    return Saucelabs
  else
    puts "Error: Please add a valid provider to your config file!"
  end
end

.list_capsObject

print defaults



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cloud_test/core.rb', line 73

def self.list_caps # print defaults
  # this output could be reformatted
  puts 'These are the defaults:' + """
  PROJECT : # name of the folder
  BUILD :  # HEAD commit hash
  NAME : # HEAD commit message
  OS : '10'
  PLATFORM : 'WINDOWS'
  BROWSER : 'CHROME'"""
  puts 'Please add additional capabilities in the cloud_test.yml file'
end


146
147
148
# File 'lib/cloud_test/core.rb', line 146

def self.list_dashboard_link
  puts "link to the dashboard: #{get_provider_class::DASHBOARD_LINK}"
end

.list_these_caps(caps) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/cloud_test/core.rb', line 85

def self.list_these_caps(caps)
  if caps.kind_of?(Enumerable)
    caps.each do |key, value|
      puts "|#{key.to_s.ljust(25)}|#{value.to_s.ljust(44)}|\n" # make a nice table like layout
    end
  else
    puts "Error: No caps"
  end
end

.load_config(env_user = 'CLOUD_TEST_USER', env_pw = 'CLOUD_TEST_PW') ⇒ Object

the optional parameter could be deleted, or used if someone does not want to put there credentials in the config



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cloud_test/core.rb', line 25

def self.load_config(env_user='CLOUD_TEST_USER', env_pw='CLOUD_TEST_PW')
  config = Hash.new

  @caps = self.get_default_caps
  path = `pwd`.to_s.gsub(/\s+/, "") + "/config/#{CONFIG_NAME}.yml" # complete path to the config file
  begin
    config = YAML.load_file(File.absolute_path path)
    if ENV.has_key?(env_user) && ENV.has_key?(env_pw)
      config['key'] = ENV[env_pw]
      config['user'] = ENV[env_user]
    end
  rescue SystemCallError
    puts 'Error: no config file found at: ' + path
    puts 'Tip: You should run your tests from your main project directory'
    puts 'Error: I need a config yml file, named ENV["CONFIG_NAME"] or "cloud_test.yml" which has at least a "user" and and "key" pair, thank you!'
  else
    if config.has_key?('user') && config.has_key?('key') && config.has_key?('provider') # check wether all the necessary keys exist
      list_to_check_input = [config['browsers'].keys, (config['cucumber_tag'] if config.has_key?('cucumber_tag')), config['provider']].flatten
      list_to_check_input.each do |str|
        if !check_if_input_is_valid?(str)
          raise "Invalid value: #{str}. Only characters followed by digits are allowed!"
        end
      end
      return config
    else
      puts 'Error: I have a config yml file, but no user, key or provider value :('
      puts "Keys: " + config.keys.to_s
    end
  end
end

.merge_caps(caps, config, provider = nil, browser = ) ⇒ Object

config overwrites in case of conflict



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
# File 'lib/cloud_test/core.rb', line 101

def self.merge_caps(caps, config, provider=nil, browser=ENV['CLOUD_TEST']) # config overwrites in case of conflict
  if !config.kind_of?(Hash)
    return caps
  end
  keys = config.keys - ['common_caps', 'browsers'] # handle those seperatly
  copy_keys caps, config, keys
  if provider && config.has_key?(provider) && config[provider].class.included_modules.include?(Enumerable)
    copy_keys caps, config[provider]
  end
  if config.has_key?('common_caps')
    caps = caps.merge(config['common_caps'])
  end
  if config.has_key?('browsers')
    if config['browsers'].kind_of?(Hash)
      if !browser.nil? && config['browsers'][browser].nil?
        puts "There is no browser with the key:#{browser} in your config file!"
        raise "No matching browser key found!"
      end
      caps = caps.merge(config['browsers'][browser || config['browsers'].keys[0]])
    else
      caps = caps.merge(config['browsers'])
    end
  end
  return caps
end

.register_driver(capsArray, user, key, server) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cloud_test/core.rb', line 56

def self.register_driver(capsArray, user, key, server)
  # some debugging options
  url =  "https://#{user.sub("@", "%40")}:#{key}@#{server}"
  if capsArray.has_key?('cloud_test_debug') and capsArray['cloud_test_debug']
    puts "Capybara.app_host = #{Capybara.app_host}"
    puts "Hub url: #{url}"
    list_these_caps capsArray
  end
    Capybara.register_driver :cloud_test do |app|
      Capybara::Selenium::Driver.new(app,
                                   :browser => :remote,
                                   :url => url,
                                   :desired_capabilities => capsArray
      )
    end
end

.upload_status(success:, session_id:, reason: "Unknown") ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cloud_test/core.rb', line 150

def self.upload_status(success:, session_id:, reason: "Unknown")
  config = load_config
  provider = get_provider_class config
  provider.check_session_id session_id
  puts session_id
  unless provider::REST_STATUS_SERVER.present?
    puts "skipping upload, not implementet for your provider yet."
    return
  end
  require 'net/http'
  require 'uri'
  require 'json'
  uri = URI.parse(provider::REST_STATUS_SERVER + session_id )
  request = Net::HTTP::Put.new(uri)
  request.basic_auth(config['user'], config['key'])
  request.content_type = "application/json"
  request.body = JSON.dump(provider.get_status_msg(success, reason))
  req_options = {
      use_ssl: uri.scheme == "https",
  }
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  if response.code != '200'
    puts "Response Code: #{response.code}"
    puts "Status upload error!"
  end

end