I'm using page-object gem with watir and RSpec to test my web application.
Here is my page object.
require 'page-object'
class UserEditPage
include PageObject
page_url "#{$context}/user/edit?id=#{#params[:id]}"
text_field :user_id, name: 'userName'
text_field :pw, name: 'password'
text_field :pw_retype, name: 'password2'
# snip uninteresting stuff
button :submit, index: 0
end
I would like to parameterize :id in page_url via PageObject::PageFactory like this:
visit UserEditPage, using_params: {id: 1000} do |page|
# operation on page
end
This document implies that above usage is possible, but I couldn't understand how exactly this is achieved.
Question: how can I pass parameter id from visit method to UserEditPage?
Running code results in
***/user/edit_page.rb:8:in `<class:UserEditPage>': undefined method `[]' for nil:NilClass (NoMethodError)
from ***/user/edit_page.rb:5:in `<top (required)>'
probably because #params is nil when evaluating page_url.
Changing using_params: {id: 1000} to id: 1000 with no luck.
The page_url needs to be:
page_url "#{$context}/user/edit?id=<%=params[:id]%>"
Note how the parameters need to be included differently - ie <%=params[:id]%> instead of #{params[:id]}.
The problem is that the page_url is evaluated when the page object class is evaluated. This means that when the class is evaluated, params[:id] must already exist. This is not going to be true when you want to include the parameters dynamically.
The page object gem uses ERB to allow you to create a template, which allows substitution of the params later - ie when calling visit.
In summary, when creating a page_url:
Use string interpolation if the value is known when the page object class is evaluated.
Use ERB if the value is to be dynamically specified on visit.
Working Example
Below is a working example of the dynamic parameters.
require 'watir-webdriver'
require 'page-object'
$context = 'http://testurl.com'
class UserEditPage
include PageObject
page_url "#{$context}/user/edit?id=<%=params[:id]%>"
end
include PageObject::PageFactory
#browser = Watir::Browser.new
visit UserEditPage, using_params: {id: 1000} do |page|
p page.current_url
#=> "http://testurl.com/user/edit?id=1000"
end
Related
I want to change the name of the #browser variable because I'm tired of typing it.
In env.rb:
before do
#b = Watir::Browser.new :chrome
end
This throws error:
Unable to pick a platform for the provided browser or element: nil.
nil was passed to the PageObject constructor instead of a valid browser or element object. (RuntimeError)
before do
#browser = Watir::Browser.new :chrome
end
works as expected.
#browser = Watir::Browser.new :chrome
#b = #browser
doesn't work either. Is there any way to modify this variable name?
In general, you can store your browser instance in a variable of any name. Watir-Webdriver does not care, nor does Cheezy's page objects. The initialization of the page object only cares what the actual object passed in is - ie it must be a Watir::Browser or Watir::Element (or the Selenium-WebDriver equivalents).
However, this is not true when using the PageObject::PageFactory. In the on_page method, and implicitly visit_page and if_page, the code is hard-coded to look for a #browser variable.
def on_page(page_class, params={:using_params => {}}, visit=false, &block)
page_class = class_from_string(page_class) if page_class.is_a? String
return super(page_class, params, visit, &block) unless page_class.ancestors.include? PageObject
merged = page_class.params.merge(params[:using_params])
page_class.instance_variable_set("#merged_params", merged) unless merged.empty?
#current_page = page_class.new(#browser, visit)
block.call #current_page if block
#current_page
end
The initialization of the page object is always done with #browser. When you stored the browser instance in #b, it meant that #browser was nil. This then led to the exception you saw. If you want to use the PageFactory as written, you will need to stick to #browser.
If the variable really bothers you, there is always the option for monkey patching. Simply re-define the on_page and on method to use whatever variable you like. The following redefines the method to use #b:
require 'page-object'
module PageObject
module PageFactory
def on_page(page_class, params={:using_params => {}}, visit=false, &block)
page_class = class_from_string(page_class) if page_class.is_a? String
return super(page_class, params, visit, &block) unless page_class.ancestors.include? PageObject
merged = page_class.params.merge(params[:using_params])
page_class.instance_variable_set("#merged_params", merged) unless merged.empty?
#current_page = page_class.new(#b, visit)
block.call #current_page if block
#current_page
end
alias_method :on, :on_page
end
end
The issue is somewhere else in your code. If you run just the line you provided (after requiring watir)
require 'watir-webdriver'
#b = Watir::Browser.new :chrome
in a IRB session it will work just fine..
Some other code dependency (page objects library perhaps) needs a browser object and is expecting a specific name, is getting a nill (un-initialized) object instead and complaining.
Also, unless you want the overhead of opening and closing the browser for each test (can add a ton of time) you may want to initialize the browser earlier, such as in a before_all hook, or early in the startup of your test code. Then just do something like clear cache and cookies in the before hook
Research: Rails 4.0 beta, fields_for not accepting pluralized model_name in one-to-many association, Rails 4 Nested Attributes with fields_for Don't Save to Database
First, let's get the most common problem out of the way: incorrectly named attributes parameters for strong parameters. Mine is correctly plural.
class AdultsController < ApplicationController
...
def update
authorize #user
respond_to do |format|
if #user.update_attributes(user_params)
format.html { redirect_to unit_adult_path(#unit, #user), notice: "#{#user.full_name} was successfully updated." }
else
format.html { render action: 'edit' }
end
end
end
def user_params
params.require(:adult).permit(:first_name, :last_name, phones_attributes: [])
end
end
And my models are setup correctly
class User < ActiveRecord::Base
has_many :phones, dependent: :destroy
accepts_nested_attributes_for :phones, allow_destroy: true, reject_if: proc { |a| a["number"].blank? }
end
class Phone < ActiveRecord::Base
belongs_to :user, touch: true
end
And the view
# adult/_form.html.haml
= bootstrap_form_for [#unit, #user] do |f|
= f.text_field :first_name, control_col: 'col-md-4'
= f.text_field :last_name, control_col: 'col-md-4'
= f.fields_for :phones do |f_phone|
= f_phone.form_group do
= f_phone.select :kind, options_for_phones, hide_label: true, layout: :default
= f_phone.phone_field :number, hide_label: true, layout: :default
= f_phone.check_box :_destroy, label: 'remove'
But, when I submit the User form to save
Started PATCH "/units/2/adults/1" for 127.0.0.1 at 2014-07-11 15:20:17 -0700
Processing by AdultsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pDjDVSiEs5qqHLqnbxQMeGWDOUGvhXPPvgyRGmitmps=", "adult"=>{"first_name"=>"Karl", "last_name"=>"Smith", "phones_attributes"=>{"0"=>{"kind"=>"other", "number"=>"888.1212", "_destroy"=>"0", "id"=>"173"}, "1"=>{"kind"=>"mobile", "number"=>"888.1212", "_destroy"=>"0", "id"=>"174"}} }, "commit"=>"Update Adult", "unit_id"=>"2", "id"=>"1"}
Unpermitted parameters: phones_attributes
I don't understand why the nested data is being rejected by the strong parameter evaluation. It looks correct to me.
The one thing I do notice is that the params data for "phones_attributes" value is a HASH not an ARRAY. In the user_params, phones_attributes: [] looks like it expecting an ARRAY. So I changed it to a HASH.
def user_params
params.require(:adult).permit(:first_name, :last_name, phones_attributes: {})
end
But now I get the following error.
Unpermitted parameters: 0, 1
So I tried specifying the field names in the "phones_attributes" array.
def user_params
params.require(:adult).permit(:first_name, :last_name, phones_attributes: [:id, :kind, :number])
end
And I still get.
Unpermitted parameters: phones_attributes
I know I must be missing something small, but I can't find my error.
EDIT: all my nested attributes forms do not work. Not 100% sure when they stopped, but ones that worked previously no longer work and have not been modified.
Figured this out. I was using javascript to copy the phone fields (kind, number) to make a new set of inputs available for entry. The script was adding non numeric characters to part of the field id, and this was causing rails to ignore all the submitted phone_attributes.
For the next person that comes along...
When fields_for renders out the fields, it will index each input name for uniqueness when the submitted post data is converted to params. In the example below, this number field
<input id="adult_phones_attributes_0_number" name="adult[phones_attributes][0][number]" type="tel" value="7773331111">
will look something like this when converted to params
"phones_attributes"=>{"0"=>{"number"=>"7773331111"}}
The hash key of "0" comes from the index created by fields_for. It's the "[0]" portion of the name.
In versions of rails past, if that nested attributes params hash key was not a number, the k/v pair was just ignored. Well now with strong parameters (I'm guessing the culprit), it will reject the entire "phones_attributes" hash.
My script was copying the input field, doing a regex on the html to change the "[0]" index to a random number. But sometimes it would replace it will non-digit characters. And this was causing the problem.
Following the Tweets example of Teacup, I'm trying to access a variable outside of the layout block
class WindowController < TeacupWindowController
stylesheet :pref_window
layout do
#loginButton = subview(
NSButton, :loginButton,
state: NSOffState,
buttonType: NSSwitchButton,
action: 'login',
target: self,
)
puts #loginButton.class
end
puts #loginButton.class
the first puts returns NSButton class, but the second one returns Nil class.
how can I access #loginButton if I need to programmatically make changes to it?
For example:
#loginButton.setState(NSOnState)
doesn't work outside the layout block.
You can use an attr_accessor on the WindowController, then in the layout block you can use self.loginButton and it will be assigned on the WindowController giving you access to the button.
I'm also assuming the second puts is actually in another method and this is just example code.
class WindowController < TeacupWindowController
stylesheet :pref_window
attr_accessor :loginButton
layout do
self.loginButton = subview(
NSButton, :loginButton,
state: NSOffState,
buttonType: NSSwitchButton,
action: 'login',
target: self,
)
puts self.loginButton.class
end
puts self.loginButton.class
end
I am trying to test my application with cucumber and capybara.
I have the following step definition:
Given(/^I fill in the create article form with the valid article data$/) do
#article_attributes = FactoryGirl.build(:article)
within("#new_article") do
fill_in('article_title', with: #article_attributes.title)
attach_file('article_image', #article_attributes.image)
fill_in('article_description', with: #article_attributes.description)
fill_in('article_key_words', with: #article_attributes.key_words)
fill_in('article_body', with: #article_attributes.body)
end
My article factory looks like this:
FactoryGirl.define do
factory :article do
sequence(:title) {|n| "Title #{n}"}
description 'Description'
key_words 'Key word'
image { File.open(File.join(Rails.root, '/spec/support/example.jpg')) }
body 'Lorem...'
association :admin, strategy: :build
end
end
And this is my uploader file:
# encoding: UTF-8
class ArticleImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
But each time i run this scenario i get ERROR message:
Given I fill in the create article form with the valid article data # features/step_definitions/blog_owner_creating_article.rb:1
cannot attach file, /uploads/article/image/1/example.jpg does not exist (Capybara::FileNotFound)
./features/step_definitions/blog_owner_creating_article.rb:5:in `block (2 levels) in <top (required)>'
./features/step_definitions/blog_owner_creating_article.rb:3:in `/^I fill in the create article form with the valid article data$/'
features/blog_owner_creating_article.feature:13:in `Given I fill in the create article form with the valid article data'
I also found that FactoryGirl returns image:nil when i run FactoryGirl.build(:article) in my rails test console.
Could anybody explain me what i am doing wrong,please?
You need to pass the path in directly:
attach_file('article_image', File.join(Rails.root, '/spec/support/example.jpg'))
What's happening here is that attach_file expects a string, not a CarrierWave uploader. When you pass it an uploader (#article_attributes.image), attach_file is calling Uploader#to_s, which calls Uploader#path. Since you haven't saved the article yet, the path at which the uploaded image will be located is invalid.
Note also that calling your variable #article_attributes is confusing, since it's actually a full article object, not just a hash. If that's what you want, you may want to try FactoryGirl.attributes_for(:article).
I'm using the YUI2 framework.
I want to be able to get a reference to an object that was created on the page. I don't want to change the existing JS code to store a global handle to it when it is created, if possible.
The page has something like this on it:
<script type="text/javascript">//<![CDATA[
new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
}));
//]]></script>
I don't see any methods in YAHOO.util.Dom specific to finding objects.
How can I get an object reference to FirstRate.Reporter? Is there anything to ennumerate the objects on the page?
Thanks
YUI contains lots of methods for finding objects in the webpage DOM. YAHOO.util.DOM contains methods:
HTMLElement | Array get ( el )
Array getElementsBy ( method , tag , root , apply , o , overrides )
HTMLElement getElementBy ( method , tag , root )
Array getElementsByClassName ( className , tag , root , apply , o , overrides )
and many more. These retrieve objects from the DOM. To find an object in the page with YUI2, you would typically use some combination of the tag type, the class name or the id to query the page using YUI methods to find the object you want. It's easiest to find a specific object in the page if you give it a unique id value and then you can just using YAHOO.util.DOM.get("myObject") to retrieve it (where "myObject" is the id of the HTML element).
If you want to get regular javascript objects, then you have to store references to them yourself in your own javascript variables. YUI doesn't do that for you except when using some UI widgets (which also have DOM elements).
If you want to keep track of the result of this:
new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
Then, you have to assign it to a variable or to a property of an object whose scope allows it to last long enough for you to use it.
var theReporter = new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
or
myObject.reporter = new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
where myObject is some other object you've created and are storing.
JavaScript objects aren't part of DOM. DOM is the Document Object Model, it's about objects in HTML document like divs, forms, inputs etc, the kind of objects Browser displays.
There is no "global enumeration" of objects in JavaScript. There are global variables.
I don't want to change the existing JS code to store a global handle to it
So you don't want to use global variable. But why?
This is the only way to do it, and it's also very simple:
var myGlobalVar = new FirstRate.Reporter("report1").setOptions({inData: "testfunc"}));
Here you'll store reference ("handle" as you say) to your object in global var myGlobalVar, which you can later access in another part of your JavaScript code.