Downloading the body of sites using user input - ruby

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. : )

Related

Launch a web url on link click - Ruby Shoes 3

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.

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

Refresh ruby stack with Shoes

this can be a very simple question but I have been searching for it a long time and haven't found any valid answer yet...
I am try to do a Shoes app with ruby to get a list of names and save it in a file. I would like to show the list that have been introduced already and as a new name enters the list the list which is shown would be updated. Also, I would like to associate a delete button to each element of the list so the user can remove that name if needed.
After all this, the only thing that I can get is the part in which you add a name and it gets saved into a list but adding this second stack with a list of all names with a delete button... not so lucky. This is how my code looks like, the functions addName and saveFile are not here but they only do some work with the list. Also, some global variables and constat definitions are not shown:
Shoes.app(title: "My Higgs field!",
width: APPW, height: APPH, resizable: false) {
stack{
flow{
#edit = edit_line
#enter = button "Add"
#enter.click do
info "Enter to add #{#edit.text}"
addName(#edit.text)
end
}
#save_file = button "Guardar ficheiro"
#save_file.click do
saveFile
end
}
stack do
# Show a list with all the names inserted
$names.each do |name|
flow{
#line = para name
info "Putting line #{#line}"
#delete = button "Apagar"
#delete.click do
deleteName name
end
}
end
end
}
Any inputs on this is very wellcome!
Thanks a lot!
A shoes question so long not answered ! That can't be.
Here a fully working version, i took the liberty to reorganise everything and use my favorite shoes color: green
require 'green_shoes'
Shoes.app(title: "My Higgs field!", width: 200, height: 200, resizable: false) do
#names, #list = [], nil
def update
#list.clear()
#names.each do |name|
#list.append do
flow do
para link('del'){del(name); update}, ' ', name
end
end
end
end
def add name
#names << name
#names = #names.uniq.sort
update
end
def del name
#names.delete(name)
update
end
stack do
flow do
#edit = edit_line(width: 110, height: 23)
keypress do|k|
if k == "\n"
add(#edit.text)
end
end
button "Add" do
add(#edit.text)
end
button "Save file" do
File.open("names.txt", "w+") { |file| file.write(#names.join("\n")) }
alert("names.txt is saved")
end
end
#list = flow do
#names.each do |p|
para p, :size => 10
end
end
end
end

How to make an element take up all-the-width-that-is-left?

Shoes.app do
flow do
file = "something with variable length"
para "Loading #{file}: "
progress :width => -300
end
end
As you can see from the code I am trying to display a progress bar that goes from the end of the text until the right edge of the application window.
When the text has a fixed length this solution works but it doesn't once the text changes length in the above fragment: there will be either too little or too much space for the progress bar.
Is there a solution to this problem?
I tried asking the para element it's width but it is 0???
As I mentioned before, you have to get the width of the textblock after it is calculated. Try this:
Shoes.app do
flow do
file = "something with variable length"
#p = para "Loading #{file}: "
#prog = progress
start do
#prog.width = #prog.parent.width - #p.width
end
end
button 'Change text!' do
text = #p.text
#p.text = text + '1'
#prog.width = #prog.parent.width - #p.width
end
end

Resources