Testing drag and drop in ruby - 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)

Related

Ruby + Selenium - Take a screenshot with each step executed

I want to take a screenshot with each step executed, the code below is taking a screenshot during the entire execution.
How to fix this?
HOOKS.RB
AfterStep do
browser.screenshot.save 'screenshot.png'
encoded_img = #browser.driver.screenshot_as(:base64)
embed("data:image/png;base64,#{encoded_img}",'image/png')
end
ENV.RB
#encoding: utf-8
require 'rspec/expectations'
require 'watir'
require 'rspec'
require 'json'
require 'magic_encoding'
RESULT EXPECTED
I discovered that the environment that I was using was with outdated gems, I solved this with the gem update command.
After refreshing the gems, screenshots are now taken correctly.

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

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

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

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"

What's the difference between Selenium and selenium gems?

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

Resources