Selenium Ruby Reporting - ruby

I'm trying to set the environment for testing using Selenium and selenium-client gem.
I prefer unit test style over RSpec style of tests.
Do I have to build my own system for reporting then?
How can I add exception handling without having begin-rescue-end in each test? Is there any way to do that using mixins?

I'm not sure I understand what your question means in terms of reporting but the selenium-client gem handles both BDD and UnitTesting.
Below is code copied from the rubyforge page:
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.16"
require "selenium/client"
class ExampleTest < Test::Unit::TestCase
attr_reader :browser
def setup
#browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
browser.start_new_browser_session
end
def teardown
browser.close_current_browser_session
end
def test_page_search
browser.open "/"
assert_equal "Google", browser.title
browser.type "q", "Selenium seleniumhq"
browser.click "btnG", :wait_for => :page
assert_equal "Selenium seleniumhq - Google Search", browser.title
assert_equal "Selenium seleniumhq", browser.field("q")
assert browser.text?("seleniumhq.org")
assert browser.element?("link=Cached")
end
end
As for exception handling, UnitTesting handles the exceptions with an Error message.
That being said, I may have misunderstood your question.

Initial build of Extent is available for Ruby. You can view the sample here. Latest source is available at github.
Sample code:
# main extent instance
extent = RelevantCodes::ExtentReports.new('extent_ruby.html')
# extent-test
extent_test = extent.start_test('First', 'description string')
# logs
extent_test.log(:pass, 'step', 'details')
extent.end_test(extent_test)
# flush to write everything to html file
extent.flush

Related

Print all logs in console when running Selenium test with Capybara

I am using Capybara with Selenium driver for my acceptance tests
Versions used:
Selenium 2.53.1
Capybara 2.7.1
cucumber 2.2.0
cucumber-core 1.3.1
According to Selenium/Logging, known log types are browser, client, driver, performance and server. I would like to log all these log types in console output in realtime.
Based on what I understood, to enable full logging I am configuring Capybara as following:
Capybara.configure do |config|
config.run_server = false
config.app_host = ENV['APP_HOST'] # APP HOST from Cucumber.yml file
config.default_driver = :selenium
config.match = :prefer_exact
config.default_max_wait_time = 10
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(
app,
:browser => ENV['BROWSER'].downcase.to_sym,
:desired_capabilities => {
:loggingPrefs => {
:browser => "ALL",
:client => "ALL",
:driver => "ALL",
:server => "ALL"
}
}
)
end
end
Where ENV['BROWSER'] is 'Chrome'.
What I am still unclear is how to print the logs to console. I am looking for a way to print the logs for all scenarios.
I tried adding following code to hooks.rb
After do |scenario|
puts #browser.driver.manage.get_log(:browser)
puts #browser.driver.manage.get_log(:client)
puts #browser.driver.manage.get_log(:server)
puts #browser.driver.manage.get_log(:driver)
end
but #browser is nil.
Also tried following in hooks.rb
After do |scenario|
puts page.driver.browser.manage.logs.get(:browser)
end
But works only for browser logs.
Question: How do I print these logs after each scenario, or even better in realtime?

Acceptance testing of sinatra app using webrat fails

I am trying to test a ruby authentication app using minitest and webrat but get errors.
Tests like visit '/' fail with an error Status 200 expected but was 404.
Tests containing code like fill_in :email, :with => "first#company.com" fail with error Could not find field: :email.
I read several sinatra, testing and webrat documents and forums. Some of them were old and suggested stuff like Sinatra::Default, but github.com/brynary/webrat/wiki/sinatra, Building a Sinatra App Driven By Webrat Tests and Learning From the Masters: Sinatra Internals are new, yet they still fail.
Basically, I didn't like sentence-like syntax of rspec, cucumber etc but do want to do behaviour driven development. I really like the minitest syntax, both tests and output and that is why I choose webrat for BDD. If I'm wrong about expecting webrat to fulfill acceptance testing requirements, please simply tell me that I should use this framework or that one.
Apart from that, the first parts of the main file and test file are below. I hope someone can explain me, what I am missing?
test_file
require "test/unit"
require "minitest/autorun"
require "rack/test"
require 'webrat'
require_relative "../lib/kimsin.rb"
Webrat.configure do |config|
config.mode = :rack
end
ENV["RACK_ENV"] = "test"
class KimsinTests < Test::Unit::TestCase
include Rack::Test::Methods
include Webrat::Methods
include Webrat::Matchers
def app
Sinatra::Application.new
end
def test_create_user
visit "/user/new"
fill_in :username, :with => "first#company.com"
fill_in :password, :with => "abC123?*"
fill_in :confirm_password, :with => "abC123?*"
click_link "Register"
assert 201, last_response.status, "Status 201 expected but was #{last_response.status}.\n#{error}"
assert_contain /Logged in as first#company.com./, "No user created"
assert_contain /Logout/, "Logout link not present"
end
main_file
require "sinatra"
require "erb"
require_relative "../lib/kimsin/version"
require_relative "../lib/kimsin/user"
class Kimsin < Sinatra::Application
use Rack::Session::Pool, :expire_after => 2592000
set :session_secret, BCrypt::Engine.generate_salt
configure :development do
DataMapper.auto_migrate!
end
get "/" do
if session[:user_id]
user = User.get session[:user_id]
email = user.email
erb :index, :locals => { :email => email }
else
email = nil
erb :index, :locals => { :email => email }
end
end
Using Sinatra with Webrat should work fine. I think that the errors that you are seeing are caused by the following method (around line 18 in your test file):
def app
Sinatra::Application.new
end
This is setting up the Sinatra::Application base class to run your tests against when you really need to set up your own subclass Kimsin (because you are creating a modular style Sinatra app), i.e.
def app
Kimsin.new
end
The 404 errors and missing fields are happening because Sinatra::Application doesn't define any of the routes you are testing.
You might also like to take a look at Capybara if you are looking for similar alternatives to Webrat.

Ruby on Rails - Sphinx doesn't index data in Rspec tests

I use sphinx and thinking sphinx to search data in my Ruby on Rails application. So, i have this test to check my work:
require 'spec_helper'
require 'thinking_sphinx/test'
describe SearchesController do
render_views
#Start search server in test mode
before(:all) do
ThinkingSphinx::Test.init
ThinkingSphinx::Test.start
end
describe "when signed in" do
before(:each) do
#user = test_sign_in( Factory( :user ) )
#micropost = Factory( :micropost,
:user => #user,
:content => "Test message of user")
ThinkingSphinx::Test.index
get :find_microposts, :q => #micropost.content #Sending data (by :q => "Text")
end
it "should find micropost of user" do
response.should have_selector( "table.microposts",
:content => #micropost.content)
end
end
end
#Stop search server in test mode
after(:all) do
ThinkingSphinx::Test.stop
end
end
Problem is - ThinkingSphinx::Test.index doesn't work. Why?
sphinx.yml
development:
port: 9312
...
test:
port: 9313
...
My system:
Mac OS X
PostgreSQL 9
Rails 3.1.3
Ruby 1.9.3
Sphinx 2.0.3-release (r3043)
Are you using transactional fixtures with RSpec? Because Sphinx can't access records that aren't saved in the database outside of the RSpec context. Also, you should allow a quarter or half a second after indexing for Sphinx to catch up with the indexed data:
sleep(0.25)
All that said, I would recommend stubbing out Sphinx in your controller tests, and only running Sphinx within integration tests (via cucumber/capybara or otherwise).

In selenium ruby tests can I combine the setup and teardown methods into one location for all my tests?

In my ruby Selenium Tests there is a lot of the same code in every test. How can I best share code between tests? For example my setup and teardown methods are the same in every file, how can I remove them from every file into one shared file or is that even possible?
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new \
:host => "#$sell_server",
:port => 4444,
:browser => "#$browser",
:url => "http://#$network.#$host:2086/",
:timeout_in_second => 60
#selenium.start_new_browser_session
end
def teardown
#selenium.close_current_browser_session
assert_equal [], #verification_errors
end
I've tried putting setup in a shared module and a required file but both present different problems with inheritance of the other methods that need access to the #selenium object that is started. What would be a good design if there is one for sharing the code?
I'm not really sure what test framework you're using, but in rspec you could place it into your spec_helper file and just do a before(:each) / after(:each). I'd check the callback documentation for your framework of choice.
For test unit framework - it seems to work to create a SharedTest class to inherit from Test::Unit::Testcase with setup and teadown methods. Then just subclass the test files SharedTest. The only negative consequence I've found is I had to add a test_default method that does nothing in SharedTest to get it to work. If I name my test method test_default that overides it and seems ok, but not very descriptive...
sharedtest.rb
class SharedTest < Test::Unit::Testcase
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new \
:host => "#$sell_server",
:port => 4444,
:browser => "#$browser",
:url => "http://#$network.#$host:2086/",
:timeout_in_second => 60
#selenium.start_new_browser_session
end
def teardown
#selenium.close_current_browser_session
assert_equal [], #verification_errors
end
def test_default
#puts self
end
end
T01_testcasename.rb
class Test_01_whatever < SharedTest
def test_default
#test code
end
I'm still open to better solutions but this seems to be working for me.

Why am I getting "Test is not a class" in this Ruby script?

I've got a problem with this class
require "test/unit"
require "selenium/client"
class Test < Test::Unit::TestCase
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*chrome",
:url => "http://change-this-to-the-site-you-are-testing/",
:timeout_in_second => 60
#selenium.start_new_browser_session
end
def teardown
#selenium.close_current_browser_session
assert_equal [], #verification_errors
end
def test_test
#selenium.open "/apj/gestionnaire/flux.ex"
#selenium.wait_for_pop_up "_self", "30000"
end
end
it says to me that it's not a class :
/test.rb:4: Test is not a class (TypeError)
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from C:/Documents and Settings/Micro/My Documents/Aptana RadRails Workspace/.metadata/.plugins/org.rubypeople.rdt.testunit/ruby/RemoteTestRunner.rb:301
anyone have any idea ?
Regards
Bussiere
Using Test as your class name is a bad idea. It's an existing constant (referring to a module) as soon you require test/unit
require "test/unit"
Test.class # => Module
Use a different name for your test case.
Using Test as your class name is a bad idea.
Wrong ! Today a new version of the rspec-rails gem has been released fixing this issue in some cases.
You can take a look at the the changelog file:
Fix "Test is not a class (TypeError)" error when using a custom Test class in Rails 4.1 and 4.2. (Aaron Kromer, #1295)

Resources