我正在尝试实现一个模块,其中包含要在测试中使用的数据。这是我的模块:
authentication.rb
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<body class="container-fluid">
...
<!-- lots of stuff -->
<div class="card">
<div class="card-header bg-warning">
<div class="row">
<div class="col-auto text-nowrap">
<img src=".../icon.png" />
<h6 class="pl-1 mt-1 font-weight-bold float-right">Label</h6>
</div>
<div class="col row small">
<div class="form-group col-3">
<label>ID</label>
<span class="form-text">1234567</span>
</div>
<div class="form-group col-3">
<label>Name</label>
<span class="form-text">My Name</span>
</div>
<div class="form-group col-3">
<label>Type</label>
<span class="form-text">Category-A</span>
</div>
<div class="form-group col-3">
<label>Code</label>
<span class="form-text">ABCDEFG</span>
</div>
</div>
</div>
</div>
<div class="card-body">
<div class="row">
<div class="col-auto text-nowrap invisible"> <img src=".../icon.png" />
<h6 class="pl-1 mt-1 font-weight-bold float-right">Label</h6>
</div>
<div class="col small">
<!-- Problem Div, breaks the auto-fill feature of its parent when "text-truncate" class is applied. -->
<div id="problem-div" class="text-truncate">
<label>Display Field Label</label>
<span class="form-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
</div>
</div>
</div>
</div>
</div>
...
<!-- more stuff -->
</body>
该模块在module Helpers
module Authentication
def sign_in_as
admin = {
mobile_number: "123456789",
password: "123456"
}
end
end
end
文件中被调用:
spec_helper.rb
spec_helper
以下文件是我用于接收登录凭据的方法:
login_screen.rb
RSpec.configure do |config|
config.include Helpers::Authentication
end
当我从规格文件中的模块调用该函数时,未输入凭据:
login_spec.rb
def login_as(**hash)
mobile_number_textfield.send_keys(hash[mobile_number])
password_textfield.send_keys(hash[password])
next_button.click()
end
如何将哈希传递给登录方法?
答案 0 :(得分:0)
访问符号时,您需要将符号用作哈希键:
def login_as(**hash)
mobile_number_textfield.send_keys(hash[:mobile_number])
password_textfield.send_keys(hash[:password])
next_button.click()
end
您的代码可能会在mobile_number_textfield.send_keys(hash[:mobile_number])
中引发错误。
答案 1 :(得分:0)
您可以执行以下任一操作:
def login_as(mobile_number:, password:)
mobile_number_textfield.send_keys(mobile_number)
password_textfield.send_keys(password)
next_button.click()
end
def login_as(hash)
mobile_number_textfield.send_keys(hash[:mobile_number])
password_textfield.send_keys(hash[:password])
next_button.click()
end
login_as({mobile_number: "02980298098", password: "password"})
答案 2 :(得分:0)
我的解决方案:
在我的模块中,我只创建了一个带有散列的函数:
authentication.rb
module Helpers
module Authentication
def sign_in_as
{
mobile_number: '123456789',
password: '123456'
}
end
end
end
我的 spec_helper 保持不变
spec_helper.rb
require_relative './helpers/authentication'
RSpec.configure do |config|
config.include Helpers::Authentication
end
在我的 login_screen 文件上,向要发送哈希值的每一行添加了一个符号:
login_screen.rb
def login_as(**hash)
mobile_number_textfield.send_keys(hash[:mobile_number])
password_textfield.send_keys(hash[:password])
next_button.click()
end
在我的 login_spec 文件中,我刚刚调用了sign_in_as
函数(在我的模块中创建)
提示:在规格文件中,您不需要模块,因为在 spec_helper 文件中添加的行config.include Helpers::Authentication
可以实现此目的。
login_spec.rb
RSpec.describe('Login') do
before(:all) do
puts "something here"
end
it('should login as founder') do
@login_screen.login_as(sign_in_as)
end
end