Écrire vos propres vérificateurs

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.

QF-Test Tool BarQF-Test Record Check 2

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)

QF-Test Record Check

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.

Nous utilisons des cookies "Matomo" pour l'évaluation anonyme de votre visite à note page web. Pour cela nous avons besoin de votre consentement qui est valable pour douze mois.

Configuration de cookies

Cookies fonctionnels

Nous utilisons des cookies fonctionnels pour garantir la fonctionnalité de base du site web.

Cookies de performance et de statistique

Nous utilisons Matomo pour analyser et améliorer notre site web. Des cookies permettent une collection anonyme des informations qui nous aident à vous offrir un visite clair et facile à utiliser de nos pages web.

Détails des cookies
Description Fournisseur Durée de vie Type But
_pk_id Matomo 13 Mois HTTP Contient un identifiant de visiteur unique et pseudonymisé interne à Matomo pour reconnaître les visiteurs qui reviennent.
_pk_ref Matomo 6 Mois HTTP Utilisé pour suivre à partir de quel site Web l'utilisateur anonymisé est arrivé sur notre site Web.
_pk_ses Matomo 1 Jour HTTP Le cookie de session Matomo est utilisé pour suivre les demandes de page du visiteur pendant la session.
_pk_testcookie Matomo Session HTTP Utilisé pour vérifier si le navigateur du visiteur prend en charge les cookies.
_pk_cvar Matomo 30 Minutes HTTP Stocker temporairement les données relatives à la visite.
_pk_hsr Matomo 30 Minutes HTTP Stocker temporairement les données relatives à la visite.