Module: RailsGwt::DSL

Includes:
Capybara::DSL
Defined in:
lib/rails_gwt/dsl.rb

Instance Method Summary collapse

Instance Method Details

#dump_pageObject



54
55
56
# File 'lib/rails_gwt/dsl.rb', line 54

def dump_page
  puts page.html.gsub(/></, ">\n<")
end

#login_session(username, use_last_pwd = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_gwt/dsl.rb', line 25

def (username, use_last_pwd = false)
  puts "login user: '#{username}'"
  if use_last_pwd
    line = File.read('log/test.log').gsub(/\r/, '').split("\n").grep(/new password :/).last
    pwd = line.gsub(/.*new password : (.*)/ , '\1') if line
  end
  if pwd.blank?
    fill_in 'username', :with => username
    click_on('password reset')
    page.should have_content('new password was sent to your email address')

    pwd = File.read('log/test.log').gsub(/\r/, '').split("\n").grep(/new password :/).last.gsub(/.*new password : (.*)/ , '\1')
  end
  fill_in 'login', :with => username
  fill_in 'password', :with => pwd
  click_on('login')

  should_have_no_loading

  yield

  click_on('logout')
  page.should have_content('password reset')
end

#should_have_action_button(name) ⇒ Object



90
91
92
# File 'lib/rails_gwt/dsl.rb', line 90

def should_have_action_button(name)
  find(:xpath, "//div[@class='gwt-rails-model']//div[@class='gwt-rails-buttons']/button[text()='#{name}' and not(@style='display: none;')]")
end

#should_have_action_buttons(*buttons) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rails_gwt/dsl.rb', line 121

def should_have_action_buttons(*buttons)
  buttons = buttons.flatten
  found = []
  all(:xpath, "//div[@class='gwt-rails-model']//div[@class='gwt-rails-buttons']/button").each do |b|
    found << b.text unless b.text.blank?
  end
  (found || bottons).each do |button|
    if buttons.member? button
      should_have_action_button(button) unless button.blank?
    else
      should_have_no_action_button(button) unless button.blank?
    end
  end 
end

#should_have_button(name) ⇒ Object



58
59
60
# File 'lib/rails_gwt/dsl.rb', line 58

def should_have_button(name)
  find(:xpath, "//button[text()='#{name}' and not(@style='display: none;')]")
end

#should_have_buttons(*buttons) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rails_gwt/dsl.rb', line 140

def should_have_buttons(*buttons)
  buttons = buttons.flatten
  found = []
  all(:xpath, "//div[contains(@class, 'gwt-rails-display')]/div[@class='gwt-rails-buttons']/button").each do |b|
    text = b.text
    found << text unless text.blank?
  end
  (found || buttons).each do |button|
    if buttons.member? button
      should_have_button(button)
    else
      should_have_no_button(button)
    end
  end
end

#should_have_menu_items(*items) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/rails_gwt/dsl.rb', line 66

def should_have_menu_items(*items)
  found = []
  all(:xpath, "//div[@class='gwt-rails-menu']/button").each do |b|
    found << b.text if b[:style].blank?
  end
  found.sort!.should == items.flatten.sort
end

#should_have_model_displayObject



82
83
84
# File 'lib/rails_gwt/dsl.rb', line 82

def should_have_model_display
  find(:xpath, "//div[@class='gwt-rails-model' and not(@style='display: none;')]")
end

#should_have_model_list_displayObject



78
79
80
# File 'lib/rails_gwt/dsl.rb', line 78

def should_have_model_list_display
  find(:xpath, "//div[@class='gwt-rails-model-list' and not(table/@style='display: none;')]")
end

#should_have_no_action_button(name) ⇒ Object



94
95
96
# File 'lib/rails_gwt/dsl.rb', line 94

def should_have_no_action_button(name)
  find(:xpath, "//div[@class='gwt-rails-buttons']/button[text()='#{name}' and @style='display: none;']")
end

#should_have_no_action_buttonsObject



117
118
119
# File 'lib/rails_gwt/dsl.rb', line 117

def should_have_no_action_buttons
  should_have_action_buttons
end

#should_have_no_button(name) ⇒ Object



62
63
64
# File 'lib/rails_gwt/dsl.rb', line 62

def should_have_no_button(name)
  find(:xpath, "//button[text()='#{name}' and @style='display: none;']")
end

#should_have_no_buttonsObject



136
137
138
# File 'lib/rails_gwt/dsl.rb', line 136

def should_have_no_buttons
  should_have_buttons
end

#should_have_no_loadingObject



50
51
52
# File 'lib/rails_gwt/dsl.rb', line 50

def should_have_no_loading
  page.should have_xpath('//div[@class = "gwt-rails-loading" and @style = "display: none;"]')
end

#should_have_no_model_displayObject



74
75
76
# File 'lib/rails_gwt/dsl.rb', line 74

def should_have_no_model_display
  find(:xpath, "//div[@class='gwt-rails-model' and @style='display: none;']")
end

#should_have_no_model_list_displayObject



86
87
88
# File 'lib/rails_gwt/dsl.rb', line 86

def should_have_no_model_list_display
  find(:xpath, "//div[@class='gwt-rails-model-list' and table/@style='display: none;']")
end

#visit_resource(config, &block) ⇒ Object



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
208
# File 'lib/rails_gwt/dsl.rb', line 156

def visit_resource(config, &block)
  puts "----- resource: '#{config.resource}'"
  page.should have_content("Welcome #{config.name}")
  page.should have_content('logout')

  should_have_menu_items(config.menu)
  
  config.action = :new
  if config.action
    within_page("/Users.html##{config.resource}/new", config.mode) do
      should_have_action_buttons(config.action_buttons)
      should_have_buttons(config.buttons)
    end
  end

  config.action = :edit
  if config.action
    id = config.mode == :singleton ? '' : "/#{config.resource_id}"
    within_page("/Users.html##{config.resource}#{id}/edit", config.mode) do
      should_have_action_buttons(config.action_buttons)
      should_have_buttons(config.buttons)
    end
  end
  
  config.action = :show
  if config.action      
    id = config.mode == :singleton ? '' : "/#{config.resource_id}"
    within_page("/Users.html##{config.resource}#{id}", config.mode) do
      should_have_action_buttons(config.action_buttons)
      should_have_buttons(config.buttons)
    end
  end

  config.action = :index
  if config.action
    within_page("/Users.html##{config.resource}", :index) do
      should_have_action_buttons(config.action_buttons)
      should_have_buttons(config.buttons)
    end

    # list
    config.content.each do |item|
      page.should have_content(item)
    end
    
    # no user list after search
    #      fill_in 'search', :with => 'pipapo'
    #      page.should have_no_content('[email protected]')
    
  end

  block.call(config) if block
end

#within_page(path, mode = :page, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rails_gwt/dsl.rb', line 98

def within_page(path, mode = :page, &block)
  puts "visit page: #{path}"
  visit "#{path}"
  should_have_no_loading
  
  case mode
  when :index
    should_have_no_model_display
    should_have_model_list_display
  when :page
    should_have_model_display
    should_have_no_model_list_display
  when :singleton
    should_have_model_display
  end

  block.call if block
end