SSL Reuse error when using ruby mechanize gem - ruby

I had my mechanize script running for roughly 3 hours before I got the following error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-
2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `initialize':
A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond. - connect(2) (Errno::ETIMEDOUT)
I'm assuming it's due to the fact that it's been running for 2-3 hours but was wondering if anyone had any special insight about the error
Here is my script
require 'rubygems'
require 'mechanize'
def next_button(web_page)
web_page.page.search('.next a').each do |next_button|
web_page.click(next_button)
web_page.page.search('.listing_content').each |info|
get_info(info)
end
next_button(web_page)
end
end
def get_info(info)
infos = info.at_css('.url').text.strip
address = info.css('.street-address').text.strip
city = info.css('.locality').text.strip
state = info.css('.region').text.strip
zip = info.css('.postal-code').text.strip
end
web_page = Mechanize.new
web_page.user_agent_alias = "Linux Firefox"
web_page.get(HOME_URL)
web_page.page.search('.page-navigation a').each do |pagination_link|
web_page.page.search('.categories-list a').each do |link|
web_page.click(link)
web_page.page.search('.listing_content').each do |info|
get_info(info)
end
next_button(web_page, worksheet)
end
web_page.click(pagination_link)
end
Thanks for any help in advance.

In my opinion persistent connections are more trouble than they're worth. When this happens to me I check to make sure I have the latest version of mechanize, and if it still happens, I fall back to mechanize 1.0.0 which doesn't use persistent connections.

Related

Prevent phantomjs from raising Capybara::Poltergeist::StatusFailError when requesting never ending assets

I am having some issues with Capybara::Poltergeist::Driver
When I visit the the following url with poltergeist, I am exerpiencing an issue where an asset that seemingly doesn't exist takes for ever to load and eventually an error gets raised: https://www.feinstein.senate.gov/public/index.cfm/e-mail-me
$ brew install phantomjs
$ gem install capybara -v 2.17.0
$ gem install poltergeist -v 1.7.0
$ gem install selenium-webdriver -v 2.53.4
Then in irb:
require 'capybara/poltergeist'
module Drivers
class Poltergeist < Capybara::Poltergeist::Driver
def needs_server?
false
end
end
end
Capybara.register_driver :poltergeist_errorless do |app|
options = ['--load-images=no', '--ignore-ssl-errors=yes', '--ssl-protocol=any', '--disk-cache=true', '--max-disk-cache-size=500000']
Drivers::Poltergeist.new(app, js_errors: false, phantomjs_options: options)
end
session = Capybara::Session.new(:poltergeist_errorless)
session.visit('https://www.feinstein.senate.gov/public/index.cfm/e-mail-me')
After 10-20 seconds, the request fails, and I get back a Capybara::Poltergeist::StatusFailError exception with a message that says:
Request to 'https://www.feinstein.senate.gov/public/index.cfm/e-mail-me' failed to reach server, check DNS and/or server status - Timed out with the following resources still waiting https://sdc1.senate.gov/NEED_VALUE/wtid.js
But if I then call:
session.save_screenshot('/tmp/sc.png', full: true)
the outputted screenshot is shows that the rest of the page loaded just fine. If this were any other browser, it would just continue to function happily without worrying about an asset that is taking forever to load.
Is there anyway to configure phantomjs to not wait for this asset and to not raise this exception?
The easiest way to deal with that is to use Poltergeists blacklist to block the url - https://github.com/teampoltergeist/poltergeist#customization -
and/or - https://github.com/teampoltergeist/poltergeist#url-blacklisting--whitelisting
If your situation is more dynamic you could rescue the exception, parse out the URL, add it to the blacklist, and then retry the visit.
Additionally, there is no need to override needs_server?. If you don't pass a second parameter (the app to run) to Session#new (which you aren't doing) then needs_server? is irrelevant.
I'll play around with the session timeout params:
session = Capybara::Session.new(:poltergeist_errorless, :timeout=>ASSET_LOAD_TIME)

PhantomJS - Watir Ruby - Watir::Exception::ObjectDisabledException: element present and enabled, but timed out after 30 seconds

I have downloaded PhantomJS and added the code below to my script:
Selenium::WebDriver::PhantomJS.path = 'C:\phantomjs-2.1.1-windows\bin\phantomjs.exe'
#browser = Watir::Browser.new :phantomjs
#browser.goto "www.samplewebsite.com"
Then I getting below error message:
Watir::Exception::ObjectDisabledException: element present and
enabled, but timed out after 30 seconds, waiting for
"ctl00$ContentPlaceHolder1$Login1$UserName",
:tag_name=>"input"}> to not be disabled
Here is the code in the login.rb file:
def browser()
Watir::Wait.until{#browser.text.include? 'Login'}
end
def credentials()
#browser.wait
username = #browser.text_field(name:"ctl00$ContentPlaceHolder1$Login1$UserName").send_keys 'abcd123' #Is this line of code showing the error
password = #browser.text_field(name:"ctl00$ContentPlaceHolder1$Login1$Password").send_keys'Password'
end
I'm pretty sure it is This Bug. It's been fixed in Watir 6.8.2, which was released yesterday.
Read some post from other sources and I tried maximize the window: #browser.window.maximize then it worked! And yes this issue is not related to Page Object. Regarding the error message I didn't write them, I copied and pasted it from my IDE log.

How to write a simple Cucumber script

I'm following the tutorial to run my first Cucumber script:
Feature: guru99 Demopage Login
In order to Login in Demopage we have to enter login details
Scenario:
Register On Guru99 Demopage without email
Given I am on the Guru99 homepage
When enter blank details for register
Then error email shown
I have the project in Idea but when I run it I get errors.
When using chrome:
Failed to open TCP connection to 127.0.0.1:9515 (No connection could be made because the target machine actively refused it.
I have no idea how to resolve it.
When using Firefox, the script successfully opens the browser but fails after that:
require 'watir'
require 'colorize'
Selenium::WebDriver::Firefox::Binary.path='C:\soft\Mozilla Firefox\firefox.exe'
case ENV['BROWSER']
when 'chrome'
browser = Watir::Browser.new :chrome
when 'firefox'
browser = Watir::Browser.new :firefox
end
Given(/^I am on the Guru99 homepage$/)do
#browser = Watir::Browser.new :firefox
#browser.goto "http://demo.guru99.com"
end
When(/^enter blank details for register$/) do
browser.text_filed(:name,"emaiid").set("")
browser.button(:name,"btnLogin").click
end
Then(/^error email shown$/) do
puts "Email is Required!".red
browser.close
end
And returns:
NoMethodError: undefined method `text_filed' for nil:NilClass
on this line:
browser.text_filed(:name,"emaiid").set("")
I found some references that I need to write a class to call a method. I tried it but didn't succeed.
Connection refused, I'm unsure but "Watir+Cucumber Connection refused" looks a fix.
Copy pasta:
AfterConfiguration do |config|
yourCodeStartUp() # Put your SETUP code here including the launch of webdriver
at_exit
yourCodeTearDown() # Put your CLOSING routine here
puts 'stopped'
end
end
The code error is a typo, it should be browser.text_field(...
Regarding the issue you are observing on chrome, it sounds like you need to update chromedriver (and make sure the exe is in PATH). If you are running chrome v56-58, you need ChromeDriver 2.29.
Regarding the NoMethodError: undefined method error, you have a typo when you call the text_field method (i.e. browser.text_filed).
Nope, I was mistaken, I had an older version of chromedriver. Now it's running in chrome as well. Thank you very much. Appreciate your time!
So, the answers are:
1. Update chromedriver.
2. Check your code for typos one more time.
Was really easy but took me a lot of time%

Ruby Windows - Eventmachine gem got installed, but throws error when executing EM.run

Sir, I follow the link https://github.com/eventmachine/eventmachine/wiki/Building-EventMachine-with-SSL-on-Windows
to install eventmachine gem in my windows system.
The gem got successfully installed.
But, I am getting this following error, when I used the following piece of code to connect to websocket and tried to fetch some data.
require 'faye/websocket'
require 'eventmachine'
require 'json'
EM.run {
ws = Faye::WebSocket::Client.new('wss://ws.binaryws.com/websockets/v3')
ws.on :open do |event|
p [:open]
ws.send(JSON.generate({ticks: 'frxUSDJPY'}))
end
ws.on :message do |event|
p [:message, event.data]
end
}
Please help.
terminate called after throwing an instance of 'std::runtime_error'
what(): Encryption not available on this event-machine
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Watir webdriver Errno::ECONNREFUSED: No connection could be made because the target machine actively refused it. - connect(2)

Updated:
I'm running the code below to execute my sample test cases: (Windows 7, Watir 3.0.0, Watir-webdriver-0.6.1)
require "watir-webdriver"
require 'test/unit'
class Teste1
$var = Watir::Browser.new :chrome
def met1
$var.goto 'google.com'
$var.text_field(:name, "q").set 'sample'
$var.button(:name =>'btnG').click
end
end
class Teste2 < Test::Unit::TestCase
$test = Teste1.new
def test_gomet1
$test.met1()
end
end
The browser opens but the script throws the following error:
test_gomet1(Teste2):
Errno::ECONNREFUSED: No connection could be made because the target machine actively refused it. - connect(2)
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:82:in `response_for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:38:in `request'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:598:in `raw_execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:576:in `execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:99:in `get'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/common/navigation.rb:14:in `to'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.1/lib/watir-webdriver/browser.rb:63:in `goto'
maisum.rb:11:in `met1'
maisum.rb:21:in `test_gomet1'
Can anyone help me on that?
I cannot get that exception for the test_closeVar test, however I can reproduce it for your other two tests. You get that exception when you try to interact with the browser after it has been closed.
When using Test::Unit, keep in mind that the tests run in alphabetically order. Therefore your test_closeVar test will close the browser before test_gomet1 and test_gomet2 even run. test_gomet1 and test_gomet2 will throw that exception because the browser is closed.
As a quick fix, you could add numbers to the test names to get them to run in a specific order.
The long term fix though is really to make your tests independent so that order does not matter.
Update
A couple of observations:
The problem occurs using Selenium-Webdriver, so not a Watir-Webdriver specific issue.
The script runs fine in Firefox, so it might be a chromedriver specific issue.
Workaround: It seems like the chrome browser does not like being declared outside the test case. I do not understand why, but the quick fix is to declare the browser in the setup of the test case. If you want to use the same browser for each test, you can just declare it if it does not already exist.
The following will run (though I would suggest cleaning it up to reduce the usage of global variables):
class Teste1
def met1
$var.goto 'google.com'
$var.text_field(:name, "q").set 'sample'
$var.button(:name =>'btnG').click
end
end
class Teste2 < Test::Unit::TestCase
def setup()
unless defined?($var)
$var = Watir::Browser.new :chrome
end
$test = Teste1.new()
end
def test_gomet1
$test.met1()
end
end

Resources