Handling alert/dialogue box when you are not sure about their time of occurrence - selenium

Is there a way to handle dialogue box that are displayed due to some background check the system keeps doing.
Ex: I have a async job that is running and on its completion it throws an alert that its done and it blocks the rest of the window until I click ok on it, because of this my rest of the tests starts to fail.
Looking for something like if you don’t find the element you are looking for , for more than 3 secs then check if some alert box is present.

Related

Colab audio alarm or pop up notification to remind the user to click captcha button when popped up, prevent inactivity

From March 2021 google colab has added a captcha that randomly pops up after some time. So it is difficult or not possible to programmatically prevent Google Colab from disconnecting on a timeout. So there's no option other than opening the tab and clicking the captcha button. I often run model which take more training time and sometimes miss going back to it in 90 mins. The captcha can be popped up even while the training is not completed and soon terminates after a few minutes if not clicked.
Are there any solutions so that I can at least get an audio notification when I get a captcha in colab or if not pop-up message in browser or OS so that I can know about the inactivity?
I had found some ways get notified at some target lines in the notebook, say after training is completed. One is the colab browser notification feature, can be checked to true in settings. Other one is an audio solution, you just add these 2 lines of code found here to get an alarm when the code executes at any line in a cell.
What I am looking for is an alarm or pop up message in case of captcha.
This is probably a half-correct answer, but here's my shot at solving this
This article discusses how to bypass Captcha's, and has some code that clicks the check box if a captcha is found. I think it could be modified to have a sound alert instead.
However, Captchas were made to counter these sort of tools, plus I'm not sure about the legality implications etc, so I wouldn't recommend it.
The use of the below js increased the time before google sent a recaptcha for me. Btw, I set this as a bookmark, to call it whenever I need to. Executing the js bit in the console should be equivalent.
javascript:void(setInterval(function(){ console.log("Connect pushed"); document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click(); },60000))

Halt code at dialog (msgbox()) command in IDE

Title quite much says it all.
In VB6, and in VBA/Access it was possible to hit break key, and jump into debug mode when using the msgbox() command.
Is there a way to do this in vb.net (desktop/winforms) applications?
Often, some code will toss up a dialog box, and it is rather nice then to jump into debug mode as a result of that message box having been displayed.
Edit
Ok, hitting pause button in most applications work, but in this application, when I hit pause, then I get this:
Edit two:
Ok, I have discovered the reason for this behavior. I have the application Frame work box un-checked. The reason for this is I did't want to specify the main application form as startup form, and I desired additional control over what occurs if the main startup form (that I don't specify) is closed. Thus, my main application form is launched via application.Run(my form)
It thus seems that due to starting the main form as a new separate application thread (which is the result of using application.Run(), then you can't use ctrl-break, or more common use/hit the pause button in the IDE to halt the code. Hitting pause will thus display that the application is running a main app thread, which indeed is the case since I use applicaiton.Run() to launch the main form from the classic and traditional Sub Main().
Edit 3
A way to fix this, and enable the pause key to work is to un-check in tools->debugging the [ ] Enable Just My Code. This will thus allow debug mode of the other "main" application thread.
Hmm. [CTRL][BREAK] clears the dialog box. However, clicking the pause button in the IDE will do what you want.
Alternatively, select Debug > Break All from the menu.

Detect or Suppress Action errors during script automation

I've run into an issue with the automation of one of my Photoshop scripts where it can be completely halted by an error in a supporting action set. The script does about 99% of the work, but the remaining 1% has to be done in an action due to the work being done (the work is NOT scriptable, or at least not efficient in the least to put to script). The problem I'm running into is that, if there is an error in the layer name in the action, Photoshop pops up an alert about the issue, halting the whole process until I click the "Ok" button on said alert.
Is there any way to catch these errors (a simple try/catch on the action activation code doesn't work), or at least suppress the errors so they don't halt the entire process? Thanks in advance for any help!
Edit: Correction on the error type; it's not an "Ok" alert, it's a "Continue/Stop" alert. The message accompanying it is "The object "layer "layerName"" is not currently available". This happens if the person creating the action failed to rename the layer before performing their work on it and halts the entire automation process until I click continue or stop.
You should convert the action to a script using ActionToJavascript.jsx. After which you can then modify the script to look out for errors - such as you mentioned with the layers not being named correctly.

How can Sikuli be used to wait for a button for a long time, with perhaps some maintenance task in between?

I have a webpage where I am waiting for a button to appear, and when it appears I would like to click it. The button is on a timer and may take as long as an hour to appear. Also, if the button takes longer than a certain length of time to appear, I'd like to move the mouse (otherwise the website will log me out automatically).
So, to wait for a button to appear I devised this Sikuli script:
button = "button.png"
while(1):
if exists(button):
print("found it")
click(button)
break
else:
print("wait longer")
wait(button,30*60)
# do a regular task
print "all done!"
The above does not seem to be functional. If the button is on screen, the script will find it... However, if it has to wait it will simply time out quickly with a FindFailed exception (on the click() even though the button does not exist on screen). I considered writing a handler, but seems like overkill.
What am I doing wrong and what is the best way to wait a long period for a visual event like this?
Some other thoughts for you...
while(1):
wait(Button, 30*60) # This will spinlock for 30 minutes for the button to appear
if exists(Button):
hover(Button) # Debug statement allowing user to see what Sikuli has matched to
click (Button)
else:
mouseMove(Location(50,100))
mouseMove(Location(50,200))
Links:
wait
mouse movement link
Location
Maybe Sikuli recognizes something that looks quite your button, and tries to click it.
If you right click in the IDE your button pattern, you can fine tune the tolerance level for recognition. Try to cut the image exactly around your button and increase the value to be more precise.
I suggest you to read this tutorial
http://doc.sikuli.org/tutorials/surveillance/surveillance.html
and to set up a event handler to manage your button when it appears
http://doc.sikuli.org/region.html#Region.onAppear
http://doc.sikuli.org/region.html#observingvisualeventsinaregion
It is not much code to write.
You can get a nice example with full source code in Sikuli's Blog here
http://sikuli.org/blog/2011/08/15/sikuli-plays-angry-birds-on-google-games/
I think you can just set up your handlers and go with
observe(FOREVER)
If you want sikuli to do stuff while your waiting for an image i would use the onAppear(pic, function) and observe(FOREVER, true) methods this is how it works
event = Sikuli.event
def function(event):
click(yourButton.png)
onAppear(picYourWaitingFor.png, function)
observe(FOREVER, true)
basically what this does is onAppear will continuously scan the screen for picYourWaitingFor.png. sikuli continues execution after words so it's scanning while its working. on the appearance of said pic it will jump to the function you put down as the second parameter of onAppear.
I have this same issue as described. Its not about waiting forever. And Observe won't work either, because that does watch forever. Think about wanting to check for event only for a certain period of time say 60 seconds. If it doesn't occur, move on. This could be happening in a specific series of events. If the image doesn't appear in the 60 seconds, move on to do another series.
wait(image,60)
...will crash after 60 seconds if it doesn't find the image, which isn't what is wanted at all in my case.
So I did something like this:
attempt = 1
count=0
while attempt:
if exists(image):
attempt=0
else:
count=count+1
if count>60:
attempt=0
else:
wait(1)
Probably a better way and doesn't give an exact time, but approach doesn't crash the script.
You could also try: except it.. Should be shorter.

selenium doesn't run when web page is loading

I am running some tests where I have to restart a server by clicking a "restart" button. There is a small text being displayed near to it which will show the status of the server.
Before clicking the "restart" button, the text shows "running" and after clicking it, the whole web page keeps loading and the text shows "restarting". After the page stops loading, the server successfully starts running again and the text shows "running".
I have to write a test case which checks if the text shows "restarting" when the "restart" button is clicked. But unfortunately I am not able to find a way to make the selenium check if the text is showing "restarting" as it is unable to do anything since the webpage is loading at that time.
Can anyone help me out by showing me a way how to make selenium run when the webpage is loading? I am not getting good replies in google search about this. It will be great if someone can help me out.
Thanks
Usually you can drive a browser even the webpage is still loading.
I would suggest to find the element for "restarting" and keeps checking it until 60 secs
In ruby, you do something like
wait = Selenium::WebDriver::Wait.new(:timeout => X)
wait.until {driver.find_element(:id => 'restart_word')}
where X is the number of seconds to wait.
After X seconds, it will timeout.