Module: ZZnamezzTestCase

Included in:
TC_R001_01_AN_EXAMPLE_TEST
Defined in:
lib/taft_files/lib/zznamezz_test_case.rb

Constant Summary collapse

WRITE_RESULTS =
false
@@browser =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

E.g. calling homepage.displayed? from a test script : Test cannot see homepage so its call is routed through method_missing If method_missing returns an instance of the class, .displayed? can be called on it (seamlessly) At present this will happen for every call to a page from a test script



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 50

def method_missing(name, *args, &block)
    #puts "ZZnamezzTestCase method_missing called; name = #{name.inspect}; #{name.class}"
 
    case name.to_s
    when /^browser$/
        browser
    when /^xxabbrevxx/i
        XXabbrevupperxxPages.find(name.to_s) # return the page so that the test can use it
    else
        super
    end
end

Instance Attribute Details

#browser_has_been_openedObject

Returns the value of attribute browser_has_been_opened.



37
38
39
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 37

def browser_has_been_opened
  @browser_has_been_opened
end

#failure_notesObject

optional field If the cause of a test’s failure is already likely to be known, the contents of this variable will automatically be added to the test result’s Notes field, to help with reporting. If there are multiple tests in a file, this variable needs to be set within each test method (if they have any relevent failure notes).



44
45
46
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 44

def failure_notes
  @failure_notes
end

Instance Method Details

#browserObject



106
107
108
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 106

def browser
    @@browser
end

#browser=(b) ⇒ Object Also known as: set_browser



110
111
112
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 110

def browser=(b)
    @@browser = b
end

#close(browser) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 86

def close(browser)
    if browser.exists? && ((ZZnamezzConfig::CLOSE_BROWSER_AFTER_TEST && passed?) || ZZnamezzConfig::FORCE_CLOSE_BROWSER_AFTER_TEST)
        browser.close
        $browsers.delete_at($current_browser_position - 1) # array indexing
        browser = $browsers[-1] # set browser to the last one that is still in the array
    end
end

#close_all_browsersObject



94
95
96
97
98
99
100
101
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 94

def close_all_browsers
    if (ZZnamezzConfig::CLOSE_BROWSER_AFTER_TEST && passed?) || ZZnamezzConfig::FORCE_CLOSE_BROWSER_AFTER_TEST
        until $browsers.empty?
            browser = $browsers.shift
            browser.close
        end
    end
end

#close_browserObject

Close the current browser



82
83
84
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 82

def close_browser
    browser.close
end

#load_pages(browser) ⇒ Object



75
76
77
78
79
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 75

def load_pages(browser)
    @browser_has_been_opened = true
    xxabbrevupperxxPages.make_pages(browser) # cannot have pages without a browser object
    $browsers << browser
end

#setupObject

Ensure that every test (that wants one) has a browser that is already logged in to the system



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 116

def setup

    @help = xxabbrevupperxxHelper.new

    Watir.always_locate = true # default is true; setting to false speeds up Watir to a degree

    # Get start time for later output in results
    @test_start_time = Time.now

    # Get the directory that the specific test lives in, so that it can be included in the results file
    @test_file_dir = @test_file.split(File::SEPARATOR)[-2] if @test_file

    # Select default certificate if none is configured
    @certificate ||= :regular

    @timeout = ZZnamezzConfig::CERTIFICATE_POPUP_TIMEOUT

    # Open the browser & ensure page contenxt and helper are available
    $browsers = [] # global array containing all browser objects
    # $current_browser_position = nil # global variable to track the position in $browsers of the active browser # TODO used?
    # When that browser is closed, we can ensure that the corresponding browser object is removed from the array
    if @initialBrowser == :xxabbrevxx
        
    elsif (@initialBrowser == :none || @initialBrowser == nil)
        browser = nil
    end

end

#teardownObject

Close all browsers and write the result of the test to the results CSV



146
147
148
149
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 146

def teardown

    begin
        # Get end time
        @test_end_time = Time.now
        elapsed_time = (@test_end_time - @test_start_time).to_s
        elapsed_time_in_minutes = (elapsed_time.to_i/60.0).to_s

        test_name = self.to_s.split("(")[0] # self.to_s gives output like test_ABC5_01(TC_ABC5_01)

        puts "Test has now finished; #{test_name} : #{passed?}"

        if WRITE_RESULTS
            puts "Will now write results to #{ZZnamezzConfig::RESULTS_BASE_DIR}"

            notes = ""
            success_text = passed? ? ZZnamezzConfig::PASSED : ZZnamezzConfig::FAILED

            unless passed?
                begin
                    if ZZnamezzConfig::MAKE_ERROR_SCREENSHOTS
                        puts "Now taking error screenshots"
                        dir_2 = ZZnamezzConfig::ERROR_SCREENSHOT_LOCATION
                        Dir.mkdir(dir_2) unless File.exists?(dir_2)
                        $browsers.each do |browser|
                            browser.screenshot.save(ZZnamezzConfig::ERROR_SCREENSHOT_LOCATION + "/#{test_name}_Time_#{@test_end_time.strftime("%H-%M-%S")}_Browser_#{$browsers.index(browser)}.png")
                        end
                    end
                rescue
                    puts "Failed to make screenshot"
                end
                notes = @failure_notes
                puts "Notes : #{notes}"
            end # end unless passed?

            
            # Write to the results file
            begin
                File.open(ZZnamezzConfig::RESULTS_CSV, "a") do |f|
                    row = [@test_file_dir, test_name, success_text, @test_start_time.strftime("%Y-%m-%d %H:%M:%S"), @test_end_time.strftime("%Y-%m-%d %H:%M:%S"), elapsed_time, elapsed_time_in_minutes, notes]
                    f.puts row.join(",")
                    puts "Result for test #{test_name} written"
                end
            rescue
                puts "Had to rescue from writing results to file #{ZZnamezzConfig::RESULTS_CSV}"
            end
        end # end if WRITE_RESULTS
        
        close_all_browsers

    rescue Timeout::Error => t_error
        puts "Timeout::Error :"
        puts t_error
        puts "Backtrace :"
        puts t_error.backtrace
    rescue Exception => error
        puts "Error :"
        puts error
        puts "Backtrace :"
        puts error.backtrace
    end # end begin
end

#xxabbrevxx_login(url = ) ⇒ Object

Connect to yyrawnameyy and reinitialise the context, etc.



70
71
72
73
# File 'lib/taft_files/lib/zznamezz_test_case.rb', line 70

def (url = ZZnamezzConfig::SERVER[:zznamezz_url])
    browser = @help.new_browser_at_url(url)
    load_pages(browser)
end