使用文本变量填充文本字段,如何?
Using text variable to fill text field, how?
我正在尝试使用变量来填充文本字段。可以用 Capybara 做到这一点吗?编辑:现在还有代码和失败,对不起大家。
describe 'test' do
it 'test0' do
visit 'https://www.submarino.com.br/'
livro = page.first('img[alt~="Livro"]').click
nome = page.find('#product-name-default').text
puts nome
autor = find('table tbody tr', text: 'Autor').text
puts autor
isbn = find('table tbody tr', text: 'ISBN-13').text
puts isbn
end
it 'teste1' do
visit 'https://www.americanas.com.br/'
fill_in 'h_search-input', with: isbn **# <- here is the error**
click_button 'h_search-btn'
end
it 'teste2' do
visit 'https://www.amazon.com.br/'
end
end
HTML 元素
<input id="h_search-input" class="src-input" type="text" name="conteudo" placeholder="tem tuuudo, pode procurar :)" autocomplete="off" tabindex="2" value="">
失败获得:
Failures:
1) teste teste1
Failure/Error: fill_in 'h_search-input', with: isbn
NameError:
undefined local variable or method `isbn' for #<RSpec::ExampleGroups::Teste "teste1" (./spec/teste_spec.rb:23)>
# ./spec/teste_spec.rb:27:in `block (2 levels) in <top (required)>'
感谢您的宝贵时间!
测试是相互隔离的,也是独立的方法。您不能在一种方法中分配局部变量并在另一种方法中访问它,因为它们不是相同的范围 - https://www.sitepoint.com/understanding-scope-in-ruby/。但是,您可以从块之前访问实例变量(因为它们是在测试实例上分配的)。您要么需要在使用它的测试块中获取该值,要么如果您希望在多个测试中使用该值,则可以使用 before(:each)
或 before(:all)
,具体取决于您是否希望为每个测试计算它或仅一次(仅一次通常不是一个好主意,因为如果在测试中更改值,它可能导致测试耦合)
describe 'test' do
before(:each) do
visit 'https://www.submarino.com.br/'
livro = page.first('img[alt~="Livro"]').click
nome = page.find('#product-name-default').text
puts nome
autor = find('table tbody tr', text: 'Autor').text
puts autor
@isbn = find('table tbody tr', text: 'ISBN-13').text
puts @isbn
end
it 'teste1' do
visit 'https://www.americanas.com.br/'
fill_in 'h_search-input', with: @isbn
click_button 'h_search-btn'
# Add whatever expectation you are testing for here
end
it 'teste2' do
visit 'https://www.amazon.com.br/'
... # do something using @isbn
end
end
我正在尝试使用变量来填充文本字段。可以用 Capybara 做到这一点吗?编辑:现在还有代码和失败,对不起大家。
describe 'test' do
it 'test0' do
visit 'https://www.submarino.com.br/'
livro = page.first('img[alt~="Livro"]').click
nome = page.find('#product-name-default').text
puts nome
autor = find('table tbody tr', text: 'Autor').text
puts autor
isbn = find('table tbody tr', text: 'ISBN-13').text
puts isbn
end
it 'teste1' do
visit 'https://www.americanas.com.br/'
fill_in 'h_search-input', with: isbn **# <- here is the error**
click_button 'h_search-btn'
end
it 'teste2' do
visit 'https://www.amazon.com.br/'
end
end
HTML 元素
<input id="h_search-input" class="src-input" type="text" name="conteudo" placeholder="tem tuuudo, pode procurar :)" autocomplete="off" tabindex="2" value="">
失败获得:
Failures:
1) teste teste1
Failure/Error: fill_in 'h_search-input', with: isbn
NameError:
undefined local variable or method `isbn' for #<RSpec::ExampleGroups::Teste "teste1" (./spec/teste_spec.rb:23)>
# ./spec/teste_spec.rb:27:in `block (2 levels) in <top (required)>'
感谢您的宝贵时间!
测试是相互隔离的,也是独立的方法。您不能在一种方法中分配局部变量并在另一种方法中访问它,因为它们不是相同的范围 - https://www.sitepoint.com/understanding-scope-in-ruby/。但是,您可以从块之前访问实例变量(因为它们是在测试实例上分配的)。您要么需要在使用它的测试块中获取该值,要么如果您希望在多个测试中使用该值,则可以使用 before(:each)
或 before(:all)
,具体取决于您是否希望为每个测试计算它或仅一次(仅一次通常不是一个好主意,因为如果在测试中更改值,它可能导致测试耦合)
describe 'test' do
before(:each) do
visit 'https://www.submarino.com.br/'
livro = page.first('img[alt~="Livro"]').click
nome = page.find('#product-name-default').text
puts nome
autor = find('table tbody tr', text: 'Autor').text
puts autor
@isbn = find('table tbody tr', text: 'ISBN-13').text
puts @isbn
end
it 'teste1' do
visit 'https://www.americanas.com.br/'
fill_in 'h_search-input', with: @isbn
click_button 'h_search-btn'
# Add whatever expectation you are testing for here
end
it 'teste2' do
visit 'https://www.amazon.com.br/'
... # do something using @isbn
end
end