黄瓜,jQuery 和重定向
我的页面上有一个选择标签,我通过 jQuery 在 domready 上创建了更改事件的侦听器。在此侦听器中,我使用 url 中的参数将用户重定向到同一页面。
当我测试功能手册时,它工作得很好。
当我运行测试时,它失败了,重定向不起作用。
我怎样才能让它发挥作用?
@javascript
Scenario:
When I select "2010" from "year_select"
And I wait 5 seconds
Then "2010" should be selected for "year_select"
Then I should see "time_sheet?year=2010" with html
测试失败,我在 html 源中看到 time_sheet?year=2011 (默认值),手册中我看到了正确的值。它不是通过 Javascript 更新,而是根据 url 中传递的年份变量(重定向后)更新。
我的 jQuery 代码:
jQuery(document).ready(function(){
var yearSelect = jQuery("#year_select");
yearSelect.change(function(){
window.location = "/time_sheet_admin?year="+yearSelect.val();
});
});
我做错了什么?
I have a select tag on my page, I have made via jQuery on domready a listener for the change event. In this listener I redirect the user to the same page with a parameter in the url.
When I test the functionality manual, it works very well.
When I run the test, it fails, the redirect doesn't work.
How I can make it work ?
@javascript
Scenario:
When I select "2010" from "year_select"
And I wait 5 seconds
Then "2010" should be selected for "year_select"
Then I should see "time_sheet?year=2010" with html
The test fails, I see time_sheet?year=2011 (the default value) in my html source, manual I see the correct value. It is not updated via Javascript, but according to te year variable passed in the url (after the redirect)
My jQuery code:
jQuery(document).ready(function(){
var yearSelect = jQuery("#year_select");
yearSelect.change(function(){
window.location = "/time_sheet_admin?year="+yearSelect.val();
});
});
What I make wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你在这个场景中错过了 _admin 。在场景中使用 time_sheet_admin 而不是 time_sheet。如果我的建议毫无用处,抱歉。
I think u have missed out _admin in the scenario. Use time_sheet_admin instead of time_sheet in the scenario. If my suggestions is useless sorry for that.
我通常测试复杂的 JS 内容时所做的是将它们放入一个函数中,然后测试该函数是否被调用:
*注意不需要变量赋值,因为您只调用了一次。
然后在你的功能:
步骤定义中:
What I usually do for testing complex JS stuff is to put them in a function and then test if that function was called:
*note no need for the variable assignment because you only called it once.
then in your feature:
step definition: