Capybara/Selenium/Chrome 无法更改选择下拉列表的值

发布于 2024-12-01 17:55:18 字数 2116 浏览 1 评论 0原文

当我使用下面的功能、步骤和 HTML 运行测试时,测试执行时没有错误(直到在断言步骤上失败),但我可以看到下拉选择没有发生任何更改。我做错了什么?

HTML:

<div class='field'> 
  <label for="verification_value">Verification Number</label> 
  <input id="verification_value" type="text" /> 
</div> 
<div class='field'> 
  <label for="month">Month</label> 
  <select id="month"> 
    <option value="1">1 - January</option> 
    <option value="2">2 - February</option> 
    <option value="3">3 - March</option> 
    <option value="4">4 - April</option> 
    <option value="5">5 - May</option> 
    <option value="6">6 - June</option> 
    <option value="7">7 - July</option> 
    <option value="8">8 - August</option> 
    <option value="9">9 - September</option> 
    <option value="10">10 - October</option> 
    <option value="11">11 - November</option> 
    <option value="12">12 - December</option> 
  </select> 
 </div>

step:

When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
  select(value, :from => field)
end

feature:

Feature: In order for this to work, a select menu should be changeable

    @javascript
    Scenario: A user follows the steps to successfully do fun stuff
        Given I go to a  page
        And I fill in "verification_value" with "12345"
        And I select "2 - February" from  "month"
        Then I should see "everything worked" within "body"

And I select "2" from "month" 实际上不会产生任何错误,它只是不会更改选择选项。它应该将选择设置为2 - February

我也用firefox驱动程序尝试过,并且得到了相同的结果

更新

关于正则表达式我添加了一个puts行,并且它确实是

When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
  puts "STEP MATCHED"   # << it did put "STEP MATCHED"
  select(value, :from => field)
end

我从默认的 web_steps.rb 复制的,所以我很惊讶它对某些人不起作用,但对我来说效果很好。

When I run my test with the features, steps, and HTML below the test executes without error (until it fails on the assertion steps), but I can see that no change occurs to the drop-down selects. What am I doing wrong?

HTML:

<div class='field'> 
  <label for="verification_value">Verification Number</label> 
  <input id="verification_value" type="text" /> 
</div> 
<div class='field'> 
  <label for="month">Month</label> 
  <select id="month"> 
    <option value="1">1 - January</option> 
    <option value="2">2 - February</option> 
    <option value="3">3 - March</option> 
    <option value="4">4 - April</option> 
    <option value="5">5 - May</option> 
    <option value="6">6 - June</option> 
    <option value="7">7 - July</option> 
    <option value="8">8 - August</option> 
    <option value="9">9 - September</option> 
    <option value="10">10 - October</option> 
    <option value="11">11 - November</option> 
    <option value="12">12 - December</option> 
  </select> 
 </div>

step:

When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
  select(value, :from => field)
end

feature:

Feature: In order for this to work, a select menu should be changeable

    @javascript
    Scenario: A user follows the steps to successfully do fun stuff
        Given I go to a  page
        And I fill in "verification_value" with "12345"
        And I select "2 - February" from  "month"
        Then I should see "everything worked" within "body"

And I select "2" from "month" doesn't actually produce any errors, it just doesn't change the select option. It should be setting the select to 2 - February

I also tried it with the firefox driver, and I get the same result

Update

Concerning the regex I added a puts line, and it was indeed ran

When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
  puts "STEP MATCHED"   # << it did put "STEP MATCHED"
  select(value, :from => field)
end

I copied that from the default web_steps.rb so I'm surprised it's not working for some people, but works fine for me.

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

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

发布评论

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

评论(2

黯淡〆 2024-12-08 17:55:18

看起来 SELECT 标记中有一个额外的双引号。这可能会导致问题。

It looks like you have an extra double quote in the SELECT tag. That may be causing the problem.

琴流音 2024-12-08 17:55:18

我还没有尝试过你的代码,而且现在也没有时间,但你可以考虑测试该正则表达式。如果您试图避免在该步骤定义中需要“I”,我建议您这样做:

When /^(?:I )?select "([^"]*)" from "([^"]*)"$/ do |value, field|
  select(value, :from => field)
end

捕获组后面的问号(用括号表示)意味着捕获组的内容是可选的。如果先前的答案表明了为什么它不起作用,这可能有助于它识别该步骤。

I haven't tried your code, and I don't really have time to right now, but you might consider testing that regular expression. If you're trying to avoid requiring the "I" in that step definition, I'd recommend doing this:

When /^(?:I )?select "([^"]*)" from "([^"]*)"$/ do |value, field|
  select(value, :from => field)
end

The question-mark after a capture group (denoted by parentheses) implies that the contents of the capture group is optional. That might help it identify the step if the prior answer is any indication as to why it's not working.

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