Module: EffectiveTestBotHelper

Defined in:
app/helpers/effective_test_bot_helper.rb

Instance Method Summary collapse

Instance Method Details

#fill_form(fills = {}) ⇒ Object

fill_form(:email => '[email protected]', :password => 'blahblah', 'user.last_name' => 'hlwerewr')



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/effective_test_bot_helper.rb', line 39

def fill_form(fills = {})
  fills = HashWithIndifferentAccess.new(fills)

  all('input,select,textarea').each do |field|
    case [field.tag_name, field['type']].compact.join('_')
    when 'input_text', 'input_email', 'input_password', 'input_tel', 'input_number', 'textarea'
      field.set(fill_value(field, fills))
    when 'input_checkbox', 'input_radio'
      field.set(fill_value(field, fills)) # TODO
    when 'select'
      field.select(fill_value(field, fills), match: :first)
    when 'input_file'
      puts "Warning, input_file not yet supported"
    when 'input_submit'
      # Do nothing
    else
      raise "unsupported field type #{[field.tag_name, field['type']].compact.join('_')}"
    end
  end
end

#fill_value(field, fills = nil) ⇒ Object

Operates on just string keys



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
118
119
120
121
122
123
124
125
126
# File 'app/helpers/effective_test_bot_helper.rb', line 61

def fill_value(field, fills = nil)
  attributes = field['name'].to_s.gsub(']', '').split('[') # user[something_attributes][last_name] => ['user', 'something_attributes', 'last_name']
  field_name = [field.tag_name, field['type']].compact.join('_')
  fill_value = nil

  if fills.present?
    key = nil
    attributes.reverse_each do |name|
      key = (key.present? ? "#{name}.#{key}" : name)

      if fills.key?(key)
        fill_value = fills[key]

        if field_name == 'select'
          break
        else
          return fill_value
        end
      end
    end
  end

  case field_name
  when 'input_email'
    Faker::Internet.email
  when 'input_number'
    Faker::Number.number(4)
  when 'input_password'
    Faker::Internet.password
  when 'input_tel'
    dig = (1..9).to_a
    "#{dig.sample}#{dig.sample}#{dig.sample}-#{dig.sample}#{dig.sample}#{dig.sample}-#{dig.sample}#{dig.sample}#{dig.sample}#{dig.sample}"
  when 'input_text'
    classes = field['class'].to_s.split(' ')

    if classes.include?('date') # Let's assume this is a date input.
      Faker::Date.backward(365).strftime('%y-%m-%d')
    elsif classes.include?('datetime')
      Faker::Date.backward(365).strftime('%y-%m-%d %H:%m')
    elsif attributes.last.to_s.include?('first_name')
      Faker::Name.first_name
    elsif attributes.last.to_s.include?('last_name')
      Faker::Name.last_name
    elsif attributes.last.to_s.include?('name')
      Faker::Name.name
    else
      Faker::Lorem.word
    end
  when 'select'
    if fill_value.present? # It might be a value or a label
      field.all('option').each do |option|
        return option.text if option.text == fill_value || option.value.to_s == fill_value.to_s
      end
    end

    field.all('option').select { |option| option.value.present? }.sample.try(:text) || ''
  when 'textarea'
    Faker::Lorem.sentence
  when 'input_checkbox'
    [true, false].sample
  when 'input_radio'
    binding.pry
  else
    raise "fill_value unsupported field type: #{field['type']}"
  end
end

#sign_in(user) ⇒ Object

Warden::Test::Helpers



16
17
18
19
20
21
22
# File 'app/helpers/effective_test_bot_helper.rb', line 16

def (user) # Warden::Test::Helpers
  if user.kind_of?(String)
    (User.find_by_email(user))
  else
    (user)
  end
end

#sign_up(email = Faker::Internet.email, password = Faker::Internet.password) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/effective_test_bot_helper.rb', line 2

def (email = Faker::Internet.email, password = Faker::Internet.password)
  visit new_user_registration_path

  within('form#new_user') do
    fill_form(:email => email, :password => password, :password_confirmation => password)
    submit_form
  end

  assert_equal page.status_code, 200
  assert_content I18n.t('devise.registrations.signed_up')

  User.find_by_email(email)
end

#submit_form(label = nil) ⇒ Object



128
129
130
131
132
133
134
135
# File 'app/helpers/effective_test_bot_helper.rb', line 128

def submit_form(label = nil)
  if label.present?
    click_on(label)
    #find_field(label).click
  else
    first(:css, "input[type='submit']").click
  end
end