Cucumber + Capybara file upload not working for me - ruby

I'm trying to test a simple file upload with Capybara. Here is my
Gemfile.lock
capybara (1.1.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
and my selenium-webdriver version is 2.18. Here is my web_steps file (it's generated):
When /^(?:|I )attach the file "([^\"]*)" to "([^\"]*)"(?: within "([^\"]*)")?$/ do |path, field, selector|
with_scope(selector) do
attach_file(field, path)
end
end
This is my feature on the line to upload file:
Then I attach the file "features/resources/empty.file" to "file" within "#uploadForm"
Actually it ran fine and green on the line, but the input didn't pick up any file so the test failed after that because there was no file selected.
Here is my form:
%form#uploadForm{:action => "/upload", :method => "POST", :enctype => "multipart/form-data"}
%input{:type => "file", :name => "file", :id => "file"}
It's pretty basic, but I'm not sure why it's not working.

Toy,
I suggest to:
attach_file(field, File.expand_path(path))
Instead of using absolute path, especially if you collaborating.
edit: File.expand_path not File.extend_path

I just wanted to answer my question, the problem is I didn't use the full path. Somehow capybara doesn't recognise relative path.

Related

Parsing direct and indirect Gems of Gemfile.lock file [duplicate]

This question already has answers here:
How to list ruby production only dependencies using Gemfile.lock and LockfileParser class
(2 answers)
Closed 1 year ago.
I'm trying to parse the following Gemfile.lock to include ALL Gems (direct and indirect dependencies) out of GEM specs:
GEM
remote: http://rubygems.org/
specs:
coderay (1.1.3)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
http-accept (1.7.0)
http-cookie (1.0.4)
domain_name (~> 0.5)
json (2.5.1)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0704)
netrc (0.11.0)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.7)
yaml (0.1.1)
PLATFORMS
ruby
x86_64-darwin-20
DEPENDENCIES
json
rest-client
yaml
RUBY VERSION
ruby 2.7.3p183
BUNDLED WITH
2.2.23
But using my function I can only get the direct dependencies without their indirect ones.
e.g: direct dependency: http-cookie (1.0.4)... indirect dependency: domain_name (~> 0.5)
My code:
require 'bundler'
def gemlock(file_path)
file = file_path
gemlock_array = []
context = Bundler::LockfileParser.new(Bundler.read_file(file))
# Gems
context.specs.each do |spec|
name = spec.name
version = spec.version.to_s
gemlock_array << {'name' => name, 'version' => version}
end
puts gemlock_array
end
gemlock('Gemfile.lock')
I'm getting the following hash back:
{"name"=>"coderay", "version"=>"1.1.3"}
{"name"=>"domain_name", "version"=>"0.5.20190701"}
{"name"=>"http-accept", "version"=>"1.7.0"}
...
As you can see indirect dependencies were automatically ignored! But I still need to get it.
I don't have any experience with bundler and don't know how to solve this problem.
Any help in this matter would be much appreciated!
Thanks in advance.
indirect dependencies were automatically ignored
It would have helped if you'd included the full output. I just ran it myself, and you get:
[{"name"=>"coderay", "version"=>"1.1.3"},
{"name"=>"domain_name", "version"=>"0.5.20190701"},
{"name"=>"http-accept", "version"=>"1.7.0"},
{"name"=>"http-cookie", "version"=>"1.0.4"},
{"name"=>"json", "version"=>"2.5.1"},
{"name"=>"method_source", "version"=>"1.0.0"},
{"name"=>"mime-types", "version"=>"3.3.1"},
{"name"=>"mime-types-data", "version"=>"3.2021.0704"},
{"name"=>"netrc", "version"=>"0.11.0"},
{"name"=>"rest-client", "version"=>"2.1.0"},
{"name"=>"unf", "version"=>"0.1.4"}, # <------ !!!!!!!!!!!!
{"name"=>"unf_ext", "version"=>"0.0.7.7"},
{"name"=>"yaml", "version"=>"0.1.1"}]
So in summary, the indirect dependency of domain_name, i.e. unf, which is presumably what you were referring to, is included further down the list. Your code already works exactly as you intended.
One minor point, though: You can simplify the implementation a little by doing this:
gemlock_array = context.specs.map { |s| {'name' => s.name, 'version' => s.version.to_s} }
In ruby, assigning a temporary array and appending to it within a .each loop is usually sub-optimal. Use map instead, to just construct the array directly.

How to get around undefined method `send_keys'

I'm getting undefined method 'send_keys' when running feature tests in our Ruby on Rails application we've been using over the past 3 years when trying to run our rspec-capybara tests
We have the gem 'selenium-webdriver', '~> 2.47.1' in our Gemfile and it shows in Gemfile.lock
we suspect we're not using the right driver but given our Gemfile, not sure why not
HTML we're targeting:
<input class="zip-code-input" id="landing-zip-code" maxlength="5" name="landing[zip_code]"
pattern="[0-9]*" placeholder="ZIP Code" size="5" tabindex="1" title="Five-Digit ZIP Code"
value="" type="text">
I see a lot of references to rack in the Gemfile.lock, i.e.
$ bundle | grep rack
Using rack 1.4.7
Using crack 0.3.2
Using rack-cache 1.5.1
Using rack-test 0.6.3
Using rack-ssl 1.3.4
Using rack-protection 1.5.3
Using rack_session_access 0.1.1
but I've been unable to find any way to change that
send_keys is documented here:
https://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Element.html#send_keys-instance_method
If you are using capybara gem and if you are trying to fill the text field, then you have to use the below syntax.
fill_in 'landing-zip-code', with: '123456'
Find the reference here

Guard not detecting file changes until I interact with terminal

I'm trying to get Guard to detect file system changes while I'm working with SASS files. I want them to be compiled to CSS. I don't want to interact with the terminal every time I make a change though. It should just happen automatically. Right now, I have to press enter at the terminal to get it to compile.
When I do gem query --loacl I get this:
coderay (1.0.9)
ffi (1.8.1)
formatador (0.2.4)
guard (1.8.0)
guard-sass (1.1.0)
listen (1.0.3)
lumberjack (1.0.3)
method_source (0.8.1)
pry (0.9.12.1)
rb-fsevent (0.9.3)
rb-inotify (0.9.0)
rb-kqueue (0.2.0)
sass (3.2.9, 3.2.8)
slop (3.4.4)
thor (0.18.1)
I should note that this will eventually be a guard init on a Samba share; the directory is not on my physical machine, but I am testing on my local hard drive.
To initialize the guard project, I did guard init and then guard on my 'Portfolio' directory. This my directory structure:
-- Portfolio
-- sass
-- css
-- Guardfile (file)
And this is the contents of my guard file:
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'sass', :input => 'sass', :output => 'css'
I read something about Ruby being compiled against libedit instead of readline. How can I check if this is the issue?
It appears the issue was in fact that Ruby wasn't using readline. I am not a Ruby expert, so I don't know for sure if this was the issue or not. I did follow the directions here and installed the Ruby Version Manager (RVM) and reinstalled Ruby to version 1.9.3 (although I think I could have used the latest 2.0.0) and I reinstalled the guard and guard-sass gems and now my filesystem changes are being detected automatically.

JRuby and SQLite3

I'm trying to make work JRuby and SQLite3 with the following gems:
bouncy-castle-java (1.5.0146.1)
bundler (1.3.2)
dbd-jdbc (0.1.6 java)
dbi (0.4.5)
deprecated (2.0.1)
jdbc-sqlite3 (3.7.2.1)
jruby-launcher (1.0.15 java)
jruby-openssl (0.8.2)
json (1.7.7 java)
rack (1.5.2)
rack-protection (1.4.0)
rake (10.0.3)
rubygems-bundler (1.1.1)
rvm (1.11.3.6)
sinatra (1.3.5)
sqlite3 (1.3.7)
tilt (1.3.5)
and this code:
require 'java'
require 'dbi'
require 'dbd/Jdbc'
require 'jdbc/sqlite3'
dbh = DBI.connect(
"DBI:jdbc:sqlite:db.sqlite", # connection string
'', # no username for sqlite3
'', # no password for sqlite3
'driver' => 'org.sqlite.JDBC') # need to set the driver
But I have this error:
DBI::InterfaceError: Unable to load driver 'jdbc' (underlying error: wrong constant name jdbc)
load_driver at /home/gl/.rvm/gems/jruby-1.7.3/gems/dbi-0.4.5/lib/dbi.rb:300
mon_synchronize at /home/gl/.rvm/rubies/jruby-1.7.3/lib/ruby/1.9/monitor.rb:211
load_driver at /home/gl/.rvm/gems/jruby-1.7.3/gems/dbi-0.4.5/lib/dbi.rb:242
_get_full_driver at /home/gl/.rvm/gems/jruby-1.7.3/gems/dbi-0.4.5/lib/dbi.rb:160
connect at /home/gl/.rvm/gems/jruby-1.7.3/gems/dbi-0.4.5/lib/dbi.rb:145
(root) at srv.rb:6
Have you got an idea?
I've just been trying to work this out myself, and eventually succeeded - the problem is that the driver name is now 'Jdbc', rather than 'jdbc' - capitalization is important, both in the connection string, and the require statement. If you change your connection string to "DBI:Jdbc:sqlite:db.sqlite" it should work fine.
I suggest you use ActiveRecord rather than DBI.
You can execute bare SQL thus:
ActiveRecord::Base.connection.execute("SELECT * FROM some_table")
The accepted answer does not work (for me at least). I searched SO and the Web for hours (there's a lot of good and bad information out there) and finally came upon a solution that works (for me at least). A large part of the problem is solved by adding the line 'Jdbc::SQLite3.load_driver'.
require 'java'
require 'jdbc/sqlite3'
module JavaSql
include_package 'java.sql'
end
Jdbc::SQLite3.load_driver
Java::org.sqlite.JDBC
conn_str = 'jdbc:sqlite:../Data/AflBettingHistory.db3'
conn = JavaSql::DriverManager.getConnection(conn_str)
stm = conn.createStatement
rs = stm.executeQuery("select Name from Team")
while (rs.next) do
puts rs.getString("Name")
end
rs.close
stm.close
conn.close

Twitter entities no longer recognized in ruby script

I have a ruby script that scans each type of entity in a given tweet:
status = Twitter::Client.new.status(tweet[:id_str], {:include_entities => "1"})
status[:entities].each do |x|
#job on the entity
end
It was doing good until yesterday. Now I get NoMethodError: undefined method 'entities' for #<Twitter::Status:0x000001033e1800>
I can't figure it out since I've checked that status does include entities after the first line.
Any clues?
EDIT: turns out it's the new version of the twitter gem (v2.0.0) which is in cause. First I'd like to downgrade it to the last version working (v1.7.2), but I'm getting an annoying gem version error:
Bundler could not find compatible versions for gem "hashie":
In Gemfile:
topsy (~> 0.3.6) depends on
hashie (~> 1.0.0)
twitter (= 1.7.2) depends on
hashie (1.1.0)
How can I work it out?
If you need specific gem's version, you can forcely set it throught Gemfile:
gem "rack", "1.0.1"
gem "rails", ">=2.3.2"
In the end it was a conflict in the Gemfile:
gem 'topsy', '~> 0.3.6'
gem 'twitter', '1.7.2'
were requesting different versions of hashie, so I just deleted the version of topsy and it worked.

Resources