Using wildcards to process objects with variable names

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).

Getting ready...

Create the file myfile.txt in the root directory of the disk C: with any contents.

How to do it...

In order to handle dynamic caption of the Notepad application we need to do the following:

  1. Create and call the function that would open the Notepad application in dual modes: in the mode of a new file and in the mode of opening existing file, as shown in the following example:
    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();
    }
  2. While the function is executing, Notepad will be opened twice and successfully recognized by TestComplete in both cases (the 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.

How it works...

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.

Tip

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:

  • * (asterisk): This corresponds to any sequence of symbols (stands for several symbols, one symbol, or a lack of any symbol)
  • ? (question mark): This stands for any singular symbol

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).

There's more…

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", "**")

See also

To know more about how to launch applications refer to the following recipes:

  • The Running external programs and DOS commands recipe in Chapter 2, Working with Tested Applications
  • The Posting messages to the log recipe in Chapter 6, Logging Capabilities
  • The Organizing script code in the project recipe
  • The Finding objects by properties' values recipe in Chapter 5, Accessing Windows, Controls, and Properties
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.66