Adding global variables via a require Common Function file

python change global variable in function
python global variables across files
node js global variable example
python create global variable in function
global variables python 3
nodejs global variable across files
python global variables in class
python multiple global variables

I am doing a POC on switching to Protractor and Jasmine to perform our automated scripting. I'm trying to build a preliminary framework, but I'm having issues trying to translate my concept to reality.

I've set up three files: conf.js, spec.js, and cf.js. Conf.js is testplan-specific configuration file, spec.js contains the actual tests, and cf.js contains the common functions that I will be using through all test plans. I am trying to include a variable in cf.js to contain the starting URL to be used in the browser.get call. So far, I have not been able to get that to work. I've tried declaring it in cf.js before the //commonfunctions// function declaration, as well as within the function itself. What is the proper way to do this?

cf.js

    var commonfunctions = function () {
      global.StartPage = 'http://google.com/';
      this.ccClick = function (clickElement) {
        browser.wait(protractor.ExpectedConditions.visibilityOf(clickElement),
         this.defaultWait);



  browser.wait(protractor.ExpectedConditions.elementToBeClickable(clickElement),
     this.defaultWait);
        clickElement.click();
    };
    // Common text search
      this.ConfirmText = function(testElement, compareString) {
        browser.wait(protractor.ExpectedConditions.visibilityOf(testElement), 
10000);
        expect(testElement.getText()).toEqual(compareString);
      };
    };
    module.exports = new commonfunctions();

spec.js

  beforeEach(function() {
    browser.waitForAngularEnabled(false);
    browser.get(commonfunctions.StartPage);
  });

Right now, it does not navigate to the webpage.

This should be possible, I have posted a similar answer before but let me know if you have further concerns about the approach. The approach I took was to require the common function file in the onPrepare as a global variable. This way anything exported from the file is accessible throughout all the tests.

Storing global variable in a separate file for Protractor Tests

Global keyword in Python, How do you assign a value to a global variable in python? If your function has a local variable with same name as global variable and you want to modify the global variable inside function then use ‘global’ keyword before the variable name at start of function i.e. global total It will make the function to refer global variable total whenever accessed. Checkout this example,

Your made a mistake at following code:

// cf.js
var commonfunctions = function () {
      global.StartPage = 'http://google.com/';

// spec.js
beforeEach(function() {
  browser.waitForAngularEnabled(false);
  browser.get(commonfunctions.StartPage);
});

// you define `StartPage` to a global variable, not a property of `commonfunctions`, 
thus you shouldn't refer it from `commonfunctions`, but from `global` as following:

browser.get(global.StartPage)

or you define StartPage to be a property of commonfunctions

// cf.js
var commonfunctions = function () {
   this.StartPage = 'http://google.com/';
   // use `this` at here, rather than `global`

// spec.js
beforeEach(function() {
  browser.waitForAngularEnabled(false);
  browser.get(commonfunctions.StartPage);
});

Why should we avoid using global variables in C/C++?, This should be possible, I have posted a similar answer before but let me know if you have further concerns about the approach. The approach  C# is an object-oriented programming (OOP) language and does not support global variables directly. The solution is to add a static class containing the global variables. Using a global variable violates the OOP concept a bit, but can be very useful in certain circumstances. Be aware, that using global variables is not the answer to all

Add the below one to your config.js

baseUrl: 'http://google.com/',

To use it in your test as below

browser.get(browser.baseUrl);

Hope this helps you

Adding global variables via a require Common Function file, If you have never heard of global variables or worked with them, no need to worry​. Scope.js let fileScope = "Can be accessed anywhere in the file"; function serious issues of two threads attempting to access and use the same variable. In case you need to access your variable $name within a function, you need to say "global $name;" at the beginning of that function. You need to repeat this for each function in the same file.

Using Global Variables in Node.js, Using global keyword outside a function is not an error. It can be used if the file is included from inside a function. Using static variables ¶. Another important  Either way (I didn't say this in my initial answer because I wasn't very experienced in Node.JS), using globally installed packages in a program is an edge use case and should seldom be done because in a project it will create problems whenever the project is committed to VCS and cloned in another environment due to that specific dependency not being in the package.json file or in yarn.lock

Variable scope - Manual, How to define global variables, global fields or global functions in C# to make them accessible from The solution is to add a static class containing the global variables. Be aware, that using global variables is not the answer to all questions and should only be used if using a static function works the same way, e.g: When a variable is defined at the top of a YAML, it will be available to all jobs and stages in the pipeline and is a global variable. Global variables defined in a YAML are not visible in the pipeline settings UI. Variables at the job level override variables at the root and stage level.

C# How to use Global Variables, Fields and Functions, If your function has a local variable with same name as global and we can use it to access / modify the global variable without using 'global'  And Local variables allow us to save resources by only using variables as long as we need - for example if we create a large array which is only needed within a single function. We scope variables by using the Global, Local and Static keywords when the variables are first declared - look at the examples below. By default, AutoIt scopes any

Comments
  • Thank you! Switching from ASP & VBScript development to Javascript & Selenium is making my head spin. This worked well.
  • I know that pain, made a similar transition myself. The mistake I made was trying to jump into Protractor without understanding any of the technologies it relies on (jasmine, NodeJS, JS, webdriver, HTML) making it extremely painful to troubleshoot. Hopefully you have learned a bit more about those than I did at the beginning.