Putting Faker Gem Values to a Hash - ruby

I'm writing automated tests using Cucumber, Capybara, WebDriver, SitePrism, and Faker. I am new to this and need some help.
I have the following steps..
Given (/^I have created a new active product$/) do
#page = AdminProductCreationPage.new
#page.should be_displayed
#page.producttitle.set Faker::Name.title
#page.product_sku.set Faker::Number.number(8)
click #page.product_save
#page.should have_growl text: 'Save Successful.'
end
When (/^I navigate to that product detail page) do
pending
end
Then (/^All the data should match that which was initially entered$/) do
pending
end
In config/env_config.rb I have set up an empty hash...
Before do
# Empty container for easily sharing data between step definitions
#verify = {}
end
Now I want to hash the value generated by Faker in the Given step so I can validate it saved properly in the When step. I also want to enter the value generated by faker in the script below into a search field.
#page.producttitle.set Faker::Name.title
How do I push the values generated by faker to the #verify has?
How do I pull that value and insert it into a text field?
How do I pull that value to verify the save value equals the value generated by faker?

1. How do I push the values generated by faker to the #verify has?
A hash is simply a dictionary of key-value pairs, which you can set with hash[key] = value.
The key can be a string #verify['new_product_name'] = Faker::Name.title
The key can also be a symbol #verify[:new_product_name] = Faker::Name.title
Since the value you generate may be used multiple times within the step definition (once for storing it in the #verify hash, and once for setting the field value) I personally prefer to first store it in a local variable, and reference that where needed.
new_product_title = Faker::Name.title
#verify[:new_product_title] = new_product_title
2. How do I pull that value and insert it into a text field?
You can reference values by their key. So after you have stored the value in the hash, you could do this
#page.producttitle.set #verify[:new_product_name]
Or if you stored it in a local variable as suggested above, you would just do this
#page.producttitle.set new_product_name
3. How do I pull that value to verify the save value equals the value generated by faker?
Similarly, you can assert that a field value equals what you've stored in the hash. An example would be #page.producttitle.value.should == #verify[:new_product_name]
Putting this all together:
Given (/^I have created a new active product$/) do
#page = AdminProductCreationPage.new
#page.should be_displayed
# Create a new title
new_product_title = Faker::Name.title
# Store the title in the hash for verification
#verify[:new_product_title] = new_product_title
# Set the input value to our new title
#page.producttitle.set new_product_title
#page.product_sku.set Faker::Number.number(8)
click #page.product_save
#page.should have_growl text: 'Save Successful.'
end
When (/^I navigate to that product detail page) do
pending
end
Then (/^All the data should match that which was initially entered$/) do
#page.producttitle.value.should == #verify[:new_product_title]
end

Related

get text with index number and then compare with the expected text

I need to write a method which will get text with the help of index number from popup and then i need to compare with the expected text
i.e i need to verify expected plan name is displayed at the bottom of the popup box
Setting the correct id for the query (which you can get by doing on calabash console the command query("*", :id)) on code below should do the trick. If you can't use id try to get another component property (like Android component by using query("*") ) and set the query inside theget_text calls.
def get_text(query)
query(plan_query, :text).first
end
def text_equals(text, expected_text)
unless text == expected_text
fail "#{text} not equal to #{expected_text}"
end
end
def verify_plan(index, expected_text)
plan_text = get_text("* id:'PLAN_TEXTS_ID' index:#{index}") # Can change 'id:...'' by Android class if plan does not have id
expected_text = get_text("* id:'BOTTOM_PLAN_ID'") # Same as above
text_equals(plan_text, expected_text)
end

Parse a string with multiple XML-like tags using Ruby

I have a string which looks like the following:
string = " <SET-TOPIC>INITIATE</SET-TOPIC>
<SETPROFILE>
<PROFILE-KEY>predicates_live</PROFILE-KEY>
<PROFILE-VALUE>yes</PROFILE-VALUE>
</SETPROFILE>
<think>
<set><name>first_time_initiate</name>yes</set>
</think>
<SETPROFILE>
<PROFILE-KEY>first_time_initiate</PROFILE-KEY>
<PROFILE-VALUE>YES</PROFILE-VALUE>
</SETPROFILE>"
My objective is to be able to read out each top level that is in caps with the parse. I use a case statement to evaluate what is the top level key, such as <SETPROFILE> but there can be lots of different values, and then run a method that does different things with the contnts of the tag.
What this means is I need to be able to know very easily:
top_level_keys = ['SET-TOPIC', 'SET-PROFILE', 'SET-PROFILE']
when I pass in the key know the full value
parsed[0].value = {:PROFILE-KEY => predicates_live, :PROFILE-VALUE => yes}
parsed[0].key = ['SET-TOPIC']
I currently parse the whole string as follows:
doc = Nokogiri::XML::DocumentFragment.parse(string)
parsed = doc.search('*').each_with_object({}){ |n, h|
h[n.name] = n.text
}
As a result, I only parse and know of the second tag. The values from the first tag do not show up in the parsed variable.
I have control over what the tags are, if that helps.
But I need to be able to parse and know the contents of both tag as a result of the parse because I need to apply a method for each instance of the node.
Note: the string also contains just regular text, both before, in between, and after the XML-like tags.
It depends on what you are going to achieve. The problem is that you are overriding hash keys by new values. The easiest way to collect values is to store them in array:
parsed = doc.search('*').each_with_object({}) do |n, h|
# h[n.name] = n.text :: removed because it overrides values
(h[n.name] ||= []) << n.text
end

Getting model value and generated ID from within simple_form custom input

I'm trying to make a custom input type with simple_form that will implement combobox-type functionality using jQuery-Autocomplete
. What I need to do is output a hidden field that will hold the ID of the value selected and a text field for the user to type in.
Here's what I have so far:
class ComboboxInput < SimpleForm::Inputs::Base
def input
html = #builder.hidden_field(attribute_name, input_html_options)
id = '' #what?
value = '' #what?
return "#{html}<input class='combobox-entry' data-id-input='#{id}' value='#{value}'".html_safe
end
end
I need to get the ID of the hidden field that simple_form is generating to place as an HTML attribute on the text entry to allow the JavaScript to "hook up" the two fields. I also need to get the value from the model to prepopulate the text input. How do I do this from within my custom input?
I'm looking for the id as well, but I did get the value:
def input
current_value = object.send("#{attribute_name}")
end
I just found a hokey id workaround:
html = #builder.hidden_field(attribute_name, input_html_options)
id = html.scan(/id="([^"]*)"/).first.first.to_s
I know it's a hack, but it does work. Since we don't have access directly to this type of resolution, it is likely to keep working even if the underlying id creation code changes.

Creating new class instances inside an array without overwriting existing ones

I've currently got a system that involves quite a lot of new class instances, so I've had to assign them using an array, as was suggested here: Create and initialize instances of a class with sequential names
However, I'll have to be constantly adding new instances whenever a new one appears, without overwriting existing ones. Might some validation and a modified version of my existing code be the best option?
This is my code, currently every time it runs, the existing data is overwritten. I want the status to be overwritten if it's changed, but I also want to be able to store one or two variables in there permanently.
E2A: Ignore the global variables, they're just there for testing.
$allids = []
$position = 0 ## Set position for each iteration
$ids.each do |x| ## For each ID, do
$allids = ($ids.length).times.collect { MyClass.new(x)} ## For each ID, make a new class instance, as part of an array
$browser.goto("http://www.foo.com/#{x}") ## Visit next details page
thestatus = Nokogiri::HTML.parse($browser.html).at_xpath("html/body/div[2]/div[3]/div[2]/div[3]/b/text()").to_s ## Grab the ID's status
theamount = Nokogiri::HTML.parse($browser.html).at_xpath("html/body/div[2]/div[3]/div[2]/p[1]/b[2]/text()").to_s ## Grab a number attached to the ID
$allids[$position].getdetails(thestatus, theamount) ## Passes the status to getdetails
$position += 1 ## increment position for next iteration
end
E2A2: Gonna paste this from my comment:
Hmm, I was just thinking, I started off by making the previous values dump into another variable, then another variable grabs the new values, and iterates over them to see if any match the previous values. That's quite a messy way to do it though, I was thinking, would self.create with a ||= work? – Joe 7 mins ago
If I understand you correctly, you need to store status and amount for each ID, right? If so, then something like this would help you:
# I'll store nested hash with class instance, status and amount for each id in processed_ids var
$processed_ids = {}
$ids.each do |id|
processed_ids[id] ||= {} #
processed_ids[id][:instance] ||= MyClass.new(id)
processed_ids[id][:status] = get_status # Nokogiri method
processed_ids[id][:amount] = get_amount # Nokogiri method
end
What does this code do: it only once creates instance of your class for each id, but always updates its status and amount.

Can I add params values to a hash?

I have a User model. I also have a form_for(#user...) form. This form spans 3 partials. In order for every partial to remember values I use the following command inside my create action in my UsersController:
session[:user_params].deep_merge!(params[:user]) if params[:user]
This way every partial adds params[:user] to session[:user_params]. I also have other form values stored inside the params hash which are not part of the User model. Is there a command which would allow me to add all single params values (not just the :user hash) to the session[:user_params] hash without adding every single value one by one like this:
session[:num_children] = params[:num_children] if params[:num_children]
...etc...
Try:
params.each {|key,value| session.deep_merge!(key=>value)}

Resources