back
Avatar of Yann Spöri
Author: Yann Spöri
16. April 2019

Writing your own checkers

While testing an application, one of the most important step is to verify that the actual state of the application is correct. For example, when we test a calculator application, then at a certain moment we need to verify that the application has calculated the right result. This means, we need some kind of action (called check) to verify that some kind of GUI component, e.g. a textfield is showing the right piece of information.

The most easiest way to record a check action is to enter the check recording mode, e.g. by clicking on the corresponding item in the toolbar.

Then right click the corresponding component and select the wanted check.

Wait a minute, what should we do, if the wanted check is not in that list? Well, either we use a SUT script in order to verify the component state or we simply implement our own check.

Please note, both ways to verify the component state are similar and both require programming knowledge and - most often - some knowledge about the used GUI technology and/or how the component itself was implemented. Thus it may be a good idea to ask one of the developers for help.

Let's give an example. As shown in the above image, the list of possible checks does include a "text color" check. I will use Jython here, but you can do the same thing with Groovy or Java Script (or any other scripting language that may be added in future).

1. Analyze the GUI component

When we don't know, how exactly the text color is implemented, we need to analyze the properties / methods of the component in order to find a method that returns the wanted information. The easiest way to do so in Jython is the dir and the type command:

com = rc.getComponent("<QF-Test component id>")
print type(com)
print dir(com)

You can get a QF-Test component id for your component by simply recording a click or any other action on the desired component. Instead of the print statements you can also use the rc.logMessage()-method. Then the output will be written into the run-log.

In my example

type(com)

will return that the component is a standard javax.swing.JTextField. So we can have a look at the corresponding documentation to find out that it is possible to implement such a thing via the setForeground() method. This would mean that we can get the corresponding data via the getForeground() method:

com = rc.getComponent("<QF-Test component id>")
print com.getForeground().getRGB()

will return the desired color as integer and bingo, this will print the color as integer. 

2. Step, add a SUT script

Now let's use our knowledge that the getForeground() method will return the desired color as integer and simply wrap this code into a checker script: 

from de.qfs.apps.qftest.extensions.checks import CheckerRegistry, \
    Checker, DefaultCheckType, CheckDataType
from de.qfs.apps.qftest.shared.data.check import StringCheckData
from de.qfs.lib.util import Pair
import jarray

componentClass = "javax.swing.JTextField"
foregroundColorCheckerType = DefaultCheckType("Color", CheckDataType.STRING, "Foreground color")

class ForegroundColorChecker(Checker):
    def getSupportedCheckTypes(self, com, item):
        return jarray.array([foregroundColorCheckerType], DefaultCheckType)

    def getCheckData(self, com, item, checkType):
        if foregroundColorCheckerType.getIdentifier() == checkType.getIdentifier():
            color = com.getForeground().getRGB()
            return StringCheckData(checkType.getIdentifier(), str(color))
        return None

    def getCheckDataAndItem(self, com, item, checkType):
        data = self.getCheckData(com, item, checkType)
        if data == None: return None
        return Pair(data, None)

global foregroundColorChecker

# unregister a maybe already present instance of this checker
try: CheckerRegistry.instance().unregisterChecker(componentClass, foregroundColorChecker)
except: pass

# after unregistering, register a new instance of this checker
foregroundColorChecker = ForegroundColorChecker()
CheckerRegistry.instance().registerChecker(componentClass, foregroundColorChecker)

Now after we have executed this script, the corresponding checker will be available in the checker list  and the corresponding checker can get recorded and replayed.

Remarks

  • This is a basic example and in real life you may want to convert the integer representation of the color into a more readable color representation.
  • You may use your own checker to implement checks for custom components.
New comment
( Will be displayed on the page )
( Will not be displayed on the page )

0 comments