Terminating each() block in Protractor - node.js

I was automating the an application (using Protractor) and I have come across situation where I wanted to select the an option from the type ahead using the DOWN arrow button from the Keyboard. Here is how I am approaching to this action.
After typing part into the text field I am getting the reference of each option that appear in the type ahead.
Now, I am using .each() method of protractor to iterate through each of the option to look for the required option.
I'm making the script to hit DOWN arrow button to iterate through each option in the type ahead.
Suppose there are 10 options displayed in the type ahead and the option that I need to select is at 5th position. Now when I reach the 5th position I am selecting the option but each() function still continues.
I want the loop to terminate when required option is selected. Something like BREAK statement in FOR loops.
BTW I have tried the above scenario with FOR loop but unable to use BREAK statement within then() handler.
Please let me know how to cope up with this situation.

You could throw an exception to terminate the loop. Put the loop inside try and use catch to wrangle your results. You can also just use a boolean variable to indicate that you have found a match and ignore everything after that point. I would just use a for loop though.
Edit:
You could add a variable to hold an action before the allBenchmarks.each
var action
Then inside the test
if(dataValue == optionToSelect){
action = function() {benchmark.click(); ...}
}
After the loop exits call the action
if (action) action()

Related

How to use Tkcalendar virtual events?

Still I’m confused on that tkcalendar virtual events usage to bind user actions on the calendar days selections. Making something like follow,
calendar.bind("<<CalendarSelected>>", print(calendar.get_date())
That print the default date once the I run the script but when I select on the displayed calendar nothing is printed. Normally any selection on any day on the calendar should be printed if was really bound. I did something wrong right ?
You need to use a lambda function instead
calendar.bind("<<CalendarSelected>>", lambda: print(calendar.get_date())
You code will currently bind the result of the print function as the callback for the bind. Since print returns None, no function will be called.
A lambda creates an anonymous function.
If your code gets more complex than a single line, put this code in to a function of its own and use the name of that function in the bind method, for example
calendar.bind("<<CalendarSelected>>", showDateToUser)

Control close event pyglet

I have a program that has multiple windows in pyglet, and I want to make one window unclosable, rather to set its visibility to false. Is there a way I can access the event handle for close, to make it instead of closing the window, possibly return a bool or a string like "should close."?
Found the answer to my own question, first define a custom event loop with window_event_loop = pyglet.app.EventLoop(), then create a handler for .event for the event loop
#window_event_loop.event
def on_window_close(window):
# Extra code here
return pyglet.event.EVENT_HANDLED

nightwatch - check for popup window, after each click event

Is there a way in nightwatch to check whether a popup window appears after each click event?
I have a problem that randomly an error message appear and I don't want to write for each click event the same callback function.
I have already tried out the after and afterEach commands in the Global.js but then the commands will only run after the whole test suite.
I also have tried it local within a test file, although it also does not cover all single click events, even though the official website writes "... while beforeEach and afterEach are ran before and after each testcase (test step)"?
Solution I'm looking for:
.waitForElementVisible('selector')
.click('selector')
.click('selector')
Solution I have come up with so far:
.waitForElementVisible('selector')
.click('selector', isPresent)
.click('selector', isPresent)
isPresent as a callback function, which does the checking and close the popup window if it appears.
Is there another way to write a function (with or without after and/or forEach), so that it will be called after each click event or after each command. Thus, I don't have to write the isPresent repetitive code?
You can insert something like this in your page object file:
var popupCommand = {
popupCheck:function(){
return this.waitForElementVisible('selector', 5000)
.click('selector', isPresent)
.click('selector', isPresent)
},
module.exports = {
commands:[popupCommand],
elements:{
firstElement: {selector: 'xpath',locateStrategy: 'xpath'},
secondElement: {selector: 'css'},
}
}
Where 'popupCommand' will be the name of your page object file, for example 'Popup'. And also you will have to insert your isPresent callback function here so you can use it.
I did my best to explain you as much as possible what and how to do that :)
you should yse .switchWindow() method.
Why don't you write your own custom command specific for that case, so that way you will avoid repetitive code?

Linux x11 XGrabKeyboard() cause keyboard to be frozen

I am writing a program which need to listen the user keyboard stroks.
I use function XGrabKeyboard() and this is my code:
XGrabKeyboard(pDisplay, DefaultRootWindow(pDisplay), True, GrabModeAsync, GrabModeAsync, CurrentTime);
XEvent event;
while (true)
{
XNextEvent(pDisplay, &event);
switch (event.type)
{
...
}
}
But it causes the keyboard and cursor to be frozen.
I looked up the man page, it only says: "The third parameter specifies a Boolean value that indicates whether the keyboard events are to be reported as usual."
I tried both true or false or the 3rd param, both GrabModeAsync and GrabModeSync for the 4th and 5th param, but it doesn't work.
After calling XGrabKeyboard(), the keyboard is frozen and mouse click doesn't response.
Any ideas?
XGrabKeyboard() (if successful - be sure to check the return value), redirects all key events to your client.
So if your "..." inside the while(true) does not properly handle those key events, or does not ever ungrab (XUngrabKeyboard) or release sync events (XAllowEvents, only applies to GrabModeSync), then the keyboard would appear to lock up.
The boolean parameter is owner_events which indicates whether to report key events always to the window provided to XGrabKeyboard, or report them to the window they normally would have gone to without the grab. Typically you want False (report to the grab window).
For typical uses of XGrabKeyboard (I don't know your use-case) the parameters you would want are:
grab window = some window in your app that relates to the reason for the grab
owner_events=False to send all events to that window
pointer_mode=Async to not screw with the pointer
keyboard_mode=Async to just redirect all key events and avoid need for AllowEvents
time=the timestamp from the event triggering the grab, ideally, or one generated by changing a property and grabbing the timestamp off the PropertyNotify
But, it depends. To give any definitive answer you'd probably need to post a compilable program, I think the bug is likely in the "..." part of your code. Try narrowing your app down to a single-file test case that can be run by others perhaps. Or explain more why you are grabbing and what you're trying to accomplish in the big picture.
I cant help with the XGrabKeyboard function - I havent used it before and dont know how it works - but I can suggest another way of getting the keyboard events.
When creating my window using XCreateWindow, the last argument is a XSetWindowAttributes object. This object has a member event_mask, which you can use to choose which events your window will receive.
I set mine like this:
XSetWindowAttributes setWindAttrs
setWindAttrs.event_mask = ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask;
That will mean you receive events for keyboard key presses and mouse button clicks if you pass this object to XCreateWindow on window creation.
Also another note you can use XPending(pDisplay) to check if there are still events waiting to be handled - so it could replace true in your while(true) line.
Edit: Also your freezing issue could be that you dont return false anywhere in your while loop? It may be stuck in an infinite loop, unless you just removed that bit for the post. Try replacing true with xpending as I suggested above and it may fix the issue, or just returning false after handling the event, but this would only handle one event per frame rather than handling all the currently pending events like XPending would do, and I assume that is what you want to do.

PyQt save to clipboard in a slot

How can I save some text to clipboard by pressing button? clipboard.setText("gg") works by itself
widget.connect(button, QtCore.SIGNAL('clicked()'), clipboard.setText("text") )
throw error, you can only use instance.methodName
widget.connect(button, QtCore.SIGNAL('clicked()'), clipboard, QtCore.SLOT('setText("text")') )
do nothing.
What is wrong?
First, there's a much better way to connect signals to slots on PyQt:
button.clicked.connect(self.method)
You can use lambda functions to pass extra arguments to methods.
Then you call
button1.clicked.connect(lambda : clipboard.setText('btn one'))
button2.clicked.connect(lambda : clipboard.setText('btn two'))
When you pass a function call, in fact the interpreter is evaluating the call and trying to pass the result to the SIGNAL/SLOT connection. That's why your first example doesn't work.
I've written something similar here: https://stackoverflow.com/questions/...from-other-functions

Resources