Unable to launch firefox browser and run test with RSpec in RubyMine - ruby

I am new to Ruby and Rspec. I just did below steps.
Clicked on New Project in RubyMine
Gave title as Selenium2(just some random name)
I am using ruby 2.7.1
Created a new directory 'src' under Selenium2 folder
Added test.rb file
I added below code in test.tb file(already did gem install for selenium-webdriver and rspec) as shown in below screenshot.
Right clicked on test.rb and selected Run 'test'.
Got the below output as shown in below screenshot. As a newbie I dont see any issue with the code. But its not even launching Firefox browser and not printing puts in the console.
Am I missing something here?

I think you're doing it wrong.
In your project root folder, add Gemfile file, this Gemfile can have something like this:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.5'
gem 'rspec', '~> 3.9'
gem 'capybara', '~> 3.30'
gem 'selenium-webdriver', '~> 3.142', '>= 3.142.6'
Create a spec/ directory in your project root
Create a log/ directory in your project root for your selinium logs.
Create a spec/spec_helper.rb file with configuration like this:
# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
Selenium::WebDriver.logger.level = :debug
Selenium::WebDriver.logger.output = File.dirname(Dir.pwd) + '/project_dir_name/log/selenium.log'
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.default_driver = :firefox
Capybara.javascript_driver = :firefox
Capybara.app_host = 'http://127.0.0.1:3005'
Capybara.default_max_wait_time = 10
RSpec.configure do |config|
config.before(:each) do
config.include Capybara::DSL
end
end
The above code will setup rspec, capybara. You can change the driver, host and other configs if you want.
Now create a new spec/features/test.rb file with something like this:
require 'spec_helper'
describe 'Google homepage test', js: true do
before(:each) do
visit('https://google.com')
end
describe "First test" do
it "check title" do
expect(page.title).to be == "some text"
end
end
end
Then run: bundle exec rspec spec/features/test.rb from your project root.
If you don't want this way, then the problem is that you're running ruby test.rb, actually test.rb this should be runned via rspec:
$ rspec test.rb
If you're using bundler, then:
$ bundle exec rspec test.rb

Related

Can't run ruby automation file with cucumber - cannot load such file -- capybara (LoadError)

When I run cucumber, I get this error
cannot load such file -- capybara (LoadError)
According to this answer that's because i don't have poltergeist installed.
And if I run gem install poltergeist, I get
ERROR: While executing gem ... (ArgumentError)
wrong number of arguments (given 1, expected 0)
If I run gem list capybara, I just get
* LOCAL GEMS *
For last, this is what part of my env.rb file looks like
Learn more or give us feedback
require 'selenium-webdriver'
require 'capybara'
require 'capybara/poltergeist'
require 'capybara/cucumber'
$browser = ENV['browser'] ||:chrome
$browser = $browser.to_sym
$driver = ENV['driver'] || :selenium
$driver = $driver.to_sym
puts "Driver: #{$driver}"
puts "Browser: #{$browser}"
# For browser Firefox, geckodriver is required: https://github.com/mozilla/geckodriver/releases
# echo $PATH
# cd into geckodriver directory (probably Downloads) and extract from zip/tar
# mv geckodriver /usr/local/bin/ (or whatever your $PATH is)
# Run Firefox with:
# cucumber /dir/some.feature driver=selenium browser=firefox
Capybara.default_driver = $driver
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => $browser,)
end
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {js_errors: false, phantomjs_options: ['--ssl-protocol=auto']})
$platform = 'poltergeist'
end
Any help will be appreciated
You don’t want or need Poltergeist anymore, it’s obsolete. Instead add capybara to your Gemfile, or if not using a Gemfile run gem install capybara. Also you should be doing require ‘capybara/dsl‘ instead of ‘require ‘capybara’`
See : https://github.com/teamcapybara/capybara/blob/master/README.md#setup

uninitialized constant SitePrism from page declariation

I'm trying to set up a basic framework using Capybara, Cucumber and SitePrism, but I keep getting the error, "uninitialized constant SitePrism (NameError)" when I kick off a test.
Gemfile:
gem 'cucumber', '2.3.3'
gem 'capybara', '2.6.2'
gem 'selenium-webdriver', '2.53.0'
gem 'rspec'
gem 'site_prism'
gem 'mime-types', '>2.6', '<2.99.1'
Env.rb
require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'site_prism'
require 'cucumber'
require_rel '../features/pages'
require_rel '../features/classes'
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
Login page
class LoginPage < SitePrism::Page
end
Login class
class Login
def initialize
#current_page = LoginPage.new
end
The error is being thrown on the line with "class LoginPage < SitePrism::Page". RubyMine can't find the SitePrism declaration to go to either. Am I missing something in the setup?
Your error looks 'require' related, but here is how I got it to work via:
https://github.com/thuss/standalone-cucumber
Haven't used cucumber in a while, but the way I see "pages" currently implemented in my Rails project:
Create a file in "features/support/pages"
Follow the namespace conventions
Use modules, then import via the World() method.
Maybe this might work:
features/support/pages/login_page.rb
module Pages
module LoginPage
class LoginPageObj < SitePrism::Page
end
def login_obj
LoginPageObj.new
end
end
end
World(Pages::LoginPage)
Env file:
require 'capybara'
require 'capybara/cucumber'
require 'site_prism'
Capybara.configure do |config|
config.default_driver = :selenium
config.app_host = 'http://www.google.com'
end
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
Notice how I didn't have to explicitly require any pages class, it looks like Cucumber might require it for you?
Note this is without RubyMine (I dont use it). If it works without RubyMine, I'd point fingers to that.
So the reason you were getting this problem is because of the files being auto-loaded. Just ensure you require the gem files first so the namespaces are understood

Is this the right way to require ruby gems?

Hi so i stumbled upon a new way to require my ruby gems in my sinatra app using bundler and i was wondering if this is how i should do it:
My gem file looks like:
source 'https://rubygems.org'
gem 'sinatra'
gem 'thin'
gem 'haml'
My config.ru file looks like:
require 'rubygems'
require 'bundler'
Bundler.require
require './web'
run Sinatra::Application
My web.rb file looks like:
class MyApp
before do
cache_control :public, :max_age => 60
end
not_found do
haml :not_found
end
get '/' do
haml :index
end
end
Get rid of these lines from your config.ru file:
require 'rubygems'
require 'bundler'
Bundler.require
Just make sure you run
bundle install
from the terminal to install your gems before starting the application.

Capybara/Selenium - NS_ERROR_MALFORMED_URI for app on http://localhost

I'm trying to set up RSpec, Capybara & Selenium to test a PHP application running on the traditional localhost:80. Whenever I run the suite, Selenium fails and complains about a malformed URI.
Here's my spec_helper.rb.
require 'bundler/setup'
require 'rspec'
require 'capybara/rspec'
Capybara.server_port = 80
Capybara.app_host = 'http://localhost'
Capybara.run_server = false
Capybara.default_driver = :selenium
And here's my only spec:
require File.dirname(__FILE__) + '/../spec_helper'
describe "visting the website", :type => :request do
it "should display an html page" do
visit ( '/' )
page.should have_selector( 'html' )
end
end
But it bails. What am I missing?
Failures:
1) visting the website should display an html page
Failure/Error: visit ( '/' )
Selenium::WebDriver::Error::UnknownError:
Component returned failure code: 0x804b000a (NS_ERROR_MALFORMED_URI) [nsIIOService.newURI]
# ./spec/requests/sign_in_spec.rb:6:in `block (2 levels) in <top (required)>'
My URI doesn't look malformed to me... visit works if I pass in 'http://localhost' but that's not ideal.
Here's my Gemfile for good measure
gem 'rspec'
gem 'capybara', :git => 'https://github.com/jnicklas/capybara.git'
gem 'launchy'
gem 'ruby-debug19'
Thanks for any help.
#andrykonchin was right - switching back to stable worked
You need install bundler if it is not install
gem install bundler
Then you need to create a file called gemfile in the root directory (For my simple example I am using just capybara and no rspec)
source "http://rubygems.org"
gem "capybara" , "1.1.3"
Create a basic ruby file as follows
require 'bundler/setup'
require 'capybara/dsl'
Capybara.server_port = 80
Capybara.app_host = 'http://google.com'
Capybara.run_server = false
Capybara.default_driver = :selenium
class Browser
include Capybara::DSL
end
w = Browser.new
w.visit("/")
Use the command
bundle install
Now you will be using the stable version of Capybara
For more information on bundler look at http://gembundler.com/

undefined method `have_content' using Cucumber / Capybara / sinatra

I'm trying to validate the start of an initial page for a Sinatra application but am struggling to get the testing framework working. Googling around suggests I add cucumber/rails/rspec or similar, but do I really need to add rails related libraries when not using Rails?
Here's my Gemfile
source :rubygems
gem 'sinatra', '1.3.1'
gem 'json', '1.6.3'
group :test do
gem 'rspec', '2.7.0'
gem 'cucumber', '1.1.3'
gem 'capybara', '1.1.2'
gem 'rack-test', '0.6.1'
end
and my steps:
Given /^a room needs to be surveyed$/ do
end
When /^I start a survey$/ do
survey_tool.start_a_survey
end
Then /^the system will prompt me for a survey summary$/ do
survey_tool.validate_start_a_survey_page
end
and my world extensions
module KnowsTheUserInterface
class UserInterface
include Capybara::DSL
def start_a_survey()
visit '/start_a_survey'
end
def validate_start_a_survey_page ()
page.should have_content('Welcome')
end
end
def survey_tool
#survey_tool ||=UserInterface.new
end
end
World(KnowsTheUserInterface)
and my env
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'survey_tool')
require 'capybara/cucumber'
require 'capybara/rspec'
Capybara.app = Sinatra::Application
Sinatra::Application.set :environment, :test
The error I receive is
Scenario: Start a survey # features/start_a_survey.feature:4
Given a room needs to be surveyed # features/step_definitions/start_a_survey_steps.rb:1
When I start a survey # features/step_definitions/start_a_survey_steps.rb:5
Then the system will prompt me for a survey summary # features/step_definitions/start_a_survey_steps.rb:9
undefined method have_content' for #<KnowsTheUserInterface::UserInterface:0x007f910465fc38> (NoMethodError)
./features/support/world_extensions.rb:10:invalidate_start_a_survey_page'
./features/step_definitions/start_a_survey_steps.rb:10:in /^the system will prompt me for a survey summary$/'
features/start_a_survey.feature:7:inThen the system will prompt me for a survey summary'
I think the problem here is that you do not have the rspec expectations included. A Capybara node has a has_content? method.
It is the rspec expectation that, when given 'have_content' as an argument, calls 'has_content?' on the target.
So try adding 'rspec-expectations' to your Gemfile, and adding require 'rspec/expectations' into your module.

Resources