Thursday, March 5, 2020

Page Object Model Design Pattern In Selenium Web Driver

What is POM(Page Object Model)?

Page Object Model is a design pattern to create Object Repository for web UI elements. Under this model, for each web page in the application, there should be corresponding page class. This Page class will find the Web-Elements of that web page and also contains Page methods which perform operations on those Web-Elements.

Page Object Model, also known as POM, is a design pattern in Selenium which has gained more popularity in the market for test automation development for the maintenance of code, such as reusability, extensibility, and avoiding code duplication etc.

Page Object Model is used for creating Control properties or Object Repository for controls on a web-page. For each web-page which we want to automate there should be a separate class such as if we are performing the automation for the login page, we need to maintain all the login page control properties in the separated class file. If we consider a login page, there are controls available in a login page such as UserName, Password, Login, ForgetPassword etc.


Each control will have unique control properties such as Name, Control ID, TagName, XPath, ClassName, CssSelector etc. To get the control properties we need to inspect the element by clicking F12 or right the webpage and clicking on Inspect Element.

There we can hover over the control to find a control property for each control. If the control doesn't have a control property then we need to use XPath. In order to take XPath right click on the control's element which is in the Inspect Element window, then click on Copy-> Copy XPath. 

If we want to automate a login page then we need to create a separate class as LoginObjectProperties and maintain all the login related control properties in LoginObjectProperties class.

Similarly, if we want to automate the registration page, we will have control under registration page like Enter Email, FirstName, LastName, City, ZipCode, Country etc. For registration page, we need to create a separate class called RegisterationObjectProperties and keep all the registration related control properties in RegisterationObjectProperties class.


The maintenance of a separate class for object properties or control properties is used for reducing the duplication of code as well as to reuse the code.

Advantages of POM
  • Page Object Pattern says operations and flows in the UI should be separated from verification. This concept makes our code cleaner and easy to understand.
  • The Second benefit is the object repository is independent of test cases, so we can use the same object repository for a different purpose with different tools. For example, we can integrate POM with TestNG/JUnit for functional Testing and at the same time with JBehave/Cucumber for acceptance testing.
  • Code becomes less and optimized because of the reusable page methods in the POM classes.
  • Methods get more realistic names which can be easily mapped with the operation happening in UI. i.e. if after clicking on the button we land on the home page, the method name will be like 'gotoHomePage()'.


Design Pattern

What is Design Pattern?

Design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects. The 23 Gang of Four (GOF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups:
Design patterns have two major benefits.
  • They provide you with a way to solve issues related to software development using a proven solution.
  • Design patterns make communication between designers more efficient.

Why should I learn patterns?

The truth is that you might manage to work as a programmer for many years without knowing about a single pattern. A lot of people do just that. Even in that case, though, you might be implementing some patterns without even knowing it. So why would you spend time learning them?
  • Design patterns are a toolkit of tried and tested solutions to common problems in software design. Even if you never encounter these problems, knowing patterns is still useful because it teaches you how to solve all sorts of problems using principles of object-oriented design.
  • Design patterns define a common language that you and your teammates can use to communicate more efficiently. You can say, “Oh, just use a Singleton for that,” and everyone will understand the idea behind your suggestion. No need to explain what a singleton is if you know the pattern and its name.

Creational Design Pattern: Provide object creation mechanisms that increase flexibility and reuse of existing code.

The Creational design patterns are categorized into two types. They are as follows:
1. Object-creational patterns: The Object-creational patterns deal with object creation. Here, it defers part of its object creation to another object.
2. Class-creational patterns: The Class-creational patterns deal with class-instantiation. Here, it defers its object creation to sub-classes.

When to use the Creational Design Pattern?

In real-time applications, the project is created with a lot of classes. A lot of classes mean we are going to deal with a lot of objects. If these objects creations are scattered on the client code, then it leads to a lot of complicated logic at the client code. The Creational Design Pattern helps us to centralize the object creation logic.

The creational Design Pattern is divided into the five categories:
CREATIONAL2.gif

  Abstract Factory: Creates an instance of several families of classes.
  Builder Separates: Object construction from its representation.
  Factory Method: Creates an instance of several derived classes.
  Prototype: A fully initialized instance to be copied or cloned.
  Singleton: A class of which only a single instance can exist.

Structural Design Pattern: Explain how to assemble objects and classes into larger structures, while keeping the structures flexible and efficient.

When to use Structural Design Patterns in C#?

In real-time applications, sometimes we need to change the structure of a class or the relationship among the classes but we don’t want this change to be affected by the project. For example, if we have two classes let say user and product. And the product class is used inside the user class making one to many relationships between the User and Product. Tomorrow, the structure or the relationships between these two classes change. The customer now wants to keep away the product class from the User class, as they want to use the User and Product class independently. This is actually a structural change and we don’t want this structural change to affect our project.

The Structural Design Pattern is divided into the seven categories:
StructDesiPatte1.gif
  Adapter: Match interfaces of different classes
  Bridge: Separates an object’s interface from its implementation
  Composite: A tree structure of simple and composite objects
  Decorator: Add responsibilities to objects dynamically
  Facade: A single class that represents an entire subsystem
  Flyweight: A fine-grained instance used for efficient sharing
  Proxy: An object representing another object

Behavioral patterns: Take care of effective communication and the assignment of responsibilities between objects.

When to use Behavioral Design Patterns in C#?

In real-time applications, sometimes we want to change the behavior of a class and again we don’t want it to affect other classes of the project. For example, we have an Invoice class which currently applying taxes as 18%. Tomorrow if we want to add another extra tax. That means we are changing the behavior of a class. To solve such type of Behavioral issues Behavioral Design Pattern comes into the picture.

The Behavioral Design Pattern is divided into the several categories: 
DesPatte1.gif
  Chain of Resp.: A way of passing a request between a chain of objects
  Command: Encapsulate a command request as an object
  Interpreter: A way to include language elements in a program
  Iterator: Sequentially access the elements of a collection
  Mediator: Defines simplified communication between classes
  Memento: Capture and restore an object's internal state
  Observer: A way of notifying change to a number of classes
  State: Alter an object's behavior when its state changes
  Strategy: Encapsulates an algorithm inside a class
  Template Method: Defer the exact steps of an algorithm to a subclass
  Visitor: Defines a new operation to a class without change

If you want know more about Design Pattern with Example you can use this URL


Selenium

What is Selenium?

Selenium is a free (open-source) automated testing framework used to validate web applications across different browsers and platforms. You can use multiple programming languages like Java, C#, Python etc to create Selenium Test Scripts. Testing done using the Selenium tool is usually referred to as Selenium Testing. It has four components..
Selenium Components

Selenium IDE: Selenium Integrated Development Environment (IDE) is the simplest framework in the Selenium suite and is the easiest one to learn. It is a Firefox plugin that you can install as easily as you can with other plugins. However, because of its simplicity, Selenium IDE should only be used as a prototyping tool. If you want to create more advanced test cases, you will need to use either Selenium RC or Web-driver.

Selenium Remote Control: Selenium RC was the flagship testing framework of the whole Selenium project for a long time. This is the first automated web testing tool that allowed users to use a programming language they prefer. (For Example: Java, C#, PHP, Python, Perl, Ruby)

Pros
Cons
Cross-browser and Cross  platform
Installation is more complicated than IED
Can perform looping and conditional operation
Must have programming knowledge
Can support Data-driven testing
Need Selenium RC server to be running
Has matured and complete API
API contains Redundant and confusing commands
Can readily support new browsers.
Browser interaction is less realistic
Faster Execution than IED
Slower execution than Web-driver

Selenium Web-Driver: The Web-Driver proves itself to be better than both Selenium IDE and Selenium RC in many aspects. It implements a more modern and stable approach in automating the browser's actions. Web-Driver, unlike Selenium RC, does not rely on JavaScript for Automation. It controls the browser by directly communicating with it. The supported languages are the same as those in Selenium RC.(Java, C#, PHP, Python, Ruby)

direct communications


Selenium Grid: Selenium Grid is a tool used together with Selenium RC to run parallel tests across different machines and different browsers all at the same time. Parallel execution means running multiple tests at once.
Features:
  • Enables simultaneous running of tests in multiple browsers and environments.
  • Saves time enormously.
  • Utilizes the hub-and-nodes concept. The hub acts as a central source of Selenium commands to each node connected to it.
How to Choose the Right Selenium Tool for Your Need:

Selenium IDE:

  • To learn about concepts on automated testing and Selenium.
  • Selenese commands such as type, open, click and wait, assert, verify, etc.
  • Locators such as id, name, x-path, CSS selector, etc.
  • Executing customized JavaScript code using run script
  • Exporting test cases in various formats.
  • To create tests with little or no prior knowledge in programming.
  • To create simple test cases and test suites that you can export later to RC or Web-Driver.
  • To test a web application against Firefox and Chrome only.
Selenium RC:
  • To design a test using a more expressive language than Selenese
  • To run your test against different browsers (except Html Unit) on different operating systems.
  • To deploy your tests across multiple environments using Selenium Grid.
  • To test your application against a new browser that supports JavaScript.
  • To test web applications with complex AJAX-based scenarios.
Selenium Web-Driver:
  • To use a certain programming language in designing your test case.
  • To test applications that are rich in AJAX-based functionalities.
  • To execute tests on the Html-Unit browser.
  • To create customized test results.
Selenium Grid:
  • To run your Selenium RC scripts in multiple browsers and operating systems simultaneously.
  • To run a huge test suite, that needs to complete in the soonest time possible.
When to use Selenium Automation?
Selenium can be used to perform repetitive functional and regression testing. Every organization wants to reduce their manual testing cost and thus have started using Selenium. This is an automation testing tool to avoid repeated manual work, receive faster feedback, reduce cost of manual testing and save time on running tests repeatedly, and ensure that tests are executed consistently with the same preconditions and expectations.

Who should use it?

Developers:- For “browser” regression testing (and replace HTML unit/HTTP unit in some cases).
Users (Business Analysts, Functional Tester, and Developer):- Can create automated keyword scripts using MS Excel or the Script Editor after getting familiar with the keyword repository.
Quality Assurance professionals:- Enhance or use it to do regression test/cross browsers testing on all platforms. QA can cover all types of functional testing (like a system, sanity, smoke, integration, etc.) and acceptance testing using Selenium until that has a GUI interface.

Featured Posts