"undefined method" when using Capybara without any framework - ruby

I'd want to use Capybara in plain Ruby without Rails, Cucumber, Rspec, Test::Unit, etc. for debugging purposes. I configured it as shown here but I still have exceptions when using Capybara API.
require 'rspec'
require 'capybara'
require 'capybara/dsl'
require 'rspec/expectations'
Capybara.app_host = "http://somedomain.com"
Capybara.run_server = false
Capybara.current_driver = :selenium
include Capybara::DSL
visit '/'
page.should have_xpath "//span[#class='my_class']"
visit method works fine. But I have exception undefined method 'have_xpath' for main:Object.
How can I solve this?

You use RSpec matchers without including them:
include RSpec::Matchers

Related

Scraping Coursera results in 404

Why would the following result in a 404?
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
class CourseraScraper
include Capybara::DSL
def initialize
Capybara.default_driver = :poltergeist
Capybara.run_server = false
Capybara.app_host = "https://www.coursera.org/"
visit '/'
save_and_open_page
end
end
CourseraScraper.new
You're not getting a 404 until the page is saved to a file and then opened in your browser and, as a guess, is being driven by some JS being loaded from the wrong referrer or not being loaded because of the referrer.
You can see this by adding assert_text("Take the world's best courses, online.") to the bottom of your test - which passes just fine because poltergeist is working with the normal coursera.org page
I wonder if there's a redirect implemented if you don't have the right referral data. When I run your code, I briefly see the site load before being taken to the 404.
If instead I visit a bad url, I don't get a 404 page at all but instead a message saying "Sorry, the class you were looking for cannot be found. Please check your URL and try again."
https://www.coursera.org/badurl

How can I test the page title while working with Sinatra + MiniTest

I am having the configuration as below :
Rakefile
require 'rake/testtask'
Rake::TestTask.new do |t|
t.pattern = 'spec/*_spec.rb'
end
Gemfile
source 'https://rubygems.org'
gem 'sinatra'
gem 'thin'
gem 'rack-test'
spec_helper.rb
ENV['RACK_ENV'] = 'test'
require 'minitest/autorun'
require 'rack/test'
require_relative '../app'
include Rack::Test::Methods
def app
MyApp
end
app_spec.rb
require_relative 'spec_helper'
describe 'Hello World' do
it 'should have hello world' do
get '/'
last_response.must_be :ok?
# I want to do something like below
last_response.title must_match /home page/i
end
end
How can I test the page title of a view using MiniTest and Sinatra.
You can't check this with minitest alone.
You should be looking at Capybara to achieve this.
Capybara helps you test web applications by simulating how a real user
would interact with your app.
Once setup use has_title? matcher to test the title of the rendered page.
Using Capybara you can test various aspects of your page like its content/text, if it has a particular link or not, a particular button, text field, email field, etc.
You can simulate the behavior of filling up a form and clicking on a button to submit the same. Putting it simply, it just behaves as a real user would by simulating the actions that a user can perform on a given page with what all a user can see on a given page.

Using Capybara live without RSpec

I'd like to use Capybara for live testing, outside RSpec. Just as I'd do using pure Selenium. Is it possible to build scenarios and do logs this way? If not and I have to stick with Selenium - how can I create scenarios and do logs using Ruby?
You can manually create a Capybara session and use that to interact with your production website. For example, the following will go to Google and get the text:
require 'capybara'
session = Capybara::Session.new(:selenium)
session.visit('https://www.google.com')
puts session.text
Note that neither Capybara nor RSpec require the system under test to be a local Ruby project. For example, the following RSpec test goes to Google and checks that the word "Google" appears:
require 'capybara/rspec'
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
feature "google", :js => true do
scenario "should have text" do
visit('/')
page.should have_content(/Google/)
end
end

How to use function present in one file in another file in watir ruby

I am new to Ruby and need help in accessing a function which is present in another file. The scenario is I have 2 files lets say test.rb and functions.rb
in test.rb i have the below code
require 'rubygems'
require 'watir'
require 'win32ole'
require 'erb'
require 'ostruct'
require 'C:\functions'
include Watir
U_RL="some url"
browser
if
ie.text.include?"There is a problem with this website's security certificate."
then
ie.link(:id, 'overridelink').click
end
now in the functions.rb file I have the below code
require 'rubygems'
require 'watir'
require 'win32ole'
include Watir
def browser
ie=IE.new
ie.maximize
ie.goto U_RL
ie.focus
ie.bring_to_front
ie.wait()
end
When I run test.rb, I get the error "Undefined local variable or method 'ie' for main:object
I can see that the browser is opened and even the the mentioned url is coming up, but when the security warning page comes up it is not clicking on ie.link(:id, 'overridelink').click.
Please let me know how to over come this
In your definition of the browser method, the scope of ie is local to that method. It can not be accessed outside of it.
This code needs to be completely refactored, but for now, you could just have browser return the local instance of ie, and set it in test.rb
functions.rb:
def browser
ie=IE.new
ie.maximize
ie.goto U_RL
ie.focus
ie.bring_to_front
ie.wait()
ie # last value is returned in ruby; can be explicit and do `return ie` as well
end
test.rb:
ie = browser
if ie.text.include?"There is a problem with this website's security certificate."
then
ie.link(:id, 'overridelink').click
end
You should require second file. Like this
require_relative 'functions'

Why is "url_for" undefined in my application?

I'm trying to use the url_for extension so my app can accurately find my static assets (tried the static assets extension as well, but it also complained of this same problem).
The problem is this:
undefined method `url_for' for Sinatra::Raffler:Class (NoMethodError)
Now, I've got the required modules listed, as per the url_for README:
require 'rubygems'
require 'sinatra/base'
require 'data_mapper'
require 'lib/authorization'
require 'pony'
gem 'emk-sinatra-url-for'
require 'sinatra/url_for'
But I'm still getting the NoMethodError when I try to call url_for
I have tried a couple of different things in regards to helpers. First, I have a helpers block for an authorization extension:
helpers do
include Sinatra::Authorization
end
So, I thought I could include the url_for helper in there:
helpers do
include Sinatra::Authorization
include Sinatra::UrlForHelper
end
But that didn't resolve the issue, so I just added the line:
helpers Sinatra::UrlForHelper
after that initial helpers do block, but still no resolution.
If you are subclassing Sinatra::Base, you need to include the helpers explicitly:
class Foobar < Sinatra::Base
helpers Sinatra::UrlForHelper
end

Resources