Error in Ruby/RSpec/WebDriver > expected respond to 'has_content?' - ruby

Can anyone explain this error?
My spec_helper.rb requires capybara, rspec, and selenium-webdriver.
My test_spec.rb file contains the following:
require_relative 'spec_helper'
#browser = Selenium::WebDriver.for :firefox
#browser.get "http://www.google.com"
describe 'ErrorCheck' do
it 'should log in to Trialnet' do
expect(#browser).to have_content('Search')
end
end
My error:
expected to respond to `has_content?`
./spec/webdriver3_spec.rb:9:in `block (2 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Any idea why this expectation is failing? Is it returning a Boolean without the proper syntax to accept it?

This is happening because the #browser instance variable is not available to the it statement. Notice that your error message doesn't have a reference to the object that it is performing an expectation on (i.e. expected to respond to 'has_content?'.
Here's a contrived demonstration that shows it fail:
require 'rspec'
#x = 1
describe 'One' do
it 'should print 1' do
expect(#x).to eq 1
end
end
Failures:
1) One should print 1
Failure/Error: expect(#x).to eq 1
expected: 1
got: nil
And--by moving the instance variable into the it statement to available--the example passes:
require 'rspec'
describe 'One' do
it 'should print 1' do
#x = 1
expect(#x).to eq 1
end
end
1 example, 0 failures
And--if you use let--you can created a memoized variable that can be shared across examples:
require 'rspec'
describe 'One' do
let(:x) { 1 }
it 'should print 1' do
expect(x).to eq 1
end
it 'should print 2' do
expect(x+1).to eq 2
end
end
Based on your example code, you could use a before block for setup and then use subject, which is probably more appropriate than let (NOTE: snippet below is untested, and the difference between let and subject is covered in other SO answers, various blog posts, and rdoc):
describe 'ErrorCheck' do
before :all do
#browser = Selenium::WebDriver.for :firefox
#browser.get "http://www.google.com"
end
subject(:browser) {#browser} # or let(:browser) {#browser}
it 'should log in to Trialnet' do
expect(#browser).to have_content('Search')
end
end

Related

Why does changing the order of 'it' and 'subject' in RSpec change my test result?

The class being tested qa.rb contains the code:
class QA
def initialize(bugs: 0)
#bugs = bugs
end
def speak
"Hello!"
end
def happy?
#bugs > 0
end
def debug
#bugs = 0
end
end
The RSpec file qa_spec.rb contains the code:
require 'rspec'
require_relative 'qa'
RSpec.describe QA do
describe '#happy?' do
context 'when bugs are more than 0' do
it 'returns true' do
subject { described_class.new(bugs: 1) }
expect(subject).to be_happy
end
end
end
end
The test fails when I run it, and gives me this error:
PS C:\Users\Jobla\repos\TDD> rspec qa_spec.rb
F
Failures:
1) QA#happy? when bugs are more than 0 returns true
Failure/Error: expect(subject).to be_happy
expected `#<QA:0x2e0d640 #bugs=0>.happy?` to return true, got false
# ./qa_spec.rb:9:in `block (4 levels) in <top (required)>'
Finished in 0.02999 seconds (files took 0.16995 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./qa_spec.rb:7 # QA#happy? when bugs are more than 0 returns true
However, when I edit qa_spec.rb and I swap the it and subject lines, the test suddenly passes:
require 'rspec'
require_relative 'qa'
RSpec.describe QA do
describe '#happy?' do
context 'when bugs are more than 0' do
subject { described_class.new(bugs: 1) } #swapped with line below
it 'returns true' do #swapped with line above
expect(subject).to be_happy
end
end
end
end
Tests pass:
PS C:\Users\Jobla\repos\TDD> rspec qa_spec.rb
.
Finished in 0.01003 seconds (files took 0.17993 seconds to load)
1 example, 0 failures
Please could someone explain why does swapping the it and subject lines change the result of the test?
subject is designed to be set in context or describe block, but not in it.
If you do not set subject before it then subject would be set automatically by calling new without parameters on described_class. bugs will be set to default 0. After that, you call it with a block subject { described_class.new(bugs: 1) } inside it, it's the same as if you call described_class.new { described_class.new(bugs: 1) } because subject inside it is an instance of QA class.

Facing this issue #driver.quit giving error in after (:context ) in rspec

I am trying this
require 'rspec'
require 'selenium-webdriver'
RSpec.describe 'New test' do
before :each do
#driver = Selenium::WebDriver.for (:firefox)
end
after :all do
#driver.quit
end
it 'should signup and create a new user' do
p 'testing'
end
end
And this is the error I get
rspec test.rb [13:04:18]
"testing"
.
An error occurred in an `after(:context)` hook.
Failure/Error: #driver.quit
NoMethodError:
undefined method `quit' for nil:NilClass
# ./test.rb:11:in `block (2 levels) in <top (required)>'
Finished in 4.61 seconds (files took 0.17828 seconds to load)
1 example, 0 failures
I am sure it is a small thing that i am missing but could not find it any help would be appreciated.
Is this a FF or selenium version issue?
I am using FF 45 and selenium 2.53 and rspec 3.5.4.
This is an RSpec issue. before/after(:all) hooks only have access to instance variables defined in their scope or outer scope levels, but not to instance variables declared in the current spec being run. after(:each) hooks do have access to the instance variables declared in the current spec.
before :all do
#a = 2
end
context "blah" do
before :all do
#b = 3
end
it "blah blah" do
#c = 4
# #a,#b,#c all accessible here
end
after :each do
##a, #b, #c accessible here
end
after :all do
# #a,#b accessible here
end
end
after :all do
#can access #a
end

Rspec undefined method 'matches?' error

I tried to create a simple test suite in Rspec.
require "selenium-webdriver"
require "rspec"
describe "User" do
before (:all) do
#ch = Selenium::WebDriver.for :chrome
end
it should "navigate to open2test" do
#ch.get"http://www.open2test.org/"
end
it should "enter user name and email" do
#ch.find_element(:id, "name").send_keys "jack"
#ch.find_element(:id, "emailID").send_keys "jack#gmail.com"
end
end
While executing rspec file_name.rb, I get:
"rspec/expectations/handler.rb:50:in `block in handle_matcher': undefined method `matches?' for "navigate to open2test":String (NoMethodError)"
Kindly update what I am missing.
You are calling both it and should methods.
it should "navigate to open2test" do
You should call only it here as you are using RSpec
it "should navigate to open2test" do
# assert something
end
Note that shoulda has the below syntax with Test::Unit
should "do something" do
# assert something
end

Ruby basic RSpec test does not pass

I'm not able to understand why the following Rspec test does not pass -
require "rspec"
require_relative "file-to-be-tested"
describe Customer do
it "is valid with firstname" do
customer = Customer.new("handy")
expect(customer).to be_valid
end
end
for the corresponding Class definition -
class Customer
attr_reader :firstname
def initialize(firstname)
#firstname = firstname
end
end
these two code snippets are in separate files in the same folder, so when i run ~rspec <first-filename> in the terminal, I get the following error -
F
Failures:
1) Customer is valid with firstname
Failure/Error: expect(customer).to be_valid
expected #<Customer:0x007f90e50f3110> to respond to `valid?`
# ./poodr/poodr_rspec.rb:8:in `block (2 levels) in <top (required)>'
Finished in 0.00551 seconds (files took 0.52876 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./poodr/poodr_rspec.rb:6 # Customer is valid with firstname
be_valid is an rspec-rails method, but it looks like you're using just straight rspec. you could do something like:
require "rspec"
require_relative "file-to-be-tested"
describe Customer do
it "is valid with firstname" do
expect { Customer.new('handy') }.to_not raise_error
end
end
What are you expecting the to be_valid test to do? The issue is that the Customer class has no method called valid? which your test is trying to test.
A hack to move your test along if your doing test driven development:
class Customer
def valid?
true
end
end
You now have a method called valid and your test will pass. Obviously it shouldn't always be true so your next step would be to expand the definition of valid?. What check needs to be done to know if a customer is valid or not?

Why browser.text.include? produces error in Watir Ruby script

Issue: I have researched online on how to verify if text exists on my page, but I keep getting error messages. I have attempted using "expect" and one without "expect". For something that seems basic, I am not sure why this is not asserting correctly.
Ruby File:
require "rubygems"
require "watir-webdriver"
require "rspec"
require "selenium-webdriver"
require "rspec/expectations"
#browser = Watir::Browser.new :internet_explorer
begin
if expect(#browser.text.include?("Welcome")).to be_true
##browser.text.include?("Welcome").should == true
puts "Test passed!"
else
puts "Test failed!"
end
end
Error:
test2.rb:61:in <main>': undefined methodexpect' for main:Object (NoMethodError)
You should have your expectations defined inside a test case
describe "IE" do
it "should have test 'Welcome'" do
expect(#browser.text.include("Welcome")).to be_true
end
end
Or if you just want your script to print pass or fail, just do
begin
if #browser.text.include?("Welcome")
puts "Test passed!"
else
puts "Test failed!"
end
end

Resources