Ruby Waitir Gem, getting unexpected results - ruby-on-rails

2 weeks ago I put up post about my watir script timing out on me, I was able to get a solution but I realized to late the results I was getting was different than the person that helped me out. Here is the original post: Ruby Watir Gem, Timing Out on Form Input
require 'watir'
browser = Watir::Browser.new
browser.goto('https://tripadvisor.com/CheapFlightsHome')
browser.text_field(name: 'orig').set('Boston, MA - Logan International Airport (BOS)')
browser.text_field(name: 'dest').set('Milan, Italy - All Airports (MIL)')
browser.execute_script('document.querySelector(".in_date").click()')
browser.execute_script('document.querySelector(".day_28").click()')
browser.execute_script('document.querySelector(".out_date").click()')
browser.execute_script('document.querySelector(".day_2").click()')
browser.span(id: "CHECK_FARES_BUTTON").fire_event :click
puts browser.url
browser.quit
The person who wrote that code got this as a result:
https://www.tripadvisor.com/CheapFlightsSearchResults-g187849-a_airport0.BOS-a_airport1.MIL-a_cos.0-a_date0.20170328-a_date1.20170402-a_nearby0.no-a_nearby1.no-a_nonstop.no-a_pax0.a-a_travelers.1-Milan_Lombardy.html
I have the same code in my script and for some reason I'm only getting:
https://www.tripadvisor.com/CheapFlightsHome
It seems as though the button click isn't happening for me, not sure. I tried both chrome and firefox.

First of all your click actually opens another two window and also the time you are clicking it's not exactly receiving the click, Please use this code, it will work for you and you will be left with your expected window
require 'watir'
caps = Selenium::WebDriver::Remote::Capabilities.firefox(marionette: false)
driver=Selenium::WebDriver.for :firefox, desired_capabilities: caps, profile: "default"
b=Watir::Browser.new driver
b.goto('https://tripadvisor.com/CheapFlightsHome')
b.text_field(name: 'orig').set('Boston, MA - Logan International Airport (BOS)')
b.text_field(name: 'dest').set('Milan, Italy - All Airports (MIL)')
b.execute_script('document.querySelector(".in_date").click()')
b.execute_script('document.querySelector(".day_28").click()')
b.execute_script('document.querySelector(".out_date").click()')
b.execute_script('document.querySelector(".day_2").click()')
begin
b.element(xpath: ".//*[#id='CHECK_FARES_BUTTON']").click
end until b.windows.count>1
b.windows[0].close
b.windows[1].close
puts b.url
b.quit

Firstly, I changed .fire_event :click to .click.
Then there was an error that appeared.
Please enter a valid airport code or city.
Secondly, I tried this:
browser.span(id: "CHECK_FARES_BUTTON").click
browser.span(id: "CHECK_FARES_BUTTON").click
puts browser.url
And it redirected me to:
https://www.tripadvisor.com/CheapFlightsSearchResults-g187849-a_airport0.BOS-a_airport1.MIL-a_cos.0-a_date0.20170401-a_date1.20170402-a_nearby0.no-a_nearby1.no-a_nonstop.no-a_pax0.a-a_travelers.1-Milan_Lombardy.html
To be honest I have no idea why it does not register the input during the first click...
I am using chrome.

Related

Rails Console output breaks when modifying

Totally lacking intuition here.
First i thought i only happens when copying and pasting code from editor. Unfortunately it's more common. Only thing I am doing is trying to insert more code somewhere in the middle of current input or modifying it.
Expected behavior:
Modify input without side effects.
What actually happens:
VERY OFTEN when modifying input it gets messed up.
If that happens every key press will copy and insert current input alongside with pressed character.
Vital notes:
Encoding is set to UTF-8 in terminal(s)
Issue persists on different emulators ( Terminator, gnome-terminal )
Issue persists when using different Ruby runtime console ( IRB, Pry )
Issue appears to be related to Ruby runtime, NOT Linux shell (i guess...)
Issue appears since:
Since system install. Didn't appear on my MacBook Air
System:
ArchLinux, although coworker reported same thing happens on his MacBook Pro.
How to reproduce (works for me):
Open rails console
Type example code: Shift.where(name: "som").where(name: "dom").where(name: "pom")
Navigate cursor to modify first where statement.
Change "som" to "SOM"
Should see it break
Press random key repeatedly to see it break even more.
Images:
Good input
Now i will navigate to first where statement to change "som" to uppercase "SOM"
I basically navigated my cursor and pressed SHIFT+S, SHIFT+O, SHIFT+M
Hope it's clear enough :-)
Thank you!
EDIT 1:
Tried using zsh instead of bash, didn't help
Disabled spring gem, didn't help
EDIT 2:
Folks on reddit suggested that i should check if there are any Ruby readline warnings eg. "Readline is not installed". None of them appear anywhere. Also reinstalled ruby 2.4.1, seems like it's not the problem in my case.
EDIT 3:
I cannot reproduce the issue in a different Rails project.
Issue appears on Rails 5.1.1, meanwhile 5.0.3 works flawlessly.
Is it possible that Rails itself (or rather one of its gems) can be the cause?
I had a very similar issue, and I boiled it down to my coloring.
I had the following in my IRBRC:
class String
def _colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
_colorize(31)
end
def yellow
_colorize(33)
end
end
IRB.conf[:PROMPT][:MY_PROMPT] = {
:AUTO_INDENT => true, # enables auto-indent mode
:PROMPT_I => "[ME]".red + " > ".yellow, # normal prompt
:PROMPT_S => "[ME]".red + " ".yellow, # prompt for continuated strings
:PROMPT_C => "[ME]".red + " * ".yellow, # prompt for continuated statement
:RETURN => "[ME]".red + "=> ".yellow + "%s\n".red # format to return value
}
IRB.conf[:PROMPT_MODE] = :MY_PROMPT
And when I removed the .red and .yellow, everything was just fine.
I think it's because my coloring characters like \e[31m was getting counted as a length 5 characters instead of 0 characters.
My solution was to remove my coloring for the time being. Hopefully someone will come up with a better solution than that.

ElementNotVisibleError when using headless chrome for testing in rails

I test following assumption And I click on the text "2018/2019" within ".year" with capybara/headless_chrome and constantly get the error
element not visible
(Session info: headless chrome=67.0.3396.87)
(Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.5 x86_64) (Selenium::WebDriver::Error::ElementNotVisibleError)
I've already tried to adjust the window size, as suggested here. But it didn't work.
My step definition:
When(/^(?:|I )click on the text "([^"]*)"(?: within "([^"]*)")?$/) do |link, selector|
begin
page.find(:css, selector).click
end
end
The element is actually visible and found by Capybara
[1] pry(#<Cucumber::Rails::World>)> page.find(:css, ".year")
=> #<Capybara::Node::Element tag="a" path="/html/body/div[2]/div[2]/section/div[2]/div/div[1]/div[1]/div[2]/div/div[2]/ul/li/a">
But click fails
[2] pry(#<Cucumber::Rails::World>)> page.find(:css, ".year").click
Selenium::WebDriver::Error::ElementNotVisibleError: element not visible
Why doesn't click work here?
EDIT:
The HAML of the link is
%ul.facet_values
- unselected.each do |facet_value|
%li.filtered{data: {hayf: {text: facet_value.name.downcase}}}
= link_to facet_value.path, title: facet_value.name, class: 'year' do
=truncate("#{facet_value.name}", length: 24)
- if facet.has_counts?
%span.small="(#{facet_value.count})"
I tried headless testing with Poltergeist and got Poltergeist detected another element with CSS selector 'html body header div.container-fluid' at this position which was solved with .trigger('click') and the test passed.
Since this doesn't work in non-headless Chrome or Poltergeist, then the simplest answer is that the element isn't actually visible. Firstly remove the Capybara.ignore_hidden_elements = false setting since that makes no sense when testing an app, and will screw up all sorts of waiting behaviors. Secondly use save_and_open_screenshot to get a picture of what the page actually looks like before you are trying to click the link.
I'm guessing you'll see that the link isn't actually visible at that point and your test is missing a step a user would have to do to make the link visible, is it in a menu where you have to hover over a different element first? is it in a popup where you have to click on something else first?, does it need to be scrolled to? etc.
Finally, you'll generally have less issues if you stick with Chrome via Selenium. Poltergeist uses PhantomJS which hasn't been updated in quite some time and doesn't support modern JS or CSS which can lead to all sorts of strangeness.

Ruby Watir Gem, Timing Out on Form Input

I'm practicing webscraping using Watir, Mechanize and Nokigiri gems.
I'm running into an issue with my Watir script. My plan is to get a list of prices from flights via http://tripadvisor.com/. When I run the script, the Chrome browser opens as it should, the script proceeds to fill out the first parts of the form, origin and destination and then it halts. Here is the error message I'm getting:
This code has slept for the duration of the default timeout waiting for an Element to be present. If the test is still passing, consider using Element#exists? instead of rescuing UnknownObjectException
/home/jaffejoe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/watir-6.2.0/lib/watir/elements/element.rb:515:in `rescue in wait_for_present': element located, but timed out after 30 seconds, waiting for true condition on #<Watir::Input: located: true; {:name=>"rt_leaveday", :tag_name=>"input"}> (Watir::Exception::UnknownObjectException)
from /home/jaffejoe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/watir-6.2.0/lib/watir/elements/element.rb:505:in `wait_for_present'
from /home/jaffejoe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/watir-6.2.0/lib/watir/elements/element.rb:522:in `wait_for_enabled'
from /home/jaffejoe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/watir-6.2.0/lib/watir/elements/element.rb:534:in `wait_for_writable'
from /home/jaffejoe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/watir-6.2.0/lib/watir/elements/element.rb:639:in `element_call'
from /home/jaffejoe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/watir-6.2.0/lib/watir/elements/element.rb:303:in `send_keys'
from watir_test.rb:8:in `<main>' </
Here is my code:
require 'watir'
browser = Watir::Browser.new
browser.goto('https://tripadvisor.com/CheapFlightsHome')
browser.input(name: 'orig').send_keys('Boston, MA - Logan International Airport (BOS)')
browser.input(name: 'dest').send_keys('Milan, Italy - All Airports (MIL)')
browser.input(name: 'rt_leaveday').send_keys('1')
browser.input(name: 'rt_leavemonth').send_keys('06/2017')
browser.input(name: 'retday').send_keys('30')
browser.input(name: 'leavemonth').send_keys('06/2017')
browser.input(value: 'Search Flights').click
puts browser.url
browser.quit
It can't set value on the rt_leaveday or rt_leavemonth because they are hidden inputs. But you can execute a script to click on dateselector:
require 'watir'
browser = Watir::Browser.new
browser.goto('https://tripadvisor.com/CheapFlightsHome')
browser.text_field(name: 'orig').set('Boston, MA - Logan International Airport (BOS)')
browser.text_field(name: 'dest').set('Milan, Italy - All Airports (MIL)')
browser.execute_script('document.querySelector(".in_date").click()')
browser.execute_script('document.querySelector(".day_28").click()')
browser.execute_script('document.querySelector(".out_date").click()')
browser.execute_script('document.querySelector(".day_2").click()')
browser.span(id: "CHECK_FARES_BUTTON").fire_event :click
puts browser.url
browser.quit
=> https://www.tripadvisor.com/CheapFlightsSearchResults-g187849-a_airport0.BOS-a_airport1.MIL-a_cos.0-a_date0.20170328-a_date1.20170402-a_nearby0.no-a_nearby1.no-a_nonstop.no-a_pax0.a-a_travelers.1-Milan_Lombardy.html

How to close all the windows before the next test in a test suite?

[7] pry(#<RSpec::Core::ExampleGroup::Nested_1>)> page.execute_script "window.close()"
Selenium::WebDriver::Error::NoSuchWindowError: Script execution failed. Script: window.close();
The window could not be found
[8] pry(#<RSpec::Core::ExampleGroup::Nested_1>)> page.driver.browser.window_handles
=> ["f1-2"]
I had a browser open with two tabs, the above command does close one but the last tab never closes. It is open but when I try to run page.execute_script "window.close()" it gives the above error.
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.execute_script "window.close()"
end
The above code was working for me sometime back but doesnt work anymore. It gives the same error.
UPDATE:
When I use,
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.driver.browser.close
end
it gives the following error Selenium::WebDriver::Error::UnknownError: 'auto_id' does not refer to an open tab
Two ways you can do it
In line with your technique using JS. You would first need to switch back to your first browser window (window_handle) and then perform "window.close()". (Not Preferred) (Not sure why its not working now for you, did you upgrade server version or different browser?)
Simply use #driver.quit (Preferred)
Update
Just write this once. This will close all windows.
after(:each) do
#driver.quit
end
If you want to close only one browser tab/window/popup, switch to that window_handle and then perform
#driver.close();
page.driver.browser.close closes current tab towards the end and the last (second) tab closes itself after each example.
in case if you are using cucumber, you can the use the BEFORE/AFTER hooks .please refer similar question on stackoverflow
for some more on cucumber,please refer this Cucumber Hooks

Feed Parsing In Rails

I am working on Ubuntu 10.04 and I am using feed-zirra to parse RSS feeds and I have MySQL database.
I am trying to parse RSS feeds from Times of India Top Stories. There seems to be problem with the first link, I am sure TOI guys will correct it soon. But anyway, I dont want to face similar error later so thats why I want to ask you guys how to solve this problem.
Just look at this and especially look for link
<item>
<title>CWG: Abhinav Bindra, Gagan Narang win first Gold for India</title
<description>Abhinav Bindra and Gagan Narang on Tuesday bagged Gold for the men's 10 m air rifle pair's event, getting India its first gold in the 19th Commonwealth Games.</description>
<link>/cwgarticleshow/6688747.cms</link>
<guid>/cwgarticleshow/6688747.cms</guid>
<pubDate>Tue, 05 Oct 2010 04:57:46 GMT</pubDate>
</item>
The link is <link>/cwgarticleshow/6688747.cms</link>
Now, when I click the link, in the view.. its getting routed to http://localhost:3000/cwgarticleshow/6688747.cms instead of http://timesofindia.indiatimes.com/cwgarticleshow/6688747.cms
And the error I am getting is
**Routing Error**
No route matches "/cwgarticleshow/6688747.cms" with {:method=>:get}
How do I correct this type of Error?
Looking forward for your help and support
Thanks
You just need to prepend http://timesofindia.indiatimes.com to the link tag value and you'll be ok.
You can use URI class. You can, for example, define following method
require "uri"
def repair_link(feed_link)
uri = URI.parse(feed_link)
uri.scheme ||= "http"
uri.host ||= "timesofindia.indiatimes.com"
uri.to_s
end
It will set the scheme and host part of the URL if they are not already filled. So if you call it for normal link (like http://foo/bar.cms) then nothing will be changed.
And last thing - you probably should catch exception somewhere as the #parse method raises exception InvalidURIError in case of invalid URI. But it's up to you how you will deal with it.

Resources