Selenium:如何使用 javascript 清除表单字段中的值?

发布于 2024-07-11 17:33:28 字数 285 浏览 5 评论 0原文

我正在使用 selenium IDE 和 Selenium-Fitnesse Bridge 固定装置,并且我正在尝试测试当我从表单字段中清除默认值时,我的表单会显示一条错误消息。

因此,当我使用 Selenium IDE 进行录制时,它所做的相当于告诉 Selenium 不要输入任何内容。

| type | text_field |  |

问题是我正在使用的 Fitnesse 装置期望第二个参数不为空。

Selenium 有没有办法“清除值”而不是“不输入任何内容”?

I'm using the selenium IDE and the Selenium-Fitnesse Bridge fixture and I'm trying to test that when I clear a default value out of a form field, my form displays an error message.

So when I record with the Selenium IDE, what it does is the equivalent of telling Selenium to type nothing.

| type | text_field |  |

The problem with this is that the Fitnesse fixture I'm using expects that second argument to not be null.

Is there a way in Selenium to "clear a value" rather than "typing nothing"?

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

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

发布评论

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

评论(3

月棠 2024-07-18 17:33:29

您可以通过 javascript 来完成此操作:

| verifyEval | javascript{this.browserbot.getCurrentWindow().document.getElementById('CONTROL_ID').value = ''} || 

实际上,verifyEval 语句允许您执行您想要的任何 javascript 片段。 使使用 Selenium 解决一些困难的问题变得更加简单。

我使用本教程(今天信不信由你)来解决问题。

You can do it via javascript as such:

| verifyEval | javascript{this.browserbot.getCurrentWindow().document.getElementById('CONTROL_ID').value = ''} || 

Effectively the verifyEval statement allows you to execute any piece of javascript that you'd like. Makes some difficult problems to accomplish with Selenium much simpler.

I used this tutorial (today believe it or not) to figure things out.

惟欲睡 2024-07-18 17:33:29

我用它来让它工作。 reg_start_date 是我的输入字段的 ID。

| storeEval | window.document.getElementById('reg_start_date').value = '' |

来自硒参考:

请注意,默认情况下,该代码段将在“selenium”对象本身的上下文中运行,因此 this 将引用 Selenium 对象。 使用 window 引用应用程序的窗口,例如 window.document.getElementById('foo')

另外,避免使用 javascript{} 包装 javascript 代码,它会不行。

I used this to get it to work. reg_start_date is the id of my input field.

| storeEval | window.document.getElementById('reg_start_date').value = '' |

From the selenium reference:

Note that, by default, the snippet will run in the context of the "selenium" object itself, so this will refer to the Selenium object. Use window to refer to the window of your application, e.g. window.document.getElementById('foo')

Also, avoid wrapping the javascript code with javascript{}, it will not work.

昔梦 2024-07-18 17:33:29

我们一开始也遇到过这个问题。 尝试: | 类型 | 文本字段 | 空白|

在 Fitnesse 文档中查找有关使用空白和 null 的更多信息。

:)

We had this issue at the beginning as well. Try: | type | text_field | blank |

Look for more info on the Fitnesse documentation for the use of blank and null.

:)

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