Class: TestSwarm::Project

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

Defined Under Namespace

Classes: SubmissionFailed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, name, options = {}) ⇒ Project

Returns a new instance of Project.



8
9
10
11
12
# File 'lib/testswarm/project.rb', line 8

def initialize(client, name, options = {})
  @client  = client
  @name    = name
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/testswarm/project.rb', line 6

def name
  @name
end

Instance Method Details

#payload(job, params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/testswarm/project.rb', line 14

def payload(job, params = {})
  cgi = {
    'action'    => 'addjob',
    'authID'    => @name,
    'authToken' => @options[:auth],
    'jobName'   => params[:name],
    'runMax'    => params[:max] || DEFAULT_MAX
  }

  query = ''
  cgi.keys.sort.each do |key|
    query += '&' unless query.empty?
    query += "#{key}=#{escape cgi[key]}"
  end

  browsers = [params[:browsers] || DEFAULT_BROWSERS].flatten
  browsers.each do |browser|
    query += "&browserSets[]=#{escape browser}"
  end

  job.each_suite do |name, url|
    query += "&runNames[]=#{escape name}&runUrls[]=#{escape url}"
  end
  query
end

#submit_job(name, job, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/testswarm/project.rb', line 40

def submit_job(name, job, options = {})
  job.inject_script(@client.url + INJECT_SCRIPT)

  http = Net::HTTP.start(@client.uri.host, @client.uri.port)
  data = payload(job, job_params(name, options))

  job.log "POST #{@client.url} #{data}"

  response = http.post('/api.php', data)
  job.log "Response: #{response.body}"
  job_data = JSON.parse(response.body)['addjob'] rescue nil

  unless job_data
    job.log 'Job submission failed'
    job.log response.body
    return nil
  end

  job.log "Job ID: #{job_data['id']}"
  job.log "Runs: #{job_data['runTotal']}, user agents: #{job_data['uaTotal']}"

  job_data['id'].to_s

rescue => e
  job.log 'Job submission failed'
  job.log e.message
  job.log e.backtrace
end