[英]selenium Cucumber hash (ruby)
我正在编写一些测试功能,我从一个表开始,然后更改了表中的数据并得到了一个副本,并希望从散列开始填充表单,这是我的代码:
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
以及将从哈希表填充数据表单的代码:
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
但是我得到了这个错误:
没有将字符串隐式转换为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`然后我可以从转换后的数据中填充全名'
谢谢,
如果将问题缩小为最小 ,完整和可验证的示例,则更容易发现问题的原因。
问题是您正在定义:
@hashes = [
# ...
{
# ...
'fname' => "Joe",
# ...
},
# ...
]
然后尝试通过以下方式访问值"Joe"
:
@hashes['fname']
这是行不通的,因为@hashes
实际上是Array 。 错误信息:
no implicit conversion of String into Integer (TypeError)
就是说,您只能通过其Integer
索引查找数组元素。
您可以通过多种方法来解决此问题,例如在列表中显式使用用户属性的第一个哈希。 (我还建议使用比@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.