使用黄瓜下的WebRat单击按钮会发生什么

时间:2010-05-28 17:14:14

标签: cucumber webrat

我正在尝试登录Java Web应用程序。

登录页面包含以下html:

  <html>
    <head><title>Login Page</title></head>
    <body onload='document.f.j_username.focus();'>
      <h3>Login with Username and Password</h3>
      <form name='f' action='/ui/j_spring_security_check' method='POST'>
       <table>
          <tr><td>User:</td><td><input type='text' name='j_username' value=''></td></tr>
          <tr><td>Password:</td><td><input type='password' name='j_password'/></td></tr>
          <tr>
             <td><input type='checkbox' name='_spring_security_remember_me'/> </td>
             <td>Remember me on this computer.</td>
          </tr>
          <tr><td colspan='2'><input name="submit" type="submit"/></td></tr>
          <tr><td colspan='2'><input name="reset" type="reset"/></td></tr>
        </table>
      </form>
    </body>
  </html>

我使用以下脚本:

Given  /^I am logged in as (.*) with password (.*)$/ do | user, password |
  visit "http://localhost:8080/ui"
  click_link "Projects"
  puts "Response Body:"
  puts response.body
  assert_contain "User:"
  fill_in "j_username", :with => user
  fill_in "j_password", :with => password
  puts "Response Body:"
  puts response.body
  click_button
  puts "Response Body:"
  puts response.body
end

这在日志文件中提供以下内容:

[INFO] Response Body:
[INFO] <html><head><title>Login Page</title></head><body onload='document.f.j_username.focus();'>
[INFO] <h3>Login with Username and Password</h3><form name='f' action='/ui/j_spring_security_check' method='POST'>
[INFO]  <table>
[INFO]     <tr><td>User:</td><td><input type='text' name='j_username' value=''></td></tr>
[INFO]     <tr><td>Password:</td><td><input type='password' name='j_password'/></td></tr>
[INFO]     <tr><td><input type='checkbox' name='_spring_security_remember_me'/></td><td>Remember me on this computer.</td></tr>
[INFO]     <tr><td colspan='2'><input name="submit" type="submit"/></td></tr>
[INFO]     <tr><td colspan='2'><input name="reset" type="reset"/></td></tr>
[INFO]   </table>
[INFO] </form></body></html>
[INFO] Response Body:
[INFO] <html><head><title>Login Page</title></head><body onload='document.f.j_username.focus();'>
[INFO] <h3>Login with Username and Password</h3><form name='f' action='/ui/j_spring_security_check' method='POST'>
[INFO]  <table>
[INFO]     <tr><td>User:</td><td><input type='text' name='j_username' value=''></td></tr>
[INFO]     <tr><td>Password:</td><td><input type='password' name='j_password'/></td></tr>
[INFO]     <tr><td><input type='checkbox' name='_spring_security_remember_me'/></td><td>Remember me on this computer.</td></tr>
[INFO]     <tr><td colspan='2'><input name="submit" type="submit"/></td></tr>
[INFO]     <tr><td colspan='2'><input name="reset" type="reset"/></td></tr>
[INFO]   </table>
[INFO] </form></body></html>
[INFO] Response Body:
[INFO] 
[INFO]     Given I am logged in as pti with password ptipti # features/step_definitions/authentication_tests.rb:2

显然,单击提交按钮后,response.body消失了。我可以从服务器日志文件中看到脚本没有到达项目页面。

我是webrat的新手,也是ruby的新手,现在我很困惑。我不知道为什么response.body消失了。我不知道我在哪里。

我推测我必须等待页面请求,但所有文档都说webrat很好地等待所有重定向,页面加载等完成。 (至少我想我读过那个)。此外,我找不到在webrat API中等待页面的方法。

有人可以提供一些有关如何进行调试的提示吗?

1 个答案:

答案 0 :(得分:1)

我使用原生ruby确认了排除任何jruby或JVM相关问题的行为。

我重新配置了webrat以使用Selenium,所以我可以看到发生了什么。

发生了什么事,登录后有302重定向。 selenium实现遵循重定向,但机械化实现没有。所以我正在解释重定向消息的主体,这当然是空的。

所以我通过exlipcitely访问我想要重定向的页面来伪造重定向,并且看起来很棒:它有效!。