Module: AutoTest::Page

Included in:
AutoTest
Defined in:
lib/page.rb

Overview

This module deals with the interaction on the page like clicking links and filling in forms

Class Method Summary collapse

Class Method Details

.all_inputs(session) ⇒ Object

get all input forms



251
252
253
# File 'lib/page.rb', line 251

def all_inputs(session)
  session.all('input')
end

get all links on the current page



246
247
248
# File 'lib/page.rb', line 246

def all_links(session)
  session.all('a')
end

.all_selects(session) ⇒ Object

get all select-fields



256
257
258
# File 'lib/page.rb', line 256

def all_selects(session)
  session.all('select')
end

.all_submits(session) ⇒ Object

get all submit buttons



261
262
263
# File 'lib/page.rb', line 261

def all_submits(session)
  session.all('input').find_all{ |i|  i[:type] == "submit"}  
end

.check_for_error_code(session) ⇒ Object

check, if the page includes an html error code if present, add the error and print the message



237
238
239
240
241
242
243
# File 'lib/page.rb', line 237

def check_for_error_code(session)
  if session.status_code >= 400 then
    message = "ERROR : Response : #{session.status_code}"
    Error.inc_error(message)
    puts message
  end
end

.choose_checkbox(name, input_texts, session) ⇒ Object

choose a checkbox



206
207
208
209
210
211
212
# File 'lib/page.rb', line 206

def choose_checkbox(name, input_texts, session)
  checkboxes = session.all('input').find_all{ |i| i[:type] == "checkbox" && i[:name] == name  }
  cb = checkboxes.first
  input_texts << "checkbox___" + name
  input_texts << cb[:value]
  session.check cb[:name]
end

.choose_radio(name, input_texts, session) ⇒ Object

choose a random radiobutton



196
197
198
199
200
201
202
203
# File 'lib/page.rb', line 196

def choose_radio(name, input_texts, session)
  radios = session.all('input').find_all{ |i|  i[:type] == "radio" && i[:name] == name}
  choices = radios.collect{ |r| r[:value] }
  i = rand(choices.length)
  input_texts << "radio___" + name
  input_texts << choices[i].to_s
  session.choose choices[i]
end

.fill_in_forms(session) ⇒ Object

fill in all inputs of type email, text or radio assuming there´s only one submit button on the page later maybe decide on random button



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
# File 'lib/page.rb', line 124

def fill_in_forms(session)
  inputs = all_inputs(session)
  button = all_submits(session)[rand(all_submits(session).size)]
  selects = all_selects(session)
  texts = []
  # handle select fields first
  selects.each do |s|
    select_option(s[:id], texts, session)
  end  
  # handle other input types
  inputs.each do |i|
    case i[:type]
      when "email" then 
        text = Faker::Internet.email
        texts << i[:name]
        texts << text
        session.fill_in i[:id], :with => text
      when "text" then
        if i[:class] == "datePicker" then
          text = Time.random.to_date
          session.fill_in i[:id], :with => text
          texts << i[:name]
          texts << text.to_s
        else
          text = Faker::Name.name
          session.fill_in i[:id], :with => text
          texts << i[:name]
          texts << text
        end
      when "radio" then
        choose_radio(i[:name], texts, session)
      when "checkbox" then  
        choose_checkbox(i[:name], texts, session)
      when "number" then
        text = rand(100)
        session.fill_in i[:id], :with => text
        texts << i[:name]
        texts << text.to_s
    end
    text = ""
  end  
  # check for values given by the tester and set these
  inputs_to_set = Test.get_inputs
  if !inputs_to_set.empty? then
    inputs.each do |i|
      if inputs_to_set[i[:id]] != nil then
        text = inputs_to_set[i[:id]][rand(inputs_to_set[i[:id]].size)]
        session.fill_in i[:id], :with => text
        texts << i[:name]
        texts << text.to_s
      end
    end
  end
  hash = Hash.new
  hash["#{Test.get_sessions_array.index(session)}:#{session.current_path}"] = texts
  Test.add_to_action_path hash
  session.click_button button.value
  Test.link_counter = Test.link_counter + 1
  check_for_error_code(session)
end

.go_backObject

go back one page



273
274
275
# File 'lib/page.rb', line 273

def go_back
   redirect_to :back
end

.handle_alert(session) ⇒ Object

if an alert pops up, randomly decide whether to accept or dismiss it



224
225
226
227
228
229
230
231
232
233
# File 'lib/page.rb', line 224

def handle_alert(session)
  begin 
    if rand(2) == 1 then
      session.page.driver.browser.switch_to.alert.accept
    else
      session.page.driver.browser.switch_to.alert.dismiss
    end
  rescue
  end
end

delete the links, not to be clicked



266
267
268
269
270
# File 'lib/page.rb', line 266

def handle_excluded_links(links)
  Test.get_links_to_exclude.each do |link|
    links.delete(links.find{ |l| l.text == link[0] && l[:href] == link[1]})
  end
end

.handle_forms(session) ⇒ Object

check for input fields and fill in the forms randomly decide, if inputs are filled in or links are clicked



187
188
189
190
191
192
193
# File 'lib/page.rb', line 187

def handle_forms(session)
  if !all_submits(session).empty? then
    if rand(2) == 1 then    
      fill_in_forms(session)
    end
  end
end

.run_session(i) ⇒ Object

click a link in a certain session



19
20
21
22
23
24
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/page.rb', line 19

def run_session(i)
  session = Test.get_sessions_array[i]
  if !Test.stop? then  
    if Test.no_auth || Test.get_sessions(i,"current_user") <= Authentication.get_users.size then
      if Test.get_sessions(i,"depth") <= Test.get_max_depth then
        if Test.get_sessions(i,"iteration") <= Test.get_sessions(i,"iterations") then
          begin
            # Login
            if Test.get_sessions(i, "current_depth") == 0 && !Test.no_auth then 
              Authentication.(Test.get_sessions(i,"user"),session)  
              Test.check_invariants
            end
            if Test.no_auth then
              session.visit "/"
            end
            if (Test.get_sessions(i,"current_depth") < Test.get_sessions(i,"depth")) then
              # get the links of the page and delete the ones the tester specified
              links = all_links(session)
              handle_excluded_links(links)
              if !links.empty? then
                Test.check_invariants
                path = session.current_path
                paths = Test.get_sessions(i,"paths")
                paths[path] = links.size
                Test.sessions(i, "paths", paths)
                # choose a random link
                random_link = links[rand(links.size)]
                # check for javascript
                if random_link[:href] != "#" then
                  hash = Hash.new
                  hash["#{i}:#{path}"] = random_link[:href]+"+++"+random_link.text
                  Test.add_to_action_path hash
                end
                # click the link
                random_link.click
                # update the console output
                STDOUT.write("\r#{Test.run}. run: \t#{Test.link_counter} links clicked, \t#{Test.users_logged_in} users logged in, \t#{Error.get_error} Errors")
                Test.link_counter = Test.link_counter + 1
                Test.check_invariants
                handle_forms(session)
                Test.check_invariants
                Test.sessions(i, "current_depth", Test.get_sessions(i, "current_depth") + 1)
              end
            else
              links = all_links(session)
              handle_excluded_links(links)
              # calculate new iterations number
              if !links.empty? then 
                Test.sessions(i,"iterations", 0)
                Test.get_sessions(i,"paths").each do |key, value|
                  Test.sessions(i,"iterations", Test.get_sessions(i, "iterations") + value)
                end
              end
              # logouz
              if !Test.no_auth then
                Authentication.logout(session)
              end
              # new initialization of session variables
              Test.sessions(i, "current_depth", 0)
              Test.sessions(i, "paths", Hash.new)
              Test.sessions(i, "iteration", Test.get_sessions(i, "iteration") + 1)
            end
          # rescue from exceptions, process the error
          rescue => e                
            if Test.stop_at_first_error? then Test.stop! end
            message = e.message.to_s + "   :" + e.backtrace.first.to_s
            Error.inc_error message
            STDOUT.write("\r#{Test.run}. run: \t#{Test.link_counter} links clicked, \t#{Test.users_logged_in} users logged in, \t#{Error.get_error} Errors")
            
          end                                       
        else
          # new initialization of session variables             
          Test.sessions(i, "iteration", 0)
          Test.sessions(i, "iterations", 0)
          Test.sessions(i, "depth", Test.get_sessions(i, "depth") + 1)
          Test.sessions(i, "paths", Hash.new)
        end           
      else
        if !Test.stop? && !Test.no_auth then
          Test.users_logged_in = Test.users_logged_in + 1
        end
        if !Test.no_auth then
          # get new random user for next round
          Test.sessions(i, "current_user", Test.get_sessions(i,"current_user")+1)
          user = Authentication.get_users[rand(Authentication.get_users.size)]
          Test.sessions(i, "user", user)
          Test.add_to_action_path "#{i}:#{user.id}ID"
        end
        if Test.no_auth then
          Test.ready(i)
        end
        Test.sessions(i, "depth", 0)
        Test.sessions(i, "iterations", 0)
      end
    else
      Test.ready(i)
    end
  end
end

.select_option(id, input_texts, session) ⇒ Object

select a random option from a select list



215
216
217
218
219
220
221
# File 'lib/page.rb', line 215

def select_option(id, input_texts, session)
  options = session.find_by_id(id).all('option').collect{ |o| o.text }
  i = rand(options.size)
  input_texts << "select___"+id
  input_texts << options[i].to_s
  session.select options[i], :from => id
end