Saturday, July 20, 2024

Understanding Page Object Model using workflows and examples

Page Object Model: Overview and Benefits

Page Object Model (POM) is a design pattern used in Selenium automation testing to enhance test maintenance and reduce code duplication.

Key concepts of Page Object Model:

Code Reusability:  Page Objects separate the test logic from the UI logic, promoting reuse of code across multiple tests.

Code Maintainability: Changes to UI elements identifiers are updated within the Page Object resulting minimal impact on test classes.

Improves Test Structure: Tests become more readable and easier to maintain as they interact with methods exposed by Page Objects rather than dealing with raw Selenium commands.

Reduces Duplication: Common interactions with elements (like clicking a button, entering text, etc.) are encapsulated within methods of Page Objects, eliminating redundant code in tests.

Scalability: Supports scaling of automation efforts by providing a structured approach to managing page interactions.


Page Object Model - Workflow



Components of Page Object Model (POM)

Page Object Model


Page Objects 

  • Classes that encapsulate the behavior and structure of a web page.
  • Each page in the application under test has its own corresponding Page Object class
  • Page Objects interact with the page elements (like button, input) and provide methods to perform actions on these elements
  • A Page class has:
    • Identifiers of elements in the Page.
    • methods to work with the identified elements in the page

Page Object Factory

  • Page Factory is a concept in POM that initializes elements of a Page Object.
  • It uses annotations (@FindBy) to locate elements on the web page.
  • These annotations are used in Page Object classes to find and initialize WebElement objects.

BasePage Class

  • This class serves as a foundational class that other Page Objects inherit from. 
  • Examples of common functionality that are shared across multiple Page Objects and should be defined in BasePage class includes:
    • WebDriver Initialization - Initialize the WebDriver instance in the base Class, so that it is used in the page Objects
    • Common Page Actions - Define reusable methods for common actions that occur across multiple pages.
    • Common Page Interactions: methods for handling common interactions  across different pages like handling alerts, executing JavaScript.

    • Wait Mechanisms: Implement methods for waiting and waiting strategies to be used across pages

Any additional method that might be beneficial across multiple Page Objects.

Relation between Test Class and Page Class



Base Class with logic for common implementations


Code example base class


Creation of Page class extending the Base class




Implementation of the Page Class in Test class




Thursday, July 11, 2024

Object Oriented Programming Concepts in a MindMap

 

Understanding Object-Oriented Programming (OOP) is fundamental for modern software development. OOP concepts, such as classes and objects, encapsulation, inheritance, and polymorphism, allow developers to create modular, reusable, and maintainable code. 

MindMap Representation of OOP's Concept


Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs.

Understanding Class

Class in Java is a  blueprint of attributes and behavior (actions) of an object.

Below are the members of a class:

  • Class Variables (Static Fields): These are common to all instances of a class and are declared with the `static` modifier. 
  • Instance Variables (Non-static Fields): These are unique to each instance of a class. Following the `Bicycle` example, `cadence`, `gear`, and `speed` would be instance variables, with each `Bicycle` object having its own copy.
  • Methods: These are functions that define the behavior of the objects created from the class. They can be static (class methods) or non-static (instance methods).
  • Constructors: Special methods used to initialize new objects. They have the same name as the class and may take parameters to set initial values for the instance variables.
  • Nested Classes: Classes can contain other classes, known as nested classes.

Principle of OOPs concept:

  •  Encapsulation: Bundling the data with the code that manipulates it
    • Bundling data (variables) and methods (functions) that operate on the data into a class. 
    • Access to the data is restricted and controlled through public methods known as getters and setters. 
    • Allows for data hiding, where internal workings of a class are hidden from outside  interference and misuse. 
    • Promotes modularity and maintainability of code 
    • Enhances data security, as only approved methods can modify the internal state of objects.

    • Data Types 

      • Public: accessible from any other class, not just the class in which it is declared.
      • Protected: accessible within its own class and by derived classes.
      • Private: accessible within the class in which it is declared, and not from any other class.

  • Abstraction: Hiding complex realities while exposing only the necessary parts.
    • Achieved through abstract classes and interfaces, allowing for the creation of classes that cannot be instantiated on their own but can be sub classed.
    • Abstract classes may contain abstract methods without a body; these methods must be implemented by subclasses.
    • An interface is a blueprint for a class
      •  Include abstract methods and constants.
      • Used to achieve abstraction and multiple inheritance, as a class can implement multiple interfaces.
      • Class implementing an interface must override all of its methods.
      • Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

  • Inheritance: Allowing new objects to take on the properties of existing one.
    • Inheritance in Java allows one class to inherit the features (fields and methods) of another class.
    • Syntax for inheritance uses the `extends` keyword
    • Java supports different types of inheritance: single, multilevel, and hierarchical.
    • Multiple inheritance is not supported in Java through classes, but it can be achieved using interfaces.
  • Polymorphism: Allows objects to take on multiple forms.
    • Manifests in two primary forms: compile-time (static) and runtime (dynamic) polymorphism.
    • Compile-time polymorphism is achieved through method overloading, where multiple methods have the same name but different parameter lists.
    • Runtime polymorphism is realized through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass.

Saturday, March 10, 2018

Java for Selenium - Mindmap Object, Classes and Inheritance

In the above example. We create a class bicycle which is defined by it's state and behavior.
members and methods are defined in the class. We can defined multiple objects of the class using class methods and members. 

Bicycle itself belongs to two wheeler class. Similarly there can be other class for motor bike which can have its own member and methods defined. 

Both bicycle and two wheeler can have specific methods and members defined in the class. 

Bicycle extends two wheeler which in turn extends vehicle, bicycle can use members and methods defined in two wheeler as well as vehicle class.

Class Declaration

class Bicycle extends TwoWheeler {
    // field, method and constructordeclarations
}

class TwoWheeler extends Vehicle {
    // field, method and constructordeclarations
}


Variables in a class:

Fields - Member variables in class
Local variable - defined inside method or block of scope
Parameters - variables in method declaration

Variable declaration : [Modifier] Type variablename