Testing countdown timer with cypress - testing

I have a web app that in one part of it a timer needs to count 1 minute. Now for doing the end to end testing I am using Cypress, In Cypress documentation I found cy.clock and cy.tick. The documentaion is vague to me. So which of these should I use for testing a countdown timer?

Cypress cy.clock() has a "freeze frame" effect on timers in your app.
The passing of time can be controlled with cy.tick() to move the time frame on.
It makes your test run much quicker than waiting for a timeout to actually happen.
cy.clock(); // take control of timers
cy.get('#timer-btn').click();
cy.tick(30000); // move time frame +30 seconds
cy.get('.btn').should('be.enabled') // button still available
cy.tick(60100); // move time frame +60.1 seconds
cy.get('#timer').should('have.text', 'TIME IS UP, your score is: 0');
cy.get('.btn').should('be.disabled') // button is not available
This only works if the timer is using the setInterval function.

Related

Createjs tweens execute once per window session and never run until page refresh

I'm trying to run code that tweens a stage child at the end of every minigame. I have several minigames with different tweens. The idea is that when you complete some goal with said minigame, a tween will run that plays some kind of animation. Unfortunately, when one tween runs, the rest in any minigame never execute. This also goes for the initial minigame: when one tween from minigame A runs, all the tweens in minigame A don't run. The only way to get tweens working again is to refresh the page, but even then, the problem will still continue.
I have these lines of code running globally in a file called universalDeclar.js that's read before any other js file besides the createjs cdn
createjs.Ticker.timingMode = createjs.Ticker.TIMEOUT;
createjs.Ticker.framerate = 24;
When I instantiate the new game, I give a specific child (for this case, a Sprite named professor) a tween animation before its added to the stage but don't play it until some condition is met in a later function, to which I write stupidTween.paused = false;
stupidTween = createjs.Tween.get(professor, {paused: true}, true)
.to({x: 700 }, 400, createjs.Ease.linear)
After running stage.update() in the initialize function, I always reset the ticker (since I plan on implementing time limits for each minigame) and call on a new function to run every tick
createjs.Ticker.reset();
createjs.Ticker.addEventListener("tick", fillTick);
Any guidance or help on this problem would be really appreciated since a lot of my games rely on tweenable animation
Fixed the problem myself. Apparently calling createjs.Ticker.reset(); prevents any other tweens from reoccurring despite functions being initialized with every new minigame load. I had to create some new variables to manage time differences between games but everything works now

How to stop PiCamera preview on keystroke?

I made a code through python to operate a preview of my PiCamera, I have set the time to 10 seconds, then automatically turns off. However I am unsure how I would be able to have a keystroke to stop the camera and return to the previous screen?
At the moment I am able to view for 10 seconds, and nothing else, the usual ctrl-c and various other keys does not work.
How would I be able to integrate a keystroke into the code following to stop the script and return to normal screen?
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(10)
camera.stop_preview()
Subprocess module you can check on the official page:
https://docs.python.org/2/library/subprocess.html#subprocess.Popen
A possible way to implement with subprocess.Popen is here on SO:
Controlling a python script from another script
Another possibility is to use multiprocesses or multithreading module. For instance creation of a thread can be done and you can take care of an ID :-)
All the possibility will lead you to learn a bit more of python!
My better suggestion will be to create easily a thread (https://docs.python.org/3/library/threading.html --> here for python 3), get the ID and leave it run.
If you want to terminate the camera running, then terminate the thread :-)

UI Automation Error: UI TESTING FAILURE-APP failed to quiesce within 30.0s

Since xCode updated i'm having trouble running any ui test case. It gives me this error when its expected to do a simple tapping action for example:
XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *passwordSecureTextField = app.secureTextFields[#"Password"];
[passwordSecureTextField tap];
Anyone have any ideas why am i getting this error? I've searched on google and here but haven't found any solutions.
Thank you.
Make sure you don't have any animations on screen during UI Automation tests. We had a text alert flashing on the login screen for debug/test builds of our app, and it would cause the "failed to quiesce" error until it was removed.
There are some other posts about this error that mention issues with UIRefreshControl, so I would suspect animating that or UIActivityIndicatorView would cause the same problem.
It might help to turn on the "All Exceptions" breakpoint. I used it and I recall getting the same error. It will break at the line with the problematic code and should show you the stack trace of the error with more info.
I had a similar error - as well as the simulator running very slowly. In my case it was fixed very simply by the method given in the accepted answer here: Xcode simulator extremely slow.
To save you a click: The issue was that I had accidentally pressed Cmd + T at some point, enabling "Slow animations".
I had to turn off the "Personal Hotspot" in order to get a working test environment (Because the blue bar in the top apparently disturbed XCTestRunner)
But as some tests need internet connection I can't do testing when being in the wild:-(
Anthony F's answer says it all. Likely something is still animating. The system seems to wait for the app UI to "settle" (go idle) and when that happens, it performs the tap action. However, when the UI constantly runs animations, it will never settle.
Xcode Console Output
Enable the console output in Xcode to see what happens when running the test.
Below is an example of the log, when it works well. The system waits for the app to go idle and when that has happened, it goes to find the button in the hierarchy.
t = 16.95s Tap "#go" Button
t = 16.95s Wait for app to idle
t = 17.00s Find the "#go" Button
t = 17.00s Snapshot accessibility hierarchy for XXX
t = 17.09s Find: Descendants matching type Button
t = 17.09s Find: Elements matching predicate '"#go" IN identifiers'
t = 17.10s Wait for app to idle
t = 17.15s Synthesize event
t = 17.41s Wait for app to idle
Below is an example when it fails. The system waits for the app to settle so that it can look for the button in the hierarchy. Since, it does not settle, it waits "forever" finally running into the timeout.
t = 18.88s Set device orientation to Unknown
t = 18.93s Tap "#go" Button
t = 18.93s Wait for app to idle
t = 79.00s Assertion Failure: UI Testing Failure - App failed to quiesce within 60s
In my case the "failed to quiesce" was caused, because at time t=18.90s, demo data was generated which caused repeated updates of a UIProgressView. From then on the app UI never settled ("quiesced").
("quiesce" as a word is not in my active vocabulary, which certainly has delayed my recognition of what was going on. The console output pushed me into the right direction. I'd bet "idle" rings more bells than "quiesce" for many developers.)

How Do I Pause a System.Timer in Visual Basic?

I am using a system.timer in a Windows Service to run a process that usually exceeds the timer's interval. I am trying to keep the timer from firing the same code more than once, a known issue with system.timers.
What I want: The timer runs my code, but the timer "pauses" to wait until the code is completed before resuming ticks.
I have two problems:
The way system.timers work is that the timer will create a race condition on you by launching new redundant threads of the same code and pile them up on you if the has not completed by the time the timer's interval has elapsed.
I would start/stop the timer to keep this from happening, but with a System.Timers.Timer, once you stop the timer for the processing to complete, it never comes back - I have never been able to restart a timer once it has been stopped, it has been destroyed and likely collected. Enabling/disabling is the same exact thing as start/stop with same results.
How on earth do you keep a system.timer from launching new redundant threads of the same code if the process has not completed by the time the timer's interval has lapsed? Obviously, starting/stopping (enabling/disabling) the timer is NOT a solution, as it doesn't work.
Help!
Start your timer when it needs to start, kick off another thread to do the work after which the timer can be stopped. The timer won't care if the thread completed or ran away with the prize money. Use Task Parallel Library (TPL) for the most effective usage.
The Start and Stop methods on the Timer do actually work in a Windows service.
I have multiple production services which use code that do that, except my code is written in C#.
However, make sure you are using the System.Timers.Timer and not the Windows.Forms.Timer
Here's a quick example of C# / pseudocode of what my services look like.
// this is the OnStart() event which fires when windows svc is started
private void OnStart()
{
// start your timer here.
MainTimer.Start();
}
private void ElapsedEventHandler()
{
try
{
// Stop the timer, first thing so the problem of another timer
// entering this code does not occur
MainTimer.Stop();
//Do work here...
}
catch (Exception ex)
{
// if you need to handle any exceptions - write to log etc.
}
finally
{
MainTimer.Start();
// finally clause always runs and will insure
// your timer is always restarted.
}
}

How do I pause a function in c or objective-c on mac without using sleep()

Hi I would like to pause the execution of a function in an cocoa project. I dont want to use sleep() because the function needs to resume after user interaction. I also want to avoid doing this with multiple calls to sleep.
Thanks for your responses. Ok I started the code while waiting for some answers. I then realized that sleep or pause would not be usefull to me because it freeses my whole program. I think I might have to do some threading. Here is the situation:
I have a program that uses coreplot. I also use it to debug and develop algorithms so I do lots of plots while the data is being processed (ie in the midfle of the code but I need the flexibility to put it anywhaere so I cant separate my function). I was able to do this with NSRunAlertPanel but having a message box like that doesnt make it very presentable and I cant do much with the main window while an alert is open.
I hope I am not too confusing with my explanation but if I am ill try to one line it here:
I would like to interact with my cocoa interface while one of my functions is stopped in the middle of what it is doing.
Its sounds to me like you're looking for -NSRunLoop runUntilDate:
Apple's Docs: runUntilDate
This code will cause the execution within your method to pause but still let other events like timers and user input occur:
while ( functionShouldPause )
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
Switching functionShouldPause back to false will allow the rest of the method to execute.
It seems more like you are interested in reacting to user events rather than "pausing" the function. You would probably want to put the code that you want to execute into another function that is called as a result of the user's actions.
In C you can use the pause() function in <unistd.h>. This causes the calling program to suspend until it receives a signal, at which point the pause call will return and your program will continue (or call a signal handler; depending on what signal was received).
So it sounds like you want to break the function into two parts; the bit that happens before the sleep and the bit that happens afterward. Before going to sleep, register for a notification that calls the "after" code, and can be triggered by the UI (by an IBAction connected to whatever UI element). Now instead of calling sleep(), run the run loop for the period you want to go to sleep for, then after that has returned post the "after" notification. In the "after" code, remove the object as an observer for that notification. Now, whichever happens first - the time runs out or the user interrupts you - you get to run the "after" code.
Isn't there a clock or timer function? When your button is pressed start running a loop like timeTillAction = 10 and do a loop of timeTillAction = timeTillAction - 1 until it reaches 0 then run whatever code after the 10 seconds.
Sorry if this isn't well explained.