Creating framework using the OOP approach

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.

Getting ready

Perform the following steps before starting:

  1. Add the TestedApps element to your project (right-click on the project name, and select Add | New Item).
  2. Add the standard Windows Notepad application to the TestedApps element (right-click on the TestedApps element, then select Add | New Item, and enter this path C:Windows otepad.exe).
  3. Create a file with the name of C: estfile.txt, in which only one string should be written: test string.

How to do it...

In order to create a simple OOP test, we need to perform the following steps:

  1. First of all, it is necessary to think through the structure of the class for our application. We will need four methods for working with the Notepad application:
    • start: This method is used to launch the Notepad application
    • close: This method is used to close the Notepad application
    • openFile: This method is used to open the file
    • checkContent: This method is used to check the text in the Notepad application

    We will also need two methods to access the windows of the application:

    • wMain: This method is used to access the main Notepad window
    • wOpen: This method is used to access the Open window
  2. The template of our class will look as follows:
    function Notepad()
    {
      this.wMain = function()  {}
      this.wOpen = function()  {}
    
      this.start = function()  {}
      this.close = function()  {}
      this.openFile = function(fileName)  {}
      this.checkContent = function(content)  {}
    }
  3. Now, we will consider each method separately. We have specifically implemented them to the simplest effect ever, so that there's no need to dwell on the particulars of the realization of the methods by and large.
  4. The 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");
    }
  5. The start and close methods are also quite simple ones.
    this.start = function()
    {
      TestedApps.notepad.Run();
    }
    this.close = function()
    {
      TestedApps.notepad.Terminate();
    }
  6. The 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();
    }
  7. And, finally, the 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);
    }
  8. Now, we have a simple class to work with Notepad, and we can get down to writing up the test. The test will consist of just four steps: open the Notepad application, open a file within Notepad by a specified filename, and check the contents of the opened file, after which Notepad will be closed. The test will look as follows:
    function TestNotepad()
    {
      var expectedContent = "test string";
      var wNotepad = new Notepad();
      wNotepad.start();
      wNotepad.openFile("c:\testfile.txt")
      wNotepad.checkContent(expectedContent);
      wNotepad.close();
    }

How it works...

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.

There's more...

JScript and VBScript languages support object-oriented programming. The DelphiScript language is short of such possibilities. This is why, if you are set upon usage of OOP approach together with the DelphiScript language, please use the ODT (Object-Driven Testing) object extended by TestComplete.

See also

  • The Organizing script code in the project recipe
..................Content has been hidden....................

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