How do I resolve a readtimeout when it is the happy path? - ruby

Without a modal or alert popup. Pressing a button takes me to a page in such a way that selenium can no longer see the controls.
I have tired do reacquire the page without success.
visit([purchase page])
login
sleep(10)
find(:xpath, './/div[#class="tab-pane active"]/div/div/button[#class="btn btn-primary"]').click
Net::ReadTimeout (Net::ReadTimeout)
What I am fairly certain of is that is not not finding my element because I didn't generate a
ElementNotFound
My question is, what are the tools I want to try to get selenium to see the page I wish to manipulate?

One way I would debug this is by putting save_and_open_page before and after xpath. My guess is that it can't find the xpath to click on and its timing out.
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)
save_and_open_page
find(:xpath, './/div[#class="blah active"]/div/div/button[#class="btn btn-primary"]').click
save_and_open_page
If this is not the case you can try and set the timeout to 90 seconds to see if it helps.
# https://github.com/teamcapybara/capybara/issues/1305
Capybara.register_driver :selenium do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 90 # instead of the default 60
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile, http_client: client)
end
Based off your Firefox error
visit https://ftp.mozilla.org/pub/utilities/profilemanager/1.0/.
download profilemanager.mac.dmg
open up profile manager
select default or create a new profile manager
click on start firefox
Error should be fixed now

Related

How do I use my own cookies in capybara?

I'm trying to (ab)use the capybara web testing framework to automate some tasks on github that are not accessible via the github API and which require me to be logged in and click on buttons to send AJAX requests.
Since capybara/selenium is a testing framework it helpfully creates a temporary session which has no cookies in it. I'd like to either stop it from doing that, or else I'd like to know how to load my cookie store into the browser session that it creates.
All I'm trying to do is this:
#!/usr/bin/env ruby
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "https://github.com"
Or this:
#!/usr/bin/env ruby
require 'capybara'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
session = Capybara::Session.new(:selenium)
session.visit "https://www.github.com"
In both cases I get the github.com landing page you'd see as a logged-out user or incognito mode in the browser. I'd like to get my logged-in landing page like I just fired up a web browser myself and navigated to that URL.
Since I have 2FA setup on github that makes automating the login process from the github landing page somewhat annoying, so I'd like to avoid automating logging into github. The tasks that I want to automate do not require re-authenticating via 2FA.
ANSWER:
For MacOSX+Ruby+Selenium this works:
#!/usr/bin/env ruby
require 'selenium-webdriver'
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"debuggerAddress" => "127.0.0.1:20480"}, detach: false)
driver = Selenium::WebDriver.for :chrome, :desired_capabilities => caps
driver.navigate.to "https://github.com"
Then fire up chrome with this:
% /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=/Users/lamont/Library/Application\ Support/Google/Chrome --profile-directory=Default --remote-debugging-port=20480
Obviously the paths will need to be adjusted because they're OSX-centric and have my homedir in them.
There is also a bug in the selenium-webdriver gem for ruby where it inserts a 'detach' option which gets into a fight with 'debuggerAddress':
/Users/lamont/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok': unknown error: cannot parse capability: chromeOptions (Selenium::WebDriver::Error::UnknownError)
from unknown error: unrecognized chrome option: detach
The lib/selenium/webdriver/chrome/bridge.rb file can be edited to take that out as a quick hack:
chrome_options['binary'] = Chrome.path if Chrome.path
chrome_options['nativeEvents'] = true if native_events
chrome_options['verbose'] = true if verbose
#chrome_options['detach'] = detach.nil? || !!detach
chrome_options['noWebsiteTestingDefaults'] = true if no_website_testing_defaults
chrome_options['prefs'] = prefs if prefs
To implement something similar in Ruby, check out this page that goes over that. Thanks to lamont for letting me know in the comments.
You can start chrome using a specific Chrome profile. I am not sure what the ruby implementation would look like, but in python it looks something like:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
options = ChromeOptions()
# more on this line here later.
options.add_experimental_option('debuggerAddress', '127.0.0.1:7878')
driver = webdriver.Chrome(chrome_options=otpions)
In order for this to work you need to do a few things.
manually start chrome from terminal/command prompt with these command line arguments
--user-data-dir=/path/to/any/custom/directory/home/user/Desktop/Chromedir --profile-directory="Profile 1" --remote-debugging-port=7878
make sure "Profile 1" is already existing in the same --user-data-dir (make sure user Profile 1 has necessary chrome://components/
to run any apps that require those components)
you can use any free port in place of 7878
verify that http://localhost:7878 is running and returns value.
This should manually launch chrome with the "Profile 1" profile, and so long as it has logged into the site in question, it will stay logged in like a normal user so long as you follow these instructions to run the tests.
I used this to write a quick netflix bot that clicks the "continue playing" button when it pops up, and it's the only way to get DRM content to play as far as I have found. But it retains the cookies for the login, and also launches chrome with whatever components the profile is set up to have.
I have tried launching chrome with specific profiles before using different methodologies, but this was the only way to really force it to work how I wanted it to.
Edit: There are methods for saving cookie info as well although I don't know how well they work. Check out this link for more info, as my solution is probably not the best solution even if it works.
The show_me_the_cookies gem provides cross-driver cookie manipulation and can let you add new cookies. The one thing to be aware of when using selenium is that you need to visit the domain before you can create cookie for it, so you'll need to do something like
visit "https://www.github.com"
create_cookie(...)
visit "https://www.github.com"
for it to work - first visit just puts the browser/driver in a state where you can create the cookie, second visit actually goes to the page with the cookies set.
I had to tweak the OP's answer (from within her question) to get this going with Ruby in 2022.
Prerequisites
Chromedriver installed and allowed to run even though it's not signed:
> brew install chromedriver
> xattr -d com.apple.quarantine /usr/local/bin/chromedriver
Chrome launched and accepting commands on a specific port:
> /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=~/Library/Application\ Support/Google/Chrome --profile-directory=Default --remote-debugging-port=20480
This created a new profile in Chrome so I signed in to my account and got the browser set up, ready to start interacting with the (legacy EdTech) site I'm trying to automate.
Actual use
require 'selenium-webdriver'
caps = Selenium::WebDriver::Remote::Capabilities.chrome("goog:chromeOptions" => {"debuggerAddress" => "127.0.0.1:20480"})
driver = Selenium::WebDriver.for :chrome, capabilities: caps
driver.navigate.to "https://www.google.com"

Why capybara codes don't work with selenium webdriver?

I am new to Capybara. I have a question about why my Capybara doesn't work when I use it together with Selenium Webdriver.
This is my sample code:
Given(/^I am on the Youtube homepage$/) do
# visit 'http://www.youtube.com'
driver = Selenium::WebDriver.for :firefox
driver.navigate.to('http://www.youtube.com')
end
When(/^I search for "([^"]*)"$/) do |search_term|
fill_in 'search_query', :with => search_term
click_on 'search-btn'
end
Then(/^videos of large rodents are returned$/) do
expect(page).to have_content 'Making Friends with a Capybara'
end
When I run it, it just open Firefox and go to Youtube homepage. But it gets error:
Capybara::ElementNotFound: Unable to find field "search_query".
Everything works with visit 'http://www.youtube.com' command.
You're creating a driver, telling it to navigate to a page and then it's going out of scope so it's getting deleted. the visit line works because it's using the current capybara driver which stays around between test steps. Rather than creating a driver manually you should be registering a driver with Capybara and then specifying which driver to use for the specific test. See drivers
Since capybara sets up a selenium driver by default for use with firefox you can just do
Capybara.default_driver = :selenium
somewhere before running your tests to make all your tests run using selenium with firefox, or since capybara registers selenium as the default javascript driver you can tag any scenarios you want to run in firefox with #javascript as shown here
#javascript
Scenario: do something something
Given ...

Using Capybara and Selenium test suite fail switching from Firefox to Chrome

Using capybara (2.4.4) to test a non Rails application. I have write some tests, I run the tests using selenium with default firefox web browser and all tests are green.
Today I have tried to run same tests against chrome with this configuration:
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.javascript_driver = :selenium
Capybara.default_driver= :selenium
Capybara.app_host = ENV['WEB_SERVER_URL']
When I start the tests they fail becouse Chrome it seems too quick, infact with Firefox to complete the form it took several seconds, instead with Chrome is very very quick but then the tests fails with random errors:
"email" field not found (??)
current_url is not the expected url (?? in the browser I see the correct url!! )
ect ect..
Errors in my opinion have no sense and is very strange because with Firefox all tests are green.
Have you ever noticed this problem?
Chromedriver with Chrome 44 returns from actions much faster than before (incorrectly apparently) so visit is basically fully asynchronous. There have been numerous issues filed against Chromedriver for this such as https://code.google.com/p/chromedriver/issues/detail?id=1158&sort=-id&colspec=ID%20Status%20Pri%20Owner%20Summary
What this means for test stability is potentially needing to specify a longer wait on finds for the first element you're looking for after a visit, and checking for content that should be on the page before checking the current url (because content checks will use capybaras waiting behavior while capybara doesn't provide a waiting url matcher currently). You could also revert to Chrome 43 which will probably fix your issues
In accordance with the link provided by Tom Walpole I have switch from Chrome 44 to Chromium 43. I prefer use Chromium becouse I use chrome during my working day and I want to always have the latest version.
In my Ubuntu 14.04:
$sudo apt-get install chromium-browser
Then:
#spec/spec_helper.rb
require 'selenium/webdriver'
if ENV["USE_CHROME_BROWSER"]
Capybara.register_driver :selenium do |app|
Selenium::WebDriver::Chrome.path = ENV["CHROME_PATH"] if ENV["CHROME_PATH"]
Capybara::Selenium::Driver.new app, browser: :chrome
end
end
Capybara.default_driver = Capybara.javascript_driver = :selenium
Capybara.app_host = ENV['WEB_SERVER_URL']
With this configuration I can easily switch from Firefox (default) to Chromium.
I use dotenv gem to manage the configurations:
#./.env
...
USE_CHROME_BROWSER = true
CHROME_PATH = "/usr/bin/chromium-browser"
...
Based on what you have provided, I cannot tell if Chrome actually does load faster than Firefox. However, the following may be of some help. Wherever you define your Capybara conditions, I suggest replacing them with the following:
Capybara.configure do |config|
config.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
config.default_wait_time = 5 # default is 2 seconds
end
config.javascript_driver = :selenium
config.default_driver= :selenium
config.app_host = ENV['WEB_SERVER_URL']
end
Not only can you change the default wait time for Capybara to search for an element, but your code will be a little DRYer!
You may want to test other JavaScript drivers, like Capybara-Webkit. If this works for Chrome, I would suggest editing the config above, so capybara-webkit is set only when using a Chrome browser.

Feature Testing with Filepicker.io and Capybara

I am currently working on feature testing a Filepicker.io upload form and cannot get a the file upload to work. I am using the Poltergeist driver for Capybara and from the debugging I have been able to do, the Filepicker iframe is never loading. I have confirmed this by using the remote debugging of Poltergeist.
Here is a sample of the test code:
within_fieldset 'Photos' do
click_button 'Find Photo'
end
within_frame 'filepicker_dialog' do
attach_file '#fileUploadInput', Rails.root.join('spec', 'files', 'photo.jpg')
end
And the error being produced:
Failure/Error: within_frame '#filepicker_dialog' do
Capybara::Poltergeist::TimeoutError:
Timed out waiting for response to {"name":"push_frame","args":["#filepicker_dialog"]}. It's possible that this happened because something took a very long time (for example a page load was slow). If so, setting the Poltergeist :timeout option to a higher value will help (see the docs for details). If increasing the timeout does not help, this is probably a bug in Poltergeist - please report it to the issue tracker.
Attempting to manually trigger Filepicker through the javascript console also yields no results.
have you tried to increase the timeout ?
Capybara.javascript_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {:timeout => 50})
end
as the default timeout is 30
The Capybara API for within_frame is a bit weird. It expects a frame name, not a CSS selector for the frame. So Poltergeist is looking for a frame named "#filepicker_dialog" - I suspect this is your problem.

Watir-webdriver: Script working on Firefox but failing on Chrome or Opera. why?

I'm testing this on ark.com..
the following code works perfectly for firefox with watir-webdriver but gives me an error for google chrome and opera browsers respectively.
heres the code:
require "rubygems"
require "watir-webdriver"
b = Watir::Browser.new :ff
b.goto "http://www.ark.com"
# Signing in
7.times{b.link(:text, "Sign in").flash}
sleep 3
b.link(:text, "Sign in").click
sleep 3
# Popup
b.text_field(:name, "login_email").set "email#gmail.com"
b.send_keys :tab
b.text_field(:name, "login_password").set "password"
b.button(:value, "Sign in").click
puts b.title
changing the code as follows gives me errors:
b = Watir::Browser.new :chrome
or
b = Watir::Browser.new :opera
I get the following error message:
You may only interact with visible elements (Selenium::WebDriver::Error::ElementNotVisibleError)
I've tried the following stack overflow suggestion to no avail How do I use Watir::Waiter::wait_until to force Chrome to wait?
Also, my code works perfectly in firefox but not in other browsers, why might this be the case?
Any help would be appreciated. Thanks.
As mentioned in some other posts, you can full screen the browser with the following:
screen_width = b.execute_script("return screen.width;")
screen_height = b.execute_script("return screen.height;")
b.driver.manage.window.resize_to(screen_width,screen_height)
b.driver.manage.window.move_to(0,0)
Then like anonygoose said, the sign in link should be there (assuming your screen isn't too small).
Looking at the website in both Chrome and Firefox I'd say there's some sort of bug or unwanted feature going on with it.
If the web browser you're using is too narrow in width the sign-up button disappears completely.
I'd say that's why you're getting strange errors.
Test this by maximizing the Chrome browser as soon as it launches. From my testing this allows the signup button to be found.
I am not sure of a way to maximize either browser by default, but perhaps someone else on here will know or be able to suggest a way to go about it.

Resources