Displaying 2nd Window Issue - qt

I am dealing with 2 windows . One is created by Qt Designer and i import it
on test.py program . what i did i make a Widget on the test program and
than add a button to it and on click event I try to popup the other
window(gui1.py) created by Qt Designer but it never pop ups and when i use
break and do line by line debugging it shows me this message after running
this command "myapp2 = MyForm()" on line number 35 test.py .
QCoreApplication::exec: The event loop is already running
and once i pressed enter on the terminal it pop up the other window .
I am confuse where i am wrong .
Thanks
test.py
gui1.py

The reason the other window doesn't appear, is because you are not keeping a reference to it, and so it gets garbage-collected immediately after it is shown.
To fix the problem, you could either store the window instance it as an attribute, or give it a parent:
def local_manag(self):
print "pressed"
# store it as an attribute
self.myapp2 = MyForm()
self.myapp2.show()
# or give it a parent
# myapp2 = MyForm(self)
# myapp2.show()

Related

changing the text in a toolbar with pywinauto

I have a file selection window from an windows application I'm trying to automate.
I can change the File name box at the bottom by using
app2.window(title_re='Select a batch file') \
.File_name_Edit.set_text(selected_filename)
I have previously entered the full filepath and filename in to the lower box, however, it will no longer accept slashes in the File name box after a windows update. Hence the problem.
So the problem is the top (directory/folder name) box.
Manually I can select the box, highlight it then overtype the directory.
I cannot find a way to do this using pywinauto.
Methods attempted are:
toolbar2 = app2.window(title_re='Select a batch file') # \
# .child_window(title_re='Address', auto_id='1001')
# .child_window(title_re='Address', control_type="ToolBar", auto_id='1001')
file_path_address = toolbar2['Address band toolbarToolbar'].click_input()
file_path_address.set_text(directory_path)
# ToolbarWrapper(toolbar2).set_text(directory_path)
# ToolbarWrapper(toolbar2).click(button='All locationsSplitButton').set_text(directory_path)
# ToolbarWrapper(toolbar2)['Address:'].set_text(directory_path)
I'm using set_text as the automation must run behind a locked screen, type_keys does not work with a locked screen as type_keys is a keyboard method and Windows blocks keyboard entry when the screen is locked.
I have no access to the internals of the program being automated in order to change the original directory path and need to change it to read in from a different location.
Any assistance would be appreciated.
See below code, It runs fine with Notepad++ on Win 10
import time
from pywinauto.application import Application
app = Application().Connect(title=u'Open', class_name='#32770')
window = app.Dialog
toolbarwindow = window.Toolbar3
toolbarwindow.Click()
time.sleep(0.30)
static = window.Edit2
directory_path = "C:\Users\Desktop"
raw_directory_path = r'{}'.format(directory_path)
static.set_edit_text(text = raw_directory_path)
static.type_keys("{ENTER}")
you can use right click then edit the field
toolbar2['Address band toolbarToolbar'].click_input(button="right")
app2.PopupMenu.child_window(title="Edit address", control_type="MenuItem").click_input()
app2.child_window(title="Address", control_type="Edit").set_edit_text(directory_path)

Check if window has focus with AutoIt

I wonder if it is possible to check to see if a window has focus in AutoIt. I have checked and not found much. I have tried using WinSetOnTop but this didn't seem to do anything so then I considered using WinActivate but this didn't seem to do what I need.
The reason I want to do this is because I have this application I am writing as a prank and I do not want the co-worker on whom I'm playing the prank to just ignore the window when it starts automatically. I am wanting to put a shortcut to it in the startup folder and we have several applications that run on startup and so I want mine to either always be on top or audibly shout rude words at the user if they try and ignore the application.
Is this possible and, if so, can you help me out because I am out of ideas.
Regardless of your motives, you may try WinWaitActive.
Syntax:
WinWaitActive ( "title" [, "text" [, timeout = 0]] )
Example that may be useful to try it out:
Func Example()
; Run Notepad
Run("notepad.exe")
; Wait 10 seconds for the Notepad window to appear.
WinWaitActive("[CLASS:Notepad]", "", 10)
; Wait for 2 seconds to display the Notepad window.
Sleep(2000)
; Close the Notepad window using the classname of Notepad.
WinClose("[CLASS:Notepad]")
EndFunc ;==>Example
Reference:
https://www.autoitscript.com/autoit3/docs/functions/WinWaitActive.htm

Getting error while operating multiple windows in robot framework

I am doing operation on 3 window.Using select window command in robot framework I will travels to 1st child window and in same way on 2nd child window.But after completion of task on 2nd window it will close automatically and then if I used select window for selecting again 1st window it will shows following error
Error:
"NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found"
Code:
#{Window_list} = Get Window Names
Select Window #{Window_list}[1]
#Do some Operations On 1st window
#Launch Second window
#{Window_list} = Get Window Names
Select Window #{Window_list}[2]
#Do some operations on 2nd window
#Save the changes on 2nd window it will close 2nd window automatically
Select Window #{Window_list}[1]...........#fails at this keyword
If anyone has solution on this please mention.Thanks
I do not know exactly the reason, but I've met this case and solved by using Run Keyword And Ignore Error.
In your case:
Run Keyword And Ignore Error Select Window #{Window_list}[1]

How to test drag and drop behavior in PyQt?

I want to unittest drag and drop for our widgets. At the moment, I instantiate a QDragEnterEvent, but this is discouraged with a big warning on the Qt documentation, because it relies on the Qt library internal state. In fact, I get segfaults that appear to be due to a violation of this Warning.
Given this premise, how can one test drag and drop behavior?
If using Unix we can use QTest, however to get a cross-platform solution, we can implement a solution where we circumvent Qt.
Using QTest
Although the Qt documentation for drag and drop says that it will not block the main event loop, a closer look at QDrag.exec will reveal that this is not true for Windows.
The call to QTest.mousePress causes the test to block until the mouse is physically moved by the user.
I got around this in Linux by using a timer to schedule the mouse move and release:
def testDragAndDrop(self):
QtCore.QTimer.singleShot(100, self.dropIt)
QtTest.QTest.mousePress(dragFromWidget, QtCore.Qt.LeftButton)
# check for desired behaviour after drop
assert something
def dropIt(self):
QtTest.QTest.mouseMove(dropToWidget)
QtTest.QTest.mouseRelease(dropToWidget, QtCore.Qt.LeftButton, delay=15)
For this solution, it is necessary to include a delay in the mouseRelease call, and to have called show on your widget.
Note that I have verified this works using pyqt4 and Python 2.7 on Fedora 20
Cross-Platform
You can use the mouse manipulation methods from the PyUserInput package. Put the mouse interaction in separate thread to avoid the locking up of the Qt main event loop. We can do this since we are not using Qt at all in our mouse control. Make sure that you have called show on the widgets you are dragging to/from.
from __future__ import division
import sys, time, threading
import numpy as np
from PyQt4 import QtGui, QtCore, QtTest
from pymouse import PyMouse
...
def mouseDrag(source, dest, rate=1000):
"""Simulate a mouse visible mouse drag from source to dest, rate is pixels/second"""
mouse = PyMouse()
mouse.press(*source)
# smooth move from source to dest
npoints = int(np.sqrt((dest[0]-source[0])**2 + (dest[1]-source[1])**2 ) / (rate/1000))
for i in range(npoints):
x = int(source[0] + ((dest[0]-source[0])/npoints)*i)
y = int(source[1] + ((dest[1]-source[1])/npoints)*i)
mouse.move(x,y)
time.sleep(0.001)
mouse.release(*dest)
def center(widget):
midpoint = QtCore.QPoint(widget.width()/2, widget.height()/2)
return widget.mapToGlobal(midpoint)
def testDragAndDrop(self):
# grab the center of the widgets
fromPos = center(dragFromWidget)
toPos = center(dropToWidget)
dragThread = threading.Thread(target=mouseDrag, args=((fromPos.x(),fromPos.y()), (toPos.x(), toPos.y())))
dragThread.start()
# cannot join, use non-blocking wait
while dragThread.is_alive():
QtTest.QTest.qWait(1000)
# check that the drop had the desired effect
assert dropToWidget.hasItemCount() > 0
Note I have tested this using PyQt4 and Python 2.7 on Fedora and Windows 7
Haven't tried, but if your drag & drop process is Qt internal (meaning, you're dragging from and to a Qt widget), QTest might help.
Basically by doing something along the lines:
QTest.mousePress(drag_widget, Qt.LeftButton) # simulate mouse press on whatever you want to drag
QTest.mouseMove(drop_widget) # move the mouse to the target - maybe this can be skipped
QTest.mouseRelease(drop_widget, Qt.LeftButton) # simulate mouse release where you want to drop
All functions may be supplied with further positional information (e.g. to click a list item within a widget) and with optional delays to emulate a human user.
Not a copy-pasteable answer, but maybe it serves as a starter...

Squish test for clicking exit/minimize/maximize button for QT application

I have an application that is suppose to run in full screen and not allow the user to minimize or exit with the use of the minimize and exit buttons that usually appear on the corner of the window. Essentially the title bar or whatever it is called is not visible.
I'm trying to make a test where squish checks whether the exit/minimize buttons can be pressed or is visible. Is there a way to do this?
When I try to use Spy or record function all I get is the window commands for minimize/exit a window but no clickbutton or a way to verify the visibility of the buttons.
Thanks!
I asked squish tech support and they recommended using the qt window flags such as:
def logWindowFlags(flags):
test.log("Qt::WindowMinimizeButtonHint = %s" % flags.testFlag(Qt.WindowMinimizeButtonHint))
test.log("Qt::WindowMaximizeButtonHint = %s" % flags.testFlag(Qt.WindowMaximizeButtonHint))
test.log("Qt::WindowCloseButtonHint = %s" % flags.testFlag(Qt.WindowCloseButtonHint))
test.log("Qt::WindowOkButtonHint = %s" % flags.testFlag(Qt.WindowOkButtonHint))
def main:
logWindowFlags(widgetObject.windowFlags())

Resources