Class: Gridium::TestRail

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

Constant Summary collapse

ENV_ERROR =
"Environment Variable not set!"
CONFIG =
{:pass => 1, :blocked => 2, :untested => 3, :retest => 4, :fail => 5}.freeze

Instance Method Summary collapse

Constructor Details

#initializeTestRail

Returns a new instance of TestRail.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/testrail.rb', line 12

def initialize
  if Gridium.config.testrail
    @url = ENV['GRIDIUM_TR_URL'].empty? || ENV['GRIDIUM_TR_URL'].nil? ? ENV_ERROR : ENV['GRIDIUM_TR_URL'] + '/index.php?/api/v2/'
    @user = ENV['GRIDIUM_TR_USER'].empty? || ENV['GRIDIUM_TR_USER'].nil? ? ENV_ERROR : ENV['GRIDIUM_TR_USER']
    @password = ENV['GRIDIUM_TR_PW'].empty? || ENV['GRIDIUM_TR_PW'].nil? ? ENV_ERROR : ENV['GRIDIUM_TR_PW']
    @pid = ENV['GRIDIUM_TR_PID'].empty? || ENV['GRIDIUM_TR_PID'].nil? ? ENV_ERROR : ENV['GRIDIUM_TR_PID']
    @retry_attempts = 5
    @time_between_retries = 3
    @tc_results = Array.new
    @tc_ids = Array.new
  end
  @run_info = {:id => 0 ,:error => false, :include_all => false}
end

Instance Method Details

#add_case(rspec_test) ⇒ bool

Adds determines what the result of the test is and adds information to various arrays for processing during closing.

Parameters:

  • rspec_test (RSpec::Example)

    the example provided by RSpec

Returns:

  • (bool)

    determine if case was added or not



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/testrail.rb', line 55

def add_case(rspec_test)
  added = false
  if Gridium.config.testrail
    Log.debug("[GRIDIUM::TestRail] Adding to list of TestRail Cases...")
    if rspec_test.nil? then
      Log.error("[GRIDIUM::TestRail] No test added to results. Turn of Gridium.config.testrail\n")
    end

    if rspec_test.exception
      status = CONFIG[:fail]
      message = rspec_test.exception.message
      screenshot_url = rspec_test.[:screenshot_url]
      if screenshot_url
        message << "\n - Screenshot: #{screenshot_url}\n"
      end

      # add backtrace to test case
      bt_search_re = rspec_test.[:backtrace_regex] || 'sitetestui'
      bt = rspec_test.exception.backtrace.grep(/#{bt_search_re}/)
      message << "\n\n -> " + bt.join("\n -> ") unless bt.empty?

      # replace rspec backtick (`): special formatting for TestRail
      # http://docs.gurock.com/testrail-userguide/userguide-editor
      message.gsub!('`', "'")
    else
      status = CONFIG[:pass]
      message = 'Test Passed.'
    end

    test_info = {:case_id => rspec_test.[:testrail_id], :status_id => status, :comment => message}

    @tc_results.push(test_info)
    @tc_ids.push(test_info[:case_id])
    added = true
  end

  return added
end

#add_run(name, desc) ⇒ int

Creates a new test Run in your TestRail instance.

Parameters:

  • name (String)

    the name of the test run. Error text will be added if an error occurs

  • desc (String)

    a description of the run being added. Error text will be added if an error occurs

Returns:

  • (int)

    The run ID of the created run or zero if no run created.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/testrail.rb', line 31

def add_run(name, desc)
  if Gridium.config.testrail
    Log.debug("[GRIDIUM::TestRail] Creating Test Run: name: #{name} desc: #{desc}")
    if name.nil? || name.empty? then
      @run_info[:error] = true
    else
      @run_info[:name] = name
      @run_info[:desc] = desc
    end
    r = _send_request('POST', "#{@url}add_run/#{@pid}", @run_info)
    if r.key?('error') || r["id"].nil?
      @run_info[:error] = true
    else
      @run_info[:id] = r["id"]
      Log.debug("[GRIDIUM::TestRail] Run Added: #{r}")
    end
  end
  return @run_info[:id]
end

#close_run(opts = {}) ⇒ bool

Updates the existing test run with test cases and results. Adds error text for missing test cases if needed. Closes the run as long as it exists.

Returns:

  • (bool)

    if the run was closed or not



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
# File 'lib/testrail.rb', line 97

def close_run(opts = {})
  closed = false
  if Gridium.config.testrail && !@run_info[:error]
    Log.debug("[GRIDIUM::TestRail] Closing test runid: #{@run_info[:id]}\n")
    if @tc_ids.size > 0
      r = _send_request('POST', "#{@url}update_run/#{@run_info[:id]}", {:case_ids => @tc_ids})
      Log.debug("[GRIDIUM::TestRail] UPDATE RUN: #{r}")
      sleep 0.25
      r = _send_request('POST', "#{@url}add_results_for_cases/#{@run_info[:id]}", {results: @tc_results})
      Log.debug("[GRIDIUM::TestRail] ADD RESULTS: #{r}")
      sleep 0.25
      Log.debug("[GRIDIUM::TestRail] #{r.class}")
      if r.is_a?(Hash)
        r = _send_request('POST', "#{@url}update_run/#{@run_info[:id]}", {:name => "ER:#{@run_info[:name]}", :description => "#{@run_info[:desc]}\nThe following was returned when adding cases: #{r}"})
        Log.error("[GRIDIUM::TestRail] ERROR: #{r}")
        sleep 0.25
      end
    end
    r = _send_request('POST', "#{@url}close_run/#{@run_info[:id]}", nil, opts)

    Log.debug("[GRIDIUM::TestRail] CLOSE RUN: #{r}")
    if r.has_key?("error")
      Log.error("[GRIDIUM::TestRail]: #{r}")
    else
      closed = true
    end
  end

  closed
end