How to make a message box to close the running process? - autoit

When the process is running I need a message box with "Cancel" and "Proceed" buttons:
Cancel = stop script (exit from script).
Proceed = kill the process
to continue the script.

As Stephan said Autoit does not have built-in functionality like that.
So you have three options:
adapt your script to one of the predefined ones, such as MessageBox($MB_OKCANCEL, "", "Click 'Ok' to kill process") or more relevant but with extra button: $MB_CANCELTRYCONTINUE
use GuiCreate() to make your own message box
download and use someone else's code that does what you want, such as: Melba23's ExtMsgBox - those are called UDFs and are generally found on AutoIt forums

MsgBox() has some predefined button combinations (first parameter "Flag"):
0 OK
1 OK and Cancel
2 Abort, Retry, and Ignore
3 Yes, No, and Cancel
4 Yes and No
5 Retry and Cancel
6 Cancel, Try Again, Continue
$x = MsgBox(1, "Proceed?", "Want to continue?")
ConsoleWrite($x & #CRLF)
If $x = 1 Then ConsoleWrite("continuing..." & #CRLF)
If $x = 2 Then ConsoleWrite("exiting..." & #CRLF)
If you don't want to use them, you will have to build your own GUI instead.

Related

How do I get input from the console in Nim?

I'm new to Nim, but it appears that there is no way to get input from the console in a similar way to input() in python or Console.ReadLine() in dotnet.
I want execution of my code to pause, wait for input, then on pressing enter to continue (just like input in other langs).
Oh no never mind found it:
var consoleInput = readLine(stdin);

NetLogo: Can't "stop" forever button from another procedure?

I have simplified my problem below. I want to stop the execution of the forever button "go" when there's no robots, and I want to call this from another procedure ("test" in this case) like so:
to go
test
end
to test
if not any? robots [ stop ]
end
The reason for this is that I want to call stop where the robot dies such that I can send an appropriate user message.
Sadly, you must re-organize your code so that the you call if not any? robots [ stop ] in your go in order for the following to be true:
See the documentation:
A forever button can also be stopped from code. If the forever button
directly calls a procedure, then when that procedure stops, the button
stops. (In a turtle or patch forever button, the button won’t stop
until every turtle or patch stops – a single turtle or patch doesn’t
have the power to stop the whole button.)
Ref:http://ccl.northwestern.edu/netlogo/docs/programming.html#buttons
stop This agent exits immediately from the enclosing procedure, ask,
or ask-like construct (e.g. crt, hatch, sprout). Only the enclosing
procedure or construct stops, not all execution for the agent.
Ref: http://ccl.northwestern.edu/netlogo/docs/dict/stop.html
One alternative hacky solution which I'm tempted to not post may be to do the following where you raise an error in which then stops.
to go
carefully[test][error-message stop]
end
to test
if not any? robots [ error "no more robots!" ]
end

Window not activating/appearing in front

I have a script that opens a webpage, logs in, then opens a program and is supposed to bring the program to the front and make it full screen. It opens the window but it does not always bring it to the front and it won't full screen. Can anyone offer any assistance? Here is my code:
; Closes last dialog if still open
Sleep(5000)
Send("{ENTER}")
Sleep(500)
; Wait for program to open
WinWait("[CLASS: Program example]","", 5)
;Brings Program to front
if WinExists("[CLASS: Program example]") Then
WinActivate("[CLASS: Program example]")
EndIf
Sleep(500)
; Sets program fullscreen
WinSetState("[ACTIVE]", "", #SW_MAXIMIZE)
I added the WinWait to see if that would help, but it has not. The window just stays in the back and never moves. Thanks for any help provided.
Sometimes AutoIt doesn't perform some task because something is happening at the same time that interfere with the command. The best way to ensure the things work is to always check if the task was performed and try again if not. This loop will solve your problem.
;Brings Program to front
While Not WinActive("[CLASS: Program example]")
WinActivate("[CLASS: Program example]")
Sleep(1000) ; Wait one second (or any seconds you want)
WEnd

How to test a confirm dialog with Cucumber?

I am using Ruby on Rails with Cucumber and Capybara.
How would I go about testing a simple confirm command ("Are you sure?")?
Also, where could I find further documentation on this issue?
The selenium driver now supports this
From Capybara you would access it like this:
page.driver.browser.switch_to.alert.accept
or
page.driver.browser.switch_to.alert.dismiss
or
page.driver.browser.switch_to.alert.text
Seems like there's no way to do it in Capybara, unfortunately. But if you're running your tests with the Selenium driver (and probably other drivers that support JavaScript), you can hack it. Just before performing the action that would bring up the confirm dialog, override the confirm method to always return true. That way the dialog will never be displayed, and your tests can continue as if the user had pressed the OK button. If you want to simulate the reverse, simply change it to return false.
page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
I've implemented these two web steps in /features/step_definitions/web_steps.rb:
When /^I confirm popup$/ do
page.driver.browser.switch_to.alert.accept
end
When /^I dismiss popup$/ do
page.driver.browser.switch_to.alert.dismiss
end
If you want to specifically test the message being displayed, here's a particularly hacky way to do so. I don't endorse it as beautiful code, but it gets the job done. You'll need to load http://plugins.jquery.com/node/1386/release, or change it to do cookies natively if you don't want jQuery.
Use this sort of story:
Given I am on the menu page for the current booking
And a confirmation box saying "The menu is £3.50 over budget. Click Ok to confirm anyway, or Cancel if you want to make changes." should pop up
And I want to click "Ok"
When I press "Confirm menu"
Then the confirmation box should have been displayed
And these steps
Given /^a confirmation box saying "([^"]*)" should pop up$/ do |message|
#expected_message = message
end
Given /^I want to click "([^"]*)"$/ do |option|
retval = (option == "Ok") ? "true" : "false"
page.evaluate_script("window.confirm = function (msg) {
$.cookie('confirm_message', msg)
return #{retval}
}")
end
Then /^the confirmation box should have been displayed$/ do
page.evaluate_script("$.cookie('confirm_message')").should_not be_nil
page.evaluate_script("$.cookie('confirm_message')").should eq(#expected_message)
page.evaluate_script("$.cookie('confirm_message', null)")
end
Updating this for current releases of Capybara. Most Capybara drivers today support the modal API. To accept a confirm modal you would do
accept_confirm do # dismiss_confirm if not accepting
click_link 'delete' # whatever action triggers the modal to appear
end
This can be used in Cucumber with something like
When /^(?:|I )press "([^"]*)" and confirm "([^"]*)"$/ do |button, msg|
accept_confirm msg do
click_button(button)
end
end
which will click the named button and then accept a confirm box with text matching msg
The capybara-webkit driver supports this as well.
Scenario: Illustrate an example has dialog confirm with text
#
When I confirm the browser dialog with tile "Are you sure?"
#
=====================================================================
my step definition here:
And(/^I confirm the browser dialog with title "([^"]*)"$/) do |title|
if page.driver.class == Capybara::Selenium::Driver
page.driver.browser.switch_to.alert.text.should eq(title)
page.driver.browser.switch_to.alert.accept
elsif page.driver.class == Capybara::Webkit::Driver
sleep 1 # prevent test from failing by waiting for popup
page.driver.browser.confirm_messages.should eq(title)
page.driver.browser.accept_js_confirms
else
raise "Unsupported driver"
end
end
Prickle adds some handy convenience methods for working with popups in selenium and webkit
This gist has steps to test a JS confirm dialog in Rails 2 and 3 with any Capybara driver.
It's an adaptation of a previous answer, but doesn't need the jQuery Cookie plugin.
Tried the above answers with no luck. In the end this worked for me:
#browser.alert.ok

How do I take keyboard input in AutoIt?

I want to write a script in AutoIt, which can take automatic input from the keyboard, let's say A-Z, without user intervention.
Is this possible?
It is unlikely that your program needs to capture all input from all keys. If you do in fact need that kind of user input AutoIt might not be for you - see the post from the author of AutoIt about keyloggers. If you need to take keyboard input of the hotkey type: doing that in AutoIt is super easy.
HotKeySet("^+{q}", "reactionFunction")
While 1
; A loop
WEnd
Func reactionFunction()
MsgBox(0, "You pressed CTRL+Shift+q", "You pressed CTRL+Shift+q")
Exit
EndFunc
If you want to take user input from an input box that is really easy also.
$data = InputBox("Enter Something", "Enter some data in the field below.")
MsgBox(0, "The String You Entered...", "The string you entered is... " & $data)
More information about HotKeySet and InputBox can be found in the AutoIt.chm help file (it's actually a great reference).
Not sure I understand your question - you want to simulate keypresses without someone actually using the keyboard? If so, that's the send command in AutoIt.
You want to let a real user submit input to the script? That's what the GUI in AutoIt is for.

Resources