Class: HooplaSalesforce::WebAgent

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

Constant Summary collapse

LOGIN_URL =
'https://login.salesforce.com'
MAX_CLASSES =
10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ WebAgent

Returns a new instance of WebAgent.



11
12
13
14
15
16
# File 'lib/hoopla_salesforce/web_agent.rb', line 11

def initialize(username, password)
  @username = username
  @password = password
  @agent = Mechanize.new
  
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



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

def agent
  @agent
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hoopla_salesforce/web_agent.rb', line 37

def get_all_test_links
  page = agent.get('/01p')

  if page.search('.next') 
    expand_link = page.search('.fewerMore a').first['href']
    enough_rows = expand_link.sub(/rowsperpage=\d+/, "rowsperpage=#{MAX_CLASSES}")
    page = agent.get(enough_rows)
  end

  page.search('.dataCell[scope="row"] a')
end

#loginObject



18
19
20
21
22
23
# File 'lib/hoopla_salesforce/web_agent.rb', line 18

def 
  agent.get(LOGIN_URL).form_with(:name => 'login') do ||
    .un = username
    .pw = password
  end.submit
end

#run_tests(test_names, namespace = '') ⇒ Object

Runs the given tests from the Web UI namespace is the package namespace without the __



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hoopla_salesforce/web_agent.rb', line 51

def run_tests(test_names, namespace = '')
  test_links = get_all_test_links

  tests = test_names.map do |test_name|
    { :id   => test_links.detect{ |a| a.text == test_name }['href'][1..-1],
      :name => test_name }
  end

  mkdir_p test_results_dir
  tests.each do |test|
    results_page = agent.get(test_run_url(test, namespace))
    results_table = results_page.search('.outer table td').first
    File.open(test_results_file(test[:name]), 'w') do |f|
      f.print results_table.inner_html
    end

    puts "#{test[:name]} complete, results in #{test_results_file(test[:name])}."
  end
end

#test_results_dirObject



29
30
31
# File 'lib/hoopla_salesforce/web_agent.rb', line 29

def test_results_dir
  "test-results"
end

#test_results_file(test_name) ⇒ Object



33
34
35
# File 'lib/hoopla_salesforce/web_agent.rb', line 33

def test_results_file(test_name)
  "#{test_results_dir}/#{test_name}.html"
end

#test_run_url(test, namespace) ⇒ Object



25
26
27
# File 'lib/hoopla_salesforce/web_agent.rb', line 25

def test_run_url(test, namespace)
  "/setup/build/runApexTest.apexp?class_id=#{test[:id]}&class_name=#{test[:name]}&ns_prefix=#{namespace}"
end