Module: AutoTest::Page

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

Class Method Summary collapse

Class Method Details

.all_inputs(session) ⇒ Object

get all input forms



237
238
239
# File 'lib/page.rb', line 237

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

get all links on the current page



232
233
234
# File 'lib/page.rb', line 232

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

.all_selects(session) ⇒ Object

get all select-fields



242
243
244
# File 'lib/page.rb', line 242

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

.all_submits(session) ⇒ Object

get all submit buttons



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

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



223
224
225
226
227
228
229
# File 'lib/page.rb', line 223

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 random checkbox



191
192
193
194
195
196
197
198
# File 'lib/page.rb', line 191

def choose_checkbox(name, input_texts, session)
  checkboxes = session.all('input').find_all{ |i| i[:type] == "checkbox" && i[:name] == name  }
  choices = checkboxes.collect{ |r| r[:name] }
  i = rand(choices.length)
  input_texts << "checkbox___" + name
  input_texts << choices[i].to_s
  session.check choices[i]
end

.choose_radio(name, input_texts, session) ⇒ Object

choose a random radiobutton



181
182
183
184
185
186
187
188
# File 'lib/page.rb', line 181

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



111
112
113
114
115
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
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
# File 'lib/page.rb', line 111

def fill_in_forms(session)
  inputs = all_inputs(session)
  button = all_submits(session)[rand(all_submits(session).size)]
  selects = all_selects(session)
  texts = []
  selects.each do |s|
    select_option(s[:id], texts, session)
  end  
  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  
  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



259
260
261
# File 'lib/page.rb', line 259

def go_back
   redirect_to :back
end

.handle_alert(session) ⇒ Object

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



210
211
212
213
214
215
216
217
218
219
# File 'lib/page.rb', line 210

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



252
253
254
255
256
# File 'lib/page.rb', line 252

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



172
173
174
175
176
177
178
# File 'lib/page.rb', line 172

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



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

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
            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
              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)
                random_link = links[rand(links.size)]
                if random_link[:href] != "#" then
                  hash = Hash.new
                  hash["#{i}:#{path}"] = random_link[:href]+"+++"+random_link.text
                  Test.add_to_action_path hash
                end
                random_link.click
                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)
              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
              if !Test.no_auth then
                Authentication.logout(session)
              end
              Test.sessions(i, "current_depth", 0)
              Test.sessions(i, "paths", Hash.new)
              Test.sessions(i, "iteration", Test.get_sessions(i, "iteration") + 1)
            end
          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
          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
          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



201
202
203
204
205
206
207
# File 'lib/page.rb', line 201

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