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

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

Related

NoMethodError: private method `browser_name' called for {:browserName=>:firefox, :version=>nil}:Hash

I am trying to learn selenium Grid, I followed a tutorial, but when I try to run my feature I got this error :
NoMethodError: private method `browser_name' called for {:browserName=>:firefox, :version=>nil}:Hash
here is the env.rb file :
require 'watir-webdriver'
require 'cucumber'
def browser_path
(ENV['BPATH'])
end
def browser_name
(ENV['BROWSER'] ||= 'firefox').downcase.to_sym
end
def environment
(ENV['ENV'] ||= 'grid').downcase.to_sym
end
def browser_version
(ENV['VER'])
end
Before do
def assert_it message, &block
begin
if (block.call)
puts "Assertion PASSED for #{message}"
else
puts "Assertion FAILED for #{message}"
fail('Test Failure on assertion')
end
rescue => e
puts "Assertion FAILED for #{message} with exception '#{e}'"
fail('Test Failure on assertion')
end
end
if browser_path != nil
Selenium::WebDriver::Firefox.path= "#{browser_path}"
end
if environment == :grid
#browser = Watir::Browser.new(:remote, :url=>"http://10.196.60.38:4444/wd/hub", :desired_capabilities=> {browserName: browser_name,version: browser_version})
#browser.window.maximize
else
#browser = Watir::Browser.new browser_name
#browser.window.maximize
end
end
After do
#browser.close
end
Thanks, your help is appreciated.
watir-webdriver is deprecated and will not work with latest versions of Firefox. Please update to the latest version of watir.
Also with latest version of watir, you should be able to just do:
Watir::Browser.new(browser_name, url: "http://10.196.60.38:4444/wd/hub", version: browser_version

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

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-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