page-object gem seems not working - watir

I am trying to use page-object gem in my watir-webdriver scripts and I think I might be missing something.
Here is my code for log_in.rb:
require "./all_deals"
class LogIn
include PageObject
text_field(:email, :id => 'UserName')
text_field(:password, :id => 'Password')
checkbox(:remember_me, :id => 'RememberMe')
button(:log_me_in, :value => 'Login')
def initialize(browser)
#browser = browser
end
def log_in (email, password)
self.email = email
self.password = password
remember_me
log_me_in
AllDeals.new(#browser)
end
end
My home_page.rb
require "./log_in"
class HomePage
def initialize(browser)
#browser = browser
end
def visit
#browser.goto "http://treatme.co.nz/"
end
def go_to_log_in
#browser.goto "https://treatme.co.nz/Account/LogOn"
LogIn.new(#browser)
end
end
Here is my log_in_test.rb
require "rubygems"
require "test/unit"
require "watir-webdriver"
require "page-object"
require "./home_page"
class LogInTest < Test::Unit::TestCase
def setup
#browser ||= Watir::Browser.new :firefox
end
def teardown
#browser.close
end
def test_fail
#home_page = HomePage.new(#browser)
#home_page.visit
#log_in_page = #home_page.go_to_log_in
#all_deals = #log_in_page.log_in("test#gmail.com","test")
assert (#all_deals.get_title == "GrabOne Daily Deals - Buy Together, Save Together")
end
end
The result of the test run is:
Finished tests in 22.469286s, 0.0445 tests/s, 0.0000 assertions/s.
1) Error:
test_fail(LogInTest):
NoMethodError: undefined method `text_field_value_set' for nil:NilClass
C:/Ruby193/lib/ruby/gems/1.9.1/gems/page-object-0.9.2/lib/page-object/accessors.rb:142:in `block in text_field'
I am using Ruby 1.9 with page-object gem 0.9.2.
Can you please help me?
Also in each of those rb file, I need to require the class files it references, is there a way I don't have to declare it every time?
Thanks so much.

Addressing the Exception
That exception is occurring do to the LogIn page re-defining the initialize method.
class LogIn
include PageObject
def initialize(browser)
#browser = browser
end
end
When you include PageObject, it already adds a specific initialization method. Your class is overriding that initialization and presumably causing something to not get setup correctly.
Removing the initialize method will get you past that exception.
However, you will then hit a remember_me is not a variable/method exception. Assuming you want to check the checkbox, it should be check_remember_me.
Requiring Class Files
I usually have my test file require a file that requires all my page_objects. It is a similar concept to how your would require any other gem or library.
For example, I would create a treatme.rb file with:
require 'log_in'
require 'home_page'
Assuming you require the files in the required order (ie files that are needed by others are required first), none of your page object files (ie log_in.rb and home_page.rb) should need to do any requiring.
Your test files would then just require your treatme.rb file. Example:
require './treatme'

Related

Test an infinite loop in minitest

I have a job named ActivityJob which fetches an user's github public activities.
class ActivityJob < ActiveJob::Base
queue_as :git
def perform(user)
begin
#fetch github activities for a user using user's auth_token
activities = ActivitiesFetcher.new(user).fetch
# process the retrieved activities.
rescue Github::Error::NotFound
#user is not present. ignore and complete the job.
rescue Github::Error::Unauthorized
#auth token is invalid. re-assign a new token on next sign in
user.auth_token = nil
user.save
# refresh_gh_client gets a new client using a random user's auth_token
user.refresh_gh_client
retry
rescue Github::Error::Forbidden
# Probably hit the Rate-limit, use another token
user.refresh_gh_client
retry
end
end
end
The method refresh_gh_client gets a random user from db and uses its auth_token to create an new gh_client. the new gh_client is assigned to the current user. This is working fine. In test cases, I am using mocha to stub the method calls.
class ActivityJobTest < ActiveJob::TestCase
def setup
super
#user = create :user, github_handle: 'prasadsurase', auth_token: 'somerandomtoken'
#round = create :round, :open
clear_enqueued_jobs
clear_performed_jobs
assert_no_performed_jobs
assert_no_enqueued_jobs
end
test 'perform with Github::Error::Unauthorized exception' do
User.any_instance.expects(:refresh_gh_client).returns(nil)
ActivitiesFetcher.any_instance.expects(:fetch).raises(Github::Error::Unauthorized, {})
ActivityJob.perform_now(#user, 'all', #round)
#user.reload
assert_nil #user.auth_token
end
end
The problem is that the 'retry' in the job calls the ActivitiesFetcher breaking the expectation that it was supposed to be called only once.
:
unexpected invocation: #<AnyInstance:User>.refresh_gh_client()
unsatisfied expectations:
- expected exactly once, invoked twice: # <AnyInstance:User>.refresh_gh_client(any_parameters)
- expected exactly once, invoked twice: #<AnyInstance:ActivitiesFetcher>.fetch
Make an interface for the ActivitiesFetcher behavior you need. You create an implementation of the interface used only for testing that deals with the idiosyncrasies of testing.

Rails 4 activeadmin - undefined local variable or method `view_factory' for :Arbre::Context

i am using activeadmin gem for my admin interface,now i created a new controller and added layout 'acitveadmin'
am getting the below error
NameError in MeetingRooms#index
Showing /home/amp/.rvm/gems/ruby-2.1.1#rails4.1/bundler/gems/active_admin-41d5176f3682/app/views/layouts/active_admin.html.arb
where line #2 raised:
undefined local variable or method `view_factory' for :Arbre::Context
my controller is
app/controllers/meeting_rooms_controller.rb
class MeetingRoomsController < ApplicationController
layout 'active_admin'
def index
#meeting_rooms = MeetingRoom.all
#render :layout => "active_admin"
end
end
can any one please help me.
I think you should use the controller block to define actions.
https://github.com/activeadmin/activeadmin/blob/master/docs/8-custom-actions.md#modifying-the-controller
ActiveAdmin.register MeetingRoom do
controller do
# This code is evaluated within the controller class
def define_a_method
# Instance method
end
end
end
In the controller block you can use the feature of the InheritedResources.
https://github.com/josevalim/inherited_resources#overwriting-actions

not handling the file upload window well

I have a page object called import_transaction_file.rb, which one of the method click_choose_file will invoke a standard file upload windows show below:
the code for the page object is:
class ImportTransactionFile
include PageObject
....
button(:choose_file, :id => 'createBulkPayment:file')
....
def click_choose_file
choose_file
end
end
In my test program below:
....
def test_go_to_direct_credit_payment_page
...
#import_transaction.click_choose_file
# #browser.window(:title => 'File Upload').use do
# #browser.button(:name => 'Cancel').click
# end
# doesn't work
end
the method click_choose_file in the test program will invoke the standard file upload window as attached below:
How do I:
put the path to file name
click open button
click close button
Will you recommend me to do it in the page-object or the test program?
Thanks for your reply.
I have a very similar thing to what you're asking and mine works using:
browser.file_field(:text, "File Upload").set("C:\path\to\file\to\upload")
Hope that helps!

Rails 4: Correct way to create objects from other model from the controller

Using strong_params from Rails 4, what is the preferred best way to do this?
I used the below solution but are uncertain if this is the best way to do it. ( it works though )
Example:
game_controller.rb ( shortcut version!)
# inside game controller we want to build an Participant object
# using .require fails, using .permits goes true
def GameController < ApplicationController
def join_game_as_participant
#participant = Participant.new(participant_params)
end
end
def participant_params
params.permit(:participant,
:participant_id,
:user_id,
:confirmed).merge(:user_id => current_user.id,
:game_id => params[:game_id])
end
Your participant_params method should be private and you should use the require method :
private
def participant_params
params.require(:participant).permit(
:participant_id, :user_id, :confirmed
).merge(
:user_id => current_user.id, :game_id => params[:game_id]
)
end
Hope this help

Why is there an error in the following code?

I would be grateful for help in sorting the error in the following code:
require 'rubygems'
require 'watir'
require 'watir-webdriver'
require 'test/unit'
class TestGoogle < Test::Unit::TestCase
def setup
#browser = Watir::Browser.new :firefox
end
def testSignInLink
#browser.goto "http://google.com/"
po = PageObjects.new(#browser)
po.clickLinkSignIn
end
end
class PageObjects
def initialize( browser )
#browser = browser
end
def clickLinkSignIn()
#browser.link(:id, "gb_70").click
end
end
tg = TestGoogle.new
tg.setup
tg.testSignInLink
The error is:
Uncaught exception: wrong number of arguments (0 for 1)
C:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:971:in `initialize'
C:/RubymineProjects/ditto/Google_01_TU_02.rb:28:in `new'
C:/RubymineProjects/ditto/Google_01_TU_02.rb:28:in `<top (required)>'
Line 28 is:
tg = TestGoogle.new
Strangely enough, the script then runs to completion with the google login page being presented.
Note that there are no asserts yet - I'm doing this one small step at a time.
Added after edit:
The initializer in C:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:971
def initialize name # :nodoc:
#__name__ = name
#__io__ = nil
#passed = nil
end
I think this whole question was unfair to all the good commenters.
Reason: As an experiment, I commented out the last 3 lines of the code:
tg = TestGoogle.new
tg.setup
tg.testSignInLink
The test ran perfectly.
I was previously assuming that I needed some way to "kick start" the methods in the TestGoogle class. Similar to a "Main" program that calls all the methods in turn.
Maybe that's what #justinko was referring to? So, the TestGoogle class is a test runner?
I think I need to apologize to the commenters.

Resources