Problem: Nested function(handler) in AppleScript? - applescript

How can I put a handler/function in another handler/function?
I write a sample code below and got error:
"Expected "end" but found "on""
Why?
Dialog1()
on Dialog1()
display dialog "Hello World!"
Dialog2()
on Dialog2()
display dialog "Welcome to AppleScript"
end Dialog2
end Dialog1

You were real close. This should work for you.
Dialog1()
on Dialog1()
display dialog "Hello World!"
Dialog2()
end Dialog1
on Dialog2()
display dialog "Welcome to AppleScript"
end Dialog2
Or this
Dialog1()
Dialog2()
on Dialog1()
display dialog "Hello World!"
end Dialog1
on Dialog2()
display dialog "Welcome to AppleScript"
end Dialog2
You can also wrap everything up inside of a script object and then just run the script object..
run twoDialogs
script twoDialogs
Dialog1()
Dialog2()
on Dialog1()
display dialog "Hello World!"
end Dialog1
on Dialog2()
display dialog "Welcome to AppleScript"
end Dialog2
end script

Related

Is there a way to abort a test cleanly?

Using Selenium, Ruby
I'm trying to learn the correct way of closing out a test should an object not exist. For example I have a test that calls a function "Click_Login" which in turn goes to the function and returns the object reference or the text "Stop Test" if it does not exist. That part is working correctly.
However after the browser is closed, the test continues on and tries to varLoginBtn.click and fails because Stop Test.click does not exist. I thought the test would have stopped after the driver.quit and not continue on to varLoginBtn.click.
My goal is to have the test stop cleanly if an object does not exist. I maybe doing this incorrectly.
#The test.rb
varLoginBtn = toNavigate.Click_LogIn(driver) #calls function down below.
if varLoginBtn == "Stop Test"
driver.quit
end
varLoginBtn.click #click on the button
#functions.rb
#in the Class toNavigate
#Login Button
def Click_LogIn(driver)
puts "In the login"
wait = Selenium::WebDriver::Wait.new(:timeout => 15)
begin
element= wait.until{driver.find_element(:xpath, "//*[#xas-string='SDE_LOG_INN']")} #Log_INN is intentional I want it to fail.
return element
rescue
puts "The Login button did not exist"
return "Stop Test"
end
end
Thanks for your help.
You don't need to rescue, you have a condition if nil and you can use abort to exit script with a message
But also use snake_case for def ruby methods
def click_login(driver)
puts "In the login"
wait = Selenium::WebDriver::Wait.new(:timeout => 15)
if element = wait.until{driver.find_element(:xpath, "//*[#xas-string='SDE_LOG_INN']")} #Log_INN is intentional I want it to fail.
return element
else
puts 'The Login button did not exist'
abort 'Stop Test'
end
end
This is an overengineered way to do this... why not just throw an exception when this occurs and let the test die with a good message? Something like
...
rescue
raise "The Login button did not exist"
end
Your test library should be able to handle this and print a nice message that you can use to investigate, etc.
Read more about exceptions in ruby here.

Checking if test failed and giving console output accordingly

I am new to ruby and would like to add a step in my ruby/capybara test where
"if Test A failed,
log this message in the console: "Microservice A is currently down."
Should this be done in the after hook or inside the test? Also, what would the commands be?
describe 'Test Description' do
before (:each) do
login end
after (:each) do
logout
if test fail do
console.log ("Error: Microservice A currently is down")
end
end
it 'Check Page X Loads', :retry => 3, :retry_wait => 3 do
page.should have_content 'Frisbee'
navigate_to_menu 'Toys'
page.has_content?("Frisbee")
expect(page).to have_content('Buy Frisbee') end
end
Thank you
The after hook receives the test that was run as a parameter so you can do
after do |example|
if example.exception
puts "Error: Microservice A currently is down"
end
end

Connect function to a QPushButton press

I want to execute a user defined function after a button was pressed. I don't know how to use the connect function correctly to achieve the behavior specified in the code snippet.
#!/usr/bin/env ruby
require 'Qt4'
def do_sth
print "did something"
end
app = Qt::Application.new(ARGV)
btn = Qt::PushButton.new('Button')
btn.resize(75, 30)
btn.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
# A button click will close the application.
#Qt::Object.connect(btn, SIGNAL('clicked()'),app, SLOT('quit()'))
#
# FIXME How to execute the function do_sth if the button was pressed?
Qt::Object.connect(btn, SIGNAL('clicked()'),app, SLOT('do_sth()'))
btn.show()
app.exec()
Thanks for your hint, it worked the way you suggested.
#!/usr/bin/env ruby
require 'Qt4'
class Qo < Qt::Object
slots 'do_sth()'
slots 'bla()'
def do_sth
puts "did something"
end
def bla
puts "blabla"
end
end
qobj = Qo.new
app = Qt::Application.new(ARGV)
btn = Qt::PushButton.new('Button')
btn.resize(75, 30)
btn.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
Qt::Object.connect(btn, SIGNAL('clicked()'),qobj, SLOT('do_sth()'))
Qt::Object.connect(btn, SIGNAL('pressed()'),qobj, SLOT('bla()'))
btn.show()
app.exec()

Rspec examples continue to run in spite of sleep being encountered

I have the following rspec
context "example 1" do
puts "hello"
it "example 1 it" do
sleep(50)
end
end
context "example 2" do
puts "thanks"
it "example 2 it"
end
end
When I run this using rake, I get the following output:
hello
thanks
example 1
example 1 it
<<waits for 50s >>
Why is thanks printed while it is sleeping for 50s? I expect thanks to be printed only after the sleep is over.
The puts is being executed as rspec processes your file, not in conjunction to the tests being run. To prove this to yourself, you could try referencing an undefined local variable after the it statement.
For example:
context "example 1" do
puts "hello"
it "example 1 it" do
sleep(50)
end
puts "goodbye" + cruel_world
end
When you run rspec, you're going to see your "hello", followed by
An error occurred while loading ./spec/your_spec.rb
Followed by the NameError from referencing cruel_world.

MacRuby xcode label not working

class HelloApp
attr_accessor :label, :text_field, :button
def clickedButton(sender)
# implementation
your_name = self.text_field.stringValue
self.label.stringValue = "Hello, #{your_name}"
end
end
The above code is in HelloApp.rb
The problem is when I type something into the textbox, and click the button, the label just says "Hello, " and not "Hello, namegoeshere"
I am using MacRuby .4 by the way.
Your code works for me in MacRuby .5
Does this log the user input to the console?
your_name = self.text_field.stringValue
puts "Your name is #{your_name}"
If not, you may not have the text_field hooked up to the NSTextField in Interface Builder.

Resources