How to get the attribute_value of a page_object element? - ruby

Imagine i have a image like:
the_image = #browser.image(:id, 'image01')
The way to get the value of its class could be:
image_status = the_image.attribute_value('class')
Ok. I'm using page_object gem and lets suppose i have the image as element, like:
image(:the_image, :id => 'image01')
To get the attribute_value i can use:
image_status = the_image_element.attribute_value('class')
...but i get a DEPRECATION WARNING:
*** DEPRECATION WARNING
*** You are calling a method named attribute_value at page.rb:68:in `get_status'.
*** 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 can i get the class value using the page_object element? Sure the answer is easy but i didn't found it. Can you please tell me the way?
Thank you in advance!

If you are using the latest versions of Page-Object and Watir (v2.2.1 and v6.7.3 respectively), the attribute_value will no longer give a deprecation warning. Both #attribute and #attribute_value are supported.
For older version of Page-Object, you need to use the #attribute method:
image_status = the_image_element.attribute('class')

Related

Upgrading to Ruby 3.1 causes Psych::DisallowedClass exception when using YAML.load_file

When upgrading to ruby 3.1, I am seeing the following sort error message when using YAML.load_file some_file_name
Psych::DisallowedClass:
Tried to load unspecified class: Matrix
Other load statements cause similar errors but cite different unspecified classes e.g. OpenStruct. It appears that the latest version of YAML only loads classes from a permitted white list, so it is necessary to use a permitted_class keyword to allow other classes. I have tried
hsh = YAML.load_file some_file_name, permitted_classes: [Matrix, OpenStruct]
but this gives the error
Psych::DisallowedClass:
Tried to load unspecified class: Symbol
how do I fix this?
The working solution is to add this line to config/application.rb
config.active_record.yaml_column_permitted_classes = [ActiveSupport::HashWithIndifferentAccess]
You can do the same with any class name, like
config.active_record.yaml_column_permitted_classes = [Symbol, Hash, Array, ActiveSupport::HashWithIndifferentAccess]
Symbol is also not allowed per default. Therefore just add Symbol to the permitted_classes too:
hash = YAML.load_file(
some_file_name,
permitted_classes: [Matrix, OpenStruct, Symbol]
)
See the list of default permitted_classes.
Had this on rails 6.1 upgrade. If you have no other choice, maybe this workaround will bring you some time (application.rb):
config.active_record.use_yaml_unsafe_load = true

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

Ruby no method error with HTTParty

I am trying to make a post request in Ruby and I am getting inconsistent behaviour. I am currently using ruby-2.0.0-p598 on OSX.
When using PRY and I type the following post command:
HTTParty.post(#base_uri + '/method/?argument1&api_key=' + #api_key)
I get a successful respond from the API. However when I run it through my specs or inside the class I get:
undefined method `+' for nil:NilClass
I know it has to do with the plus sign, but I find it weird that I am getting a different behaviour. Can you please suggest what is the correct way of doing this?
Thanks in advance.
Good day
Behavior correct - some variable = nil.
You have check variables, or (in this case it is better not to do) call to_s:
HTTParty.post(#base_uri.to_s + '/method/?argument1&api_key=' + #api_key.to_s)
It looks like #base_uri and/or #api_key is null. Please double check if they are initialized with valid strings or not. Then try
HTTParty.post("#{#base_uri}/method/?argument1&api_key=#{#api_key}")
In this case, ruby will automatically try to convert #base_uri and #api_key to string so no need to call to_s method explicitly.

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

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

How to use Bigquery streaming insertall on Ruby Rails

EDIT: Fixed - for ruby use "insert_all" instead of "insertAll" like the api specifies. The api for ruby needs updating.
Im using v 0.6.4 of the google-api-client gem and trying to create a streaming insert, but keep getting the following error:
google_bigquery.rb:233:in undefined method `insertAll' for #<Google::APIClient::Resource:0xcbc974 NAME:tabledata> (NoMethodError)
My code is as follows:
def streaming_insert_data_in_table(table, dataset=DATASET)
body = {"rows"=>[
{"json"=> {"person_id"=>1, "name"=>"john"}},
{"json"=> {"person_id"=>2, "name"=>"doe"}},
]}
result = #client.execute(
:api_method=> #bigquery.tabledata.insert_all,
:parameters=> {
:projectId=> #project_id.to_s,
:datasetId=> dataset,
:tableId=>table},
:body_object=>body,
)
puts result.body
end
Could someone tell me if the insetAll has been created for the google-api-client gem? I have tried 'insert' as that is what table, dataset etc use and get the same error as well.. I can however run tabledata.list perfectly fine.. I've tried digging throught the gem source code and didn't get anywhere with that.
Is the body object that I created correct or do I need to alter it?
Any help is much appreciated.
Thanks in advance and have a great day.
Ok. So fixed it and updated the code in the question. For ruby: the method is called "insert_all". Also note that the table & schema must be created BEFORE the insert_all. This id different when compared to the the "jobs.insert" method which will create the table if it doesn't exist

Resources