Testing the calculator in TestComplete

Introduction

The basics of TestComplete can be read here and about Python here

This article uses TestComplete version 15, current as of 2023.

The article that inspired this one is in the Habr sandbox at this link . It was written in 2013 using TestComplete 9. If you are the author of that article or know him, write in the chat I would like to know about your experience with modern applications.

Recording a simple test

Click on New Project, or File → New Project, or press SHIFT + CTRL + N

Select project name, location and type of test to record TestComplete, image from www.andreyolegovich.ru
TestComplete - Project Name

Set the project name and choose the language in which the script will be written. In this example the script will be in Python

TestComplete, image from www.andreyolegovich.ru
TestComplete

Specify the location of the application

TestComplete, image from www.andreyolegovich.ru
TestComplete

calc.exe is located in

C:\Windows\System32\

TestComplete, image from www.andreyolegovich.ru
TestComplete

TestComplete, image from www.andreyolegovich.ru
TestComplete

TestComplete, image from www.andreyolegovich.ru
TestComplete

Add 1 and 1

TestComplete, image from www.andreyolegovich.ru
TestComplete

TestComplete, image from www.andreyolegovich.ru
TestComplete

Convert the test into a script.

Keyword Test Log TestComplete, image from www.andreyolegovich.ru
TestComplete

The script code will be in the file Unit1.py at the address

C:\Users\Andrei\Documents\TestComplete 15 Projects\Calculator\Calculator\Script

Choosing a browser, Run Browser TestComplete, image from www.andreyolegovich.ru
Choose a browser for TestComplete

I recommend choosing Yes

Keyword Test Log TestComplete, image from www.andreyolegovich.ru
TestComplete

Formatting the script

TestComplete does not follow the recommendations for Python formatting in the PEP 8 code, so there are no spaces after # in comments. I add them myself.

The script obtained as a result of the conversion and formatting will look quite original.

def Test1(): # Runs the "calc" tested application. TestedApps.calc.Run() # Clicks the 'One' object. Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.Number_pad.One.Click() # Clicks the 'Plus' object. Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.Standard_operators.Plus.Click() # Clicks the 'One' object. Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.Number_pad.One.Click() # Clicks the 'Equals' object. Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.Standard_operators.Equals.Click()

Keyword Test Log TestComplete, image from www.andreyolegovich.ru
TestComplete

Keyword Test Log TestComplete, image from www.andreyolegovich.ru
TestComplete

Keyword Test Log TestComplete, image from www.andreyolegovich.ru
TestComplete

The name of the object that was selected with Object Spy

Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.UIAObject("Display_is_0").UIAObject("TextContainer").UIAObject("NormalOutput")

You may also get the following name

Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.Display_is_30.TextContainer

Note the object name: the value is hard-coded there (especially in the second case).

This can greatly complicate automation with test data generation.

We are interested in the attribute Text, which can be seen by opening Advanced view

Keyword Test Log TestComplete, image from www.andreyolegovich.ru
TestComplete

We will get the value of this attribute with aqObject.GetPropertyValue()

result = aqObject.GetPropertyValue(Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.UIAObject("Display_is_0").UIAObject("TextContainer").UIAObject("NormalOutput"), "Text") assert int(result) == 2

In its original form the script looks terrible, so it can be rewritten with some changes

def Test1(): # Runs the "calc" tested application. TestedApps.calc.Run() # Defines objects one = Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.Number_pad.One plus = Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.Standard_operators.Plus equals = Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.Standard_operators.Equals output = Aliases.Microsoft_WindowsCalculator.Calculator.NavView.LandmarkTarget.UIAObject("Display_is_0").UIAObject("TextContainer").UIAObject("NormalOutput") # Clicks the 'One' object. one.Click() # Clicks the 'Plus' object. plus.Click() # Clicks the 'One' object. one.Click() # Clicks the 'Equals' object. equals.Click() result = aqObject.GetPropertyValue(output, "Text") assert int(result) == 2

If the test fails, the error warning will depend on the type of check. In our example assert from Python is used, so a Python runtime error will appear.

TestComplete Python runtime error, image from www.andreyolegovich.ru
TestComplete

Running the script

To run the test from a script you need to put the mouse cursor in the required function. This may seem non-obvious at first glance, but if you put the cursor in the wrong place, the test will not start, or some secondary function will start.

Article author: Andrei Olegovich