问题
I try to do an Selenium-Acceptancetest 'Search Result List' with RSpec spec
- And my Issue is It should verify the count of a Button "Detail"
I would be happy to get help for start thinking as a coder; i am a manual tester stil.
My Problem now is:
Method Error
Failure/Error: expect(@driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 4)
NoMethodError:
undefined method `to' for #<Selenium::WebDriver::Element:0x00000003fd4938>
- Sources: On my way to resolve the issue I tried to modify this and I found this But its not working
thanksfully remaining!
my Test File:
# coding: utf-8
puts "this is #{File.basename(__FILE__)}"
extend RSpec::Expectations
extend RSpec::Matchers
describe 'SEL' do
before(:each) do
@driver = loadDriver()
end
after(:each) do
@driver.quit
end
it 'test_Page (SEL)' do
#get the page
...
#do input keyword 'Restaurant'
...
#click submit
...
#(Works!) temp Validation1: Is there a "Btn Details" in SearchResultList?
expect(@driver.find_element(:xpath, "//a[contains(text(),'Details')]").displayed?)
#(Works not!) Validation: Are there more than "5 Btns Detail"
expect(@driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 5)
end
end
Update after first answer:
Given I use
expect(@driver.find_elements(:xpath, "//a[contains(text(),'Details')]")).to be > 0
it hits that error:
Failure/Error: expect(@driver.find_elements(:xpath, "//a[contains(text(),'Details')]")).to be > 0
expected: > 0
got: [#<Selenium::WebDriver::Element:0xa6219008 id="a2a2c83a-e52d-4464-81ec-4fce07ccc0b6">, #<Selenium::We...64378279">, #<Selenium::WebDriver::Element:0x..f90cc8d0e id="de990e21-eecf-48db-873b-76515dba7c3e">]
回答1:
Given the error message:
undefined method `to' for #<Selenium::WebDriver::Element:0x00000003fd4938>
I suspect your issue is a missing bracket. Normal Rspec syntax is
expect(something).to eq(some value).
I would imagine you are missing the closing parenthesis
expect(@driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 5)
and this should be something more like:
expect(@driver.find_element(:xpath, "//a[contains(text(),'Details')]")).to be > 5
Although I might guess you need to call a size function on the latter part as well as maybe use find elements. Maybe this will also be useful for you.
来源:https://stackoverflow.com/questions/46935723/rspec-cant-verify-there-are-more-than-5-btns-on-search-result-page