Forum Discussion

Anand5Phadke's avatar
Occasional Contributor
2 years ago

How we can use the element or object or variables declared in one script in another script?

We want to create a modular code where we will store all elements, objects and variables in one java script(ObjectRepository).

Another script will have the common functions which will consume the elements,objects and variables from ObjectRepository.

Another script will have the actual test cases which will consume the elements,objects and variables from ObjectRepository, along with the common functions.


Would like to know how it can be implemented? Where can I get the examples as per the above structure mentioned.


Will appreciate if any one can share any dummy sample codes or files

7 Replies

    • Anand5Phadke's avatar
      Occasional Contributor

      This is for using functions created in one script and using it in another.

      From the functions also if we want to use the generated output in the calling do we need to return the variable and in the calling function assign it to another variable?

      function a()


      let value = 10 +3;

       return value;


      function main()


      var c = a();


      Is there any way where we can use the generated output as it is in the calling function like global variable


      Also want to use elements, objects and variables from one script in another script what are the options for it?

      • Anand5Phadke's avatar
        Occasional Contributor

        Tried suggested solution in my current code.

        There is no consistent success. Out of 5 runs only 2 runs succeeded but 3 failed.

        What can be reason?

  • rraghvani's avatar
    Champion Level 3

    If you provide detailed information such as what the failures are, and your code. Then I can provide a possible solution.


    The example that I had provided shows how to use a function and variable from another file.

  • JesusElliott's avatar
    Occasional Visitor

    To implement a modular code structure in JavaScript, you can create separate files for the ObjectRepository, CommonFunctions, and TestCases. In the ObjectRepository.js file, define and export the elements, objects, and variables. The CommonFunctions.js file should contain the common functions that consume the elements from the ObjectRepository. Lastly, in the TestCases.js file, import the necessary elements, objects, variables, and common functions to write your actual test cases. By organizing your code in this modular manner, you promote reusability and maintainability, making it easier to manage and enhance your test automation framework (brilliance sf). For specific examples, I recommend exploring open-source test automation frameworks or repositories on platforms like GitHub, as they often demonstrate modular design principles and can provide helpful code samples for reference and adaptation.
    Thanks and Regards