Search This Blog

Tuesday, September 27, 2022

Page Object Model(POM) based Testing using Selenium WebDriver

 Page Object Model(POM) based Testing using Selenium WebDriver


Introduction

Maintaining 1000 lines of code in a single class file is a heavy task and also it increases its complexity. In order to maintain the project structure and efficient performance of the selenium scripts, it is necessary to use different pages for different tasks.

To ease the access of distributing the code into different modules, the Page Object Model(POM) comes to the rescue. In this blog, we will be learning some of the core concepts of Page Object Model(POM).

What is Page Object Model?

Page Object Model is a design pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT.

The tests than use the methods of this page object class whenever they need to interact with the UI of that page. The benefit is that if UI changes for the page, the tests don’t need to be changed, only the code within the page object needs to change.

Subsequently all changes to support that new UI are located in one place so we don’t have to edit much.

How to Implement POM?

Create a New Maven Project

Create a Maven Project so we don’t have to add all jar files into the library which are needed for our project.

It will automatically download and add all the jar files into your project which are required by just adding the dependencies into the POM.xml file of your project.

Advantages of POM

  • Makes our code cleaner and easy to understand – keeps our tests and element locators separately.
  • Easy to visualise each step of the scenario, view and edit test cases intuitively.
  • Test cases become short and optimised as we can reuse page object methods in the POM classes.
  • Any UI change can easily be implement, update and maintain into the Page Objects and Classes.
  • Re-usability of code – object repository is independent of test cases.

How to Implement POM?
Create a New Maven Project

Create a Maven Project so we don’t have to add all jar files into the library which are needed for our project.

It will automatically download and add all the jar files into your project which are required by just adding the dependencies into the POM.xml file of your project.




Create a new Package

After the above step create a new package under our project by any desired name of your choice. The package which you are creating is a user-defined package which will create a folder in your workspace.

Create a New Class

After creating the package you have to create a class under the package which you have created in above step. We can also create an object of a class and access it from another class.

Create a Class for Browser
We will create a method while passing parameters with it having driver, browser name and URL of the website.

Under this method we will provide multiple browser options for the user to choose between them and pass the requirement drivers under the specific conditions of the browser.

After above step we will maximise the browser and add ImplicitWait and pageLoadTimeout with it, so if the processor of the system is slow or internet is slow than elements can wait according to the given time.

Than we will pass the url of the website using driver.get(appURL); and in the appURL we will pass the website URL.


Now, we will create a method for browser quit having parameter driver. So when our script is completed than the browser will be quit automatically.


Create a Class for Login Page
We will now create all the elements which are on Login Page using @FindBy so we can directly call them using WebElement. Initialise all the elements together under the class so they can be accessible from anywhere.



Now create a method where all the WebElements of the Login Page will be called to perform some actions. In the method we will pass two parameters for username and password of the website.




Create a Class for Login Page Test
Now we will create another class where all other classes will be called. Before starting of a function we will call @Test which is used to run your script with TestNG.

Create a void method and first call startapplication() method from BrowserFactory class to start the browser while passing parameters which are defined in the method.

Create an object for Login Page with any name and initialise all WebElements using PageFactory. Now call the method where WebElements actions are defined under a method from an object which you have created.

Now, call the method QuitBrowser() from the class BrowserFactory which is used to close the browser after your script.





View of Page Object Model(POM)
Now, if the AUT undergoes any change at the login page or at any page we just need to change the page object. Thus we don’t need to change our test script again and again (even for the new release or built).

The project structure will look like :




Monday, September 26, 2022

Test Automation framework

 A well structure design helps reduce the extra cost and conflicts. We need to develop support libraries for re-usability and expandability of the code in our automation

We have to comment in scripts and function headers which will improve the readability and understand-ability of the structure.

Test automation is the process of automating repetitive processes of users to make the work efficient which saves a lot of time.

What is test automation framework ?

A test automation framework is define as a real or conceptual structure; created to provide support which could expand in future. Test Automation framework is not a single process or tool. It’s a collection of tool and process working together to automate the manual process.

Testing Framework is a set of rules which is used for creating and designing test cases.

Automation Framework is also useful when user want to repeat the same process using test scripts when ever it is required to test on multiple browsers at the same time.

Purpose of a Test Automation Framework

• It improves the design and development of automated test scripts which involves re-usability of components or code.

• Provides a structured development of all test scripts which reduces dependency on individual test-cases which saves time to write the same code.

Detects issues and bug with proper root causes with minimum human involvement.

Reduces dependency on teams by automatically selecting the test to execute according to test scenarios.

Enhance test accuracy and reduces test maintenance cost which involves lower risk of test failure.

• Improves utilization of various resources and enables maximum returns on efforts.

• Ensures an uninterrupted automated testing process with little man-power involvement.


Different Types of Framework used in Automation Testing

Most common types of Test Automation Frameworks are:-

• Linear Scripting Framework: This framework is based on the concept of record and playback mode that is always achieved in a linear manner. Linear Scripting Framework mostly used for testing on the small applications in which step are written in a sequential order.

• Modular Testing Framework: Modular Test Frameworks break down test cases into small modules. There, the modules are independently tested first and then the application is tested as a whole which makes each test independent.

• Data Driven Testing Framework: In this testing framework, a separate file in a tabular format is used to store both the input and expected output results. A driver script where all test cases are called can execute all the test cases with multiple sets of data. This driver script contains navigation that spreads through the program which covers both reading of data files and logging of test status information.

• Keyword Driven Testing Framework: Keyword Driven test framework separates script logic from test data, then stores the data externally. After that, it stores the keywords in a different location. Since user can use the same keyword across different test scripts which re-uses the code.

• Hybrid Testing Framework: A hybrid test framework improves the weaknesses of different test frameworks. It is a combination of many types of end-to-end testing approaches which uses the advantages of other frameworks.

• Test Driven Development Framework (TDD): Test driven development is an approach in which test cases are developed to specify and validate that what will code do. It starts with designing part and develop test cases for small functionalities for your application, which instructs developers to write code if previous script fails.

• Behaviour Driven Development Framework (BDD): This has been derived from the TDD approach and in this method tests are more focused and are based on the system behavior. Testers can create test cases in simple English language which helps even the non-technical people to easily analyze and understand the tests.


Benefits of Test Automation Frameworks

• Optimization of Resources: Test framework helps in making the best use of resources, it does this by making the process easier with use of different resources according to organizational needs.

• Increased Volume of Testing: Test automation frameworks increase the volume of testing by perform test on many devices as it’s not possible to perform manual testing on all devices.

• Simultaneous Testing: Test automation frameworks enable simultaneous testing on different types of devices. When test scripts are automated, than testers can perform the script on other devices at the same time.

• Enhanced Speed and Reliability: Performing different test cases manually can be very time consuming so instead of it we can run all the test cases from our script in very less time.

• More Output in Less Time: An automation script minimises the time taken to prepare and run tests. With increased efficiency and speed, we can gain more output in less time.

• Fixing Bugs at an Early Stage: Test automation framework helps in fixing bugs at an early stage which don’t need much manpower to carry which saves time and expenses of the organisation.

• Remote Testing: With a test automation framework, you don’t need to see all test cases, you can run the test cases and come back later and view the result. User doesn’t need to be present at execution time physically.

• Reusable Automation Code: You can use your automation script in any of your other application or website which has same functionality which increase the code re-usability.


Steps for an Effective Test Automation Approach




• Evaluate to understand real need for automation based on Website/Application type: We should first evaluate what need to be automated which saves the time of the user which he losses it by doing it manually.

• Define automation goals and priorities: The Goals should be first set so we can move ahead in that direction to make the test script and achieve it while having priorities to automate.

• Plan automated testing strategy: Automated Testing Strategy should be planned so that test cases should be built accordingly.

• Select right automation testing tool & testing framework based on your project requirements: The Testing tools & testing framework should be selected before automating the manual process so the script which we are making should be constructed in a proper way with right tool.

• Decide which test case to automate: We should know which test case need to be automated because it can save the time of the user which he spends doing it manually.

• Develop good quality of test data: The script which you are creating should be of good quality which can be used later also and the data which it produces as a result should be in readable format.

• Create automated tests which are more stable to UI changes: Automated Tests which are created should be stable enough even if there are some minor changes in the UI than also your script is able to run the test.

• Execute tests for test scripts developed: Test Scripts which are developed should be executed so the result can be known whether it is correct or not and we can check whether the script which we have created is stable enough or not.

• Test early and often with continuous integration and continuous delivery (CI/CD) pattern: We can test our script early with Continuous Integration and Continuous Delivery that whether the script which we have created is good enough or not.

• Maintain test scripts for future use: Test Script which you have created should always be kept for the future use so you don’t have to write the same code again and again, which you can reuse the code of the script which is required in your new script.


Common Misconceptions about Automated Testing

• Automation will provide you with more free time: The misconception that automation will provide you more free time is both true and false. In manual testing, most of the time is devoted to explore and do functional testing where we manually search for errors.

With automated testing, that time is cut drastically. The work for automation testers is instead spend in coding the test script and making improvements to tests repeatedly as adjustments are needed.

• The Cost of Automated Testing is Too High: Investment in automated testing might feel costly, especially for a smaller company. But according to report, over time, automated testing pays for itself.

Automation testing also reduces the cost for multiple code revisions, over the course of time, the investment pays out. Manually repeating these tests is costly and time-consuming; but automated tests can be run over and over again at no additional cost.

• Automated Testing is better than Manual Testing: There is no superiority in the automation vs manual; they are just “different”. Manual testing is performed by a human sitting in-front of a computer carefully going through application; trying various input combinations, comparing the results to the expected behavior and recording the results.

Automated testing is often used after the initial software has been developed. Lengthy tests that are often avoided during manual testing can be automated to save the time. They can even be run on multiple computers with different configurations.

• Automated Testing Inhibits Human Interaction: Automated testing is more clear-cut and faster than what humans could do with less human errors, so this misconception is understandable.

Automation testing do not replace face-to-face communication which is a necessary part of a software development. Instead, it increases the aspect by providing another channel through which to communicate.


Important points consider while Designing Test Automation Framework

• Handle Scripts and Data separately – Automated test scripts should be separated from input data files (eg – XML, Ms-Excel or Databases) and code, so that no modifications are needed to the test scripts whenever some data has to be changed.

• Library – Library should contain all reusable functions such as databases, generic functions, application functions etc. so that we have to just call the function rather than writing the whole code again and again.

• Coding Standards – Coding standards should always be maintained across your test automation framework, which will encourage individual coding practices and help in maintaining code structure, which makes it easier for others to understand the code.

• Extensibility and Maintenance – An ideal test automation framework should regularly support all new updation to the software application which allows modification. e.g. Some new library can be created, which would help in updating application features with less effort.

• Script/Framework Versioning – Versions of your Test Automation Framework / scripts should be maintain in a local repository or some versioning tool, which would help in easy checking of changes to the software code.


Goals for designing a Test Automation Framework

  • The framework design should be easy to expand and maintain
  • Provide abstraction from complexities
  • Identification of the common functions used across scripts
  • Separate complex logic functions with utility functions
  • Separate test data and test scripts
  • Creation of robust functions
  • Appropriate functional break down which can be changed
  • Ensure scripts are executed without human intervention, even in false conditions
  • Improve Design documentation


Using Page Object Model in Test Automation Framework

You need classes that interact with the pages of your website. These classes should be within the framework layer. The most popular design pattern for creating these types of classes is the page object model (POM). This model recommends creating a separate class for each page of your website, (e.g. buttons, text fields, etc), as well as methods for interacting with those elements. You can use a browser automation tool, such as Selenium WebDriver, to handle the actual interaction.


The role of Inheritance in Test Automation Framework
Inheritance, an object-oriented programming principle that enables objects to receive properties from a parent object, also has its place in your automation test code.

We have to always launch the browser for running our scripts. Rather than duplicating your code in every test method, you can use this functionality, and place into a method that runs before each test. To verify, this method does not get duplicate in every test class, place this method in a base test class from which all test classes inherit. The base page can also contain objects for other parts of the website that are visible from any page, such as navigation menus, headers, and footers. All of these would be inherit by any page, and therefore accessible without duplicating code.

Test runner tools, such as JUnit and TestNG, provide “before” annotations that you can use to denote methods that should run before the test. They also provide “after” annotations that you can use and can be inherit in the same way to clean up after tests.


How To Design a Test Automation Framework
Some points which should be consider while designing a framework:-

  • Create Wrapper Method: Writing a wrapper method is one of the solutions for extending the library features. An example of extending the wrapper method is to allow better logging capabilities and handling the errors well in Selenium.
  • Implement Custom Logger: While running the test script; all information should be logged into the file. This information can be use as a reference for understanding the code. The popular logging framework for java is log4j and python is Custom logger.
  • Choosing the Right Design Pattern: Choosing the right design pattern speeds up the test case development and helps in preventing minor issues that can cause major problems and therefore improves code readability. The most popular design pattern for creating a selenium automation framework is the Page Object Model (POM).
  • Separate Tests From Automation Framework: Separate the Test script logic and the input data from the automation framework. It increases code readability and makes the code readable.
  • Create a Proper Folder Structure For The Code: Always define the folder structure which makes the code readable and makes it easy to understand. Eg – Test Cases, Utilities, Input Data etc.
  • Build & Continuous Integration: Continuous Integration is a development practice that integrates with a build automation tool like Maven to ensure whether the software is running without any breaks after making a commit decision.