【问题标题】:Selenium can't find fields with type numberSelenium 找不到类型为 number 的字段
【发布时间】:2014-04-28 22:07:07
【问题描述】:

我在让 Cucumber 使用 HTML5 type="number" 查找字段时遇到问题。我不太喜欢它们在浏览器中的外观,但我有一些字段需要移动设备上的数字键盘,这似乎是最简单的方法。我正在使用 SimpleForm 构建表单,当我设置 :as => :text 时一切正常,但如果我设置 :as => :number,则不会填写这些字段。我没有收到任何错误,只是没有填写该字段。

具体来说,当我有这样的步骤时:

And I fill in "application_form_age" with "52"

那么这个标签就不会被填写:

<input class=​"numeric integer required" id=​"application_form_age" min=​"0" name=​"application_form[age]​" size=​"50" step=​"1" type=​"number">​

但这一个有效:

<input class=​"string required" id=​"application_form_age" name=​"application_form[age]​" size=​"50" type=​"text">​

而且,它只发生在@javascript 场景中。在不需要 @javascript 并且场景不启动浏览器的情况下,这也可以正常工作。

事物的版本:

capybara (2.2.1)
cucumber (1.3.14)
selenium-webdriver (2.41.0)
simple_form (2.1.1)
webrat (0.7.3)

Firefox 29.0

我被难住了。我试着拉出一堆我的应用程序 JS 和 CSS,看看我正在做的事情是否破坏了它,但没有运气。我只是通过强制这些字段在我的测试环境中没有 HTML5 类型号来修补它,但我不想那样生活。还有人看到这个吗?我找不到任何对它的引用,这使它看起来像是我正在做的事情。但我一直无法弄清楚。

【问题讨论】:

    标签: ruby selenium cucumber capybara


    【解决方案1】:

    好的,我发现 Firefox 有一个禁用数字输入字段支持的选项:'dom.forms.number'。

    因此,如果您在 env.rb 中添加以下行,数字输入将被禁用,测试将再次运行。

    Capybara.register_driver :selenium do |app|
      profile = Selenium::WebDriver::Firefox::Profile.new
      profile["dom.forms.number"] = false
      Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
    end
    

    【讨论】:

      【解决方案2】:

      首先,我认为您对这些框架有些困惑。 Cucumber 是一个 BDD 框架,它不会以任何方式自动化浏览器,所以这个问题与它无关(这就是我从你的问题标题中删除它的原因)。

      看起来您正在使用 Capybara,它是一个 ATDD 框架。您可能会考虑向我们展示您用来诊断问题的 Capybara 代码。

      在后台,我假设您使用 Selenium WebDriver,我可以确认 Selenium 与 &lt;input type="number"&gt; 一起工作正常(使用 Firefox 28 测试,这是 selenium-webdriver (2.41.0) 支持的一个)。

      require 'selenium-webdriver'
      
      driver = Selenium::WebDriver.for :firefox
      
      DEMO_PAGE = <<-eos
        data:text/html,
        <input class=​"numeric integer required" id=​"application_form_age" min=​"0" name=​"application_form[age]​" size=​"50" step=​"1" type=​"number">
      eos
      
      driver.get(DEMO_PAGE)
      driver.find_element(:tag_name, 'input').send_keys('25')
      

      因此,您可能希望使用 Capybara 创建一个类似的演示来测试此功能。

      如果演示有效,那么我们需要仔细查看您的应用程序。 否则,请为 Capybara 开发者开张票。

      【讨论】:

      • 强制 Firefox 回到版本 28 可以解决 Selenium 的问题。
      猜你喜欢
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多