Quick way to wait/sleep a program until a key is pressed - ruby

I am writing a code where it is automated after the user interacts with the site, I currently have it on a 30 second timer (sleep(30)) where the user can interact for 30 seconds and the the automated code happens. Is there a quick ruby gem that I can use to delay / sleep the code until say the "r" key is pressed by the user.

The gets method will halt execution until the user enters input:
loop do
if gets.chomp == "r"
# ... do what you need when the key is pressed
break
end
end
The user will have to press return to submit their input though. If you don't want to require the return button to be pressed, you can use something like STDIN.getch as described here: https://stackoverflow.com/a/34594382/2981429

Related

Ruby Cucumber: Pausing the Suite for demos and debugging, with the ability to resume

As a software tester, I want to be able to pause test execution with cucumber at any stage during test execution, in order to debug failing tests and show features to clients during demos in more detail.
Scenario: Pause suite mid demo, then resume
Given I am in a meeting to show off a new feature our team has developed
And a client asks us a question about the page we are currently on
When I press the 'p' key in the terminal
Then I should have enough time to answer the question in detail to the client
And I should be able to resume test execution by pressing the 'enter' key in the terminal
Scenario: Pause suite at a known point for debugging
Given there is a failed test
When I add the "pause" method to the step definition
Then the test should pause execution until pressing the 'enter' key in the terminal
You can try to use AfterStep hook
AfterStep('#pauseable') do
answer = 'n'
begin
Timeout.timeout 1 do # wait for a second for user to press 'p'
answer = STDIN.getch
end
rescue Timeout::Error # answer is 'n' when no key is pressed during 1 second
end
STDIN.getch if answer == 'p' # wait until user presses any key
end
Now you can tag your feature as #pauseable. Run it and press p to pause it. Then press any key to continue
The two functions here work on Windows, and so should work on most other operating systems.
# Checks for a pause command in the command line and if it has been pressed, will pause
def paused?
if STDIN.ready?
last_input = STDIN.gets
while last_input == 'p'
sleep 1
end
end
end
# Pauses for debugging, will continue when enter is pressed
def pause
print 'p'
last_input = STDIN.gets
while last_input == 'p'
sleep 1
end
end
The first, when placed once at the start of each step definition, gives the ability to pause straight after a step is completed by pressing 'p' in the terminal.
Alternatively, creating an abstraction library may produce better results, as the paused? method can be used throughout this to pause at other points during execution than the beginning of a new step. This will resume when any standard key other than 'p' is pressed, but I would recommend a new line.
The second, will simply pause the test until any key is pressed, at which point, execution can resume - ideal for debugging.
Happy Pausing!

Can't quit from AppleScript application

I have an endless loop AppleScript application started at login. But there is a problem: I can not quit it unless im using SIGKILL. Is there any way to add some quit handler to it? Or there is better approach to make background process in AppleScript then "repeat - delay - end repeat"?
It doesn't sound like you're using an on idle handler, that's what you want to do.
on run
-- prep code goes here
end run
on idle
-- your code here
display dialog "TEST" giving up after 4
return 10
end idle
The above code will repeat itself every 10 seconds (based on the return value of 10, change as needed). The only other thing to keep in mind is this script needs to be saved as "Stay Open".
Hope this helps

Watir webdriver - click an element and not wait for load

I have 2 sides in my page, clicking one of the buttons on the left side, refreshes the right side.
Now I want to see whether the site gets stuck if i click the buttons too fast while not letting the right side to fully load.
Right now watir waits for the click command to return, so the test doesnt do what it should:
arr = ["div1", "div2", "div3"]
for i in 1..20
print "#{i}\r"
choise = arr.sample
b.div(:id=>choise).click
end
Any way to make it send the command and return without any delays?
Are you getting the same result in multiple browsers?
The spec (which may or may not be implemented or implemented the same way by the different browsers), says that webdriver prevents other commands from being executed while there are outstanding network requests. Though, it also says it should wait for document.readyState to be present for the frame currently handling the commands, so it is unclear if the outstanding network request is supposed to apply to only the current frame or to all frames.
But since Webdriver is designed for commands to be handled in a synchronous manner, it is likely just not designed to do what you are trying to do.
Not sure but you can try with javascripts:
#browser.execute_script("document.getElementById('choise').click")
b.div(:id => 'choise').exist? - to check exist it or aren't
b.div(:id => 'choise').click
I think titusfortner is right. But look this answer.
begin
Timeout::timeout(10) do
# perform actions that may hang here
end
rescue Timeout::Error => msg
put "Recovered from Timeout"
end

Press any key in 5 seconds

To clarify, I am using ComputerCraft (emulator: http://gravlann.github.io/, language: Lua)
I know that to wait for a keypress
os.pullEvent("key")
and to wait 5 seconds I need to use this
sleep(5)
but I want to wait for a keypress and disable the event waiter after 5 seconds.
I'm not familiar with ComputerCraft API, but I guess, You could use parallel API for this. Basically, it allows executing two or more functions in parallel.
To be specific - parallel.waitForAny. Which returns after any of function finished, so, only the one being executed. In contrary, parallel.waitForAll waits for all functions being executed.
I'd use something like this:
local action_done = 0
local function wait_for_keypress()
local event, key_code = os.pullEvent("key")
--do something according to separate key codes? :}
end
local function wait_some_time()
sleep(5)
end
action_done = parallel.waitForAny(wait_for_keypress, wait_some_time)
--action done now contains the number of function which was finished first.
EDIT: if using only ComputerCraft API, You should change to this (using timer event):
local function wait_some_time()
os.startTimer(5)
end

Stop loading page watir-webdriver

Is there a way to stop loading a page with watir-webdriver on firefox? Or is there a way to force something in my script even if the page is still loading? At a point in my script, the website will hang and the script eventually timeouts.
You can use the Timeout class to force it to give up after waiting a reasonable amount of time (and this is internally how Watir performs it's waits as well)
begin
Timeout::timeout(10) do
# perform actions that may hang here
end
rescue Timeout::Error => msg
put "Recovered from Timeout"
end
If the website hangs, you should be able to use a wait method to timeout the script if an element does not appear. These are mainly used as an answer to AJAX, but they should work for this condition as well. For example, if the script hangs after you click a link, and you expect the next page to have a specific title or text:
#browser.link(:name => "Let's Hang!").click
Watir::Wait.until(30) { #browser.title == "new page" }
or
Watir::Wait.until(30) { #browser.text.include? ("confirmation text") }
or
#browser.image(:src => "awesome.jpg").wait_until_present(30)
Each of these will wait 30 seconds for the condition to be met before exiting with an error. You can change the time (30) to exit within your app's hang window.
please have a look at the Selenium issue http://code.google.com/p/selenium/issues/detail?id=687 it's not fixed yet, watir-webdriver is also based on Selenium, hope it answers your question.

Resources