How do i clear my capybara steps using Page Object Pattern? - ruby

I have the following step definitions with Page Object Pattern gem 'site_prism':
class Main < SitePrism::Page
element :login_link, "a.log-in-link"
element :login_field, "input[name='userLogin']"
element :pass_field, "input[name='userPassword']"
element :enter_button, ".button_pretty"
end
If /I'm log in as "([^"]*)" with password "([^"]*)"$/ do |login, pass|
#main = Main.new
#main.login_link.click
#main.login_field.set login
#main.pass_field.set pass
#main.enter_button.click
end
It works fine but looks very heavy and unbeautiful. Is there any way to write it like capybara 'within' method? The following isn't work (Error: "can't convert Main into String (TypeError)")
within #main do
login_link.click
login_field.set login
pass_field.set pass
enter_button.click
end

You can hack support for a with statement into Ruby:
http://www.ruby-forum.com/topic/128781#574402
(I've seen this in VBScript, not sure if any other languages support it out of the box).

No, you can't use capybara's within functionality with site_prism.

Related

How to find element in page-object gem with custom parameter?

I use page-object gem with RSpec and I want to create an element with a custom parameter like
link(:derect_link, text: "#{custom_text}"
Because I want to get the link to text and this text was changed every time when I starting the test.
How can I use it in spec scenario?
The accessor methods do not support custom parameters at run time. You will have to manually create the methods for the link. The equivalent of the methods created by the link accessor would be:
class MyPage
include PageObject
def derect_link_element(text)
link_element(text: text)
end
def derect_link(text)
derect_link_element(text).click
end
def derect_link?(text)
derect_link_element(text).exists?
end
end
This would be used like the standard methods, except that you would specify the text of the link:
# Click the link
page.derect_link('custom_text')
# Check if the link exists
page.derect_link?('custom_text')
# Get the link element to perform other actions (eg inspect attribute values)
link = page.derect_link_element('custom_text')
link.attribute('href')

What is the page variable in Capybara tests?

When using Capybara, what is the difference between calling page.find('#name') and find('#name').
Is it that same thing, as this answer states What's the meaning of page and page.body in Capybara
I am just looking for more of an explanation and when I would need to use page outside of asserts.
As it is described in source code:
# Shortcut to accessing the current session.
# #return [Capybara::Session] The current session object
def page
Capybara.current_session
end
When you do find('#name') current session's find method is called. So there is no difference between calling page.find('#name') and find('#name').
I guess this shortcut was created just to keep asserts code intuitive and understandable:
expect(page).to have_css(#name)
looks better than
expect(Capybara.current_session).to have_css(#name)

Using SitePrism with Rspec and Capybara feature specs

I recently discovered SitePrism via the rubyweekly email.
It looks amazing. I can see its going to be the future.
The examples I have seen are mostly for cucumber steps.
I am trying to figure out how one would go about using SitePrism with rspec.
Assuming #home_page for the home page, and #login_page for the login_page
I can understand that
#home_page.load # => visit #home.expanded_url
however, the part I am not sure about, is if I think click on for example the "login" link, and the browser in Capybara goes to the login page - how I can then access an instance of the login page, without loading it.
#home_page = HomePage.new
#home_page.load
#home.login_link.click
# Here I know the login page should be loaded, so I can perhaps do
#login_page = LoginPage.new
#login_page.should be_displayed
#login_page.email_field.set("some#email.com")
#login_page.password_field.set("password")
#login_page.submit_button.click
etc...
That seems like it might work. So, when you know you are supposed to be on a specific page, you create an instance of that page, and somehow the capybara "page" context, as in page.find("a[href='/sessions/new']") is transferred to the last SitePrism object?
I just feel like I am missing something here.
I'll play around and see what I can figure out - just figured I might be missing something.
I am looking through the source, but if anyone has figured this out... feel free to share :)
What you've assumed turns out to be exactly how SitePrism works :) Though you may want to check the epilogue of the readme that explains how to save yourself from having to instantiate page objects all over your test code. Here's an example:
# our pages
class Home < SitePrism::Page
#...
end
class SearchResults < SitePrism::Page
#...
end
# here's the app class that represents our entire site:
class App
def home
Home.new
end
def results_page
SearchResults.new
end
end
# and here's how to use it:
#first line of the test...
#app = App.new
#app.home.load
#app.home.search_field.set "sausages"
#app.home.search_button.click
#app.results_page.should be_displayed

What can I put as my watir-webdriver page "element" in a condition where it's not there?

I'm testing a nightmarish website that in most situations sticks all the important stuff in an iframe.
However, there are other common situations where the system will, annoyingly, open a page in a new tab, but not wrapped in the iframe.
I'm trying to figure out a conditional method that will check for the existence of the iframe and use it, otherwise not.
Here's what I've come up with, so far:
# The browser object...
#br = Watir::Browser.new
"frm" is the conditional method I'm trying to get working...
# Just an example element definition...
def click_my_button
#br.frm.button(id: "button").click
end
I define it in Watir's Container module, like so:
module Watir
module Container
def frm
if frame(id: "iframeportlet").exist?
frame(id: "iframeportlet")
else
# This is the part that I can't figure out.
end
end
end
end
That works fine when the iframe is there, but not surprisingly I get a NilClass error when it's not.
So, my question is: what can go into the else clause to make it work? More broadly, is there perhaps a better way to accomplish this? As you can imagine, I really want to avoid having to define every element in the web site twice.
I figured it out, and it's quite simple. The frm method's else clause just needs a "self"...
else
self
end
That's it. I'd love to know if there are any hidden pitfalls with this approach, though.

PageObject with Ruby - set text in a text field only works in the main file

I'm automating a site that has a page with a list of options selected by a radio button. When selecting one of the radios, a text field and a select list are presented.
I created a file (test_contracting.rb) that is the one through which I execute the test (ruby test_contracting.rb) and some other classes to represent my page.
On my class ContractPage, I have the following element declaration:
checkbox(:option_sub_domain, :id => "option_sub_domain")
text_field(:domain, :id => "domain_text")
select_list(:tld, :id => "domain_tld")
I've created in the ContractPage a method that sets the configuration of the domain like this:
def configure_domain(config={})
check_option_sub_domain
domain = config[:domain]
tld = config[:tld]
end
When I call the method configure_domain from the test_contracting.rb, it selects the radio button, but it doesn't fill the field with the values. The params are getting into the method correctly. I've checked it using "puts". Even if I change the params to a general string like "bla" it doesnt work. The annoying point is that if on test_contracting.rb I call the exact same components, it works.
my_page_instance = ContractPage.new(browser)
my_page_instance.domain = "bla"
my_page_instance.tld = ".com"
What I found to work was to in the configure_domain method, implement the following:
domain_element.value = config[:domain]
tld_element.send_keys config[:locaweb_domain]
Then it worked.
The documentation for the PageObjects module that I'm using as reference can be found here: http://rubydoc.info/github/cheezy/page-object/master/PageObject/Accessors#select_list-instance_method
Do you guys have any explation on why the method auto generated by the pageobject to set the value of the object didnt work in this scope/context ?
By the way, a friend tried the same thing with Java and it failed as well.
In ruby all equals methods (methods that end with the = sign) need to have a receiver. Let me show you some code that will demonstrate why. Here is the code that sets a local variable to a value:
domain = "blah"
and here is the code that calls the domain= method:
domain = "blah"
In order for ruby to know that you are calling a method instead of setting a local variable you need to add a receiver. Simply change your method above to this and it will work:
def configure_domain(config={})
check_option_sub_domain
self.domain = config[:domain]
self.tld = config[:tld]
end
I'm pretty new to this world of Selenium and page objects but maybe one of my very recent discoveries might help you.
I found that that assignment methods for the select_list fields only worked for me once I started using "self" in front. This is what I have used to access it within my page object code. e.g., self.my_select_list="my select list value"
Another note - The send_keys workaround you mention is clever and might do the trick for a number of uses, but in my case the select list values are variable and may have several options starting with the same letter.
I hope something in here is useful to you.
UPDATE (Jan 3/12)
On diving further into the actual Ruby code for the page object I discovered that the select_list set is also using send_keys, so in actuality I still have the same limitation here as the one I noted using the send_keys workaround directly. sigh So much to learn, so little time!

Resources