Thursday, March 10, 2022

Understanding - Are Framework and Library the Same?


We Have This Question

  • What is a Framework?
  • What is a Library?  

I had these questions when I started my testing career.  At times, I ask this question today as well, when I listen to fellow engineers talk about it.  

I can say firmly that you had these questions.  You would have asked these questions to yourself or to your fellow testers.


Using Interchangeably

In the teams in which I have worked and in the community, I have seen these two terms are used interchangeably.  This has added to the confusion.  At times you might see the programmers using these two words interchangeably; but, they might know what it does and how to use it.

If I were to start my automation practice, to what should I call framework and library?  Does this question have crossed your mind and thoughts?


Framework and Library - What it is?

I understand both Framework and Library as code.  Yes, both are programming code that is structured and organized in a way.  This code is written by a programmer and will be consumed by other programmers and testers.  

Why do we use it?  We use it to assist in solving a problem we have in our work.  May be is what leads one to get confused or use these words interchangeably.  But are they one and the same?  I had this question.  I spoke to programmers and testers with whom I worked.  I observe each of them having their own ideas and perspectives about it.  

But are they one and the same?  As of today, I understand Framework and Library are not the same. It differs in -- the ease of consumption as is and the code to be written and organized for using it.  I see these two factors differentiate Framework and Library; there could be more factors and I'm not aware of it.


Framework and Library - My Experiences


I use libraries in testing and automation I do.  For example, 
  • the Selenium library to interact with the browser and perform the actions on the web pages
  • the RestAssured library to build the HTTP request
  • the Appium library to interact with the mobile OS and its app's UI

I shared the above examples as most of us are aware of them and relate to them in our work.  Then, what is the framework?  

Have we heard of these words -- React, Angular, Bootstrap, and Flutter?  Today we use them to build web apps and mobile apps (using Flutter).  These are frameworks consumed by programmers to build the applications.  Some of these frameworks provide libraries to test as well.  For example, Chai and Jest assertion libraries.

Trying to understand further, one of my programming friends said me about the principle -- Inversion of Control.  Inversion of Control allows a framework to inject dependencies, inject configuration and manage lifecycle. That way programmer's program can focus on what it must do and not have to worry about any additional responsibilities.

I learn, Inversion of Control refers to transferring the control of objects and their dependencies from the main program to a container or framework.  How this will be implemented depends on the programmer and the design used to implement the same.

Note: In one of the perspectives I learn the framework provides the abstraction to its libraries.  We use these abstractions of a framework and develop our applications.  Can I say framework means abstraction?



Difference: Framework and Library


In the way we communicate, most times it is communicated as there is no difference between them and both mean the same.  Probably this is the reason we have the confusion, especially the Test Engineers who want to start the practice of automation will have this confusion.  This is being passed on to the next generation of Test Engineers who come take up Software Testing as a career.

Technically, the difference exists between the Framework and Library.  One of the key differences lies in the principle mentioned above -- Inversion of Control.  Let me walk through this with a use case that we Test Engineers cross every day.


Use Case in using a Library: Automating a user action on a web page

  • I make use of the Selenium library
  • I initialize the WebDriver and a driver of a browser
  • I open a browser on my machine using WebDriver communicating with the browser's driver
  • I talk to the browser to launch a URL
  • I make actions on the web page

I'm not writing a code to do all of the above said.  I'm making use of APIs exposed by the Selenium library to do all of these actions.  The library's code handles all these for me.  My code will be for -- to consume Selenium's API and to aid evaluation of business logic -- the Utils is a commonly used word to describe the Class which has methods for the same.  

When I'm using a library, I have control over the code that I write in automation in deciding how the automation should execute that is in what order and what flow.  I can decide when to make what actions on the web page here.  The Selenium library does not mandate it on me.  

The only mandate I see is the sequence of actions that has to be made when using a particular API.  For example, the action events to be made in what order when used the Touch Actions API.


Use Case in using a Framework: Building a web and mobile app

  • The team developing Android and iOS mobile apps using Flutter
  • The team developing a web using Flutter or React

Just by using the Flutter and React frameworks, the apps and website will not be ready.  The programmer follows the structure and sequence insisted by the framework in her/his code.  The framework will tell how the code should be written and probably this also means the flow of code is controlled by Framework.  For example, when loading the content on the web page and screen.

In one of my current projects, the place where the message is displayed, how it is displayed, and how to interact with it, is a flow in code.  The programmer has to work oblige how the framework parses the error message.  It has to be done that way unless the programmer writes a custom code and plugs it into the framework, provided the framework supports it.  

This code which the programmer plugs in now, it is a custom library developed to use with a being used framework.  Now using this custom-built library, the reading or/and display of error messages can be made as per the need of the product.



Automation: Do We Write Library or Framework?


When we testers say automation framework, I see that we are referring to the structured library we have written consuming a framework and library.  For example, Arjuna developed by Rahul Verma is a framework.  I will consume Arjuna and build a library that has tests to automate web applications using the library as Selenium.

The automation I write consuming a framework and/or a library will have methods. Few of these methods will be written once and can be used in multiple places.  That is no repeating code and practicing the reuse of code.  It is like DRY -- Do Not Repeat Yourself.  We can see this practice in the automation we write.

For example, the below code can be written once in a class and invoked in different places where we test the business logic.  Now, these two methods will act as a library code and it is also part of a library code.  


protected int addTwoNumbers(int numberOne, int numberTwo){
int sumOfTwoNumbers = numberOne + numberTwo;
return sumOfTwoNumbers;
}

protected double multiplyTwoNumbers(int numberOne, int numberTwo){
double productOfTwoNumbers = numberOne * numberTwo;
return productOfTwoNumbers;
}


We just call the method name and pass the parameters in different classes where it is needed.  No need to implement these methods each time in business logic where it is needed.  

Doesn't this give a perspective that a framework includes a library within it?  And, this questions me --  should I call automation what I do as an Automation Framework?  I say NO!  Unless someone uses my work as a foundation to build their tests and it dictates how the code should be written and structured, I do not call it a framework.  

But, one can use the classes that I write in their work.  This class act as a library.  Recall the class file name with Utils; this file has methods that we use by invoking it across the package.  One good example of this is, the logging part we write in our automation.  This logging part of code can be built as a library and can be used in different automation projects in which we work.

This is my understanding of Library and Framework at this point in time.  You have any questions or other perspectives that I'm not seeing, do share.



No comments:

Post a Comment

Please, do write your comment on the read information. Thank you.