Results 1 to 4 of 4

Thread: Leveraging global namespace

  1. #1
    Sencha User
    Join Date
    Mar 2017
    Posts
    42

    Default Answered: Leveraging global namespace

    WebDriver Scenario
    ST version 2.2.1.83

    I'm trying to create a global namespace object that contains a few separate properties. I then want the ability to call the global object.property when creating new helper/controllers/specs/etc files in my project. This should allow me to access these helper/controllers/specs/etc across my entire automation suite.

    The global namespace & properties:

    Code:
    let lm = {    controllers: {
            
        },
        specs: {
    
    
        }
    };
    This is a controller file that I'd like to contain certain automation functions to test my application.

    Code:
    lm.controllers.promptController = {    test: function () {
            alert('blah');
        }
    };
    I'd like the ability to call the test function in a test case by using the following:

    Code:
    describe("test the prompt controller", function() {
        it("should pass", function() {
            lm.controllers.promptController.test();
        });
    });
    I've include the lm.js file as an additional library at top level of the project which I thought would allow me access to the lm namespace but it is not. I receive the "ReferenceError: lm is not defined at Object.<anonymous> (C:\Users\Shawn\test\test\globalNameSpace\prompt.js:3:9) failure when running the test.

    This is the project.json for this project. Ignore the datepicker & New Scenario 1 data. it's not relevant to this question.
    Code:
    {
        "framework": "jasmine",
        "libs": [
            {
                "disabled": false,
                "path": "/test/lib/lm.js",
                "type": "file"
            }
        ],
        "coverageFilters": [],
        "scenarios": [
            {
                "libs": [
                    {
                        "disabled": false,
                        "path": "/test/datepicker/lib",
                        "type": "directory"
                    }
                ],
                "coverageFilters": [],
                "webdriverContext": "webdriver",
                "sandbox": true,
                "globals": "*",
                "directory": "datepicker",
                "name": "datepicker"
            },
            {
                "libs": [
                    {
                        "disabled": false,
                        "path": "/test/New_Scenario_1/lib",
                        "type": "directory"
                    }
                ],
                "coverageFilters": [],
                "webdriverContext": "webdriver",
                "sandbox": true,
                "globals": "*",
                "directory": "New_Scenario_1",
                "name": "New Scenario 1"
            },
            {
                "libs": [
                    {
                        "disabled": false,
                        "path": "/test/globalNameSpace/lib",
                        "type": "directory"
                    },
                    {
                        "disabled": false,
                        "path": "/test/lib/lm.js",
                        "type": "file"
                    }
                ],
                "coverageFilters": [],
                "webdriverContext": "webdriver",
                "sandbox": true,
                "globals": "*",
                "directory": "globalNameSpace",
                "name": "globalNameSpace"
            }
        ],
        "pageObjects": [
            {
                "name": "locators",
                "url": "http://examples.sencha.com/extjs/6.5.3/examples/kitchensink/frame-index.html#simple-combo"
            }
        ],
        "subjectUrl": "http://examples.sencha.com/extjs/6.5.3/examples/kitchensink/frame-index.html#simple-combo",
        "globals": "*"
    }
    controllers.jpg
    additional libraries.jpg

  2. Try structuring your lib files like this, adding "lm" to the "global" object:

    Code:
    global.lm = {
        controllers: {
            
        },
        specs: {
    
    
        }
    };
    and

    Code:
    global.lm.controllers.promptController = {
        test: function () {
            alert('blah');
        }
    };
    Then you can call these lib file methods as you are already doing in your test.

  3. #2
    Sencha - Sales Team daniel.gallo's Avatar
    Join Date
    Apr 2009
    Location
    Redwood City, CA
    Posts
    297
    Answers
    41

    Default

    Try structuring your lib files like this, adding "lm" to the "global" object:

    Code:
    global.lm = {
        controllers: {
            
        },
        specs: {
    
    
        }
    };
    and

    Code:
    global.lm.controllers.promptController = {
        test: function () {
            alert('blah');
        }
    };
    Then you can call these lib file methods as you are already doing in your test.
    Daniel Gallo
    Senior Solutions Architect
    Sencha Inc.

  4. #3
    Sencha User
    Join Date
    Mar 2017
    Posts
    42

    Default

    Works great. Thanks!

  5. #4
    Sencha User
    Join Date
    Mar 2017
    Posts
    42

    Default

    Some additional info regarding an issue I ran into with how ST sequences your additional library files. I had previously created a new directory to contain my helper/specs/factory files then set that directory as an additional library before adding my lm.js namespace object file. The additional library is added to the project.json file in the order they're added to the project.


    ST was loading all files in in the directory before the lm.js namespace file which was causing a object reference error when trying to add additional properties to the namespace object. I had to delete & recreate my directory AFTER adding the lm.js namespace file so the global variable was loaded before loading all my helper/specs/etc files located in the directory.

    So for any future users that may want to leverage the global namespace pattern just make sure you add your global file as an additional library file before adding your other files. This ensures they're sequenced properly in the project.json file and loaded in the right order.

Similar Threads

  1. HtmlEditor pollutes global namespace
    By ragzovskii in forum Ext 5: Bugs
    Replies: 1
    Last Post: 21 Aug 2014, 9:56 PM
  2. Leveraging ViewModel logic in grid
    By fruel in forum Ext 5: Q&A
    Replies: 5
    Last Post: 5 May 2014, 11:26 AM
  3. Replies: 7
    Last Post: 26 Feb 2014, 7:10 PM
  4. Global Config Namespace
    By intsims in forum Sencha Cmd
    Replies: 2
    Last Post: 5 Apr 2013, 10:12 AM
  5. Replies: 1
    Last Post: 24 Aug 2011, 11:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •