Failure/Error: $driver.navigate.to ENV['URL'] in ruby - ruby

While running the test case in the terminal with headless = true. I am getting the below error message.
Error message
Please find the details below:
I am using chromedriver version 86 with chrome browswer version 86 and same has been updated in .env file.
Also, I am running the scripts with Ubuntu in windows.

The error, ERR_NAME_NOT_RESOLVED means that the URL could not be resolved into an IP address. Most likely, ENV['URL'] is not returning a valid URL, there is a typo in the hostname, or it is referencing a private host that the script's DNS cannot resolve. Consider temporarily adding some debugging information, such as
url = ENV['URL']
STDERR.puts "Navigating to [#{url}]"
$driver.navigate.to url
or using Pry to pause your code, and check to make sure things are as you expect:
require 'pry'; binding.pry
$driver.navigate.to ENV['URL']
From the Pry shell, you can see what ENV['URL'] evaluates to, and also manually drive the $driver to see if it works as you expect.
If the URL in fact seems correct, then the issue is likely related to a private host and DNS issue. Ensure you can navigate to the url by other means on that same server (if it's a server, consider trying to ping the hostname, or use curl or wget for a quick check).

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)

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 rest-client ipv6 request failure with Apache 2.2.31

I've been stuck with this for about two days...
I use ruby(version 2.3.3p222) gem rest-client(v2.0.0) to send a GET request with a ipv6 url to the server (Apache/2.2.31):
url = 'https://[fd36:4928:8040:dc10:0000:0000:0000:0160]:8080/resources/1'
resource = RestClient::Resource.new(url, :ssl_version => 'TLSv1', :verify_ssl => false, :headers => {'Authorization' => 'Basic cm9vdDAbCdEfwYXNzMSE='})
resource.get
I got a 400 bad response and the body says:"Your browser sent a request that this server could not understand. Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request"
However I can use curl command with the same parameters and get the right response, so I suspect maybe it's something wrong with the header of my rest-client request.
PS: I also tested with adding the 'host'
header: {'Authorization' => 'Basic cm9vdDAbCdEfwYXNzMSE=', 'host' => '[fd36:4928:8040:dc10:0000:0000:0000:0160]:8080' }
It still failed with the same bad response.
I just noticed the appache error for this request, it says:
"httpd[29124]: [error] Hostname fd36:4928:8040:dc10:0000:0000:0000:0160 provided via SNI and hostname fd36:4928:8040:dc10:0000:0000:0000 provided via HTTP are different
"
The curl command you supplied would translate --user admin:password into the following header:
Authorization: Basic YWRtaW46cGFzc3dvcmQ=
However, you're sending
Basic: cm9vdDAbCdEfwYXNzMSE=
which is not the same thing... so the server is probably complaining about not getting the correct auth...
After debugging and googling for another day, this problem seems to be clear:
From a similar bug report to chrome https://bugs.chromium.org/p/chromium/issues/detail?id=500981, "SNI is only hostnames, and should never contain IPs.". However ruby does use ip as hostname (in this case rest-client is nothing to blame since it just delegate everything down to ruby lib). You can find evidence in Net::HTTP#connect (around line 922):
# Server Name Indication (SNI) RFC 3546
s.hostname = #address if s.respond_to? :hostname=
Just comment out the last line it will work (to workaround this you have to do a monkey patch). Additionally, as pointed out by #alberge, host header does not contain brackets, the final request host header is like this: "FD36:4928:8040:DC10::162", no "[ ]" around.
Also on Apache side, it does something wrong since it just strips off everything from the last colon to get the host name without any extra check- this still exists in version 2.4.10, not sure if it is fixed or not.
This appears to be either a bug in rest-client or a regression in Ruby Net::HTTP.
https://github.com/rest-client/rest-client/issues/583
What version of Ruby are you using? Have you tried using Ruby 2.1 to see if it works there?
EDIT:
This is Ruby Bug #12642. The Host header for an IPv6 address is sent with no enclosing [ ].
Ruby in 2.1.6 - 2.1.10 doesn't have the bug, but versions >= 2.2.0 are affected.
And worse still, there's a bug with setting an explicit IPv6 Host header so you get an exception URI::InvalidComponentError: bad component(expected host component): [

Headless operations don't work inside Sinatra route

I am using the headless and selenium-webdriver gems to launch a headless Firefox browser:
headless = Headless.new(
video: {
frame_rate: 12,
codec: 'libx264'
}
)
headless.start
driver = Selenium::WebDriver.for(:firefox)
With this code I can write the following:
driver.navigate.to("http://google.com")
Yet the following raises an error after I visit '/' in the browser:
get '/' do
driver.navigate.to("http://google.com")
erb :root
end
The error is as follows:
*** Errno::ECONNREFUSED Exception: Failed to open TCP connection to 127.0.0.1:7055 (Connection refused - connect(2) for "127.0.0.1" port 7055)
I'm pretty sure this is because the driver.navigate is not being called in the headless scope, therefore it can't connect to the Firefox instance.
I have also tried using the modular sinatra style, but the same error occurred.
workaround
What I ended up doing is separating the headless server in a separate script. This script has loop and gets input, printing the output of running the command in the headless scope. Then from the sinatra server, i use PTY.spawn to instantiate the server and pass around its stdin and stdout so I can use it in my Sinatra app. This way the headless script is only run once (therefore multiple headless servers aren't started) and I can connect to it from my Sinatra routes. I can't interact with the headless script's variables or methods directly - I need to just use i/o.
I am hoping for an answer which hows how to make the original code work, though (when the sinatra app's routes are called in headless scope)
One possible trick that can help:
this = self
get '/' do
this.driver.navigate.to("http://google.com")
erb :root
end
I ended up getting this working.
At first I thought that what fixed it was doing the headless environment initialization in the scope of a sinatra route, i.e.
get '/' { do_initialization_here }
The real fix may have been in the way I was calling Headless.new (I originally had a bunch of options tacked on and I removed all them).

I am having issue seeing jekyll in browser. Nothing appears when I type 'localhost:4000' into url

I am new to this so hopefully I make sense when I ask this question.
I am doing this on windows 7 through the command prompt.
I have installed jekyll but I am having trouble with seeing the jekyll web page. Nothing appears. What I see when I write 'jekkyl serve' is:
c:\users\spiridon\desktop\portfolio\jekyll serve
Configuration file: c:users/spiridon/desktop/portfolio/_config.yml
source: c:users/spiridon/desktop/portfolio
destination: : c:users/spiridon/desktop/portfolio/_site
generating...
c:/ruby193/lib/ruby/gems/1.9.1/gems/posix-spawn-0.3.9/lib/posix/spawn.rb:164: warning: cannot close fd before spawn
'which' is not recognized as an internal or external command, operable program or batch file.
←[31m Liquid Exception: no such file or directory - python c:/ruby193/lib/ruby/gems/1.9.1/gems/pygments/mentos.py in _posts/2014-10-23-welcome-to-jekyll.markdown←[0m
done.
Please add the following to your Gemfile to avoid polling for changes:
require 'rbconfig'
if RBConfig: :CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
gem 'wdm', >= 0.1.0'
end
Auto-regeneration: enabled for 'c:/users/spiridon/desktop/portfolio'
Configuration file: c:/users/spiridon/desktop/portfolio/_config.yml
server address: http://0.0.0.0:4000/
server running... press ctrl-c to stop.
i am using ruby193
jekyll 2.4.0
this is my _config.yml:
# Site settings
title: Your awesome title
email: your-email#domain.com
description: > # this means to ignore newlines until "baseurl:"
Write an awesome description for your new site here. You can edit this
line in _config.yml. It will appear in your document head meta (for
Google search results) and in your feed.xml site description.
baseurl: "" # the subpath of your site, e.g. /blog/
url: "http://yourdomain.com" # the base hostname & protocol for your site
twitter_username: jekyllrb
github_username: jekyll
# Build settings
markdown: kramdown
I am brand new to this language and most other programming languages. I have also watched youtube videos and searched online for a solution but could not find anything. thank you.
From the Jekyll homepage
While Windows is not an officially-supported platform, it can be used
to run Jekyll with the proper tweaks.
Please follow all instructions on this page dedicated to hints to get Jekyll running on Windows or try this Jekyll Windows project.
The errors you get will probably be gone once you followed one of the above hints but still a short explanation:
'which' is not recognized as an internal or external command, operable program or batch file.
i.e. windows has no program called which.
which does not exists on Windows systems. On unixoid systems it's used to resolve what exactly is executed when you run the command given as attribute to which.
From the Wikipedia:
In the Internet Protocol version 4 the address 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non applicable target.
So it's no surprise opening it in the browser doesn't work. Jekyll cannot start a server at it runs into errors. So it displays this generic IP as server address.

Resources