The majority of the controls elements have unchanging headers, texts, and so on (that is, properties by which TestComplete makes a difference). In some cases, these properties are dynamic, that means, they change depending on their conditions. For example, heading of the Notepad application starts with the name of the open file (or the word Untitled for a newly created file). Some URLs contain dynamic parts in parameters (for example, session identifier, which is not liable to change, but to be generated randomly). In such cases we apply symbols of the batch replacement (that is, the wildcards).
In order to handle dynamic caption of the Notepad application we need to do the following:
function testWildcardsUsage() { var notepad; var filename = "C:\myfile.txt"; var notepadCmd = "C:\Windows\notepad.exe"; var ws = Sys.OleObject("WScript.Shell"); ws.Run(notepadCmd); notepad = Sys.Process("notepad").Window("Notepad", "* - Notepad"); Log.Message(notepad.WndCaption); notepad.Close(); ws.Run(notepadCmd + " " + filename); notepad = Sys.Process("notepad").Window("Notepad", "* - Notepad"); Log.Message(notepad.WndCaption); notepad.Close(); }
notepad
variable is successfully initialized and used for closing Notepad); regardless of the fact that the heading of the file is different in either case.In the first block of code, we declared and initialized a variable for further usage. In the two subsequent blocks of code, we will execute similar actions: opening the Notepad application, sending the header of the just-opened window to the log, and then closing the Notepad application.
These two recurring blocks of code are not a good example for programming style, because they contain one and the same code with an insignificant difference. We have used this approach only for the purpose of helping visualize the example, in real projects such an approach is banned from usage and should not be recommended.
There are only minute differences in these two blocks of code. In each case the headings of the Notepad window are different; however, we still have the possibility to use one and the same code to work with the window (this has been highlighted with the bold text). This is achieved by using a batch-replacement symbol *
(the asterisk). In TestComplete, we have two symbols for the batch-replacement:
We use such a form to address the Notepad window:
Window("Notepad", "* - Notepad")
We have notified in TestComplete that the window should pertain to the Notepad class, and the heading of the file should begin with any sequence of symbols and end with the line " - Notepad "
. This is exactly why in both the cases we could leave the script code unchanged in order to proceed working with the window of the application. Batch-replacement symbols can be placed anywhere across the string of symbols (in the beginning, in the middle, or at the end) or they can crop up anywhere inside the string once or multiple times.
Besides, batch-replacement symbols can be usable in TestComplete almost everywhere, not just to cover the heading of the applications windows. For example, we could create description of the Notepad window as follows:
Window("*", "* - Notepad")
This would correspond to the window with any class with the heading that we have taken up so far. We should, however, abide by discretion not to use batch-replacement symbols just anywhere, as too generic makeup of the such strings can lead to conflicting matches and mismatches (when one and the same description of an object matches several real objects in the application).
The preceding example will not work for non-English system locales, because the name of the Notepad window will be different in the application caption. To solve this, we can also use wildcards: Window("Notepad", "* - *")
. Here, we left the class
Window
untouched (as it is the same for all locales), but replaced the Notepad
text in the caption with asterisk to match any word. If the text in the control contains asterisk, you can use double-asterisk to match the character. For instance, a Calculator application has multiplication button. We can address this button like this:
Window("Button", "**")
To know more about how to launch applications refer to the following recipes:
18.222.182.66