2

I just started to use Bryntum Siesta and ExtJs for Automated testing. My first, very simple test work like i expected. Now i came across a problem with DRY-Code.

I am trying to write DRY Code for a Login Test.

There are actually two cases here: - a Login with valid data - a login with non-valid data (wrong password AND/OR Username)

In java it would work with global variables/functions. And i thought, that i've done something like javascript global variables before. But it doesn't work.

So i have my tryout function:

    var testFunction = function(){
    console.log("I am a global function, and i am working!");
};

and my goal is to call it in my t.chain() in my Testfile:

{
            type: LoginUsername, 
            target: ">>#loginviewIdemId #loginForm #pwTriggerForm #fldName",
            desc: 'Type in Username',
        },

        function(next) {
            testFunction();
            next();
        },

When i call a function that was created in this testfile above the chain, it does work. I am sorry if it some basic javascript question, but im getting devastated :D

Thanks in advance!

adamswebspace
  • 43
  • 1
  • 8

1 Answers1

3

You can extend your Test class to add utility methods and avoid DRY violations. This guide post sums it up: https://www.bryntum.com/docs/siesta/#!/guide/extending_test_class

Let's create 2 special assertions, which will be checking the odd parity of a passed number. Usually, an assertion needs to check its statement and report the result with either {@link Siesta.Test#pass} or {@link Siesta.Test#fail} methods.

Class('MyProject.MyTestClass', {
    isa     : Siesta.Test.ExtJS,

    methods : {

        isOdd : function (number, description) {
            if (number % 2) {
                this.pass(description);
            } else {
                this.fail(description, {
                    assertionName   : 'isOdd',
                    got             : number,
                    annotation      : 'Need odd number'
                });
            }
        },

        isEven : function (number, description) {
            if (!(number % 2)) {
                this.pass(description);
            } else {
                this.fail(description, {
                    assertionName   : 'isEven',
                    got             : number,
                    annotation      : 'Need even number'
                });
            }
        }
    }
})

When failing, try to provide as much information about the failure as possible and format the failure message in a readable form. Please refer to {@link Siesta.Test#fail} method documentation for additional options.

To make the Harness use your new test class you have to specify the test class to use by setting the {@link Siesta.Harness#testClass} configuration option:

harness.configure({
    title       : 'Awesome Test Suite',

    testClass   : MyProject.MyTestClass,

    preload     : [
        ...
    ]
})

The test class should be loaded right after the siesta-all.js file:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="__path_to_siesta__/resources/css/siesta-all.css">
        <script type="text/javascript" src="__path_to_siesta__/siesta-all.js"></script>

        <!-- The file with new test class -->
        <script type="text/javascript" src="lib/MyTestClass.js"></script>

        <script type="text/javascript" src="index.js"></script>
    </head>

    <body>
    </body>
</html>

Now you can use your custom assertion or utility methods in all your tests:

describe('My test', function(t) {
    var nbr = 1;

    t.isEven(nbr); // Will fail
})
Mats Bryntse
  • 564
  • 3
  • 12
  • Hey @mats i ran into a new problem. – adamswebspace Oct 18 '17 at 08:03
  • I used the way you showed and made a test class. With my first Test Function (which was a Test Function which takes a value and prints it out with a console.log. If i add another method to the class and use it just as you did with the isEven() function, i get an error. The error Message says that: isOdd is not a function. This evens happens when i use the Function from the docs. You know what is wrong there? – adamswebspace Oct 18 '17 at 08:05
  • ah it worked now. i needed to reload the page. sorry to bother! thank you! you helped me a lot – adamswebspace Oct 18 '17 at 09:36