Rspec undefined method 'matches?' error - ruby

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

Related

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

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

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

Watir / MiniTest - Undefined local variable or method 'browser'

I have 66 watir scripts that I have been creating over the past week to automate testing on the clients website.
However I have recently found out about a test framework called MiniTest which I am trying to implement now.
The reason I have set the URL as a variable is because there are 5 different sites that these tests need to run on so when they want me to run my pack on a different website I just need to update that 1 variable and not in each individual test.
require 'minitest/autorun'
require "watir-webdriver"
class MPTEST < MiniTest::Unit::TestCase
def setup()
url = "http://thewebsite.com/"
$browser = Watir::Browser.new :chrome
$browser.goto url
end
def test_myTestCase
$browser.link(:text, "Submit your CV").click
sleep(2)
$browser.button(:value,"Submit").click
assert($browser.label.text.includes?("This field is required"))
def teardown
$browser.close
end
end
When running that I receive the following output:
NameError: undefined local variable or method 'browser' for #<MPTEST:0x4cc72f8>c:/directory stuff...
Any ideas?
EDIT I have browser working however now there is an issue with my assert:
New code:
require 'minitest/autorun'
require "watir-webdriver"
class MPTEST < MiniTest::Unit::TestCase
def setup()
url ="http://thewebsite.com"
$browser = Watir::Browser.new :chrome
$browser.goto url
end
def test_myTestCase
$browser.link(:text, "Submit your CV").click
sleep(2)
$browser.button(:value,"Submit").click
assert($browser.label.text.includes?("This field is required"))
end
def teardown
$browser.close
end
end
And the error is:
NoMEthodError: undefined method 'includes?' for "":String
it seems to me you can you use #browser instead of $browser (but the problem might be not in this code)
The exception
NoMEthodError: undefined method 'includes?' for "":String
Is due to strings, in this case the value returned by $browser.label.text do not have an includes? method.
The method you actually want is include? (no plural):
assert($browser.label.text.include?("This field is required"))

Ruby-rspec how can I include a file that is a "common" before (:all) block?

I have the following code:
describe Line do
before :all do
puts "In #{self.class.description}"
end
...
which works fine.
I would like that code (just the three lines) to be in a helper file (called header.rb) but when I try that with:
load "header.rb"
I get:
undefined method `before' for main:Object (NoMethodError)
I also tried require_relative and got the same result.
Option 1: If this applys to all your tests, you can set it in configure
# spec/spec_helper.rb
RSpec.configure do |config|
config.before(:all) do
puts "In #{self.class.description}"
end
config.before(:all) do
puts "More stuff can be added in chain"
end
end
Option 2: If you only want to use it in some tests and the context would be a bit more complex, you can use shared_context
# spec/support/some_shared_context.rb
shared_context "putting class" do
before :all do
puts "In #{self.class.description}"
end
end
# Test file
require 'spec/support/some_shared_context.rb'
describe "test foo" do
include_context "putting class"
# normal test code
end
More about shared_context: https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-context

Webrat Mechanize outside of Rails

I'm trying to use Webrat in a standalone script to automate some web browsing. How do I get the assert_contain method to work?
require 'rubygems'
require 'webrat'
include Webrat::Methods
include Webrat::Matchers
Webrat.configure do |config|
config.mode = :mechanize
end
visit 'http://gmail.com'
assert_contain 'Welcome to Gmail'
I get this error
/usr/lib/ruby/gems/1.8/gems/webrat-0.6.0/lib/webrat/core/matchers/have_content.rb:57:in 'assert_contain': undefined method assert' for #<Object:0xb7e01958> (NoMethodError)
assert_contain and other assertions are methods of test/unit, try to require it and use webrat from inside a test method:
require 'test/unit'
class TC_MyTest < Test::Unit::TestCase
def test_fail
assert(false, 'Assertion was false.')
end
end
anyway i haven't tested it but I have a working spec_helper for rspec if this can interest you:
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
require 'spec/rails'
require "webrat"
Webrat.configure do |config|
config.mode = :rails
end
module Spec::Rails::Example
class IntegrationExampleGroup < ActionController::IntegrationTest
def initialize(defined_description, options={}, &implementation)
defined_description.instance_eval do
def to_s
self
end
end
super(defined_description)
end
Spec::Example::ExampleGroupFactory.register(:integration, self)
end
end
plus a spec:
# remember to require the spec helper
describe "Your Context" do
it "should GET /url" do
visit "/url"
body.should =~ /some text/
end
end
give it a try I found it very useful (more than cucumber and the other vegetables around) when there is no need to Text specs (features) instead of Code specs, that I like the most.
ps you need the rspec gem and it installs the 'spec' command to execute your specs.

Resources