使用 Behat 填充隐藏输入

发布于 2024-12-06 12:02:21 字数 355 浏览 3 评论 0原文

我正在编写 Behat 测试,我需要更改隐藏输入字段的值

<input type="hidden" id="input_id" ..... />

我需要更改此输入字段的值,但我不断收到

Form field with id|name|label|value "input_id" not found

我一直在使用的步骤

$steps->And('I fill in "1" for "input_id"', $world);

是否有一些特殊的事情需要完成来修改隐藏输入字段?

I am writing Behat tests and I need to change the value of a hidden input field

<input type="hidden" id="input_id" ..... />

I need to change the value of this input field, but I keep getting

Form field with id|name|label|value "input_id" not found

I have been using the step

$steps->And('I fill in "1" for "input_id"', $world);

Is there something special which needs to be done to modify hidden input fields?

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

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

发布评论

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

评论(3

治碍 2024-12-13 12:02:21

尽管用户无法填充隐藏字段,但在某些情况下,希望能够填充隐藏字段以进行测试(因为通常规则有例外)。您可以使用要素上下文类中的下一步按名称填充隐藏字段:

/**
 * @Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/
 */
public function iFillHiddenFieldWith($field, $value)
{
    $this->getSession()->getPage()->find('css',
        'input[name="'.$field.'"]')->setValue($value);
}

Despite the fact that user can't fill hidden fields, there are some situations when this is desirable to be able to fill hidden field for testing (as usually rules have exceptions). You can use next step in your feature context class to fill hidden field by name:

/**
 * @Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/
 */
public function iFillHiddenFieldWith($field, $value)
{
    $this->getSession()->getPage()->find('css',
        'input[name="'.$field.'"]')->setValue($value);
}
栩栩如生 2024-12-13 12:02:21

牧师是对的。如果真实用户可以通过单击按钮或链接通过 javascript 更改输入字段。尝试这样做。用户不可见的字段对于 Mink 来说也是不可见的。

否则你可以做的是从你的上下文中使用 $javascript 调用 $session->executeScript($javascript)

$javascript = "document.getElementById('input_id').value='abc'";
$this->getSession()->executeScript($javascript);

并检查它是否有效

Rev is right. If real user can can change input fields via javascript by clicking a button or link. try doing that. Fields that are not visible to user are also not visible to Mink also.

Or else what you can do is Call $session->executeScript($javascript) from your context with $javascript like

$javascript = "document.getElementById('input_id').value='abc'";
$this->getSession()->executeScript($javascript);

and check if that works

酷遇一生 2024-12-13 12:02:21

这是设计的目的。 Mink 是用户+浏览器模拟器。它模拟真实用户在真实浏览器中可以执行的所有操作。用户肯定无法填写页面上的隐藏字段 - 他只是看不到它们。

Mink 不是爬虫,它是一个浏览器模拟器。 Mink 的整体思想是通过简单干净的 API 来描述真实的用户交互。如果有什么事情,用户无法通过真正的浏览器完成 - 你无法使用 Mink 完成。

(来源:http://groups.google.com/group/behat/browse_thread/线程/f06d423c27754c4d

It's intended by design. Mink is user+browser emulator. It emulates everything, that real user can do in real browser. And user surely can't fill hidden fields on the page - he just don't see them.

Mink is not crawler, it's a browser emulator. The whole idea of Mink is to describe real user interactions through simple and clean API. If there's something, that user can't do through real browser - you can't do it with Mink.

(source: http://groups.google.com/group/behat/browse_thread/thread/f06d423c27754c4d)

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