Object-oriented Programming (OOP) is widely used not only in program development, in automated testing as well. With its help, tests become more readable and simple in maintenance and follow up.
In the given recipe, we will consider an example of creating a simple object-oriented framework for testing the Notepad application and writing up a simple test applying the OOP approach. The task of the test is launching the Notepad application, open the existing file, check if the file has opened, and then close the Notepad application.
Perform the following steps before starting:
C:Windows
otepad.exe
).C: estfile.txt
, in which only one string should be written: test string
.In order to create a simple OOP test, we need to perform the following steps:
We will also need two methods to access the windows of the application:
function Notepad() { this.wMain = function() {} this.wOpen = function() {} this.start = function() {} this.close = function() {} this.openFile = function(fileName) {} this.checkContent = function(content) {} }
wMain
and wOpen
methods simply return the main window of the Notepad application and the dialog window Open.this.wMain = function() { return Sys.Process("notepad").Window("Notepad", "*"); } this.wOpen = function() { return Sys.Process("notepad").Window("#32770", "Open"); }
start
and close
methods are also quite simple ones.this.start = function() { TestedApps.notepad.Run(); } this.close = function() { TestedApps.notepad.Terminate(); }
openFile
method gets more complicated and includes several actions: menu selection, file name input, and clicking on the Open button.this.openFile = function(fileName) { this.wMain().Activate(); this.wMain().MainMenu.Click("File|Open..."); var fileEdit = this.wOpen().Window("ComboBoxEx32").Window("ComboBox").Window("Edit"); fileEdit.Keys(fileName) this.wOpen().Window("Button", "*Open").Click(); }
checkContent
method is also very simple, as it compares real value in the text field with the expected one.this.checkContent = function(content) { var editArea = this.wMain().Window("Edit"); aqObject.CompareProperty(editArea.wText, cmpEqual, content); }
function TestNotepad() { var expectedContent = "test string"; var wNotepad = new Notepad(); wNotepad.start(); wNotepad.openFile("c:\testfile.txt") wNotepad.checkContent(expectedContent); wNotepad.close(); }
Peculiarity of the object-oriented approach to test creation consists of encapsulated realization inside the methods so that the tests themselves would be more simplistically written, maintained, and followed up.
Pay attention to all the available methods in our class. If we had recorded the same actions, the code would consist of tens of lines and would be more difficult to understand. In case of the OOP approach, the test consists of just four lines of code, each being understandable effortlessly, since the names of the methods are self-explanatory.
Ideally, working with the tested application in the test should consist of calls of various methods of the class only. This is not always possible, but this should be strived for. Of course, this method has its drawbacks. For example, TestComplete does not support object-oriented possibilities of Jscript language, that's why you won't be able to see the names of the methods in the drop-down auto-filled list, and won't be able to move to the necessary method by keeping the Ctrl key pressed and clicking on the name of the method. However, these drawbacks are well made up for by convenience of handling the tests that have been written in the preceding mentioned manner.
Another gap is that the writing effort increases in case of growing number of GUI controlsto describe. But in return we're gaining an advantage in maintainability.
52.14.134.130