Entering text into text fields

The major TestComplete workload consists of interacting with the tested application via text-input, button-click, selecting elements from the drop-down list, and so on. Of course, automated checks execution is the basic purpose of our tests; however, in order to carry out any checks whatsoever, one should begin with a number of actions, all of which can take much time to complete.

In this recipe, we will consider two methods to work with the controls elements, using text field as an example and decide when it's better to apply either method.

Getting ready...

Launch the standard Notepad application (C:Windows otepad.exe).

How to do it...

In order to review two methods of text inputting, we will create two functions testEditControl1 and testEditControl2.

  1. Create and launch the first function:
    function testEditControl1()
    {
      var pNotepad = Sys.Process("notepad");
      var wNotepad = pNotepad.Window("Notepad", "*");
      var tEdit = wNotepad.Window("Edit");
      var str = "let's try writing something here[Enter]in two lines";
    
      wNotepad.Activate();
      tEdit.Keys("^a[Del]")
      tEdit.Keys(str);
    }
  2. The second function is as follows:
    function testEditControl2()
    {
      var pNotepad = Sys.Process("notepad");
      var wNotepad = pNotepad.Window("Notepad", "*");
      var tEdit = wNotepad.Window("Edit");
      var enterStr = String.fromCharCode(13) + String.fromCharCode(10);
      var str = "let's try writing something else here" + enterStr + "in two lines again";
      wNotepad.Activate();
      tEdit.wText = "";
      tEdit.wText = str;
    }
  3. After executing these two functions, the difference is clear: the speed of executing the second function is much faster than the first one, though the result is the same in both cases.

How it works...

The first few lines of both of the preceding functions are the same. We simply declare the variables corresponding to the application objects (process, main window, and text field) that are to be used further. After that, we declare a variable of the String type, whose text we will enter to a Notepad input field (note that the values of the same variable are different in these functions. We will talk about this difference later in this recipe).

Further, we will activate the Notepad window and clear the text field, for starters (against any prior possibility of some text therein). Then we will proceed in inputting a new text. The procedure of clearing the field and inputting text is of an interest to us in particular.

In the first instance, we have applied the Keys method, which allows inputting text to any window or a controls element, as does the user.

In the second instance, we assign new values to the wText property directly, as seen in the example, and this method works faster than the Keys method.

This, however, does not mean one should always use the second method. To tell the truth, assigning values directly to a property is not a straightforward method, since users behave otherwise. This is just a method to expedite filling out the window data, and in some cases it is truly justifiable. For example, when working with Internet Explorer, the Keys method works quite slowly (due to Internet Explorer peculiarities, TestComplete has to forcibly decrease the inputting speed, otherwise a number of symbols will be omitted at the point of entry). This is why, when testing applications in Internet Explorer, it is best to apply the properties intrinsically.

There's more...

Another example, when using properties is preferable is when entering large amount of data. If we have many text fields having several text lines inputted into, the process will be a bottleneck. By assigning values directly to the properties, we can significantly bring our scripts execution up to speed.

However, along with its advantages, working directly with properties has some downsides. Specifically, when assigning values to properties directly, some events may misfire (for example, an event-handler that is bound onto inputted text to the text field, thus providing the auto-filling of the text). In simple cases, we will not be able to test such a function.

In sophisticated instances, we will be able to impact application performance and evoke some errors, otherwise not reproducible manually. This is why the thumb rule of applying properties instead of the Keys method is this: use the property-assigning method only in those cases when you are absolutely positive that this will not impact the application workability! It is best to discuss with the programmers, who are responsible for the application interface creation, whether or not it is acceptable to access properties directly.

Another complexity of the two methods lies in the fact they handle specific symbols differently. The Keys method transmutes some specific lines into button-click events. In our example, we are dealing with the [Enter] line, which is getting transmuted to pressing the Enter key at the point of text entry. If we are to try assigning this value directly to the property, the text field will have the [Enter] line appearing. This is why, in order to break over to the new line in the second function, we have to create a specific variable enterStr with the two symbols that correspond with returning the caret in Windows OS. Should we wish for the line to be outputted to the log with the help of the Log.Message method, we would have to replace the sequence with the symbol of (which corresponds to the line-break in JScript). Thus, if we would like to use one and the same string for both of the methods, we may come across some hardships which we would have to resolve additionally by replacing the symbols in the string, which can be grueling at times.

Note

The names of the properties can be different for various controls elements. For instance, in our example, the text of the input field is stored in the property wText; however, for the other types of controls elements (.NET, Java, or simply own-drawn controls elements) this could be the Text, Caption, or any other property.

..................Content has been hidden....................

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