Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

selenium - RSpec: Cant Verify there are more than 5 Btns on Search Result Page

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">]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...