I have Rails 4.2 app, using bundler, rvm.
Added capybara and selenium-webdriver to Gemfile, bundle install ok.
I wrote small class that use selenium for some purpose:
require 'capybara'
class GoogleSite
include Capybara::DSL
def initialize
Capybara.default_driver = :selenium
end
def find_all(param)
url = 'https://google.com'
visit url
end
end
GoogleSite.new.find_all({v: "4"})
When I am calling it from rails console i got error:
Capybara's selenium driver is unable to load selenium-webdriver,
please install the gem and add gem 'selenium-webdriver' to your
Gemfile if you are using bundler.
On line with "visit url"
When I call this script from IRB or by ruby file.rb, it's working fine but not from rails console when i include it in lib and call as a class.
I want to use capybara with poltergeist (rails controller will create sidekiq job that will call this class) but I am trying to debug with selenium (to see errors and correct form filling).
Related
I'm putting some shared models for a rails app inside it's own gem. It's just models, so I'm not using an engine. Getting it set up seemed to work fine until I added the "acts_as_list" gem.
# gem - domain.gemspec
spec.add_dependency "acts_as_list"
# gem - lib/domain.rb
require "acts_as_list"
# gem - lib/domain/models/page.rb
acts_as_list scope: [:ancestry]
This works fine in the console for my gem, I can run methods specific to acts_as_list as usual. However, when I add my gem to another project, it gives me an error.
# project - Gemfile
gem "www_domain", path: "../www_domain"
Bundler::GemRequireError: There was an error while trying to load the gem 'domain'.
NoMethodError: undefined method `acts_as_list' for #<Class:0x0055da70121ab0>
/home/shaun/sites/demo/domain/lib/domain/models/page.rb:32:in `<class:Page>'
Is there something special I have to do in this case to access the acts_as_list method because my model is inside a gem?
Update: Here is my complete lib/domain.rb file for the gem:
require "yaml"
require "active_record"
require "acts_as_list"
require "ancestry"
# rbfiles = File.join(File.dirname(__FILE__), "lib", "**", "*.rb")
# Dir.glob(rbfiles).each do |file|
# require file.gsub("lib/", "")
# end
module Domain
# Your code goes here...
def self.root
File.dirname(__dir__)
end
def self.db_config
YAML.load_file("#{Domain.root}/db/config.yml")["development"]
end
end
require "domain/version"
require "domain/models/page"
require "domain/models/menu"
require "domain/models/article"
require "domain/models/page_part"
I can use acts_as_list and ancestry methods in the console of my gem (running bin/console from the gem directory). But the project console (running bundle exec rails console from the project directory) will not start because of the gem error I mentioned.
This might result from a load order issue if the model being required before the require :acts_as_list statement. Check to see if the gem specifies the load order. If not, you could try something like:
# gem - lib/domain.rb
require "acts_as_list"
require "models/page"
If the load order is unclear, I find it helpful to simply add a
puts __FILE__
at the top of the relevant source files.
I am building an application which subclasses Sinatra like so:
require 'rubygems'
require 'sinatra/base'
require 'sinatra/assetpack'
class App < Sinatra::Base
...
run!
end
How can I access irb? Options are not parsed when executing sinatra this way, how do I programmatically open an irb shell?
Just type as below (at the shell prompt):
irb -r ./my_app.rb
I'm a little confused whether you want to open an IRB session from within your app (?) or use IRB to debug your Sinatra project?
For debugging Rack-based apps (such as Sinatra), I like using the racksh gem, which "is like script/console in Rails" for Rack applications. Its main advantage over IRB is that racksh loads the entire application environment into the shell, making debugging a breeze.
From racksh's Github page: "It's purpose is to allow developer to introspect his application and/or make some initial setup. You can for example run DataMapper.auto_migrate! or make a request to /users/666 and check response details. It's mainly aimed at apps that don't have console-like component (ie. apps built with Sinatra) but all frameworks can benefit from interactive Rack stack and request introspection."
However, racksh requires your app to have a config.ru file, so you would have to re-write your app:
# in config.ru
require 'rubygems'
require 'sinatra/base'
require 'sinatra/assetpack'
require 'app.rb'
# in app.rb
class App < Sinatra::Base
...
run!
end
Then in your app folder (where config.ru resides):
$ gem install racksh # or add gem 'racksh' to your Gemfile and run bundle
$ racksh
Check this simple search interface for Microsoft's Bing using Sinatra and binger gem.
If you follow the instructions from there you will understand better.
First at all, create a Gemfile and add:
source "https://rubygems.org"
gem 'sinatra'
gem 'binger'
Then run the bundle command that will generated Gemfile.lock.
Then create a config.ru file, and add by example:
require 'rubygems'
require 'bundler'
Bundler.require
require './app.rb'
run MyApp
Your app.rb could look like this:
class MyApp < Sinatra::Base
get '/' do
#title = "Index"
erb:index
end
end
You must have a folder named views. Create index.erb and add:
< % = #title % >
Finally, run rackup.
Source: https://github.com/thinkphp/sinatra-bing
Demo: http://sinatra-bing.herokuapp.com/
I am trying to test a UI that has the ability to drag and drop. All im looking to do is to drag an element to another element on the page. The code is below.
it 'should drag and drop' do
draggable = #driver.find('//*[#id="2"]').first
droppable = #driver.find('//*[#id="dropmembers4"]').first
draggable.drag_to(droppable)
#driver.find('//div[contains(., "Dropped!")]').should_not be_nil
end
Currently im getting an error:
Failure/error: draggable = #driver.find('//*[#id="2"]').first
No method error: undefined method 'find' for nil:NilClass
Any help would be great.
Thanks
The #driver variable does not exist, that mean the initialization is not working. Here's the minimal initialization code:
$ [sudo] gem install selenium
$ selenium install
And the code for using it:
require 'selenium'
#driver = Selenium::WebDriver.for(:chrome)
And if you're using bundler to define dependencies, you should run:
$ bundle install
And then this code:
require 'rubygems'
require 'bundler/setup'
require 'selenium'
#driver = Selenium::WebDriver.for(:chrome)
I have installed Watir 1.7.1, Ruby 1.9.2, ci_reporter 1.6.4, test unit 2.2.0.
Since testunit donot come with Ruby 1.9.2, I installed the testunit seperately.
When I run the script, ci_reporter donot create 'test/reports' folder in the directory iam running the tests. When i execute the script, script runs fine but the folder is not created.
Does the folder 'test/reports' gets created by itself or the 'test/reports' folder should be as part of testunit.
Following is the simple test iam running, please look at the code snippet :
gem 'test-unit'
require 'test/unit/ui/console/testrunner.rb'
require 'ci/reporter/rake/test_unit_loader.rb'
require 'watir'
class My_Test < Test::Unit::TestCase
def test_me
browser = Watir::IE.start('http://www.google.com')
assert(browser.link(:text, 'About Google').exists?)
browser.close
end
end
Does ruby 1.9.2 support ci_reporter 1.6.4?
Can any one help me with an example as to how the reports would be created by ci_reporter and where the reports get stored?
Based on Tiffany's response and the link she gave, I think the answer to your question is 'NO. it is not supported at this time'
I'd also echo the recommendation to use 1.8.7 that's what I've been using for a while now and it is working good with watir.
I think most folks are still using ruby 1.8.7 with Watir. I'm still on 1.8.7, and I use ci_reporter with Watir every day without any problems.
Any chance you can try your code in a 1.8.7 environment? I just found this blog post that indicates that ci_reporter has not been updated to work with ruby 1.9.2: http://www.larkware.com/posts/fix-ci-reporter-for-test-unit-2-dot-0
A newbie question on Selenium for Ruby. What's the difference between "gem install selenium" and "gem install Selenium"? I'm trying to figure out which one I should install.
I always used:
$ gem install selenium-client
I think that's the official one, but I can't find a place where it says this.
Once you installed it, all you have to add to your tests is:
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.16"
require "selenium/client"
class ExampleTest < Test::Unit::TestCase
attr_reader :browser
def setup
#browser = Selenium::Client::Driver.new(...