简体   繁体   English

硒黄瓜哈希(红宝石)

[英]selenium Cucumber hash (ruby)

I am writing some testing features, I started with a table, then I changed data in the table and got a copy and wanted to start population a form from the hash, here is my code : 我正在编写一些测试功能,我从一个表开始,然后更改了表中的数据并得到了一个副本,并希望从散列开始填充表单,这是我的代码:

 Given /^I start with this table$/ do |table|
  @hashes = []
  table.hashes.each do |hash|
  Faker::Config.locale = 'en-US'
    hash['fname'] = "Joe"
  hash['lname'] = "Doe"

    @hashes << hash
  end
end

and the code that will populate the data form from the hash : 以及将从哈希表填充数据表单的代码:

Then /^I can populate full name from the transformed data$/ do
  @hashes.each do |hash|
    puts hash
  end

  fname = $driver.find_element(:id,"applicant_first_name")
  fname.send_keys(@hashes['fname'])   <-- LINE 55

  lname = $driver.find_element(:id,"applicant_last_name")
  lname.send_keys(@hashes['lname'])

end

But I got this error : 但是我得到了这个错误:

no implicit conversion of String into Integer (TypeError) ./features/step_definitions/custom_steps.rb:55:in []' ./features/step_definitions/custom_steps.rb:55:in /^I can populate full name from the transformed data$/' features/brc_epic03_emp_F_bulk_pp.feature:23:in `Then I can populate full name from the transformed data' 没有将字符串隐式转换为Integer(TypeError)./ []' ./features/step_definitions/custom_steps.rb:55:in / []' ./features/step_definitions/custom_steps.rb:55:in /custom_steps.rb:55:in []' ./features/step_definitions/custom_steps.rb:55:in / []' ./features/step_definitions/custom_steps.rb:55:in / []' ./features/step_definitions/custom_steps.rb:55:in / ^我可以从转换后的数据中填充全名$ /'features / brc_epic03_emp_F_bulk_pp.feature:23:in`然后我可以从转换后的数据中填充全名'

Thanks, 谢谢,

It would be much easier to see the cause of the problem if you narrow it down to a Minimal , Complete and Verifiable example. 如果将问题缩小为最小 ,完整和可验证的示例,则更容易发现问题的原因。

The problem is that you are defining: 问题是您正在定义:

@hashes = [
  # ...
  { 
    # ...
    'fname' => "Joe",
    # ...
  },
  # ...
]

and then trying to access the value "Joe" via: 然后尝试通过以下方式访问值"Joe"

@hashes['fname']

This does not work, because @hashes is actually an Array . 这是行不通的,因为@hashes实际上是Array The error message: 错误信息:

no implicit conversion of String into Integer (TypeError) 

is saying that you can only look up an array element by its Integer index. 就是说,您只能通过其Integer索引查找数组元素。

There are various ways you could fix this, such as explicitly using the first hash of user properties in your list. 您可以通过多种方法来解决此问题,例如在列表中显式使用用户属性的第一个哈希。 (I'd also recommend using a more descriptive variable name than @hashes , but that's a separate issue!) (我还建议使用比@hashes更具描述性的变量名,但这是一个单独的问题!)

Then /^I can populate full name from the transformed data$/ do
  @hashes.each do |hash|
    puts hash
  end

  fname = $driver.find_element(:id,"applicant_first_name")
  fname.send_keys(@hashes.first['fname']) # <-- !!!

  lname = $driver.find_element(:id,"applicant_last_name")
  lname.send_keys(@hashes.first['lname']) # <-- !!!
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM