How to save and load cookies using Ruby + Selenium WebDriver - ruby-on-rails

How can I save all cookies in Ruby's Selenium WebDriver to a txt-file, then load them later? I don't find any answer which does export and import at the same time
In python looks like a very easy way to do that, how to do it in RUBY?
How to save and load cookies using Python + Selenium WebDriver:
import pickle
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
Edits:
I am only using Selenium WebDriver (no capybara etc)

In tests you can use gem show_me_the_cookies (a wrapper/adapter)
cookies = show_me_the_cookies # => [{:name, :domain, :value, :expires, :path, :secure}]
# here you can write them as you like
# and then load and
cookies.each{|c|
create_cookie(c[:name], c[:value], path: c[:path], domain: c[:domain]) # etc.
}
Without gems you can call directly to selenium driver:
driver = Capybara.current_session.driver # or get your selenium driver other way if not using capybara
cookies = driver.browser.manage.all_cookies
# be sure that you've visited a page in your app, selenium cannot create cookies at `about:blank`
driver.browser.manage.add_cookie(name: ..., value:...)

Related

Selenium WebDriver Chrome timeout and invalid URL

I am using Selenium Webdriver Chrome initialized in my Rails app as follows
host=XXX
port=XXX
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("--proxy-server=#{host}:#{port}")
options.add_argument("--headless") # Remove this option to run on Chrome browser
Capybara::Selenium::Driver.new( app,
browser: :chrome,
options: options
)
end
However, it is timing out always when I run a spec giving this error
Net::ReadTimeout upon running the command visit url
URI.parse(current_url) returns #<URI::Generic data:,> which looks incorrect and probably the reason why it is timing out. I looked into the Selenium Webdriver gem and added debugging to see how the request is fetched for this command but for some reason it is not stopping at the pry when the command is get_current_url
Why does current_url look incorrect and why would it not stop for the command get_current_url ?
EDIT: the url is obtained from here and returns the following locally
[6] pry(Selenium::WebDriver::Remote::Bridge)> Remote::OSS::Bridge.new(capabilities, bridge.session_id, **opts).url
=> "data:,"
Adding a pry to the URL method doesn't stop, so wondering how it is obtaining the value.
Ruby: ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a) [x86_64-darwin20]
Selenium-Webdriver: 3.142.7

Disable rest-client output on minitest

I've searched for this, but without success.
I have some tests (minitest) that use RestClient and Webmock. When passing for those tests I always have the request logged, polluting the test ouput:
[$] rake
Run options: --seed 60435
Running:
.........................................................RestClient.get "http://example.com/some_controller/some_action?userLocale=fr_FR", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client/2.0.0 (darwin14.1.0 x86_64) ruby/2.2.1p85" # => 200 OK | 4 bytes
Is there a way to disable this ?
[EDIT]
Just to add, if I call the same address using ruby URL I have nothing logged (even using webmock) so it really is something related with Rest-client.
I already tried to set the ENV['RESTCLIENT_LOG'] variable, but without success.
What about:
RestClient.stub :log, '' do
# Your test code here
end
http://www.rubydoc.info/gems/minitest/4.2.0/Object:stub
You have many other options to redirect the log output:
In your test_helper.rb:
RestClient.log = 'tmp/test.log'
http://www.rubydoc.info/gems/rest-client/1.8.0/RestClient.log=
From the command line:
RESTCLIENT_LOG=tmp/restclient.log bundle exec rails test
In last resort you could monkey patch:
# test_helper.rb
RestClient.class_eval do
def self.log
''
end
end

Why is Ruby failing to connect to my Tor network?

I’m using Ruby on Rails 4.2.7 on Mac El Capitan and just installed the Tor browser (v 6.0.4). I fired up my Tor browser, have verified its running by viewing a couple of web pages, but using this gem — https://github.com/dryruby/tor.rb , when I run my script, Ruby doesn’t believe Tor is running
require 'tor'
...
puts "avaailble: #{Tor.available?}"
puts "version: #{Tor.version}"
Returns
avaailble: false
version:
Indeed, when I try and make a Tor request using the https://github.com/brunogh/tor_requests gem, the web page request returns immediately, leading me to believe the Tor network isn’t being used because in a Tor browser it takes much longer (here is the code I’m using to amen a web page request) …
uri = URI.parse(url)
Net::HTTP.SOCKSProxy('127.0.0.1', 9150).start(uri.host, uri.port) do |http|
f = http.get(uri.path)
end
How do I make my Ruby/Rails code connect to my locally running Tor network?
Edit: In respnse to the answer given, here is what I set my PATH and DYLD_LIBRARY_PATH variables to …
localhost:myproject davea$ echo $PATH
/usr/local/opt/coreutils/libexec/gnubin:/opt/local/bin:/opt/local/sbin:/Users/davea/.rvm/gems/ruby-2.3.0/bin:/Users/davea/.rvm/gems/ruby-2.3.0#global/bin:/Users/davea/.rvm/rubies/ruby-2.3.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin/:/opt/gradle-2.7/bin:/opt/apache-maven-3.3.3/bin:/Users/ davea/.rvm/bin:/usr/local/mysql/bin:/Applications/TorBrowser.app/Contents/MacOS/Tor:/Users/davea/.rvm/bin:/usr/local/mysql/bin:/Applications/TorBrowser.app/Contents/MacOS/Tor
localhost:myproject davea$ echo $DYLD_LIBRARY_PATH
/Applications/TorBrowser.app/Contents/MacOS/Tor:/usr/local/mysql/lib:/usr/local/mysql/lib:
and here is ht output in my Rails console trying the commands listed …
localhost:myproject davea$ rails console
Running via Spring preloader in process 49987
Loading development environment (Rails 4.2.7.1)
2.3.0 :001 >
2.3.0 :002 > Tor::Config::CONFDIR = '/Applications/TorBrowser.app//Contents/MacOS/Tor'
(irb):2: warning: already initialized constant Tor::Config::CONFDIR
/Users/davea/.rvm/gems/ruby-2.3.0/gems/tor-0.1.2/lib/tor/config.rb:21: warning: previous definition of CONFDIR was here
=> "/Applications/TorBrowser.app//Contents/MacOS/Tor"
2.3.0 :003 > Tor.available?
Here is how you can make brunogh/tor_requests work with Tor Browser (easy):
require 'tor_requests'
Tor.configure do |config|
config.ip = "127.0.0.1"
config.port = "9150"
config.add_header('User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0')
end
res = Tor::HTTP.get(URI('https://drew-phillips.com/ip-info/'))
p res.code
p res.body
To get dryruby/tor working involved a bit more work:
It depends on your ENV PATH variable to find the Tor binary and Tor browser has some libraries (at least on Linux) within it's path that aren't found if you try to execute it directly. Seems this should support allowing you to add the path in code instead of relying on PATH in my opinion.
Trying to run Tor Browser's tor binary from the console yields (more on this later, may not apply to Mac):
tor: symbol lookup error: tor-browser_en-US/Browser/TorBrowser/Tor/tor: undefined
symbol: evutil_secure_rng_set_urandom_device_file
Also, installing the Gem from source doesn't give us the latest version available on GitHub and there appears to be a fix to the version method that isn't included with the Gem version 0.1.2. Because of this I pulled the source and tell the program to load the Gem from a custom path.
The working code:
require 'rubygems'
$:.unshift "./tor/lib"
require 'tor'
Tor::Config::CONFDIR = '/home/me/tor-browser_en-US/Browser/TorBrowser/Data/Tor'
p Tor::Config::CONFDIR
p Tor.available?
p Tor.version
Now, in order to have it run successfully, you'll need to set your PATH and LD_LIBRARY_PATH (on Mac this is DYLD_LIBRARY_PATH I believe).
So I run the Ruby code like this:
PATH=/home/me/tor-browser_en-US/Browser/TorBrowser/Tor:$PATH \
LD_LIBRARY_PATH=/home/me/tor-browser_en-US/Browser/TorBrowser/Tor:$LD_LIBRARY_PATH \
ruby tor.rb
This puts Tor Browser as the first search path for binaries and libraries.
Then with this I was able to get the following output:
true
"0.2.8.6"
Hope that helps!

Getting an error when running headless jasmine tests in rails

I've got a rake task set up to run headless jasmine tests on a build server and output the results in junit format. Here's the task:
namespace :jasmine do
desc "Runs Jasmine tests headlessly and writes out junit xml."
task :headless_junit do |t, args|
run_jasmine_tests(Dir.pwd)
end
end
def run_jasmine_tests(output_dir)
require 'headless'
require 'jasmine'
require 'rspec'
require 'rspec/core/rake_task'
output_file = "#{output_dir}/jasmine_results.xml"
Headless.ly do
RSpec::Core::RakeTask.new(:jasmine_continuous_integration_runner) do |t|
t.rspec_opts = ['--format', 'RspecJunitFormatter', '--out', output_file ]
t.verbose = true
t.rspec_opts += ["-r #{File.expand_path(File.join(::Rails.root, 'config', 'environment'))}"]
t.pattern = [Jasmine.runner_filepath]
end
Rake::Task['jasmine_continuous_integration_runner'].invoke
end
end
When I run the this I get this error:
TypeError: jasmine.getEnv(...).currentSpec is null in http://localhost:34002/assets/jquery.js?body=true (line 1129)
expect#http://localhost:34002/assets/jquery.js?body=true:1129
#http://localhost:34002/__spec__/activity_catalog_search_filters_spec.js:15
jasmine.Block.prototype.execute#http://localhost:34002/__jasmine__/jasmine.js:1064
jasmine.Queue.prototype.next_#http://localhost:34002/__jasmine__/jasmine.js:2096
jasmine.Queue.prototype.next_/onComplete/<#http://localhost:34002/__jasmine__/jasmine.js:2086
... LOTS MORE ...
I'm using rails 3.2.13, jasmine 1.3.2, headless 1.0.1, rspec 2.14.1 and Jasmine-jQuery 1.5.8
I think it could be similar to the problem this guy is having:
TypeError: jasmine.getEnv().currentSpec is null
Turns out the issue was with a test that was using jQuery.get to load a url into the dom. An empty string was being passed down as the url (since the test writer didn't really care what was loaded I guess) but that caused jQuery to fetch the current page (the jasmine tests themselves) and load that into the dom. Massive chaos ensued.
The more interesting thing (and perhaps more helpful) was how we figured that out. Turns out the fancy rake task was not the issue. It's just that the headless tests use Firefox and I usually load them manually in chrome, where this error didn't seem to happen. One I had the error reproduced in Firefox it was easy enough to track down the cause with the debugger.
So the bottom line is, if you're ci tests are failing and you can't reproduce it, try loading them manually in Firefox.

Cucumber + Webrat + Selenium guide

I have been using Cucumber and Webrat for a while. I now need to start writing behaviour that involve AJAX interactions so I was thinking to use the Selenium adapter for Webrat.
Can anyone point out a easy and updated step-by-step guide for installing and configuring selenium+webrat+cucumber?
I would like to be able to mix javascript scenario with non-javascript scenarios.
I'm using Selenium with rspec on my project and generate code from a custom formatter for Selenium IDE.
There is many selenium for rails but i success using Selenium-RC http://seleniumhq.org/download/ , so download to your pc.
Here are my steps:
Unzip and run> java -jar selenium-server.jar
Open selenium-client-ruby, read the doc, follow it you will get success!
gem install rspec, rspec-rails version 1.2.6 (it not, you need to comment version restrict of selenium-client source code)
gem install selenium-client
Open Selenium-IDE (Firefox of course), Open Options -> Options -> Formats
Click Add, and paste this code in http://www.techdarkside.com/rspec_export.txt
Now, You just export spec to your spec folder for me, I use spec/features/xxxx_spec.rb see code below.
Very similar approach can find at here
For webrat+cucumber, the latest Rspec book will give all you need. (They don't have selenium + cucumber chapter finish yet)
example
require 'rubygems'
gem "rspec", "=1.2.6"
gem "selenium-client", ">=1.2.15"
require "selenium/client"
require "selenium/rspec/spec_helper"
describe "Google Search" do
attr_reader :selenium_driver
alias :page :selenium_driver
before(:all) do
#selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
end
before(:each) do
selenium_driver.start_new_browser_session
end
# The system capture need to happen BEFORE closing the Selenium session
append_after(:each) do
#selenium_driver.close_current_browser_session
end
it "can find Selenium" do
page.open "/"
page.title.should eql("Google")
page.type "q", "Selenium seleniumhq"
page.click "btnG", :wait_for => :page
page.value("q").should eql("Selenium seleniumhq")
page.text?("seleniumhq.org").should be_true
page.title.should eql("Selenium seleniumhq - Google Search")
page.text?("seleniumhq.org").should be_true
page.element?("link=Cached").should be_true
end
end

Resources