Squish/Clicking a QLabel Link: Difference between revisions
AutoSpider (talk | contribs) (Move [[Category::Tools::Squish]] -> [[Category::Squish]]) |
No edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 4: | Line 4: | ||
As is commonly known, you can put in hyper text links in a regular QLabel in Qt, by using simple HTML syntax. Then of course, you want to be able to click this link in your Squish tests. However, Squish does not have a clickLink() function, as it does for e.g. clickButton() or clickTab(), so what happens during recording is that you will get code similar to: | As is commonly known, you can put in hyper text links in a regular QLabel in Qt, by using simple HTML syntax. Then of course, you want to be able to click this link in your Squish tests. However, Squish does not have a clickLink() function, as it does for e.g. clickButton() or clickTab(), so what happens during recording is that you will get code similar to: | ||
<code>mouseClick(waitForObject(":MyQLabel"), 31, 14, 0, Qt.LeftButton)<code> | <code>mouseClick(waitForObject(":MyQLabel"), 31, 14, 0, Qt.LeftButton)</code> | ||
That is, hard coded coordinates for where the link is. So if the position of the link would change for whatever reason (different platform, new text added, different font) the test will break. | That is, hard coded coordinates for where the link is. So if the position of the link would change for whatever reason (different platform, new text added, different font) the test will break. | ||
Line 10: | Line 10: | ||
In Squish 4.1, a new function named installSignalHandler() was added. This lets your test script react to signals emitted from the application code. We can use this function combined with the fact that a signal is emitted from a QLabel whenever a link is hovered. This will let us implement a function for clicking arbitrary links in a label. | In Squish 4.1, a new function named installSignalHandler() was added. This lets your test script react to signals emitted from the application code. We can use this function combined with the fact that a signal is emitted from a QLabel whenever a link is hovered. This will let us implement a function for clicking arbitrary links in a label. | ||
< | <code> | ||
_lookingFor = None | |||
_found = False | _found = False | ||
def handleLinkHovered(obj, link): | def handleLinkHovered(obj, link): | ||
global _found | |||
if link == _lookingFor: | |||
_found = True | |||
def wasFound( obj ): | |||
global _found | |||
if _found: | |||
uninstallSignalHandler( obj, "linkHovered(QString)", "handleLinkHovered" ) | |||
return True | |||
return False | |||
def findLink(objectName, link): | def findLink(objectName, link): | ||
global _lookingFor | |||
global _found | |||
_lookingFor = link | |||
_found = False | |||
return (–1,–1 | obj = waitForObject(objectName) | ||
installSignalHandler(obj, "linkHovered(QString)", "handleLinkHovered") | |||
mouseMove( obj.x, obj.y ) | |||
width = obj.x + obj.width | |||
height = obj.y + obj.height | |||
y = obj.y | |||
while y < height: | |||
x = obj.x | |||
while x < width: | |||
mouseMove( x, y ) | |||
if wasFound( obj ): | |||
return [x-obj.x, y-obj.y] | |||
x += 5 | |||
y += 10 | |||
if not wasFound( obj ): | |||
uninstallSignalHandler(obj, "linkHovered(QString)", "handleLinkHovered") | |||
return [–1, –1] | |||
def clickLink(objectName, link): | def clickLink(objectName, link): | ||
point = findLink(objectName, link) | |||
if point[0] != -1 and point[1] != -1: | |||
mouseClick(objectName, point[0], point[1], 0, Qt.LeftButton) | |||
</code> | |||
Usage is simply: | Usage is simply: | ||
< | <code>clickLink(":MyQLabel", "some-action")</code> |
Latest revision as of 14:14, 6 November 2020
Clicking link in label
As is commonly known, you can put in hyper text links in a regular QLabel in Qt, by using simple HTML syntax. Then of course, you want to be able to click this link in your Squish tests. However, Squish does not have a clickLink() function, as it does for e.g. clickButton() or clickTab(), so what happens during recording is that you will get code similar to:
mouseClick(waitForObject(":MyQLabel"), 31, 14, 0, Qt.LeftButton)
That is, hard coded coordinates for where the link is. So if the position of the link would change for whatever reason (different platform, new text added, different font) the test will break.
In Squish 4.1, a new function named installSignalHandler() was added. This lets your test script react to signals emitted from the application code. We can use this function combined with the fact that a signal is emitted from a QLabel whenever a link is hovered. This will let us implement a function for clicking arbitrary links in a label.
_lookingFor = None
_found = False
def handleLinkHovered(obj, link):
global _found
if link == _lookingFor:
_found = True
def wasFound( obj ):
global _found
if _found:
uninstallSignalHandler( obj, "linkHovered(QString)", "handleLinkHovered" )
return True
return False
def findLink(objectName, link):
global _lookingFor
global _found
_lookingFor = link
_found = False
obj = waitForObject(objectName)
installSignalHandler(obj, "linkHovered(QString)", "handleLinkHovered")
mouseMove( obj.x, obj.y )
width = obj.x + obj.width
height = obj.y + obj.height
y = obj.y
while y < height:
x = obj.x
while x < width:
mouseMove( x, y )
if wasFound( obj ):
return [x-obj.x, y-obj.y]
x += 5
y += 10
if not wasFound( obj ):
uninstallSignalHandler(obj, "linkHovered(QString)", "handleLinkHovered")
return [–1, –1]
def clickLink(objectName, link):
point = findLink(objectName, link)
if point[0] != -1 and point[1] != -1:
mouseClick(objectName, point[0], point[1], 0, Qt.LeftButton)
Usage is simply:
clickLink(":MyQLabel", "some-action")