Launch a web url on link click - Ruby Shoes 3 - ruby

How would I go about launching a web url e.g. https://google.com after clicking a link on Ruby shoes 3?
I used the below code to no effect
flow do
###
para link("some text", click: proc {
visit "https://google.com"
})
###
end
And
flow do
###
para link "some text" do visit "https://google.com" end
###
end
Could someone show me how please?

Try this
flow do
para(link("some text", :click => "https://google.com"))
end

Fixed the link using the method provided on this question
flow do
proc = Proc.new {
system("cmd /c start https://google.com")
}
para link("some text", &proc)
end
Any further suggestions before I close this is welcome.

Related

Unexpected behaviour when using TTY-Prompt

require 'tty-prompt'
def landing_page
prompt = TTY::Prompt.new
choices = ["Create", "Update", "Delete", "Quit"]
answer = prompt.multi_select("Select", choices)
case answer
when choices[0]
puts "works"
else
puts "doesn't work"
sleep(1)
self.landing_page
end
end
landing_page
It's my first time trying to make a terminal app using TTY-Prompt. I'm trying to test the ^above piece of code and expect it to return "works" when the user selects "Create" from the list but it doesn't. Am I missing something?
EDIT: Answer is replace multi_select with select. Thanks to Stefan for clearing that up.

Save or copy text in Edit_box or line to an external file in Ruby Shoes

Using Shoes 3.3.7
How do I go about grabbing the text typed in an Edit_box and saving it to a file on a button click?
This is what i used. It created the file but it just stays empty...
Shoes.app do
Stack do
flow do
new_box = edit_box "placeholdertext"
end
flow do
button "Save" do
note_save = ask_save_file
File.open("#{note_save}", "a") do |copy|
copy.para "#{new_box.text}"
end
end
end
end
end
Edit : setting code to,
copy.write(new_box.text)
Still creates a file with empty content
I'm pretty new to all this. Any help is appreciated 😊
you have to use instance variables in such cases:
Shoes.app do
stack do
flow do
#new_box = edit_box "placeholdertext"
end
flow do
button "Save" do
note_save = ask_save_file
File.open("#{note_save}", "w") do |file|
file.write #new_box.text
end
end
end
end
end
Best, seba

StaleElementReferenceError in ruby selenium

I was trying to automate rediff.com . I went from one page to other but when i came back i got staleException . I tried a lot but couldn't fix it.
I am attaching the code snippet too. Any help would be appreciated.
#driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon"
#driver.manage.window.maximize
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
begin
element = wait.until { #driver.find_element(:xpath,".//*[#id='popular_cat']") }
ensure
box=#driver.find_element(:xpath,".//*[#id='popular_cat']")
end
links=box.find_elements(:tag_name,"a")
puts "Total links are:#{links.size}"
links.each do |i|
puts "--------------------"
puts "Value of all links is:#{i.text}"
i.click
puts "Title of page is :#{#driver.title}"
#driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon"
box=#driver.find_element(:xpath,".//*[#id='popular_cat']")
links=box.find_elements(:tag_name,"a")
end
Every time you reload the page (because you are going to other page and then going back or because you simple reloaded the page) your references to the links 'links=box.find_elements(:tag_name,"a")' are lost.
I would suggest a few changes in order to workaround this (might not be the best solution)
links = box.find_elements(:tag_name,"a").size
links_counter = 0
while links_counter < links
box = #driver.find_element(:xpath,".//*[#id='popular_cat']")
current_link = box.find_elements(:tag_name,"a")[links_counter]
links_counter += 1
puts "--------------------"
puts "Value of all links is:#{current_link.text}"
current_link.click
puts "Title of page is :#{#driver.title}"
#driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon"
end
I hope this helps you!
Best,
Fernando

Downloading the body of sites using user input

I want to use shoes to be able to download the body of a website that a user enters in an edit_line. How can I make this work? Can anyone help explain why the below code does not work? It just pops up a new window, and does not download the site from the text entered....
Here's my code thus far:
Shoes.app do
stack (:left => 175, :top => 200) do
para "Enter a url:"
flow do
#url = edit_line
button "OK" do
window do
stack do
title "Searching site", :size => 16
#status = para "One moment..."
# Search site for query and print body
download #url.text do |site|
#status.text = "Body: " + site.response.body.inspect
end
end
end
end
end
end
end
Nevermind - I figured it out. I just didn't pop to a new window and it downloads and prints the body fine. : )

Is it possible to navigate back in browser with watir?

I'm creating an automated tests that should navigate back a few steps.
Is it possible to do so in Watir??
I'm navigating to a resultlist page and want to go back to the start of my test just by clicking back (windows browser).
Thnx
If you have a Watir::IE object, then #back is a method that does what you are looking for:
http://wtr.rubyforge.org/rdoc/classes/Watir/IE.html#M000245
What error are you seeing?
This is how i tried to do it:
#site.ie.wait_until(1200) { |ie| ie.contains_text(/Sort after/) }
2.times{#site.ie.back
}
Example code that uses Watir's #back call (based on the standard Wikipedia example):
require 'watir'
test_site = 'http://www.google.com/'
ie = Watir::IE.new
ie.goto(test_site)
ie.text_field(:name, "q").set("pickaxe")
ie.button(:name, "btnG").click
if ie.text.include?("Programming Ruby")
puts "Test Passed. Found the test string: 'Programming Ruby'."
else
puts "Test Failed! Could not find: 'Programming Ruby'."
end
ie.back
if ie.url == test_site
puts "Test Passed. Returned to search page."
else
puts "Test Failed! URL is now #{ie.url}."
end
ie.close
You certainly can execute JavaScript using Watir.
#browser.execute_script('window.history.back();')

Resources