0

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?

enter image description here

2
  • you're launching it as a Ruby script. Does it work the way you need it in case you do it manually from the command line?
    – Olivia
    Jun 1, 2020 at 14:44
  • No Olivia! I tried to run it from project root from command line.. No luck!.
    – Kiran
    Jun 1, 2020 at 15:21

1 Answer 1

2

I think you're doing it wrong.

  1. 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'
    
  2. Create a spec/ directory in your project root

  3. Create a log/ directory in your project root for your selinium logs.

  4. 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
    
  5. The above code will setup rspec, capybara. You can change the driver, host and other configs if you want.

  6. 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
    
  7. 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
2
  • Is it necessary to do all the above steps? Because I was referring this video in YouTube => youtube.com/watch?v=ny2Mdl5uE9Q (just try to watch it between 3.15 mins to 4.00 mins). He is not doing all the above steps. Am very curious to know!! Really appreciate your reply. :)
    – Kiran
    Jun 1, 2020 at 15:20
  • That tutorial is very old. It's better to use Capybara. Anyways that all the above is not necessary, I've just shown how it can be done better. I've updated the answer on how to run the script.
    – cdadityang
    Jun 2, 2020 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.