webrat 的 button_press 中缺少 POST 变量

发布于 2024-12-19 16:59:12 字数 3421 浏览 3 评论 0原文

我在尝试用 Cucumber 测试我的 php Web 应用程序时遇到了麻烦。基本上,button_press方法在提交表单时无法提交buttonName_x和buttonName_y POST变量。

我希望有人能够帮助我解决这个问题 - 提前致谢!

编辑:我能够为 webrat 编写一个脏补丁来暂时解决该问题。不过,我仍然很高兴听到更好的解决方案。

这是有问题的 html:

                <form id="newsForm" action="/index.php?page=adminpanel&amp;view=newscontrol" method="post">
                    <table cellpadding="0" cellspacing="0" class="tableList">
                        <thead id="editor">
                            <tr>
                                <th colspan="3">News Editor</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="rowA">
                                <td colspan="3">Titel:&nbsp;<input type="text" name="subject" value="'.utf8htmlentities($subject).'" size="121" /></td>
                            </tr>
                            <tr class="rowB">
                                <td colspan="3">
                                    <textarea id="content" name="content" rows="10" cols="10">'.utf8htmlentities($content).'</textarea>
                                </td>
                            </tr>
                            <tr class="submitRow">
                                <td colspan="3"><input type="image" src="res/images/design/button_preview.gif" name="previewNews" alt="preview" /> <input type="image" src="res/images/design/button_sendx.gif" name="submitNews" alt="submit" /></td>
                            </tr>
                        </tbody>
                    </table>
                </form>

提取失败的功能:

Scenario: post news
    [...]
    When I insert "This is a subject!" for newsForm.subject
    And I insert "Magnificient News Post" for newsForm.content
    And I press newsForm.submit
    [...]

var_dump($_POST) 导致:

array(2) { ["content"]=> string(22) "Magnificient News Post" ["subject"]=> string(18) "This is a subject!" } 

而通过 firefox 的请求导致:

array(4) { ["content"]=> string(22) "Magnificient News Post" ["subject"]=> string(18) "This is a subject!" ["submitNews_x"]=> string(1) "0" ["submitNews_y"]=> string(1) "0" } 

我的步骤定义如下所示:

When /^I insert "(.*?)" for (.*?)\.(.*?)$/ do |input, form, item|
    within 'form[id="' + form + '"]' do |scope|
        scope.fill_in(item, :with => input)
    end
end

When /^I press (.*?)\.(.*?)$/ do |form, item|
    within 'form[id="' + form + '"]' do |scope|
        scope.click_button(item)
    end
end

最后,我的 env.rb:

# RSpec
require 'rspec/expectations'

# Webrat
require 'webrat'

require 'test/unit/assertions'
World(Test::Unit::Assertions)

Webrat.configure do |config|
  config.mode = :mechanize
end

class MechanizeWorld < Webrat::MechanizeAdapter
  include Webrat::Matchers
  include Webrat::Methods
  Webrat::Methods.delegate_to_session :response_code, :response_body, :response, :redirected_to
end

World do
  MechanizeWorld.new
  session = Webrat::Session.new
  session.extend(Webrat::Methods)
  session.extend(Webrat::Matchers)
  session
end

I have been running into trouble while trying to test my php web application with cucumber. Basically, the button_press method fails to submit the buttonName_x and buttonName_y POST variables when submitting the form.

I hope someone is able to help me with this - thanks in advance!

EDIT: I was able to write a dirty patch for webrat to be able to temporarily resolve the issue. However, I'd still be glad to hear of a better solution.

This is the html in question:

                <form id="newsForm" action="/index.php?page=adminpanel&view=newscontrol" method="post">
                    <table cellpadding="0" cellspacing="0" class="tableList">
                        <thead id="editor">
                            <tr>
                                <th colspan="3">News Editor</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="rowA">
                                <td colspan="3">Titel: <input type="text" name="subject" value="'.utf8htmlentities($subject).'" size="121" /></td>
                            </tr>
                            <tr class="rowB">
                                <td colspan="3">
                                    <textarea id="content" name="content" rows="10" cols="10">'.utf8htmlentities($content).'</textarea>
                                </td>
                            </tr>
                            <tr class="submitRow">
                                <td colspan="3"><input type="image" src="res/images/design/button_preview.gif" name="previewNews" alt="preview" /> <input type="image" src="res/images/design/button_sendx.gif" name="submitNews" alt="submit" /></td>
                            </tr>
                        </tbody>
                    </table>
                </form>

an extraction of the failing Feature:

Scenario: post news
    [...]
    When I insert "This is a subject!" for newsForm.subject
    And I insert "Magnificient News Post" for newsForm.content
    And I press newsForm.submit
    [...]

a var_dump($_POST) resulted in:

array(2) { ["content"]=> string(22) "Magnificient News Post" ["subject"]=> string(18) "This is a subject!" } 

whereas a request via firefox results in:

array(4) { ["content"]=> string(22) "Magnificient News Post" ["subject"]=> string(18) "This is a subject!" ["submitNews_x"]=> string(1) "0" ["submitNews_y"]=> string(1) "0" } 

my step definitions look as follows:

When /^I insert "(.*?)" for (.*?)\.(.*?)$/ do |input, form, item|
    within 'form[id="' + form + '"]' do |scope|
        scope.fill_in(item, :with => input)
    end
end

When /^I press (.*?)\.(.*?)$/ do |form, item|
    within 'form[id="' + form + '"]' do |scope|
        scope.click_button(item)
    end
end

and finally, my env.rb:

# RSpec
require 'rspec/expectations'

# Webrat
require 'webrat'

require 'test/unit/assertions'
World(Test::Unit::Assertions)

Webrat.configure do |config|
  config.mode = :mechanize
end

class MechanizeWorld < Webrat::MechanizeAdapter
  include Webrat::Matchers
  include Webrat::Methods
  Webrat::Methods.delegate_to_session :response_code, :response_body, :response, :redirected_to
end

World do
  MechanizeWorld.new
  session = Webrat::Session.new
  session.extend(Webrat::Methods)
  session.extend(Webrat::Matchers)
  session
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看起来这个未完成的请求已经有一段时间了。

有一些从该线程链接的补丁,您可以在本地应用到 webrat gem,尽管这可能是您已经完成的,因为您提到您使用了“脏补丁”!

最好对该线程发表评论,特别是回答布莱恩的问题:

传入 0,0 或 1,1 的 X/Y 是否足够,或者有人在做任何依赖于确切值的事情吗?

您可能会看到它得到正确解决。

It looks as though this has been an outstanding request for some time.

There are a few patches linked from that thread which you could apply to the webrat gem locally, although this is probably what you've done already as you mention you've used a 'dirty patch'!

It would probably be best to comment on that thread, in particular responding to Bryan's question:

Is it sufficient to pass in an X/Y of 0,0 or 1,1, or is anyone doing anything that depends on the exact values?

And you may see it get resolved properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文