I'm trying to write some ruby to perform the following:
I'm testing a web page which has a form, a search button, and a search results underneath.
When I enter an invoice number into the form e.g. 123456 and I click the "Search" button, if the Invoice has been successfully saved , then it will show up under the "Search Results" section, and if not, then it will say "No results found". Sometimes it takes a few seconds for the database to finish processing an invoice so I would like to make a loop that keeps pressing the search button every few seconds until the invoice is found, and once the condition is met, then do something else.
You can set up a while loop that breaks once a WebElement meeting certain criteria has been located.
# Keep track of whether or not invoice has been found
found = false
# First, attempt to locate the desired invoice
begin
# click the search button
driver.find_element(:name, "search_button").click
# check if invoice exists, set found to true if it does
expect(driver.find_element(some_locator_here).displayed?).to eql true
found = true
rescue Selenium::WebDriver::Error::NoSuchElementError
# catch NoSuchElementError if invoice does not exist, leave found as false
puts("Element not found")
# if invoice is not found, continue trying to find it in a loop
while found == false do
begin
# click the search button
driver.find_element(:name, "search_button").click
# attempt to locate the invoice
expect(driver.find_element(some_locator_here).displayed?).to eql true
found = true
rescue Selenium::WebDriver::Error::NoSuchElementError
puts("Element not found")
This will click the search button and attempt to locate the desired invoice in a loop, then catch the NoSuchElementError if the desired invoice does not exist. You will need to replace some_locator_here with something like :name, "invoice_name" so that you can locate your invoice and determine whether or not it has been found. Hope this helps a bit.
I have create multiple button response with following , now how can get the action from clicking on this button click
class ActionSearchCat(Action):
def name(self):
return "action_search_cat"
def run(self, dispatcher, tracker, domain):
buttons = []
resp = requests.get('http://localhost:3001/api/categoryList/0')
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('GET /tasks/ {}'.format(resp.status_code))
msg = resp.json().get('category_list').get('text')
for list in resp.json().get('category_list').get('choices').items():
for title, value in list.items():
buttons.append({"title": title, "payload": "/"+value})
dispatcher.utter_button_template(msg,buttons)
return []
So first of all, use dispatcher.utter_button_message(msg, buttons) instead of utter_button_template.
Now the buttons will be displayed on the channel to the end-user. After they click one, the next message your AI assistant receives will have the intent value (the payload from the selected button).
You'll have to write a story to handle this intent. If it is a unique intent to this button (i.e. you're not using something generic like Yes or No for your buttons) then the best approach would be to use the MappingPolicy. Either way, you'll also have to add the following story to your data as well:
## button_specific_action
* value
- corresponding_action_for_value
I have the following screen:
And I use the following Ruby script to click the "Add New" button:
vendorTab = driver.a id: "tab-master-tab-vendor"
vendorTab.wait_until_present
if vendorTab.exists?
vendorTab.click
end
addNewButton = driver.button class: ['btn btn-primary']
addNewButton.wait_until_present
if addNewButton.exists?
addNewButton.click
end
But, when I move to another tab and try to click the same "Add New" button, the Ruby script doesn't work.
Is there anything wrong with my Ruby code?
buildingTypeTab = driver.a id: "tab-master-tab-building"
buildingTypeTab.wait_until_present
if buildingTypeTab.exists?
buildingTypeTab.click
end
addNewButton = driver.button class: ['btn btn-primary']
addNewButton.wait_until_present
if addNewButton.exists?
addNewButton.click
end
I Appreciate your help. Thank you very much.
I guess all of these tabs are part of the same web page? I.e., all in the same HTML?
If that is the case, driver.button class: ['btn btn-primary'] is going to stop when it finds the first instance in the HTML, but that isn't the button you are looking for every time (it's the button in the first tab, where your script worked as you expected).
The best options in my mind are
find a way to uniquely identify the button in each tab (for example, use id instead of class if possible), or
pull all the buttons into a collection and click the button using its collection index after you figure out which index aligns with each tab. For example,
button_collection = browser.buttons(:class, ['btn', 'btn-primary'])
button_collection[2].click # Will click the 3rd button in the collection
After reading the suggestions from pjd,
I modified it a bit and got it working like this:
buildingTypeTab = driver.a id: "tab-master-tab-building"
buildingTypeTab.wait_until_present
if buildingTypeTab.exists?
buildingTypeTab.click
end
addNewButton = driver.button(:class => ['btn btn-primary'], :index => 2)
addNewButton.wait_until_present
if addNewButton.exists?
addNewButton.click
end
As pjd said, yes all these tabs are part of the same HTML
Thank you.
I am a Rails newbie. I want to use Koala's Graph API.
In my controller
#graph = Koala::Facebook::API.new('myFacebookAccessToken')
#hello = #graph.get_object("my.Name")
When I do this, I get something like this
{
"id"=>"123456",
"name"=>"First Middle Last",
"first_name"=>"First",
"middle_name"=>"Middle",
"last_name"=>"Last",
"link"=>"http://www.facebook.com/MyName",
"username"=>"my.name",
"birthday"=>"12/12/1212",
"hometown"=>{"id"=>"115200305133358163", "name"=>"City, State"}, "location"=>{"id"=>"1054648928202133335", "name"=>"City, State"},
"bio"=>"This is my awesome Bio.",
"quotes"=>"I am the master of my fate; I am the captain of my soul. - William Ernest Henley\r\n\r\n"Don't go around saying the world owes you a living. The world owes you nothing. It was here first.\" - Mark Twain",
"work"=>[{"employer"=>{"id"=>"100751133333", "name"=>"Company1"}, "position"=>{"id"=>"105763693332790962", "name"=>"Position1"}, "start_date"=>"2010-08", "end_date"=>"2011-07"}],
"sports"=>[{"id"=>"104019549633137", "name"=>"Sport1"}, {"id"=>"103992339636529", "name"=>"Sport2"}],
"favorite_teams"=>[{"id"=>"105467226133353743", "name"=>"Fav1"}, {"id"=>"19031343444432369133", "name"=>"Fav2"}, {"id"=>"98027790139333", "name"=>"Fav3"}, {"id"=>"104055132963393331", "name"=>"Fav4"}, {"id"=>"191744431437533310", "name"=>"Fav5"}],
"favorite_athletes"=>[{"id"=>"10836600585799922", "name"=>"Fava1"}, {"id"=>"18995689436787722", "name"=>"Fava2"}, {"id"=>"11156342219404022", "name"=>"Fava4"}, {"id"=>"11169998212279347", "name"=>"Fava5"}, {"id"=>"122326564475039", "name"=>"Fava6"}],
"inspirational_people"=>[{"id"=>"16383141733798", "name"=>"Fava7"}, {"id"=>"113529011990793335", "name"=>"fava8"}, {"id"=>"112032333138809855566", "name"=>"Fava9"}, {"id"=>"10810367588423324", "name"=>"Fava10"}],
"education"=>[{"school"=>{"id"=>"13478880321332322233663", "name"=>"School1"}, "type"=>"High School", "with"=>[{"id"=>"1401052755", "name"=>"Friend1"}]}, {"school"=>{"id"=>"11482777188037224", "name"=>"School2"}, "year"=>{"id"=>"138383069535219", "name"=>"2005"}, "type"=>"High School"}, {"school"=>{"id"=>"10604484633093514", "name"=>"School3"}, "year"=>{"id"=>"142963519060927", "name"=>"2010"}, "concentration"=>[{"id"=>"10407695629335773", "name"=>"c1"}], "type"=>"College"}, {"school"=>{"id"=>"22030497466330708", "name"=>"School4"}, "degree"=>{"id"=>"19233130157477979", "name"=>"c3"}, "year"=>{"id"=>"201638419856163", "name"=>"2011"}, "type"=>"Graduate School"}],
"gender"=>"male",
"interested_in"=>["female"],
"relationship_status"=>"Single",
"religion"=>"Religion1",
"political"=>"Political1",
"email"=>"somename#somecompany.com",
"timezone"=>-8,
"locale"=>"en_US",
"languages"=>[{"id"=>"10605952233759137", "name"=>"English"}, {"id"=>"10337617475934611", "name"=>"L2"}, {"id"=>"11296944428713061", "name"=>"L3"}],
"verified"=>true,
"updated_time"=>"2012-02-24T04:18:05+0000"
}
How do I show this entire hash in the view in a good format?
This is what I did from what ever I learnt..
In my view
<% #hello.each do |key, value| %>
<li><%=h "#{key.to_s} : #{value.to_s}" %></li>
<% end %>
This will get the entire thing converted to a list... It works awesome if its just one key.. but how to work with multiple keys and show only the information... something like
when it outputs hometown : City, State rather than something like
hometown : {"id"=>"115200305133358163", "name"=>"City, State"}
Also for education if I just say education[school][name] to display list of schools attended?
The error i get is can't convert String into Integer
I also tried to do this in my controller, but I get the same error..
#fav_teams = #hello["favorite_teams"]["name"]
Also, how can I save all these to the database.. something like just the list of all schools.. not their id no's?
Update:
The way I plan to save to my database is.. lets say for a user model, i want to save to database as :facebook_id, :facebook_name, :facebook_firstname, ...., :facebook_hometown .. here I only want to save name... when it comes to education.. I want to save.. school, concentration and type.. I have no idea on how to achieve this..
Looking forward for help! thanks!
To show the hash in a pretty-printed way, use the gem 'awesome_print'.
Add this to your Gemfile:
gem 'awesome_print'
And then run:
bundle install
And then, in your view, you can add:
<%= ap #hello %>
The question of how to store in the database requires a little more information on what you plan to do with it, but at minimum you could create a model, add a 'facebook_data' (type would be 'text') on that model, and then serialize it (add this line near the top of your model file: serialize :facebook_data). Then you could assign the hash (#hello in this case) to the model's 'facebook_data' property, and then save the model. But you won't be able to query your database for individual attributes of this facebook data very easily this way.
you can just do #hello["name"] then it will give you the value of the name
Your #hello object should be of the class Koala::Facebook::API::GraphCollection or something similar. You should be able to loop through this object, like your question demonstrates. As for what code to put inside your loop that will help you save records to the database, assuming your rails user model class name is User, try something like this:
#hello.each do |h|
u = User.where(:facebook_id => h["id"]).first_or_initialize
u.update_attributes(
:name => h["name"],
:first_name => h["first_name"],
:hometown_city => h["hometown"]["name"].split(",").first,
:hometown_state => h["hometown"]["name"].split(",").last.strip
# ETC, ETC
)
end
In the case of the hometown and education fields, you're just going to have to traverse the ruby hash the proper way. See the docs for more info.
Watir::Browser.default = "firefox"
ie = Watir::Browser.start("http://cars.com")
ie.select_list(:id, 'make_1').set('Chevrolet')
ie.select_list(:id, 'model_1').set('Cobalt')
ie.select_list(:id, 'pricehighnew').set('No Max')
ie.select_list(:id, 'rdnew').set('30 miles')
ie.text_field(:name, "zc").set "44109"
ie.form(:method, "GET").submit #Here is the problem...
URL: http://www.cars.com/
Can anyone help me initiate button click action searching for "New Cars" form on the top left. Seems like they are using JavaScript as well. I appreciate any help.
There's probably a way to do it with JavaScript, but taking just a minute I was able to click the button two different ways:
ie.span(:text=>"Search New").click
ie.link(:href=>"javascript:checkZipFirst(document.newForm, quickSubmitNew, document.newForm.zc.value);").click
Also any of these would work:
browser.a(:class => "button primary zc-submit").click
or
browser.link(:name => "submit").click
or
browser.a(:id => "submit", :index => n).click
where n is the index number