Sunday, February 16, 2014

How to run some code before each test?



Let’s suppose you need to run some general “code” (a few commands), like a login process or a button click before every test in siesta. You want that each and every test in your test suite will be executed from where the “general code” stopped.

For example, assume that there are two types of tests suites – the first test suite is running on a specific window, and the other is running from a different window. The windows are two different windows entirely, and each of them can be reached from a “main menu” which has two buttons – one button for each window. This means that you need a click on button “x” to be performed before the first type of tests, and a click on button “y” to be performed before the other type of tests. 

In siesta there isn’t yet a solution for this kind of problems, but we can overcome them by adding a new test class (extending the test class) and by overriding the isReady function, so before each test will start to run the code in the new test class will be executed. 

Setting the environment for a new test class

Now it’s time to configure the environment to use a new test class. There are several phases to do this:

1       1. You need to add the test class name to the harness.configure function in index.js file:

Harness.configure({
    title       : ‘This is my test Suite’,
    testClass  : Siesta.Test.Whatever,
});

NOTE: the new test class MUST extend the siesta test class, so its name should be inside the siesta name space. For example, names like “This.Test.Class” will not work, but names like “Siesta.Test.Whatever” will.

         2. You need to create a file which will contain the new test class, and locate it in the webapp folder. Since all the paths are relative to the index.js file, you can create it in the same folder as index.js located in.

3       3. Add this line (which is the new file you created) to the index.html file, right after the “siesta-all.js”  file and under “script” tags:

<script type="text/javascript" src="Pre-Code.js"></script>

          4. In the new class file, you need to override the “isReady” function. You can read more about the function here: http://www.bryntum.com/docs/siesta/#!/api/Siesta.Test-method-isReady

There are a few important things to note when using siesta to test ExtJS and when wanting to use Ext commands or siesta commands (like Ext.ComponentQuery or t.click):

  • The “isa” value should be Siesta.Test.ExtJS since we are testing ExtJS application.
  • When using Ext commands, instead of the usual “Ext.ComponentQuery” you should use “this.getExt().ComponentQuery”.
  • When using variables that are defined in the preload files, you should add “this.global” before their names.
  • When using siesta methods (like click or chain), you should add “this” before the method instead on the usual “t”.
  • After you finish writing the code you want to do before each test, don’t forget to set the value of “isCustomSetupDone” to true – this will start the test itself.

No comments:

Post a Comment