Why does the deprecation warning appear when using the label method? - ruby

A DEPRECATION WARNING appears when I run following code:
class MatchingPage
include PageObject
include Watir
div(:choose_competitor_dialog, :class => 'dijitDialogPaneContentArea pf-matching-competitors-dlg')
def competitor_name_select (name)
self.choose_competitor_dialog_element.label(:text => name).parent.checkbox(:class => 'dijitReset dijitCheckBoxInput').set
end
end
on(MatchingPage) do |matching_page|
matching_page.competitor_name_select 'shop.com'
end
The warning says:
* DEPRECATION WARNING
You are calling a method named label at /home/spoonest/workspace/csv_ui_checker/pages.rb:77:in
`competitor_name_select'.
This method does not exist in page-object so it is being passed to the driver.
This feature will be removed in the near future.
Please change your code to call the correct page-object method.
* If you are using functionality that does not exist in page-object please request it be added.
How can I locate the label element without getting this warning?

If a Page Object element does not know a method called, in this case label, the method is delegated to the underlying Watir (or Selenium) element. When this happens, you will get the warnings.
To locate a child label element, the method is called label in Watir. However, to avoid the warning in the page object gem, it should be label_element:
def competitor_name_select (name)
self.choose_competitor_dialog_element.label_element(:text => name).parent.checkbox(:class => 'dijitReset dijitCheckBoxInput').set
end

Related

Cheezy Page-Object Gem Dynamic Locator for Generic Element?

Using Cheezy's page-object gem I've come across the ability to have dynamic element locators. (Noted at this github issue: https://github.com/cheezy/page-object/issues/203).
So for example I can do span_element(id: 'some id'), div_element(class: 'some_class'), etc. However what can I do if I need to locate a generic element? For example I could be working on a page that has angular so the elements are not traditional (like instead of a traditional html select control with options, it is a custom angular dropdown). I've tried element_element(class: 'class_name') and just element(class: 'class_name') but ruby says method missing
The generic dynamic element locator is defined in PageObject::ElementLocators#element as:
def element(tag, identifier={:index => 0})
platform.element_for(tag, identifier.clone)
end
The first argument is the element's tag name. If you don't know the tag name, you can specify "element" for any tag. For example:
class MyPage
include PageObject
def do_stuff
element('element', class: 'class_name').text
end
end

Which is new method to set value for text field

I use:
self.txtLogin_element.when_present.set(email)
But when it executes I get a warning:
*** You are calling a method named set at C:/login_page.rb:12:in `specify_email'.
*** This method does not exist in page-object so it is being passed to the driver.
*** This feature will be removed in the near future.
How to specify a new variant with when_present
Variants:
self.txtLogin_element.when_present = email
self.txtLogin.when_present.set(email)
do not work.
Assuming that txtLogin_element is a text field (PageObject::Elements::TextField), there is no set method. The Page-Object gem sets text fields via the value= method instead. Therefore, to remove the warning, use:
self.txtLogin_element.when_present.value = email
If you have made the switch to Page-Object v2.0 and therefore Watir v6.0, when_present is no longer needed. Watir now waits for elements to be present before interacting with them. You can now simply do:
self.txtLogin_element.value = email
Which ultimately means that you can just use the methods generated by the accessor:
self.txtLogin = email

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

Wait on a text to be appear in SitePrism Capybara Framework

I tried to wait on a text before perform any action by follows SitePrism URL https://github.com/natritmeyer/site_prism in this section>> "Methods Supporting Capybara Options".
#page.wait_until_<Element>_visible :text => "Some Text!!!"
But i am getting below error:
undefined method `zero?' for {:text=>"Some Text!!!"}:Hash (NoMethodError)
Why i am getting this error? Am i doing something wrong?
Looking at the site_prism code - https://github.com/natritmeyer/site_prism/blob/master/lib/site_prism/element_container.rb#L134 the generated method takes a timeout, and the options. It seems like you need to pass the timeout value if you want to pass other options
wait_until_<Element>_visible <timeout value in seconds>, text: "Some Text!!!"
Seems like an error in the documentation, or some old defaulting behavior was removed or something
Old question
For those still hitting this SO answer, this has been remedied in v3 of the API and is no longer an issue. See: https://github.com/natritmeyer/site_prism/blob/master/UPGRADING.md#wait_until-methods
wait_for_ methods now no longer exist, and you should just implicitly wait by calling element i.e. my_button
If you want it to wait, you can modify the Capybara.default_wait_time or pass in a wait key i.e. my_button(wait: 3)

Getting deprecation warnings when calling methods on elements - Could use some advice

I am getting deprecation warnings pretty frequently when calling methods on page object elements. This signals to me that I may not be using the gem as intended yet. I could use some help, could anyone recommend a better way to handle something like this?
I am working with a page containing a list of divs that each hold a checkbox element. In other words, something like this:
<div class="item-checkbox"><input type="checkbox"></div>
<div class="item-checkbox"><input type="checkbox"></div>
<div class="item-checkbox"><input type="checkbox"></div>
I would like to access a checkbox by index, and then check it when needed. Here is what I currently have:
def select_checkbox(index)
fail "Nothing in list" unless checkboxes.length > 0
checkbox = self.checkboxes[index].checkbox_element
if checkbox.exists?
checkbox.set
else
fail "could not select a checkbox at index #{index} - check that it exists"
end
end
protected
def checkboxes
div_elements(:class=> 'item-checkbox')
end
This works, however I get a deprecation warning on line 5, checkbox.set. Changing it to checkbox.click clears it up. Nevertheless, I am not convinced that I am doing this the "page-object gem" way. Using watir-webdriver, divs gives me an array to work with, and I can accomplish the same thing in a similar way. Has anyone done anything like this using the gem?
What you have above looks fine except the set method does not exist on the CheckBox element. Instead, there are check, uncheck, and checked? methods. I think you can safely change your above method to this:
def select_checkbox(index)
fail "Nothing in list" unless checkboxes
checkbox = self.checkboxes[index].checkbox_element
if checkbox.exists?
checkbox.check
else
fail "could not select a checkbox at index #{index} - check that it exists"
end
end
protected
def checkboxes
div_elements(:class=> 'item-checkbox')
end
Another way to possibly do this is to declare the divs in the class like this:
class MyPage
include PageObject
divs(:checkbox, :class => 'item-checkbox')
def select_checkbox(index)
fail "Nothing in list" unless checkboxes
checkbox_elements[index].checkbox_element.check
end
end
In this case you are still checking to see if there are any checkbox divs on the page. In the second line of the method you will fail if the checkbox does not exist so it takes care of the condition you guarded against.
-Cheezy
You need to use checkbox.check, not checkbox.set.
Here is a similar SO thread for reference:
How to avoid page-object deprecated for checkbox

Resources