How can page-object-gem be used with IRB? - ruby

Using watir-webdriver, you can test your code while developing using IRB (interactive ruby).
But this does not seem to work when using the page-object gem.
When I run the ruby program, I can see my page-object calls are working.
This is not apparent from IRB. E.g. Here's what I type in IRB:
require 'page-object'
require 'watir-webdriver'
b = Watir::Browser.new :chrome
I then manually navigate to the page I want and go back to IRB:
b.table(:id => 'manage-groups-list').present?
IRB returns true. So, watir-webdriver knows the table control is present on the page. Can we do the same thing in page-object?
Continue coding in IRB:
class ManageGroupsPage
include PageObject
table(:groupsList, :id => 'manage-groups-list')
end
I then try to get the contents of the table from IRB. However, this returns an error:
irb(main):021:0> puts ManageGroupsPage.groupsList.to_s
NoMethodError: undefined method `groupsList' for ManageGroupsPage:Class
from (irb):21
from C:/Ruby193/bin/irb:12:in `<main>'
I am using page-object 1.0.3, watir-webdriver 0.6.11 and Ruby 1.9.3.

Looking at their basic usage docs, it seems like calling
table :groupsList, id: 'manage-groups-list'
generates an instance method rather than a class method. So you would need to call the generated method like so:
b = Watir::Browser.new :chrome
page = ManageGroupsPage.new(b) # build a new page object
page.groupsList # call the instance method

You can also use the 'pry' gem. Add to your env file, require 'pry' and then where ever you want the code to stop/inspect, add binding.pry

Related

Requiring ActiveRecord on IRB - Ruby (NO Rails)

How can I load ActiveRecord on an IRB session?
I have the following
# config/app.rb
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'db/mydb.sqlite3'
)
But when I start IRB and try to load it
irb#1(main):001:0> require config/application.rb
I get
NameError: undefined local variable or method `config' for main:Object
Did you mean? conf
I'd like to be able to interact with my ActiveRecord objects from IRB.
I'm NOT using Rails but only ActiveRecord.
Thanks
Two things to change here:
Always put quotes around the path you're requiring. The reason Ruby is saying "undefined local variable or method" is that it's trying to interpret config as a variable name. Put the name in quotes and it won't.
Use require_relative when loading files that are part of your application. require only looks in the default Ruby load paths.
Try this:
require_relative 'config/application.rb'
You can use pry to build a console started from command line. Simple console solution below. This way you don't have to require in irb every time you stare interactive session.
# bin/console
#!/usr/bin/env ruby
require_relative '../config/app.rb'
require 'pry'
binding.pry
More about pry https://github.com/pry/pry
P.S. You should set +x on bin/console, i.e.
$ chmod +x bin/console
Then you just call
$ bin/console
and get all the code run from config/app.rb and interactive session ready. No need to require anything from irb to start working.
Poor-man's rails console equiv. :-)

Capybara's selenium driver is unable to load `selenium-webdriver`

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).

why is this not working, undefined blank for string in ruby

I have the following:
input = gets.chomp
basket = input.strip.split(',')
basket.delete_if(&:blank?)
which should allow you to do: cat, dog,,
from there we can split that up, strip it clean and split it on , and then check for empty elements and remove them.
but this code gives me an error: delete_if: undefined method 'blank?' for "cat":String (NoMethodError) which does not make any sense to me. I thought that the whole purpose of blank? or empty? was to say remove this element if this is true.
ActiveSupport that comes with Rails adds the blank? method to String and many other classes. Since this method is not part of Ruby core, you need to have Rails or the ActiveSupport gem installed. If that gem is installed than you can require ActiveSupport's core extensions like this:
> 'foo'.blank?
# => NoMethodError: undefined method `blank?' for "foo":String
> require 'active_support/core_ext'
# => true
> 'foo'.blank?
# => false
require 'active_support'
require 'active_support/core_ext'
import "active_support" first
blank? is a method augmented to the String class from rails. It's not part of the ruby String class by default.

uninitialized constant Selenium

I'm trying to use the Selenium Webdriver gem to write a test but am getting the following error.
"test.rb:4:in `': uninitialized constant Selenium (NameError)"
I can't seem to figure out why its giving me this error. I have pasted the code for the test below.
"require rubygems"
"require selenium-webdriver"
driver = Selenium::WebDriver.for :chrome
that is where it fails. The rest of the test is pretty page-specific js executions.
Your requires do nothing, you just wrote unused String literals, change it to:
require "rubygems"
require "selenium-webdriver"

Testing drag and drop in ruby

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)

Resources