How to restart browser with Capybara on every scenario? - ruby

I run tests using Cucumber in conjunction with Capybara and Selenium-Webdriver. I want restart browser after each scenario. Here is my env.rb. I can add in After section something like this:
After do |scenario|
onError scenario if scenario.failed?
page.driver.browser.close
end
but this kills browser after first scenario passed and all other scenarios failed with reasonable error:
Errno::ECONNREFUSED: Connection refused - connect(2) for "127.0.0.1" port 7055
Is there way to refactor my env.rb to use Before hook to start browser on every scenario?

Simple:
Before do
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end

Related

Simple Ruby Sinatra application runs, does not receive requests, times out

Required Sinatra, got '/', did 'hello, world.', requested application root in browser and received nothing.
Literally the barest of bones Sinatra web application will not run as intended on my Mac, after previously working just fine. Page requests do not show up in the console.
I feel this may be due to force quitting an instance of a running application but after searching for a solution on the web I am at a loss and do not know how to rectify the issue.
edit:
require 'sinatra'
get '/' do
'Hello, World.'
end
Problem does persist after a restart
Definitely the correct URL (exactly as I've been working with all day). localhost:4567 as stated in the output when the application is run:
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
edit 2: scorched earth strategy; full reinstall with rvm fixed this issue.

How to run Capybara against a Rails server?

I have a test that uses OAuth2 gem to make make an HTTP request:
token = client.auth_code.get_token( code, redirect_uri:redirect_uri )
Capybara does not seem to be running the Rails app as a server:
Faraday::ConnectionFailed:
Connection refused - connect(2) for "localhost" port 3000
Here is the config:
Capybara.app_host = "http://localhost:3000"
Capybara.run_server = true
Capybara.server_port = 3000
Capybara.register_driver :rack_test do |app|
Capybara::RackTest::Driver.new app,
follow_redirects:false
end
Various docs seem to suggest that the above config will run the app as a server, but it does not seem to be the case.
How to run Capybara against a server so that the server responds to HTTP requests?
EDIT: After digging in the Capybara code, it seems that different drivers have different servers. Working with Poltergeist. So perhaps Poltergeist doesn't support running a server.
As usual, a small detail was overlooked.
The spec must be tagged as :js. Otherwise it uses Rack::Test and the server is not started.

Redis on Heroku

I am using Redis Objects with Redis To Go on Heroku. I have a counter on a model, like this:
class Performance < ActiveRecord::Base
include Redis::Objects
counter :tickets_sold, start: 0
end
Accessing this value from Heroku console is working great as well.
irb(main):002:0> Performance.last.tickets_sold.value
Performance Load (3.9ms) SELECT `performances`.* FROM `performances` ORDER BY `performances`.`id` DESC LIMIT 1
=> 0
I confirmed that Redis.current is present:
irb(main):003:0> Redis.current
=> # Redis client v2.2.2 connected to redis://ray.redistogo.com:9023/0 (Redis v2.4.11)
However, accessing the same counter from a template on the website runs into a Errno::ECONNREFUSED error.
Connection refused - Unable to connect to Redis on 127.0.0.1:6379
Why is it trying to connect to the local Redis url? Inspecting Redis.current on the website is also failing with the connection error above. Considering that the same command is working just fine from the Heroku console, I'm a little puzzled as to what's going on here. I hope someone has seen this before and knows how to solve it...

Monitoring of an active SMTP service on a website

I am trying to check if network services i.e. HTTP, HTTPs,TCP, SMTP, FTP, Telnet are activated on a website and running correctly without any breaks. I wrote the following code to open the TCP connection to the remote host on remote port and check if the connection can be established with the website.
require 'timeout'
require 'socket'
def ping(host,port)
begin
Timeout::timeout(5) do
s=TCPSocket.new(host,port)
s.close
return true
end
rescue Err::ECONNREFUSED
return false
rescue Err::Timeout
return false
end
end
I also wrote code to monitor the SMTP service already running on a website.
smtp=Net::SMTP.start("mail.google.com",25).started?
But the output is false whereas gmail supports SMTP service. So the question is:
What should be done to monitor the SMTP service already running on a website?

Simple socket program in Ruby

I want to write a simple server socket in Ruby, which, when a client connects to it, prints a message and closes the client connection. I came up with:
require 'socket'
server = TCPServer.open('localhost',8800)
loop {
client = server.accept
Thread.start do
s = client
s.puts "Closing the connection. Bye!"
s.close
end
}
However, when I access "localhost:8800" in my browser, I am not getting that message, instead, it says page not found.. What am I doing wrong here?
It is quite likely that your browser is expecting something on the remote end that talks Http.
This is dependant upon your browser and also the exact URI you typed in. It is also possible that your browser is connecting getting the connection close and then displaying an error page.
If you want to see the server working then use telnet from a command prompt. So in one window type ruby ./myfilename.rb and then in another type telnet localhost 8800

Resources